To v4 from v3

This guide only shows major changes that affect end users. For more details please see the changelog.

Node.js v4

If you are still using Node.js v4 or lower, you need to upgrade your Node.js installation to Node.js v6 or higher.

CLI

The CLI has moved to a separate package: webpack-cli. You need to install it before using webpack, see basic setup.

Update plugins

Many 3rd-party plugins need to be upgraded to their latest version to be compatible.

mode

Add the new mode option to your config. Set it to production or development in your configuration depending on config type.

webpack.config.js

  1. module.exports = {
  2. // ...
  3. mode: 'production',
  4. }

Alternatively you can pass it via CLI: --mode production/--mode development

Deprecated/Removed plugins

These plugins can be removed from configuration as they are default in production mode:

webpack.config.js

  1. module.exports = {
  2. // ...
  3. plugins: [
  4. - new NoEmitOnErrorsPlugin(),
  5. - new ModuleConcatenationPlugin(),
  6. - new DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") })
  7. - new UglifyJsPlugin()
  8. ],
  9. }

These plugins are default in development mode

webpack.config.js

  1. module.exports = {
  2. // ...
  3. plugins: [
  4. - new NamedModulesPlugin()
  5. ],
  6. }

These plugins were deprecated and are now removed:

webpack.config.js

  1. module.exports = {
  2. // ...
  3. plugins: [
  4. - new NoErrorsPlugin(),
  5. - new NewWatchingPlugin()
  6. ],
  7. }

CommonsChunkPlugin

The CommonsChunkPlugin was removed. Instead the optimization.splitChunks options can be used.

See documentation of the optimization.splitChunks for more details. The default configuration may already suit your needs.

When generating the HTML from the stats you can use optimization.splitChunks.chunks: "all" which is the optimal configuration in most cases.

import() and CommonJS

When using import() to load non-ESM the result has changed in webpack 4. Now you need to access the default property to get the value of module.exports.

non-esm.js

  1. module.exports = {
  2. sayHello: () => {
  3. console.log('hello world');
  4. }
  5. };

example.js

  1. function sayHello() {
  2. import('./non-esm.js').then(module => {
  3. module.default.sayHello();
  4. });
  5. }

json and loaders

When using a custom loader to transform .json files you now need to change the module type:

webpack.config.js

  1. module.exports = {
  2. // ...
  3. rules: [
  4. {
  5. test: /config\.json$/,
  6. loader: 'special-loader',
  7. + type: 'javascript/auto',
  8. options: {...}
  9. }
  10. ]
  11. };

When still using the json-loader, it can be removed:

webpack.config.js

  1. module.exports = {
  2. // ...
  3. rules: [
  4. {
  5. - test: /\.json$/,
  6. - loader: 'json-loader'
  7. }
  8. ]
  9. };

module.loaders

module.loaders were deprecated since webpack 2 and are now removed in favor of module.rules.


贡献人员

EugeneHlushko EugeneHlushko sokra sokra