Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify cacheDirectory option when using babel-loader with webpack?

There is a cacheDirectory option in babel-loader. I can't figure out how to use it with the following webpack setup:

var compiler = webpack( {
    context: path.resolve( __dirname + "/../../" + rootModuleDir + "/" + modules[ module ] ),
    entry: "./index.jsx",
    resolve: {
        root: path.resolve( __dirname + "/../../assets/js/lib/react" ),
        extensions: [ "", ".js", ".jsx" ]
    },
    output: {
        path: targetDir,
        filename: modules[ module ] + ".js"
    },
    module: {
        loaders: [
            { test: /\.jsx?$/, exclude: /node_modules/, loader: "babel-loader?optional=runtime" }
        ]
    },
    plugins: [
        //new webpack.optimize.UglifyJsPlugin(),
        new webpack.SourceMapDevToolPlugin( {
            filename: "[file].map"
        } )
    ]
} );

Where should it go?

like image 989
Vladislav Rastrusny Avatar asked Apr 21 '15 07:04

Vladislav Rastrusny


People also ask

How does Babel work with Webpack?

If Babel is a translator for JS, you can think of Webpack as a mega-multi-translator that works with all kinds of languages (or assets). For example, Webpack often runs Babel as one of its jobs. Another example, Webpack can collect all your inline CSS styles in your Javascript files and bundle them into one.

Does Webpack use Babel by default?

Webpack does not contain Babel or uglify by default. These are contained within the loaders. These are seperate npm packages you need to install used in the configuration.

Can not Resolve babel-loader?

To solve the error "Module not found: Error: Can't resolve 'babel-loader'", make sure to install the babel-loader package by opening your terminal in your project's root directory and running the command npm install -D babel-loader and restart your development server.


2 Answers

You can add it to the babel-loader configuration like this:

loaders: [
    {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader?optional=runtime&cacheDirectory"
    }
]

Note, you should not add =true, that's unnecessary and will set the cacheDirectory to be use a directory named true. Reference: using cacheDirectory fails with Error

You can also use the query property:

rules: [
    {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        query: {
            optional: "runtime",
            cacheDirectory: true
        }
    }
]

When using the query property, specifying true will enable the option, and specifying a string value will enable the option and configure it to use that directory name. Reference: query parameters

like image 151
Will Klein Avatar answered Oct 10 '22 20:10

Will Klein


Add it to the loader string like so:

module: {
    loaders: [
        { test: /\.jsx?$/, exclude: /node_modules/, loader: "babel-loader?optional=runtime&cacheDirectory=true" }
    ]
},
like image 2
twobit Avatar answered Oct 10 '22 21:10

twobit