You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As mentioned there are some pitfalls to this approach:
85
97
86
98
- If there are any duplicated modules between entry chunks they will be included in both bundles.
87
99
- It isn't as flexible and can't be used to dynamically split code with the core application logic.
88
100
89
-
The first of these two points is definitely an issue for our example, as `lodash` is also imported within `./src/index.js` and will thus be duplicated in both bundles. See the `CommonsChunkPlugin` example below for a solution to this problem.
101
+
The first of these two points is definitely an issue for our example, as `lodash` is also imported within `./src/index.js` and will thus be duplicated in both bundles. Let's remove this duplication by using the `CommonsChunkPlugin`.
90
102
91
103
92
104
## Prevent Duplication
@@ -118,7 +130,20 @@ __webpack.config.js__
118
130
119
131
With the `CommonsChunkPlugin` in place, we should now see the duplicate dependency removed from our `index.bundle.js`. The plugin should notice that we've separated `lodash` out to a separate chunk and remove the dead weight from our main bundle. Let's do an `npm run build` to see if it worked:
Here are some other useful plugins and loaders provide by the community for splitting code:
124
149
@@ -131,7 +156,7 @@ Here are some other useful plugins and loaders provide by the community for spli
131
156
132
157
Two similar techniques are supported by webpack when it comes to dynamic code splitting. The first and more preferable approach is use to the [`import()` syntax](/api/module-methods#import-) that conforms to the [ECMAScript proposal](https://github.com/tc39/proposal-dynamic-import) for dynamic imports. The legacy, webpack-specific approach is to use [`require.ensure`](/api/module-methods#require-ensure). Let's try using the first of these two approaches...
133
158
134
-
Before we start, let's remove the `entry` and `CommonsChunkPlugin` from our config as they won't be needed for this next demonstration:
159
+
Before we start, let's remove the extra `entry` and `CommonsChunkPlugin` from our config as they won't be needed for this next demonstration:
135
160
136
161
__webpack.config.js__
137
162
@@ -143,17 +168,16 @@ __webpack.config.js__
143
168
entry: {
144
169
+ index: './src/index.js'
145
170
- index: './src/index.js',
146
-
- vendor: [
147
-
- 'lodash'
148
-
- ]
171
+
- another: './src/another-module.js'
149
172
},
150
173
- plugins: [
151
174
- new webpack.optimize.CommonsChunkPlugin({
152
-
- name: 'vendor' // Specify the common bundle's name.
175
+
- name: 'common' // Specify the common bundle's name.
153
176
- })
154
177
- ],
155
178
output: {
156
179
filename: '[name].bundle.js',
180
+
+ chunkFilename: '[name].bundle.js',
157
181
path: path.resolve(__dirname, 'dist')
158
182
}
159
183
};
@@ -190,7 +214,18 @@ __src/index.js__
190
214
191
215
Note the use of `webpackChunkName` in the comment. This will cause our separate bundle to be named `lodash.bundle.js` instead of just `[id].bundle.js`. For more information on `webpackChunkName` and the other available options, see the [`import()` documentation](/api/module-methods#import-). Let's run webpack to see lodash separated out to a separate bundle:
If you've enabled [`async` functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) via a pre-processor like babel, note that you can simplify the code as `import()` statements just return promises:
0 commit comments