Skip to content

Commit 18c3590

Browse files
authored
Merge pull request #15315 from webpack/fix/issue-13022
update topLevelDeclarations for ConcatenatedModule
2 parents 4edf949 + 4951965 commit 18c3590

9 files changed

Lines changed: 97 additions & 8 deletions

File tree

lib/Compilation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const { isSourceEqual } = require("./util/source");
182182

183183
/**
184184
* @typedef {Object} ChunkHashContext
185+
* @property {CodeGenerationResults} codeGenerationResults results of code generation
185186
* @property {RuntimeTemplate} runtimeTemplate the runtime template
186187
* @property {ModuleGraph} moduleGraph the module graph
187188
* @property {ChunkGraph} chunkGraph the chunk graph
@@ -4137,6 +4138,7 @@ This prevents using hashes of each other and should be avoided.`);
41374138
chunk.updateHash(chunkHash, chunkGraph);
41384139
this.hooks.chunkHash.call(chunk, chunkHash, {
41394140
chunkGraph,
4141+
codeGenerationResults: this.codeGenerationResults,
41404142
moduleGraph: this.moduleGraph,
41414143
runtimeTemplate: this.runtimeTemplate
41424144
});

lib/javascript/JavascriptModulesPlugin.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const printGeneratedCodeForStack = (module, code) => {
103103
/**
104104
* @typedef {Object} RenderBootstrapContext
105105
* @property {Chunk} chunk the chunk
106+
* @property {CodeGenerationResults} codeGenerationResults results of code generation
106107
* @property {RuntimeTemplate} runtimeTemplate the runtime template
107108
* @property {ModuleGraph} moduleGraph the module graph
108109
* @property {ChunkGraph} chunkGraph the chunk graph
@@ -332,6 +333,7 @@ class JavascriptModulesPlugin {
332333
{
333334
hash: "0000",
334335
chunk,
336+
codeGenerationResults: context.codeGenerationResults,
335337
chunkGraph: context.chunkGraph,
336338
moduleGraph: context.moduleGraph,
337339
runtimeTemplate: context.runtimeTemplate
@@ -344,6 +346,7 @@ class JavascriptModulesPlugin {
344346
compilation.hooks.contentHash.tap("JavascriptModulesPlugin", chunk => {
345347
const {
346348
chunkGraph,
349+
codeGenerationResults,
347350
moduleGraph,
348351
runtimeTemplate,
349352
outputOptions: {
@@ -361,6 +364,7 @@ class JavascriptModulesPlugin {
361364
{
362365
hash: "0000",
363366
chunk,
367+
codeGenerationResults,
364368
chunkGraph: compilation.chunkGraph,
365369
moduleGraph: compilation.moduleGraph,
366370
runtimeTemplate: compilation.runtimeTemplate
@@ -373,6 +377,7 @@ class JavascriptModulesPlugin {
373377
}
374378
hooks.chunkHash.call(chunk, hash, {
375379
chunkGraph,
380+
codeGenerationResults,
376381
moduleGraph,
377382
runtimeTemplate
378383
});
@@ -978,7 +983,13 @@ class JavascriptModulesPlugin {
978983
* @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} the generated source of the bootstrap code
979984
*/
980985
renderBootstrap(renderContext, hooks) {
981-
const { chunkGraph, moduleGraph, chunk, runtimeTemplate } = renderContext;
986+
const {
987+
chunkGraph,
988+
codeGenerationResults,
989+
moduleGraph,
990+
chunk,
991+
runtimeTemplate
992+
} = renderContext;
982993

983994
const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);
984995

