Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack IgnorePlugin() on multiple modules

Following the webpack 4.x documentation, we can ignore plugins with IgnorePlugin() method, but the following example only ignore one module (moment/locales)

new webpack.IgnorePlugin({
  resourceRegExp: /^\.\/locale$/,
  contextRegExp: /moment$/
});

If i want to ignore another module (ex : react-tooltip/dist/index.es.js), is there any way to implement another module to ignore, by passing an array with resourceRegExp or contextRegExp for example ?

I tried that :

new webpack.IgnorePlugin(/^\.\/index\.es\.js$/, /react-tooltip\/dist$/),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),

But the file seems to be always present in the bundle

like image 911
Dujard Avatar asked Jun 28 '26 12:06

Dujard


1 Answers

You can do it using the filter function option in IgnorePlugin().

This is how you might do it in Webpack 5:

https://webpack.js.org/plugins/ignore-plugin/#using-filter-functions

new webpack.IgnorePlugin({
    checkResource(resource, context) {
        const isLocaleFromMomentImport =
                /^\.\/locale$/.test(resource) && /moment$/.test(context);
        const isReactTooltipIndexFile = /^\.\/index\.es\.js$/.test(resource) && /react-tooltip\/dist$/.test(context);

        return (
           isLocaleFromMomentImport ||
           isReactTooltipIndexFile
        );
    },
}),

Regarding Webpack 4, IgnorePlugin() has also a filter function option, but it's a bit different (checks resource and context separately). If you are still using that version of webpack you should modify the code from above slightly to match your case... although, I can see that this can be a bit tricky because of the separation of checks...

like image 157
Tevu Avatar answered Jun 30 '26 01:06

Tevu



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!