Skip to content

Commit 8b8905f

Browse files
committed
fix(esm): hash whether a module's exports are values or getters
An `export const` or `export default` is defined as a value unless the module is in a cycle, which is decided after the module is built and was in no hash. So when another module's edit formed or broke a require() cycle around it, watch kept the old form: mid-cycle, the module showed none of those exports. Hash the decision where each export makes it.
1 parent fa15f02 commit 8b8905f

14 files changed

Lines changed: 103 additions & 2 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+
Regenerate a module's exports when a require() cycle forms or breaks around it.

‎lib/dependencies/esm/ESMExportExpressionDependency.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const { ExportBindingType } = ESMExportInitFragment;
2121
const DEFAULT_EXPORT_NAME = "default";
2222

2323
/** @import { ReplaceSource } from "webpack-sources" */
24-
/** @import { ExportsSpec, LazyUntil } from "../../graph/Dependency" */
24+
/** @import { ExportsSpec, LazyUntil, UpdateHashContext } from "../../graph/Dependency" */
25+
/** @import Module, { BuildInfo } from "../../module/Module" */
26+
/** @import Hash from "../../util/Hash" */
2527
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
2628
/** @import ModuleGraph from "../../graph/ModuleGraph" */
2729
/** @import { ConnectionState } from "../../graph/ModuleGraphConnection" */
@@ -119,6 +121,24 @@ class ESMExportExpressionDependency extends NullDependency {
119121
return false;
120122
}
121123

124+
/**
125+
* Updates the hash with the data contributed by this instance.
126+
* @param {Hash} hash hash to be updated
127+
* @param {UpdateHashContext} context context
128+
* @returns {void}
129+
*/
130+
updateHash(hash, context) {
131+
// the module's cycles, known only after it is built, pick value or getter
132+
const module = /** @type {Module} */ (
133+
context.chunkGraph.moduleGraph.getParentModule(this)
134+
);
135+
hash.update(
136+
/** @type {BuildInfo} */ (module.buildInfo).isCircular === false
137+
? "value"
138+
: "getter"
139+
);
140+
}
141+
122142
/**
123143
* Serializes this instance into the provided serializer context.
124144
* @param {ObjectSerializerContext} context context

‎lib/dependencies/esm/ESMExportSpecifierDependency.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const ESMExportInitFragment = require("./ESMExportInitFragment");
1414
const { ExportBindingType } = ESMExportInitFragment;
1515

1616
/** @import { ReplaceSource } from "webpack-sources" */
17-
/** @import { ExportsSpec, LazyUntil } from "../../graph/Dependency" */
17+
/** @import { ExportsSpec, LazyUntil, UpdateHashContext } from "../../graph/Dependency" */
18+
/** @import Module, { BuildInfo } from "../../module/Module" */
19+
/** @import Hash from "../../util/Hash" */
1820
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
1921
/** @import ModuleGraph from "../../graph/ModuleGraph" */
2022
/** @import { ConnectionState } from "../../graph/ModuleGraphConnection" */
@@ -101,6 +103,25 @@ class ESMExportSpecifierDependency extends NullDependency {
101103
return false;
102104
}
103105

106+
/**
107+
* Updates the hash with the data contributed by this instance.
108+
* @param {Hash} hash hash to be updated
109+
* @param {UpdateHashContext} context context
110+
* @returns {void}
111+
*/
112+
updateHash(hash, context) {
113+
// the module's cycles, known only after it is built, pick value or getter
114+
if (this.constValue === undefined) return;
115+
const module = /** @type {Module} */ (
116+
context.chunkGraph.moduleGraph.getParentModule(this)
117+
);
118+
hash.update(
119+
/** @type {BuildInfo} */ (module.buildInfo).isCircular === false
120+
? "value"
121+
: "getter"
122+
);
123+
}
124+
104125
/**
105126
* Serializes this instance into the provided serializer context.
106127
* @param {ObjectSerializerContext} context context
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import b from "./b.js";
2+
3+
export const value = 1;
4+
export function keys() {
5+
return b.keys;
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.keys = null;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import d from "./d.js";
2+
3+
export default 2;
4+
export function keys() {
5+
return d.keys;
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.keys = null;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// `export const` in a.js, `export default` in c.js
2+
for (const [file, load, names] of [
3+
["a.js", () => require("./a.js"), ["keys", "value"]],
4+
["c.js", () => require("./c.js"), ["default", "keys"]]
5+
]) {
6+
it(`should define ${file}'s exports up front once a require() cycle reaches it`, () => {
7+
const module = load();
8+
// every export used as a whole object, so none is inlined or renamed
9+
expect(Object.keys(module).sort()).toEqual(names);
10+
expect(module.keys()).toEqual(WATCH_STEP === "1" ? names : null);
11+
});
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// read while a.js is still evaluating, so only bindings defined up front show
2+
exports.keys = Object.keys(require("./a.js")).sort();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// read while c.js is still evaluating, so only bindings defined up front show
2+
exports.keys = Object.keys(require("./c.js")).sort();

0 commit comments

Comments
 (0)