Skip to content

Commit a6c595e

Browse files
authored
docs(guides/tree-shaking): document #__NO_SIDE_EFFECTS__ annotation (#8238)
1 parent 99cf930 commit a6c595e

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/content/guides/tree-shaking.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,34 @@ This behavior is enabled when [`optimization.innerGraph`](/configuration/optimiz
518518
/* #__PURE__ */ double(55);
519519
```
520520

521+
## Mark a function declaration as side-effect-free
522+
523+
<Badge text="5.107.0+" />
524+
525+
Webpack also supports the [`#__NO_SIDE_EFFECTS__`](https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/no-side-effects-notation-spec.md) annotation to mark a function declaration as pure. Calls to a function annotated this way can be eliminated from the bundle when their return value is unused, even if the function body is not statically analyzable as pure. This is useful for factory or builder functions whose call sites would otherwise need a `/*#__PURE__*/` annotation each time.
526+
527+
{/* eslint-disable */}
528+
529+
```js
530+
// utils.js
531+
/*#__NO_SIDE_EFFECTS__*/
532+
export function createLogger(prefix) {
533+
return (msg) => console.log(`[${prefix}] ${msg}`);
534+
}
535+
```
536+
537+
{/* eslint-enable */}
538+
539+
```js
540+
// app.js
541+
import { createLogger } from "./utils";
542+
543+
// dropped, because `createLogger` is annotated and its result is unused
544+
const unused = createLogger("debug");
545+
```
546+
547+
W> The annotation currently only takes effect within the module where it is declared. Cross-module propagation is planned for a future release.
548+
521549
## Minify the Output
522550

523551
So we've cued up our "dead code" to be dropped by using the `import` and `export` syntax, but we still need to drop it from the bundle. To do that, set the `mode` configuration option to [`production`](/configuration/mode/#mode-production).

0 commit comments

Comments
 (0)