Skip to content

Commit 28d9eef

Browse files
committed
docs(guides): begin moving code-splitting-async content
Much of this content is reference-like and fits more into the module-methods page. The rest is either being moved into code-splitting.md or lazy-loading.md.
1 parent a996f66 commit 28d9eef

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

content/api/module-methods.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export default {
5353

5454
### `import()`
5555

56-
Dynamically load modules.
56+
`import('path/to/module') -> Promise`
57+
58+
Dynamically load modules. Calls to `import()` are treated as split points, meaning the requested module and it's children are split out into a separate chunk.
59+
60+
T> The [ES2015 Loader spec](https://whatwg.github.io/loader/) defines `import()` as method to load ES2015 modules dynamically on runtime.
5761

5862
``` javascript
5963
if ( module.hot ) {
@@ -63,8 +67,33 @@ if ( module.hot ) {
6367
}
6468
```
6569

66-
The compiler treats this as a split point and will split everything from `lodash` into a separate bundle. This returns a promise that will resolve to the module once the bundle has been loaded. See the [async code splitting guide](/guides/code-splitting-async) for more information.
70+
W> This feature relies on [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) internally. If you use `import()` with older browsers, remember to shim `Promise` using a polyfill such as [es6-promise](https://github.com/stefanpenner/es6-promise) or [promise-polyfill](https://github.com/taylorhakes/promise-polyfill). See [Shimming](/guides/shimming) for more information.
71+
72+
One problem with direct spec-based usage is that we have no control over the split chunk's name or other properties. Luckily webpack allows some special parameters via comments so as to not break the spec:
73+
74+
``` js
75+
import(
76+
/* webpackChunkName: "my-chunk-name" */
77+
/* webpackMode: "lazy" */
78+
'module'
79+
);
80+
```
81+
82+
`webpackChunkName`: A name for the new chunk. Since webpack 2.6.0, the placeholders `[index]` and `[request]` are supported within the given string to an incremented number or the actual resolved filename respectively.
83+
84+
`webpackMode`: Since webpack 2.6.0, different modes for resolving dynamic imports can be specified. The following options are supported:
85+
86+
- `"lazy"` (default): Generates a chunk per request, meaning everything is lazy loaded.
87+
- `"lazy-once"`: Generates a single chunk for all possible requests. The first call initiates a network request for all modules, and any following requests are already fulfilled. This is only available for when importing an expression.
88+
- `"eager"`: Generates no chunk. All modules are included in the current chunk and no additional network requests are made. A `Promise` is still returned but is already resolved. In contrast to a static import, the module isn't executed until the request is made.
89+
90+
T> Note that both options can be combined like so `/* webpackMode: "lazy-once", webpackChunkName: "all-i18n-data" */`. This is parsed as a JSON5 object without curly brackets.
91+
92+
W> Fully dynamic statements, such as `import(foo)`, __will fail__ because webpack requires at least some file location information. This is because `foo` could potentially be any path to any file in your system or project. The `import()` must contain at least some information about where the module is located, so bundling can be limited to a specific directory or set of files.
93+
94+
W> The entire module namespace is included. For example, ``import(`./locale/${language}.json`)`` will cause every `.json` file in the `./locale` directory to be bundled into the new chunk. At run time, when the variable `language` has been computed, any file like `english.json` or `german.json` will be available for consumption.
6795

96+
W> The use of `System.import` in webpack [did not fit the proposed spec](https://github.com/webpack/webpack/issues/2163), so it was deprecated in webpack [2.1.0-beta.28](https://github.com/webpack/webpack/releases/tag/v2.1.0-beta.28) in favor of `import()`.
6897

6998

7099
## CommonJS

content/guides/code-splitting.md

+37
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,45 @@ Here are some other useful plugins and loaders provide by the community for spli
106106

107107
## Dynamic Imports
108108

109+
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...
110+
109111
...
110112

113+
?> Update the following example to match with _Getting Started_
114+
115+
__src/index.js__
116+
117+
```javascript
118+
function determineDate() {
119+
import('moment').then(function(moment) {
120+
console.log(moment().format());
121+
}).catch(function(err) {
122+
console.log('Failed to load moment', err);
123+
});
124+
}
125+
126+
determineDate();
127+
```
128+
129+
?> Note `async`/`await`... from code-splitting-async:
130+
131+
> To use ES2017 [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)/[`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) with `import()`:
132+
>
133+
> ```bash
134+
> npm install --save-dev babel-plugin-transform-async-to-generator babel-plugin-transform-regenerator babel-plugin-transform-runtime babel-plugin-syntax-async-functions
135+
> ```
136+
>
137+
> __index-es2017.js__
138+
>
139+
> ```javascript
140+
> async function determineDate() {
141+
> const moment = await import('moment');
142+
> return moment().format('LLLL');
143+
> }
144+
>
145+
> determineDate().then(str => console.log(str));
146+
> ```
147+
111148
112149
## Next Steps
113150

content/guides/lazy-loading.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ contributors:
55
- iammerrick
66
- chrisVillanueva
77
- skipjack
8+
related:
9+
- title: Lazy Loading ES2015 Modules in the Browser
10+
url: https://dzone.com/articles/lazy-loading-es2015-modules-in-the-browser
811
---
912

1013
T> This guide is a small follow-up to [Code Splitting](/guides/code-splitting). If you have not yet read through that guide, please do so now.

0 commit comments

Comments
 (0)