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
{{ message }}
Repository navigation
Commit c04058e
Browse filesBrowse the repository at this point in the historyBrowse files
docs(examples): show how to expose a module to the global object (#21952)
* docs(examples): keep the add-exports plugin in the configuration
It is the shape the add-imports example settled on for the same hook:
the plugin is a function next to the table it reads, so no `internals/`
file stands between the reader and the configuration.
* docs(examples): show how to expose a module to the global object
A script that is not part of the bundle reads what it needs off the
global object, and putting it there is one appended line - the same
`processResult` hook the add-exports and add-imports examples use.
webpack parses the line as the module's own, so the reference is
analyzed like any other: in production both modules are hoisted into the
entry, the exposed bindings are renamed and the assignments rename with
them, while `PI`, which nothing reads, is shaken out.
* docs(examples): make the processResult plugins classes
A class with `apply()` is what a webpack plugin is, and what the three
examples on this hook now show - the data it works from passed to its
constructor, so the configuration reads as the table it is. No function
wrapper stands in for the class in any of them.
The emitted output of all three is byte for byte what it was.
* docs(examples): say which mode the expose-global output is from
The renaming, hoisting and shaking the prose points at are production's;
the bundle printed under it is the unoptimized one, which keeps both
exports. And nothing rewrites the appended `globalThis` - a target
without it is one where the line itself has to say `self` or `window`.
* docs(examples): say which mode the add-exports output is from
Production folds `add(PI, 1)` to `3.14 + 1` - the inlining and hoisting
the prose points at - while the bundle printed under it is the
unoptimized one, which keeps both exports and calls through the import.
Copy file name to clipboardExpand all lines: examples/add-exports/README.md
+6-16Lines changed: 6 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,20 @@
2
2
3
3
Some files export nothing: a legacy script that only defines globals, a vendored file, a bundle that was never written as a module. Appending the exports before webpack parses the file is enough — the parser reads them as the module's own.
4
4
5
-
No loader is needed for that. `NormalModule`'s `processResult` hook hands a plugin what the loaders produced — source, source map and any preparsed AST — and takes back a replacement.
5
+
No loader is needed for that. `NormalModule`'s `processResult` hook hands a plugin what the loaders produced — source, source map and any preparsed AST — and takes back a replacement. It is the same hook [adding imports](../add-imports) uses, from the other end of the file, and it is small enough to keep in the configuration.
6
6
7
-
Which format to append is the module's own, and the two are not equivalent. `export { … }` is what makes a file an ES module, exactly as it would be in the source, so those exports are analyzable: below, `math.js` has its `PI` inlined into the call site and its `add` hoisted into the entry, like any `export` webpack reads. `module.exports = …` keeps the file the script it is, and webpack treats it as it treats any module whose whole exports object is assigned a value — `legacy-global.js` below keeps runtime-defined exports and is not analyzed that way. Prefer the ES module form where the file tolerates it.
7
+
Which format to append is the module's own, and the two are not equivalent. `export { … }` is what makes a file an ES module, exactly as it would be in the source, so those exports are analyzable: under `mode: "production"`, `math.js` has its `PI` inlined into the call site and its `add` hoisted into the entry, like any `export` webpack reads, and the unoptimized output below keeps both. `module.exports = …` keeps the file the script it is, and webpack treats it as it treats any module whose whole exports object is assigned a value — `legacy-global.js` below keeps runtime-defined exports and is not analyzed that way. Prefer the ES module form where the file tolerates it.
8
8
9
9
Appending moves nothing before it, so the source map is still valid; a preparsed AST is dropped, because webpack would otherwise parse that instead of the appended code.
Copy file name to clipboardExpand all lines: examples/add-exports/template.md
+2-8Lines changed: 2 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,12 @@
2
2
3
3
Some files export nothing: a legacy script that only defines globals, a vendored file, a bundle that was never written as a module. Appending the exports before webpack parses the file is enough — the parser reads them as the module's own.
4
4
5
-
No loader is needed for that. `NormalModule`'s `processResult` hook hands a plugin what the loaders produced — source, source map and any preparsed AST — and takes back a replacement.
5
+
No loader is needed for that. `NormalModule`'s `processResult` hook hands a plugin what the loaders produced — source, source map and any preparsed AST — and takes back a replacement. It is the same hook [adding imports](../add-imports) uses, from the other end of the file, and it is small enough to keep in the configuration.
6
6
7
-
Which format to append is the module's own, and the two are not equivalent. `export { … }` is what makes a file an ES module, exactly as it would be in the source, so those exports are analyzable: below, `math.js` has its `PI` inlined into the call site and its `add` hoisted into the entry, like any `export` webpack reads. `module.exports = …` keeps the file the script it is, and webpack treats it as it treats any module whose whole exports object is assigned a value — `legacy-global.js` below keeps runtime-defined exports and is not analyzed that way. Prefer the ES module form where the file tolerates it.
7
+
Which format to append is the module's own, and the two are not equivalent. `export { … }` is what makes a file an ES module, exactly as it would be in the source, so those exports are analyzable: under `mode: "production"`, `math.js` has its `PI` inlined into the call site and its `add` hoisted into the entry, like any `export` webpack reads, and the unoptimized output below keeps both. `module.exports = …` keeps the file the script it is, and webpack treats it as it treats any module whose whole exports object is assigned a value — `legacy-global.js` below keeps runtime-defined exports and is not analyzed that way. Prefer the ES module form where the file tolerates it.
8
8
9
9
Appending moves nothing before it, so the source map is still valid; a preparsed AST is dropped, because webpack would otherwise parse that instead of the appended code.
0 commit comments