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
Copy file name to clipboardExpand all lines: src/content/configuration/experiments.mdx
+53-3Lines changed: 53 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ Available options:
25
25
-[`buildHttp`](#experimentsbuildhttp)
26
26
-[`cacheUnaffected`](#experimentscacheunaffected)
27
27
-[`css`](#experimentscss)
28
+
-[`deferImport`](#experimentsdeferimport)
28
29
-[`futureDefaults`](#experimentsfuturedefaults)
29
30
-`layers`: Enable module and chunk layers.
30
31
-[`lazyCompilation`](#experimentslazycompilation)
@@ -190,9 +191,9 @@ Enable native CSS support. Note that it's an experimental feature still under de
190
191
Experimental features:
191
192
192
193
- CSS Modules support: webpack will generate a unique name for each CSS class. Use the `.module.css` extension for CSS Modules.
193
-
- <Badgetext="5.87.0+" /> Style-specific fields resolution in `package.json` files:
194
-
webpack will look for `style` field in `package.json` files and use that if it
195
-
exists for imports inside CSS files.
194
+
- <Badgetext="5.87.0+" /> Style-specific fields resolution in `package.json`
195
+
files: webpack will look for `style` field in `package.json` files and use
196
+
that if it exists for imports inside CSS files.
196
197
197
198
For example, if you add `@import 'bootstrap';` to your CSS file, webpack will look for `bootstrap` in `node_modules` and use the `style` field in `package.json` from there. If `style` field is not found, webpack will use the `main` field instead to preserve backward compatibility.
198
199
@@ -209,6 +210,55 @@ Enable additional in-memory caching of modules which are unchanged and reference
209
210
210
211
Defaults to the value of [`futureDefaults`](#experimentsfuturedefaults).
211
212
213
+
### experiments.deferImport
214
+
215
+
Enable support of the tc39 proposal [the `import defer` proposal](https://github.com/tc39/proposal-defer-import-eval).
216
+
This allows deferring the evaluation of a module until its first use.
217
+
This is useful to synchronously defer code execution when it's not possible to use `import()` due to its asynchronous nature.
218
+
219
+
- Type: `boolean`
220
+
221
+
This feature requires the runtime environment to have [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) (ES6) support.
You should make sure your loaders do not remove the magic comment.
248
+
249
+
TypeScript, Babel, SWC, and Flow.js can be configured to preserve the magic comment.
250
+
251
+
Esbuild is _not_ compatible with this feature (see [evanw/esbuild#1439](https://github.com/evanw/esbuild/issues/1439) and [evanw/esbuild#309](https://github.com/evanw/esbuild/issues/309)), but it may support this in the future.
252
+
253
+
#### Not implemented
254
+
255
+
The asynchronous form of this proposal is not implemented yet.
256
+
257
+
```js
258
+
import.defer('module-name'); // not implemented
259
+
import(/* webpackDefer: true */'module-name'); // not implemented
260
+
```
261
+
212
262
### experiments.futureDefaults
213
263
214
264
Use defaults of the next major webpack and show warnings in any problematic places.
Copy file name to clipboardExpand all lines: src/content/guides/lazy-loading.mdx
+119-1Lines changed: 119 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ T> This guide is a small follow-up to [Code Splitting](/guides/code-splitting).
21
21
22
22
Lazy, or "on demand", loading is a great way to optimize your site or application. This practice essentially involves splitting your code at logical breakpoints, and then loading it once the user has done something that requires, or will require, a new block of code. This speeds up the initial load of the application and lightens its overall weight as some blocks may never even be loaded.
23
23
24
-
## Example
24
+
## Dynamic Import Example
25
25
26
26
Let's take the example from [Code Splitting](/guides/code-splitting/#dynamic-imports) and tweak it a bit to demonstrate this concept even more. The code there does cause a separate chunk, `lodash.bundle.js`, to be generated and technically "lazy-loads" it as soon as the script is run. The trouble is that no user interaction is required to load the bundle – meaning that every time the page is loaded, the request will fire. This doesn't help us too much and will impact performance negatively.
W> This feature does not lazily "load" a module, but it lazily "evaluates" a module. This means that the module is still downloaded and parsed, but its evaluation is lazy.
106
+
107
+
In some cases, it might be annoying or hard to convert all uses of a module to asynchronous, since it enforces the unnecessary asyncification of all functions, without providing the ability to only defer the synchronous evaluation work.
108
+
109
+
The TC39 proposal [Deferring Module Evaluation](https://github.com/tc39/proposal-defer-import-eval) is to solve this problem.
110
+
111
+
> The proposal is to have a new syntactical import form which will only ever return a namespace exotic object. When used, the module and its dependencies would not be executed, but would be fully loaded to the point of being execution-ready before the module graph is considered loaded.
112
+
>
113
+
> _Only when accessing a property of this module, would the execution operations be performed (if needed)._
114
+
115
+
This feature is available by enabling [experiments.deferImport](/configuration/experiments/#experimentsdeferimport).
116
+
117
+
W> This feature is still in the experimental stage and may change in future versions of webpack.
118
+
119
+
**project**
120
+
121
+
```diff
122
+
webpack-demo
123
+
|- package.json
124
+
|- package-lock.json
125
+
|- webpack.config.js
126
+
|- /dist
127
+
|- /src
128
+
|- index.js
129
+
+ |- print.js
130
+
|- /node_modules
131
+
```
132
+
133
+
**src/print.js**
134
+
135
+
```js
136
+
console.log(
137
+
'The print.js module has loaded! See the network tab in dev tools...'
0 commit comments