Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migration guide #1939

Merged
merged 7 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/content/migrate/3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: To webpack v2, v3
sort: 1
title: To v2 or v3 from v1
sort: 2
contributors:
- sokra
- jhnns
Expand Down
158 changes: 158 additions & 0 deletions src/content/migrate/4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
title: To v4 from v3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep titles consistent.

sort: 1
contributors:
- sokra
- EugeneHlushko
---

This guide only shows major changes that affect end users. For more details please see [the changelog](https://github.com/webpack/webpack/releases).


## 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](/guides/getting-started/#basic-setup).


## Update plugins

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


## mode

Add the new [`mode`](/concepts/mode/) option to your config. Set it to production or development in your configuration depending on config type.

__webpack.config.js__

``` diff
module.exports = {
// ...
mode: 'production',
}
```

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__

``` diff
module.exports = {
// ...
plugins: [
- new NoEmitOnErrorsPlugin(),
- new ModuleConcatenationPlugin(),
- new DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") })
- new UglifyJsPlugin()
],
}
```

These plugins are default in development mode

__webpack.config.js__

``` diff
module.exports = {
// ...
plugins: [
- new NamedModulesPlugin()
],
}
```

These plugins were deprecated and are now removed:

__webpack.config.js__

``` diff
module.exports = {
// ...
plugins: [
- new NoErrorsPlugin(),
- new NewWatchingPlugin()
],
}
```


## CommonsChunkPlugin

The `CommonsChunkPlugin` was removed. Instead the [`optimization.splitChunks`](/configuration/optimization/#optimization-splitchunks/) options can be used.

See documentation of the [`optimization.splitChunks`](/configuration/optimization/#optimization-splitchunks/) for more details. The default configuration may already suit your needs.

T> 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__

``` javascript
module.exports = {
sayHello: () => {
console.log('hello world');
}
};
```

__example.js__

``` javascript
function sayHello() {
import('./non-esm.js').then(module => {
module.default.sayHello();
});
}
```

## json and loaders

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

__webpack.config.js__

``` diff
module.exports = {
// ...
rules: [
{
test: /config\.json$/,
loader: 'special-loader',
+ type: 'javascript/auto',
options: {...}
}
]
};
```

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

__webpack.config.js__

``` diff
module.exports = {
// ...
rules: [
{
- test: /\.json$/,
- loader: 'json-loader'
}
]
};
```

## module.loaders

`module.loaders` were deprecated since webpack 2 and are now removed in favor of [`module.rules`](/configuration/module/#rule).