Skip to content

docs(guides): update code-splitting against webpack5 #4173

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

Merged

Conversation

chenxsan
Copy link
Member

@chenxsan chenxsan commented Nov 15, 2020

@vercel
Copy link

vercel bot commented Nov 15, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/webpack-docs/webpack-js-org/n2sgne0qf
✅ Preview: https://webpack-js-org-git-feature-code-splitting-against-webpack5.webpack-docs.vercel.app

@@ -40,15 +40,15 @@ related:
url: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
---

T> This guide extends the examples provided in [Getting Started](/guides/getting-started) and [Output Management](/guides/output-management). Please make sure you are at least familiar with the examples provided in them.
T> This guide extends the example provided in [Getting Started](/guides/getting-started). Please make sure you are at least familiar with the example provided there and the [Output Management](/guides/output-management/) chapter.
Copy link
Member Author

@chenxsan chenxsan Nov 15, 2020

Choose a reason for hiding this comment

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

It's very confusing to extend example of this guide from two sources. Hence I limit it to one.

- Prevent Duplication: Use the [`SplitChunksPlugin`](/plugins/split-chunks-plugin/) to dedupe and split chunks.
- Dynamic Imports: Split code via inline function calls within modules.
- __Entry Points__: Manually split code using [`entry`](/configuration/entry-context) configuration.
- __Prevent Duplication__: Use [Entry dependencies](/configuration/entry-context/#dependencies) or [`SplitChunksPlugin`](/plugins/split-chunks-plugin/) to dedupe and split chunks.
Copy link
Member Author

Choose a reason for hiding this comment

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

We have two sections below instead of one.


### `SplitChunksPlugin`

The [`SplitChunksPlugin`](/plugins/split-chunks-plugin/) allows us to extract common dependencies into an existing entry chunk or an entirely new chunk. Let's use this to de-duplicate the `lodash` dependency from the previous example:

W> The `CommonsChunkPlugin` has been removed in webpack v4 legato. To learn how chunks are treated in the latest version, check out the [`SplitChunksPlugin`](/plugins/split-chunks-plugin/).
Copy link
Member Author

Choose a reason for hiding this comment

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

There's no CommonsChunkPlugin in this page anymore.

```

As `import()` returns a promise, it can be used with [`async` functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function). However, this requires using a pre-processor like Babel and the [Syntax Dynamic Import Babel Plugin](https://babeljs.io/docs/plugins/syntax-dynamic-import/#installation). Here's how it would simplify the code:
As `import()` returns a promise, it can be used with [`async` functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function). Here's how it would simplify the code:
Copy link
Member Author

Choose a reason for hiding this comment

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

Webpack 5 supports async modules https://webpack.js.org/blog/2020-10-10-webpack-5-release/#async-modules, no need to configure babel in my test.


Simple prefetch example can be having a `HomePage` component, which renders a `LoginButton` component which then on demand loads a `LoginModal` component after being clicked.

__LoginButton.js__

```js
//...
import(/* webpackPrefetch: true */ 'LoginModal');
import(/* webpackPrefetch: true */ './path/to/LoginModal.js');
Copy link
Member Author

Choose a reason for hiding this comment

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

Relative path makes more sense here.

@chenxsan chenxsan closed this Nov 15, 2020
@chenxsan chenxsan reopened this Nov 15, 2020
@chenxsan chenxsan marked this pull request as ready for review November 15, 2020 13:38
@chenxsan chenxsan closed this Nov 15, 2020
@chenxsan chenxsan reopened this Nov 15, 2020
@chenxsan
Copy link
Member Author

Have no idea why Vercel Deployment keeps failing. I was able to run yarn build locally.

@chenxsan chenxsan merged commit f94c9e4 into webpack:master Nov 17, 2020
@chenxsan chenxsan deleted the feature/code-splitting-against-webpack5 branch November 17, 2020 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants