Given you’re using module logger
:
// src/index.js
const logger = require('./logger');
logger('hi');
Your bundle is built using the webpack config:
// webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
},
};
When you build the bundle, how can logger
be removed?
NODE_ENV=development npx webpack
asset main.js 2.46 KiB [emitted] (name: main)
./src/index.js 50 bytes [built] [code generated]
./src/logger.js 30 bytes [built] [code generated]
webpack 5.37.0 compiled successfully in 67 ms
IgnorePlugin
With IgnorePlugin, a regular expression can be tested to prevent the module from being generated:
// webpack.config.js
+const { IgnorePlugin } = require('webpack');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
},
+ plugins: [
+ new IgnorePlugin({
+ resourceRegExp: /logger/,
+ }),
+ ],
};
When you build the bundle, notice how logger
is no longer part of the generated code:
webpack-dev-serverNODE_ENV=development npx webpack
asset main.js 2.33 KiB [emitted] (name: main)
./src/index.js 50 bytes [built] [code generated]
webpack 5.37.0 compiled successfully in 61 ms
But when you open your webpage:
<!-- index.html -->
<script src="dist/main.js"></script>
The Console will show a JavaScript error:
Uncaught Error: Cannot find module './logger'
at webpackMissingModule (index.js:1)
at eval (index.js:1)
at Object../src/index.js (main.js:18)
at __webpack_require__ (main.js:42)
at main.js:53
at main.js:55
This is because logger
is still required in src/index.js
.
DefinePlugin
To have webpack prune unused code, use DefinePlugin:
// webpack.config.js
-const { IgnorePlugin } = require('webpack');
+const { DefinePlugin, IgnorePlugin } = require('webpack');
+const { NODE_ENV } = process.env;
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
},
plugins: [
- new IgnorePlugin({
- resourceRegExp: /logger/,
- }),
+ NODE_ENV === 'production' &&
+ new IgnorePlugin({
+ resourceRegExp: /logger/,
+ }),
+ new DefinePlugin({
+ 'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
+ }),
],
};
The plugin allows you to create global constants that are injected at compile time:
// src/index.js
if (process.env.NODE_ENV === 'development') {
const logger = require('./logger');
logger('hi');
}
Build the bundle:
NODE_ENV=production npx webpack
asset main.js 0 bytes [emitted] [minimized] (name: main)
./src/index.js 102 bytes [built] [code generated]
webpack 5.37.0 compiled successfully in 134 ms
Open the webpage to see no Console errors.
open index.html
To learn more about Webpack global constants, check out the following post.