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?
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.
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.
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.
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
Add it to the loader string like so:
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loader: "babel-loader?optional=runtime&cacheDirectory=true" }
]
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With