Skip to content

Commit 99cf930

Browse files
authored
docs: add sourceImport experiment feature (#8157)
* docs: add sourceImport experiment feature * docs(experiments): clarify sourceImport scope (WASM-only) and add usage example Address review feedback on #8157: the current 5.106 implementation of experiments.sourceImport is WebAssembly-only, but the docs as written suggested generic source-phase support for any module. Updates the section to: - State explicitly that the current implementation targets WASM and that JavaScript source imports are planned for a future release. - Add a short usage example with the static (import source ...) and dynamic (import.source(...)) syntax for a .wasm import. - Link to the wasm-simple-source-phase example in the webpack repo. Also moves the version badge directly under the heading to match the style of other experiment sections.
1 parent f992f03 commit 99cf930

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/content/configuration/experiments.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Available options:
2929
- [`futureDefaults`](#experimentsfuturedefaults)
3030
- [`lazyCompilation`](#experimentslazycompilation)
3131
- [`outputModule`](#experimentsoutputmodule)
32+
- [`sourceImport`](#experimentssourceimport)
3233
- `syncWebAssembly`: Support the old WebAssembly like in webpack 4.
3334
- `layers`: Enable module and chunk layers, removed and works without additional options since `5.102.0`.
3435
- `topLevelAwait`: Transforms a module into an `async` module when an `await` is used at the top level. Starting from webpack version `5.83.0` (however, in versions prior to that, you can enable it by setting `experiments.topLevelAwait` to `true`), this feature is enabled by default, removed and works without additional options since `5.102.0`.
@@ -43,6 +44,7 @@ export default {
4344
buildHttp: true,
4445
lazyCompilation: true,
4546
outputModule: true,
47+
sourceImport: true,
4648
syncWebAssembly: true,
4749
topLevelAwait: true,
4850
},
@@ -278,6 +280,43 @@ It's suggested to put the magic comment after the `from` keyword. Other position
278280

279281
Putting the magic comment after the `import` keyword is incompatible with the filesystem cache.
280282

283+
### experiments.sourceImport
284+
285+
<Badge text="5.106.0+" />
286+
287+
Enable support for the tc39 proposal [Source Phase Imports](https://github.com/tc39/proposal-source-phase-imports).
288+
289+
This proposal introduces a way to import a module at the _source phase_ instead of immediately evaluating it. In webpack, this experimental support is currently implemented for **WebAssembly modules**: you obtain a compiled [`WebAssembly.Module`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module) first and instantiate it later with your own imports. Support for JavaScript source imports is planned for a future release.
290+
291+
- Type: `boolean`
292+
293+
Enable it alongside `asyncWebAssembly`:
294+
295+
```js
296+
// webpack.config.js
297+
export default {
298+
// ...
299+
experiments: {
300+
asyncWebAssembly: true,
301+
sourceImport: true,
302+
},
303+
};
304+
```
305+
306+
Then use either the static or dynamic source-phase syntax with a `.wasm` import:
307+
308+
```text
309+
// Static form
310+
import source wasmModule from "./module.wasm";
311+
312+
// Dynamic form
313+
const wasmModule2 = await import.source("./module.wasm");
314+
315+
const instance = await WebAssembly.instantiate(wasmModule);
316+
```
317+
318+
A full example is available in the webpack repository: [examples/wasm-simple-source-phase](https://github.com/webpack/webpack/tree/main/examples/wasm-simple-source-phase).
319+
281320
{/* eslint-skip */}
282321

283322
```js

0 commit comments

Comments
 (0)