Skip to content

Commit 3d912a0

Browse files
authored
docs(guides): mention webpackExports in tree shaking and code splitting (#8067)
1 parent 789ca05 commit 3d912a0

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/content/guides/code-splitting.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ cacheable modules 530 KiB
376376
webpack 5.4.0 compiled successfully in 268 ms
377377
```
378378

379+
T> You can also use magic comment `webpackExports` with `import()` to expose specific exports from a dynamically imported module:
380+
381+
```js
382+
import(
383+
/* webpackExports: ["default", "namedExport"] */
384+
"./module"
385+
);
386+
```
387+
388+
This can help webpack tree shake other unused exports. See [Magic Comments](/api/module-methods/#magic-comments) for details.
389+
379390
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:
380391

381392
**src/index.js**

src/content/guides/tree-shaking.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ The [`sideEffects`](/configuration/optimization/#optimizationsideeffects) and [`
187187

188188
`usedExports` relies on [terser](https://github.com/terser-js/terser) to detect side effects in statements. It is a difficult task in JavaScript and not as effective as straightforward `sideEffects` flag. It also can't skip subtree/dependencies since the spec says that side effects need to be evaluated. While exporting function works fine, React's Higher Order Components (HOC) are problematic in this regard.
189189

190+
If you're using dynamic `import()`, you can also use the `webpackExports` magic comment to specify the exports that should be exposed, allowing webpack to tree shake the others. See [Magic Comments](/api/module-methods/#magic-comments).
191+
190192
Let's make an example:
191193

192194
```js

0 commit comments

Comments
 (0)