Skip to content

Commit 0573cad

Browse files
committed
update
1 parent 7037f39 commit 0573cad

4 files changed

Lines changed: 87 additions & 19 deletions

File tree

‎lib/FlagDependencyExportsPlugin.js‎

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,54 @@ const getLazyRequestsHash = (module, lazyBarrelController, hashFunction) => {
4545
return /** @type {string} */ (hash.digest("hex"));
4646
};
4747

48+
/**
49+
* DefinePlugin (and similar) can change provided exports without changing the
50+
* source hash; fold valueDependencies into the provided-exports cache key.
51+
* @param {Module} module the module
52+
* @param {HashFunction} hashFunction hash function for long keys
53+
* @returns {string} key fragment, or "" when absent
54+
*/
55+
const getValueDependenciesHash = (module, hashFunction) => {
56+
const valueDependencies =
57+
/** @type {BuildInfo | undefined} */
58+
(module.buildInfo) &&
59+
/** @type {BuildInfo} */
60+
(module.buildInfo).valueDependencies;
61+
if (!valueDependencies || valueDependencies.size === 0) return "";
62+
const parts = [];
63+
for (const key of [...valueDependencies.keys()].sort()) {
64+
const value = valueDependencies.get(key);
65+
parts.push(
66+
`${key}=${
67+
typeof value === "string"
68+
? value
69+
: value === undefined
70+
? ""
71+
: `[${[...value].sort().join(",")}]`
72+
}`
73+
);
74+
}
75+
const joined = parts.join("|");
76+
if (joined.length < 100) return joined;
77+
const hash = createHash(hashFunction);
78+
hash.update(joined);
79+
return /** @type {string} */ (hash.digest("hex"));
80+
};
81+
82+
/**
83+
* @param {Module} module the module
84+
* @param {LazyBarrelController} lazyBarrelController the compilation's lazy barrel controller
85+
* @param {HashFunction} hashFunction hash function for long keys
86+
* @returns {string} etag for provided-exports mem/persistent cache
87+
*/
88+
const getProvidedExportsEtag = (module, lazyBarrelController, hashFunction) => {
89+
const lazy = getLazyRequestsHash(module, lazyBarrelController, hashFunction);
90+
const valueDeps = getValueDependenciesHash(module, hashFunction);
91+
if (!lazy) return valueDeps;
92+
if (!valueDeps) return lazy;
93+
return `${lazy}|${valueDeps}`;
94+
};
95+
4896
class FlagDependencyExportsPlugin {
4997
/**
5098
* Applies the plugin by registering its hooks on the compiler.
@@ -107,7 +155,7 @@ class FlagDependencyExportsPlugin {
107155
const memCache = moduleMemCaches && moduleMemCaches.get(module);
108156
/** @type {MemCacheEntry | undefined} */
109157
const memCacheEntry = memCache && memCache.get(this);
110-
const hash = getLazyRequestsHash(
158+
const etag = getProvidedExportsEtag(
111159
module,
112160
lazyBarrelController,
113161
hashFunction
@@ -116,7 +164,7 @@ class FlagDependencyExportsPlugin {
116164
// detection cannot invalidate the mem cache when the un-lazied
117165
// connections are unsafe-cached (they are skipped when comparing
118166
// references)
119-
if (memCacheEntry !== undefined && memCacheEntry.etag === hash) {
167+
if (memCacheEntry !== undefined && memCacheEntry.etag === etag) {
120168
statRestoredFromMemCache++;
121169
exportsInfo.restoreProvided(memCacheEntry.data);
122170
return callback();
@@ -126,7 +174,7 @@ class FlagDependencyExportsPlugin {
126174
);
127175
cache.get(
128176
module.identifier(),
129-
hash ? `${buildHash}|${hash}` : buildHash,
177+
etag ? `${buildHash}|${etag}` : buildHash,
130178
(err, result) => {
131179
if (err) return callback(err);
132180

@@ -349,12 +397,13 @@ class FlagDependencyExportsPlugin {
349397
changed = true;
350398
}
351399

352-
if (
353-
inlined !== undefined &&
354-
exportInfo.canInlineProvide === undefined
355-
) {
356-
exportInfo.canInlineProvide = inlined;
357-
changed = true;
400+
if (inlined !== undefined) {
401+
const prev = exportInfo.canInlineProvide;
402+
// Watch rebuilds keep ExportInfo; refresh when the constant changes
403+
if (prev === undefined || !prev.equals(inlined)) {
404+
exportInfo.canInlineProvide = inlined;
405+
changed = true;
406+
}
358407
}
359408

360409
if (terminalBinding && !exportInfo.terminalBinding) {
@@ -511,15 +560,15 @@ class FlagDependencyExportsPlugin {
511560
.getRestoreProvidedData();
512561
const memCache =
513562
moduleMemCaches && moduleMemCaches.get(module);
514-
const hash = getLazyRequestsHash(
563+
const etag = getProvidedExportsEtag(
515564
module,
516565
lazyBarrelController,
517566
hashFunction
518567
);
519568
if (memCache) {
520569
/** @type {MemCacheEntry} */
521570
const entry = {
522-
etag: hash,
571+
etag,
523572
data: cachedData
524573
};
525574
memCache.set(this, entry);
@@ -529,7 +578,7 @@ class FlagDependencyExportsPlugin {
529578
);
530579
cache.store(
531580
module.identifier(),
532-
hash ? `${buildHash}|${hash}` : buildHash,
581+
etag ? `${buildHash}|${etag}` : buildHash,
533582
cachedData,
534583
callback
535584
);

‎lib/optimize/ConstExportsPlugin.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,23 @@ class ConstExportsPlugin {
189189
}
190190
for (const exportInfo of exportsInfo.ownedExports) {
191191
const inlined = exportInfo.canInline();
192-
const doInline =
193-
!exportInfo.hasUsedName() &&
194-
inlined !== undefined &&
195-
exportInfo.provided === true;
196-
if (doInline) {
197-
exportInfo.setUsedName(new InlinedUsedName(inlined));
198-
exportsInfo.markInlinedExports();
192+
if (inlined !== undefined && exportInfo.provided === true) {
193+
if (!exportInfo.hasUsedName()) {
194+
exportInfo.setUsedName(new InlinedUsedName(inlined));
195+
exportsInfo.markInlinedExports();
196+
} else {
197+
// Watch rebuilds keep ExportInfo; refresh a stale literal
198+
const usedName = exportInfo.getUsedName(
199+
exportInfo.name,
200+
undefined
201+
);
202+
if (
203+
usedName instanceof InlinedUsedName &&
204+
!usedName.value.equals(inlined)
205+
) {
206+
exportInfo.setUsedName(new InlinedUsedName(inlined));
207+
}
208+
}
199209
}
200210
if (exportInfo.exportsInfoOwned && exportInfo.exportsInfo) {
201211
const used = exportInfo.getUsed(undefined);

‎lib/optimize/InlineExports.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class InlinedValue {
6060
return `(${comment}${this.renderLiteral()})`;
6161
}
6262

63+
/**
64+
* @param {InlinedValue} other other inlined value
65+
* @returns {boolean} true when kind and value match
66+
*/
67+
equals(other) {
68+
return this.kind === other.kind && this.value === other.value;
69+
}
70+
6371
/**
6472
* @param {ObjectSerializerContext} context context
6573
*/

‎types.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11694,6 +11694,7 @@ declare abstract class InlinedValue {
1169411694
value?: null | string | number | boolean;
1169511695
renderLiteral(): string;
1169611696
render(comment: string): string;
11697+
equals(other: InlinedValue): boolean;
1169711698
serialize(__0: ObjectSerializerContextObjectMiddlewareObject_3): void;
1169811699
deserialize(__0: ObjectDeserializerContextObjectMiddlewareObject_2): void;
1169911700
}

0 commit comments

Comments
 (0)