Webpack 2 config export as function
sidonaldson opened this issue · 10 comments
I've seen the other two issues about webpack 2 but this one is slightly more specific;
You can now export a function as your webpack config file which is their recommended way of doing so if you want to use custom command arguements.
module.exports = function(env) {
var customStuff = env.customStuff;
/* ... */
return config;
};
I believe parallel-webpack expects an object so this isn't supported. Is this something you would consider? Without knowing the code inside out I believe it's a straightforward amend.
PR submitted for any .js config file. #35
+1ing this. Missing support for function export is the only reason I'm not completely moving to parallel-webpack.
module.exports = function(env) {
var customStuff = env.customStuff;
/* ... */
return config;
}();
calling the anonymous function might help
I'm having trouble reading the env
though.
module.exports = function (env) {
console.warn(env.mode)
return createVariants({
config: [
require('./webpack.config-a')
]
}, createConfig);
}()
Not working. Can someone point me in the right direction?
Well what you're trying to do there doesn't make any sense.
(function(env) {} )()
calls the function you just declared without any parameters. Of course the parameters won't be set.
@sidonaldson started a PR to support this but its not yet finished. Not sure if he's going to continue with it.
Here is my workaround solution: Adding extra --env parameters in the webpack.config.js file
const componentsConfig = require('./webpack-components.config');
const lessConfig = require('./webpack-less.config');
const contentConfig = require('./webpack-content.config.js');
module.exports = env => {
let configs = [componentsConfig, lessConfig, contentConfig];
const devtool = (env && env.production) ? 'source-map' : 'eval';
return configs.map(config => {
if (!config.devtool) { // allow a config to overwrite the global devtool. i.e: a config for .less, .sass can not use 'eval' option
config.devtool = devtool;
}
return config;
});
};
My workaround is to generate all of my webpack configs and other meta info on the first stage of the build so I generate a file that exports the result of calling my make-webpack-config with the overrides for each bundle. This way it exports an object and parallel-webpack can consume it.
module.exports = [
require('webpack.prod.config.js')({
...bundle 1 metadata here
}),
require('webpack.prod.config.js')({
...build n metadata here
}),
];