Skip to content

Commit 70d17c0

Browse files
alexander-akaitclaude
authored andcommitted
fix(library): keep destructured, mixed var, function and class exports live
Hoist a destructuring declaration, every var declared beside a hoisted one, and a reassigned function or class. Only bindings written after their declaration move. Exports bind to generated names, never shadowing a global.
1 parent ba6ff09 commit 70d17c0

22 files changed

Lines changed: 528 additions & 217 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep module library exports live without shadowing other modules' globals.

‎.changeset/060-module-library-live-let-exports.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎lib/dependencies/esm/ESMLiveExportDependency.js‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const NullDependency = require("../core/NullDependency");
2020
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[string[], number[], boolean]>} ObjectSerializerContext */
2121

2222
/**
23-
* A module-level `let` or `var` a module library may declare in the chunk's scope under a
23+
* A module-level binding a module library may declare in the chunk's scope under a
2424
* reserved name, so the library exports the binding itself; it holds every reference.
2525
*/
2626
class ESMLiveExportDependency extends NullDependency {
@@ -134,10 +134,17 @@ ESMLiveExportDependency.Template = class ESMLiveExportDependencyTemplate extends
134134
case ESMLiveExportDependency.SHORTHAND:
135135
source.replace(start, end - 1, `${names[index]}: ${hoisted[index]}`);
136136
break;
137-
default:
137+
case ESMLiveExportDependency.NAMED:
138138
// A property key gives the function or class the name the binding did
139139
source.insert(start, `{ ${names[index]}: `);
140140
source.insert(end, ` }.${names[index]}`);
141+
break;
142+
case ESMLiveExportDependency.PARENTHESIZED:
143+
source.insert(start, "(");
144+
source.insert(end, ")");
145+
break;
146+
default:
147+
source.insert(start, `\n${hoisted[index]} = ${names[index]};\n`);
141148
}
142149
}
143150
}
@@ -151,8 +158,12 @@ ESMLiveExportDependency.SHORTHAND = 1;
151158
ESMLiveExportDependency.NAMED = 2;
152159
/** A declaration's `let` or `var`, dropped with the space up to its first name */
153160
ESMLiveExportDependency.KEYWORD = 3;
161+
/** A destructuring declarator that, without its keyword, needs parentheses */
162+
ESMLiveExportDependency.PARENTHESIZED = 4;
163+
/** Where a function or class declaration, which stays, sets the binding */
164+
ESMLiveExportDependency.INITIALIZE = 5;
154165
/** How many kinds an occurrence's third number tells apart per name */
155-
ESMLiveExportDependency.KINDS_PER_NAME = 4;
166+
ESMLiveExportDependency.KINDS_PER_NAME = 6;
156167

157168
/** @type {Set<keyof DependencyKinds>} */
158169
ESMLiveExportDependency.KINDS = new Set([

0 commit comments

Comments
 (0)