I have this Babel loader that's working
{ test: /\.jsx?$/, loader: 'babel', query: babelSettings, exclude: /node_modules/ },
But now I want a CoffeeScript loader but I want to pipe it through Babel to get the the fancy HMR stuff
{ test: /\.coffee$/, loader: 'babel!coffee', query: babelSettings, exclude: /node_modules/ },
This doesn't work though, and results in the following error.
Error: Cannot define 'query' and multiple loaders in loaders list
Any idea how to define the query just for the Babel part of the loader chain? The query is a complicated object and I don't think I can encode it.
var babelSettings = { stage: 0 };
if (process.env.NODE_ENV !== 'production') {
babelSettings.plugins = ['react-transform'];
babelSettings.extra = {
'react-transform': {
transforms: [{
transform: 'react-transform-hmr',
imports: ['react'],
locals: ['module']
}, {
transform: 'react-transform-catch-errors',
imports: ['react', 'redbox-react']
}]
// redbox-react is breaking the line numbers :-(
// you might want to disable it
}
};
}
In webpack 2 & 3 this can be configured much more cleanly.
Loaders can be passed in an array of loader objects. Each loader object can specify an
options
object that acts like the webpack 1query
for that particular loader.For example, using both
react-hot-loader
andbabel-loader
, withbabel-loader
configured with some options, in webpack 2 & 3For comparison, here is the same configuration in webpack 1, using the query string method.
Notice the changed property names all down the chain.
Also, note that I changed the
es2015
preset toes2015-native-modules
preset in thebabel-loader
configuration. This has nothing to do with the specification ofoptions
, it's just that including es6 modules allows you to use the tree-shaking feature introduced in v2. It could be left alone and it would still work, but the answer would feel incomplete without that obvious upgrade being pointed out :-)Sokra, the creator of Webpack, gives his own take on how to do this here, but you'll probably be better served with the webpack-combine-loaders helper that's more similar in style to defining a single loader with the query object.
With
webpack-combine-loaders
, you can define multiple loaders as such:The way to do this is to set the query parameters in the loader string itself, as the
query
object key will only work for one loader.Assuming your settings object can be serialized to JSON, as your example indicates, you could easily pass your settings object as a JSON query. Then only the Babel loader will get the settings.
The feature for doing this is somewhat documented here:
The
test
property is just regex, so you can run a check for both jsx and coffee at the same time:test: /\.(jsx|coffee)$/
Sass/SCSS is a little easier:
test: /\.s[ac]ss$/