@brillout/webpack-config-mod

0.5.13 • Public • Published

@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 with filenameExtension.

  • getRule(config, filenameExtension, {canBeMissing=false}={})
    Get the rule that matches file names ending with filenameExtension.

  • getEntries(config)
    Get all the entries of config.

  • addBabelPlugin(config, babelPlugin)
    Add a babel plugin to all babel-loader loaders' options object.

  • addBabelPreset(config, babelPreset)
    Add a babel preset to all babel-loader loaders' options object.

  • modifyBabelOptions(config, action)
    Apply the action function to all babel-loader loaders' options object.

  • addExtension(config, extension)
    Add extension to config.resolve.extensions (only if missing).

Dependencies (2)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @brillout/webpack-config-mod

    Weekly Downloads

    5

    Version

    0.5.13

    License

    none

    Unpacked Size

    25.3 kB

    Total Files

    6

    Last publish

    Collaborators

    • brillout