@brillout/webpack-config-mod
Webpack config modifiers.
Contents
Usage Example
// ./example.js
const mod = require('@brillout/webpack-config-mod'); // npm install @brillout/webpack-config-mod
const assert = require('reassert');
const path = require('path');
const config = {
entry: './path/to/entry-file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
]
}
]
}
};
mod.setRule(config, '.css', {use: ['style-loader', 'css-loader']});
mod.addBabelPreset(config, '@babel/preset-react');
mod.addBabelPlugin(config, '@babel/plugin-proposal-decorators');
const jsRule = mod.getRule(config, '.js');
const babelLoader = jsRule.use.find(({loader}) => loader==='babel-loader');
assert(babelLoader.options.presets.includes('@babel/preset-env'));
assert(babelLoader.options.presets.find(preset => preset[0]==='@babel/preset-react'));
assert(babelLoader.options.plugins.find(preset => preset[0]==='@babel/plugin-proposal-decorators'));
const entries = mod.getEntries(config);
assert(entries['main'][0] === './path/to/entry-file.js');
console.log("Success");
API
-
setRule(config, filenameExtension, newRule)
Add or modify the rule that matches file names ending withfilenameExtension
. -
getRule(config, filenameExtension, {canBeMissing=false}={})
Get the rule that matches file names ending withfilenameExtension
. -
getEntries(config)
Get all the entries ofconfig
. -
addBabelPlugin(config, babelPlugin)
Add a babel plugin to allbabel-loader
loaders' options object. -
addBabelPreset(config, babelPreset)
Add a babel preset to allbabel-loader
loaders' options object. -
modifyBabelOptions(config, action)
Apply theaction
function to allbabel-loader
loaders' options object. -
addExtension(config, extension)
Addextension
toconfig.resolve.extensions
(only if missing).