Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set diffrent plugin options with webpack-merge

base https://webpack.js.org/guides/production/

I want to use diffrent html template in webpack.dev.js & webpack.prod.js

webpack.common.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        app: './src/index.js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'Production'
        }),
    ]
};

webpack.dev.js

const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
    mode: 'development',
    plugins: [
        new HtmlWebpackPlugin({
            template:'./public/index-dev.html',
        })
    ]
});

webpack.prod.js

const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
    mode: 'production',
    plugins: 
        new HtmlWebpackPlugin({
            template:'./public/index.html',
        })
    ]
});

report ReferenceError: HtmlWebpackPlugin is not defined when npm run with these config

like image 784
LinTao Avatar asked Jul 21 '26 20:07

LinTao


1 Answers

With out an explicit require/import you cann't directly access HtmlWebpackPlugin or anything else from one config file in another. Merge only joins the config portion not the whole files.

Therefor you must have const HtmlWebpackPlugin = require('html-webpack-plugin'); in all files where HtmlWebpackPlugin is used.

webpage-merge is does not merge all file data just the module.exports object itself.

like image 122
wheredidthatnamecomefrom Avatar answered Jul 24 '26 10:07

wheredidthatnamecomefrom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!