Skip to content

Commit 0500b33

Browse files
committed
fix: hash inlined export literals at the dependency level
1 parent 7a76fef commit 0500b33

6 files changed

Lines changed: 85 additions & 7 deletions

File tree

‎lib/dependencies/HarmonyExportImportedSpecifierDependency.js‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const Template = require("../Template");
1414
const {
1515
InlinedUsedName,
1616
isExportInlined,
17-
isInlineEnabled
17+
isInlineEnabled,
18+
isInlineExportsEnabled
1819
} = require("../optimize/InlineExports");
1920
const {
2021
getKnownExportNames,
@@ -45,7 +46,8 @@ const processExportInfo = require("./processExportInfo");
4546
* RawReferencedExports,
4647
* ReferencedExports,
4748
* TRANSITIVE,
48-
* ExportInfoName
49+
* ExportInfoName,
50+
* UpdateHashContext
4951
* } from "../Dependency"
5052
*/
5153
/** @import { DependencyTemplateContext } from "../DependencyTemplate" */
@@ -59,6 +61,7 @@ const processExportInfo = require("./processExportInfo");
5961
* } from "../Module"
6062
*/
6163
/** @import ModuleGraph from "../ModuleGraph" */
64+
/** @import Hash from "../util/Hash" */
6265
/**
6366
* @import ModuleGraphConnection, {
6467
* ConnectionState
@@ -830,6 +833,31 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
830833
};
831834
}
832835

836+
/**
837+
* Updates the hash with the data contributed by this instance.
838+
* @param {Hash} hash hash to be updated
839+
* @param {UpdateHashContext} context context
840+
* @returns {void}
841+
*/
842+
updateHash(hash, context) {
843+
const { chunkGraph, runtime } = context;
844+
const moduleGraph = chunkGraph.moduleGraph;
845+
if (!isInlineExportsEnabled(moduleGraph)) return;
846+
const module = moduleGraph.getModule(this);
847+
if (!module || !isInlineEnabled(module)) return;
848+
const mode = this.getMode(moduleGraph, runtime);
849+
if (mode.type !== "normal-reexport") return;
850+
const exportsInfo = moduleGraph.getExportsInfo(module);
851+
for (const item of /** @type {NormalReexportItem[]} */ (mode.items)) {
852+
if (item.hidden || item.checked) continue;
853+
const usedName = exportsInfo.getUsedName(item.ids, runtime);
854+
if (usedName instanceof InlinedUsedName) {
855+
hash.update(`${item.name}:${item.ids.join(".")}:`);
856+
usedName.updateHash(hash);
857+
}
858+
}
859+
}
860+
833861
/**
834862
* Gets module evaluation side effects state.
835863
* @param {ModuleGraph} moduleGraph the module graph

‎lib/dependencies/HarmonyImportSpecifierDependency.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const { ImportPhaseUtils } = require("./ImportPhase");
3030
/** @import { DependencyTemplateContext } from "../DependencyTemplate" */
3131
/** @import Module, { BuildMeta } from "../Module" */
3232
/** @import ModuleGraph from "../ModuleGraph" */
33+
/** @import { UpdateHashContext } from "../Dependency" */
34+
/** @import Hash from "../util/Hash" */
3335
/**
3436
* @import ModuleGraphConnection, {
3537
* ConnectionState
@@ -262,6 +264,28 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
262264
return condition;
263265
}
264266

267+
/**
268+
* Updates the hash with the data contributed by this instance.
269+
* @param {Hash} hash hash to be updated
270+
* @param {UpdateHashContext} context context
271+
* @returns {void}
272+
*/
273+
updateHash(hash, context) {
274+
const { chunkGraph, runtime } = context;
275+
const moduleGraph = chunkGraph.moduleGraph;
276+
if (!isInlineExportsEnabled(moduleGraph)) return;
277+
const module = moduleGraph.getModule(this);
278+
if (!module || !isInlineEnabled(module)) return;
279+
const ids = this.getIds(moduleGraph);
280+
if (ids.length === 0) return;
281+
const exportsInfo = moduleGraph.getExportsInfo(module);
282+
if (!exportsInfo.hasInlinedUsedName(ids, runtime)) return;
283+
const usedName = exportsInfo.getUsedName(ids, runtime);
284+
if (usedName instanceof InlinedUsedName) {
285+
usedName.updateHash(hash);
286+
}
287+
}
288+
265289
/**
266290
* Gets module evaluation side effects state.
267291
* @param {ModuleGraph} moduleGraph the module graph

‎lib/optimize/InlineExports.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { propertyAccess } = require("../util/property");
1717
/** @import ModuleGraph from "../ModuleGraph" */
1818
/** @import Module from "../Module" */
1919
/** @import { RuntimeSpec } from "../util/runtime" */
20+
/** @import Hash from "../util/Hash" */
2021