@@ -1102,8 +1113,18 @@ class JavascriptModulesPlugin {
11021113
);
11031114
result.allowInlineStartup = false;
11041115
}
1116+
1117+
let data;
1118+
if (codeGenerationResults.has(entryModule, chunk.runtime)) {
1119+
const result = codeGenerationResults.get(
1120+
entryModule,
1121+
chunk.runtime
1122+
);
1123+
data = result.data;
1124+
}
11051125
if (
11061126
result.allowInlineStartup &&
1127+
(!data || !data.get("topLevelDeclarations")) &&
11071128
(!entryModule.buildInfo ||
11081129
!entryModule.buildInfo.topLevelDeclarations)
11091130
) {

lib/library/AssignLibraryPlugin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
222222
* @param {LibraryContext<T>} libraryContext context
223223
* @returns {string | undefined} bailout reason
224224
*/
225-
embedInRuntimeBailout(module, { chunk }, { options, compilation }) {
225+
embedInRuntimeBailout(
226+
module,
227+
{ chunk, codeGenerationResults },
228+
{ options, compilation }
229+
) {
230+
const { data } = codeGenerationResults.get(module, chunk.runtime);
226231
const topLevelDeclarations =
227-
module.buildInfo && module.buildInfo.topLevelDeclarations;
232+
(data && data.get("topLevelDeclarations")) ||
233+
(module.buildInfo && module.buildInfo.topLevelDeclarations);
228234
if (!topLevelDeclarations)
229235
return "it doesn't tell about top level declarations.";
230236
const fullNameResolved = this._getResolvedFullName(

lib/optimize/ConcatenatedModule.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,6 @@ class ConcatenatedModule extends Module {
822822
const topLevelDeclarations = this.buildInfo.topLevelDeclarations;
823823
if (topLevelDeclarations !== undefined) {
824824
for (const decl of m.buildInfo.topLevelDeclarations) {
825-
// reserved names will always be renamed
826-
if (RESERVED_NAMES.has(decl)) continue;
827-
// TODO actually this is incorrect since with renaming there could be more
828-
// We should do the renaming during build
829825
topLevelDeclarations.add(decl);
830826
}
831827
}
@@ -1113,6 +1109,8 @@ class ConcatenatedModule extends Module {
11131109

11141110
// List of all used names to avoid conflicts
11151111
const allUsedNames = new Set(RESERVED_NAMES);
1112+
// Updated Top level declarations are created by renaming
1113+
const topLevelDeclarations = new Set();
11161114

11171115
// List of additional names in scope for module references
11181116
/** @type {Map<string, { usedNames: Set<string>, alreadyCheckedScopes: Set<TODO> }>} */
@@ -1257,6 +1255,7 @@ class ConcatenatedModule extends Module {
12571255
);
12581256
allUsedNames.add(newName);
12591257
info.internalNames.set(name, newName);
1258+
topLevelDeclarations.add(newName);
12601259
const source = info.source;
12611260
const allIdentifiers = new Set(
12621261
references.map(r => r.identifier).concat(variable.identifiers)
@@ -1283,6 +1282,7 @@ class ConcatenatedModule extends Module {
12831282
} else {
12841283
allUsedNames.add(name);
12851284
info.internalNames.set(name, name);
1285+
topLevelDeclarations.add(name);
12861286
}
12871287
}
12881288
let namespaceObjectName;
@@ -1300,6 +1300,7 @@ class ConcatenatedModule extends Module {
13001300
allUsedNames.add(namespaceObjectName);
13011301
}
13021302
info.namespaceObjectName = namespaceObjectName;
1303+
topLevelDeclarations.add(namespaceObjectName);
13031304
break;
13041305
}
13051306
case "external": {
@@ -1311,6 +1312,7 @@ class ConcatenatedModule extends Module {
13111312
);
13121313
allUsedNames.add(externalName);
13131314
info.name = externalName;
1315+
topLevelDeclarations.add(externalName);
13141316
break;
13151317
}
13161318
}
@@ -1323,6 +1325,7 @@ class ConcatenatedModule extends Module {
13231325
);
13241326
allUsedNames.add(externalNameInterop);
13251327
info.interopNamespaceObjectName = externalNameInterop;
1328+
topLevelDeclarations.add(externalNameInterop);
13261329
}
13271330
if (
13281331
info.module.buildMeta.exportsType === "default" &&
@@ -1336,6 +1339,7 @@ class ConcatenatedModule extends Module {
13361339
);
13371340
allUsedNames.add(externalNameInterop);
13381341
info.interopNamespaceObject2Name = externalNameInterop;
1342+
topLevelDeclarations.add(externalNameInterop);
13391343
}
13401344
if (
13411345
info.module.buildMeta.exportsType === "dynamic" ||
@@ -1349,6 +1353,7 @@ class ConcatenatedModule extends Module {
13491353
);
13501354
allUsedNames.add(externalNameInterop);
13511355
info.interopDefaultAccessName = externalNameInterop;
1356+
topLevelDeclarations.add(externalNameInterop);
13521357
}
13531358
}
13541359

@@ -1618,6 +1623,7 @@ ${defineGetters}`
16181623
const data = new Map();
16191624
if (chunkInitFragments.length > 0)
16201625
data.set("chunkInitFragments", chunkInitFragments);
1626+
data.set("topLevelDeclarations", topLevelDeclarations);
16211627

16221628
/** @type {CodeGenerationResult} */
16231629
const resultEntry = {

test/__snapshots__/StatsTestCases.basictest.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ chunk (runtime: main) trees.js (trees) 215 bytes [rendered]
11291129
`;
11301130

11311131
exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = `
1132-
"asset main.js 1.37 KiB [emitted] (name: main)
1132+
"asset main.js 989 bytes [emitted] (name: main)
11331133
orphan modules 617 bytes [orphan] 9 modules
11341134
./index.js + 9 modules 790 bytes [built] [code generated]
11351135
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const doc = console;
2+
const setTimeout = 1;
3+
4+
export default 1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "./a";
2+
3+
setTimeout(() => {}, 0);
4+
5+
const doc = console;
6+
7+
export default 1;
8+
9+
it("should compile and run", () => {
10+
expect(doc).toBe(console);
11+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require("path");
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [
5+
{
6+
entry: {
7+
index: path.resolve(__dirname, "./index.js")
8+
},
9+
output: {
10+
library: "[name]",
11+
libraryExport: "default"
12+
},
13+
optimization: {
14+
concatenateModules: true
15+
}
16+
},
17+
{
18+
entry: {
19+
index: path.resolve(__dirname, "./index.js")
20+
},
21+
output: {
22+
library: "[name]_doc",
23+
libraryExport: "default"
24+
},
25+
optimization: {
26+
concatenateModules: true
27+
}
28+
}
29+
];

types.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,11 @@ declare abstract class ChunkGroup {
10491049
}
10501050
type ChunkGroupOptions = RawChunkGroupOptions & { name?: string };
10511051
declare interface ChunkHashContext {
1052+
/**
1053+
* results of code generation
1054+
*/
1055+
codeGenerationResults: CodeGenerationResults;
1056+
10521057
/**
10531058
* the runtime template
10541059
*/
@@ -9221,6 +9226,11 @@ declare interface RenderBootstrapContext {
92219226
*/
92229227
chunk: Chunk;
92239228

9229+
/**
9230+
* results of code generation
9231+
*/
9232+
codeGenerationResults: CodeGenerationResults;
9233+
92249234
/**
92259235
* the runtime template
92269236
*/

0 commit comments

Comments
 (0)