2122
const MAX_INLINE_BYTES = 6;
2223

@@ -104,12 +105,21 @@ class InlinedUsedName {
104105
}
105106

106107
/**
107-
* @param {string} comment leading comment text
108+
* @param {string=} comment leading comment text
108109
* @returns {string} rendered literal with property access suffix
109110
*/
110-
render(comment) {
111+
render(comment = "") {
111112
return this.value.render(comment) + propertyAccess(this.suffix);
112113
}
114+
115+
/**
116+
* Updates the hash with the data contributed by this instance.
117+
* @param {Hash} hash the hash used to track dependencies
118+
* @returns {void}
119+
*/
120+
updateHash(hash) {
121+
hash.update(this.render());
122+
}
113123
}
114124

115125
/**

‎test/watchCases/cache/inline-exports-value-change/0/index.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import "./trigger";
55
const fs = __non_webpack_require__("fs");
66
const path = __non_webpack_require__("path");
77

8+
const expectedFlag = WATCH_STEP === "0" ? "on" : "off";
9+
const expectedNum = WATCH_STEP === "2" ? 6 : 5;
10+
811
it("should refresh the inlined constants a cached build baked into consumers", () => {
912
const source = fs.readFileSync(
1013
path.join(STATS_JSON.outputPath, "bundle.js"),
1114
"utf8"
1215
);
13-
const expectedFlag = WATCH_STEP === "0" ? "on" : "off";
14-
const expectedNum = WATCH_STEP === "2" ? 6 : 5;
1516
expect(FLAG).toBe(expectedFlag);
1617
expect(NUM).toBe(expectedNum);
1718
expect(REEXPORTED_NUM).toBe(expectedNum);
@@ -20,3 +21,9 @@ it("should refresh the inlined constants a cached build baked into consumers", (
2021
);
2122
expect(source).toContain(`(/* inlined export .NUM */${expectedNum})`);
2223
});
24+
25+
it("should refresh the literal a re-exporter with side effects bakes into its getter", () =>
26+
import("./reexport-side-effect").then(({ SIDE_EFFECT_NUM }) => {
27+
expect(SIDE_EFFECT_NUM).toBe(expectedNum);
28+
delete global.__inlineExportsBarrel;
29+
}));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// The side effect keeps this barrel from being skipped, so its getter is
2+
// where the literal lands
3+
global.__inlineExportsBarrel = true;
4+
export { NUM as SIDE_EFFECT_NUM } from "./env";

‎types.d.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11687,7 +11687,12 @@ declare class InitFragment<GenerateContext> {
1168711687
declare abstract class InlinedUsedName {
1168811688
value: InlinedValue;
1168911689
suffix: string[];
11690-
render(comment: string): string;
11690+
render(comment?: string): string;
11691+
11692+
/**
11693+
* Updates the hash with the data contributed by this instance.
11694+
*/
11695+
updateHash(hash: Hash): void;
1169111696
}
1169211697
declare abstract class InlinedValue {
1169311698
kind: InlinedValueKind;

0 commit comments

Comments
 (0)