mirror of https://github.com/webpack/webpack.git
				
				
				
			Merge pull request #15315 from webpack/fix/issue-13022
update topLevelDeclarations for ConcatenatedModule
This commit is contained in:
		
						commit
						18c3590b28
					
				|  | @ -182,6 +182,7 @@ const { isSourceEqual } = require("./util/source"); | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @typedef {Object} ChunkHashContext |  * @typedef {Object} ChunkHashContext | ||||||
|  |  * @property {CodeGenerationResults} codeGenerationResults results of code generation | ||||||
|  * @property {RuntimeTemplate} runtimeTemplate the runtime template |  * @property {RuntimeTemplate} runtimeTemplate the runtime template | ||||||
|  * @property {ModuleGraph} moduleGraph the module graph |  * @property {ModuleGraph} moduleGraph the module graph | ||||||
|  * @property {ChunkGraph} chunkGraph the chunk graph |  * @property {ChunkGraph} chunkGraph the chunk graph | ||||||
|  | @ -4137,6 +4138,7 @@ This prevents using hashes of each other and should be avoided.`); | ||||||
| 				chunk.updateHash(chunkHash, chunkGraph); | 				chunk.updateHash(chunkHash, chunkGraph); | ||||||
| 				this.hooks.chunkHash.call(chunk, chunkHash, { | 				this.hooks.chunkHash.call(chunk, chunkHash, { | ||||||
| 					chunkGraph, | 					chunkGraph, | ||||||
|  | 					codeGenerationResults: this.codeGenerationResults, | ||||||
| 					moduleGraph: this.moduleGraph, | 					moduleGraph: this.moduleGraph, | ||||||
| 					runtimeTemplate: this.runtimeTemplate | 					runtimeTemplate: this.runtimeTemplate | ||||||
| 				}); | 				}); | ||||||
|  |  | ||||||
|  | @ -103,6 +103,7 @@ const printGeneratedCodeForStack = (module, code) => { | ||||||
| /** | /** | ||||||
|  * @typedef {Object} RenderBootstrapContext |  * @typedef {Object} RenderBootstrapContext | ||||||
|  * @property {Chunk} chunk the chunk |  * @property {Chunk} chunk the chunk | ||||||
|  |  * @property {CodeGenerationResults} codeGenerationResults results of code generation | ||||||
|  * @property {RuntimeTemplate} runtimeTemplate the runtime template |  * @property {RuntimeTemplate} runtimeTemplate the runtime template | ||||||
|  * @property {ModuleGraph} moduleGraph the module graph |  * @property {ModuleGraph} moduleGraph the module graph | ||||||
|  * @property {ChunkGraph} chunkGraph the chunk graph |  * @property {ChunkGraph} chunkGraph the chunk graph | ||||||
|  | @ -332,6 +333,7 @@ class JavascriptModulesPlugin { | ||||||
| 								{ | 								{ | ||||||
| 									hash: "0000", | 									hash: "0000", | ||||||
| 									chunk, | 									chunk, | ||||||
|  | 									codeGenerationResults: context.codeGenerationResults, | ||||||
| 									chunkGraph: context.chunkGraph, | 									chunkGraph: context.chunkGraph, | ||||||
| 									moduleGraph: context.moduleGraph, | 									moduleGraph: context.moduleGraph, | ||||||
| 									runtimeTemplate: context.runtimeTemplate | 									runtimeTemplate: context.runtimeTemplate | ||||||
|  | @ -344,6 +346,7 @@ class JavascriptModulesPlugin { | ||||||
| 				compilation.hooks.contentHash.tap("JavascriptModulesPlugin", chunk => { | 				compilation.hooks.contentHash.tap("JavascriptModulesPlugin", chunk => { | ||||||
| 					const { | 					const { | ||||||
| 						chunkGraph, | 						chunkGraph, | ||||||
|  | 						codeGenerationResults, | ||||||
| 						moduleGraph, | 						moduleGraph, | ||||||
| 						runtimeTemplate, | 						runtimeTemplate, | ||||||
| 						outputOptions: { | 						outputOptions: { | ||||||
|  | @ -361,6 +364,7 @@ class JavascriptModulesPlugin { | ||||||
| 							{ | 							{ | ||||||
| 								hash: "0000", | 								hash: "0000", | ||||||
| 								chunk, | 								chunk, | ||||||
|  | 								codeGenerationResults, | ||||||
| 								chunkGraph: compilation.chunkGraph, | 								chunkGraph: compilation.chunkGraph, | ||||||
| 								moduleGraph: compilation.moduleGraph, | 								moduleGraph: compilation.moduleGraph, | ||||||
| 								runtimeTemplate: compilation.runtimeTemplate | 								runtimeTemplate: compilation.runtimeTemplate | ||||||
|  | @ -373,6 +377,7 @@ class JavascriptModulesPlugin { | ||||||
| 					} | 					} | ||||||
| 					hooks.chunkHash.call(chunk, hash, { | 					hooks.chunkHash.call(chunk, hash, { | ||||||
| 						chunkGraph, | 						chunkGraph, | ||||||
|  | 						codeGenerationResults, | ||||||
| 						moduleGraph, | 						moduleGraph, | ||||||
| 						runtimeTemplate | 						runtimeTemplate | ||||||
| 					}); | 					}); | ||||||
|  | @ -978,7 +983,13 @@ class JavascriptModulesPlugin { | ||||||
| 	 * @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} the generated source of the bootstrap code | 	 * @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} the generated source of the bootstrap code | ||||||
| 	 */ | 	 */ | ||||||
| 	renderBootstrap(renderContext, hooks) { | 	renderBootstrap(renderContext, hooks) { | ||||||
| 		const { chunkGraph, moduleGraph, chunk, runtimeTemplate } = renderContext; | 		const { | ||||||
|  | 			chunkGraph, | ||||||
|  | 			codeGenerationResults, | ||||||
|  | 			moduleGraph, | ||||||
|  | 			chunk, | ||||||
|  | 			runtimeTemplate | ||||||
|  | 		} = renderContext; | ||||||
| 
 | 
 | ||||||
| 		const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk); | 		const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk); | ||||||
| 
 | 
 | ||||||
|  | @ -1102,8 +1113,18 @@ class JavascriptModulesPlugin { | ||||||
| 						); | 						); | ||||||
| 						result.allowInlineStartup = false; | 						result.allowInlineStartup = false; | ||||||
| 					} | 					} | ||||||
|  | 
 | ||||||
|  | 					let data; | ||||||
|  | 					if (codeGenerationResults.has(entryModule, chunk.runtime)) { | ||||||
|  | 						const result = codeGenerationResults.get( | ||||||
|  | 							entryModule, | ||||||
|  | 							chunk.runtime | ||||||
|  | 						); | ||||||
|  | 						data = result.data; | ||||||
|  | 					} | ||||||
| 					if ( | 					if ( | ||||||
| 						result.allowInlineStartup && | 						result.allowInlineStartup && | ||||||
|  | 						(!data || !data.get("topLevelDeclarations")) && | ||||||
| 						(!entryModule.buildInfo || | 						(!entryModule.buildInfo || | ||||||
| 							!entryModule.buildInfo.topLevelDeclarations) | 							!entryModule.buildInfo.topLevelDeclarations) | ||||||
| 					) { | 					) { | ||||||
|  |  | ||||||
|  | @ -222,9 +222,15 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin { | ||||||
| 	 * @param {LibraryContext<T>} libraryContext context | 	 * @param {LibraryContext<T>} libraryContext context | ||||||
| 	 * @returns {string | undefined} bailout reason | 	 * @returns {string | undefined} bailout reason | ||||||
| 	 */ | 	 */ | ||||||
| 	embedInRuntimeBailout(module, { chunk }, { options, compilation }) { | 	embedInRuntimeBailout( | ||||||
|  | 		module, | ||||||
|  | 		{ chunk, codeGenerationResults }, | ||||||
|  | 		{ options, compilation } | ||||||
|  | 	) { | ||||||
|  | 		const { data } = codeGenerationResults.get(module, chunk.runtime); | ||||||
| 		const topLevelDeclarations = | 		const topLevelDeclarations = | ||||||
| 			module.buildInfo && module.buildInfo.topLevelDeclarations; | 			(data && data.get("topLevelDeclarations")) || | ||||||
|  | 			(module.buildInfo && module.buildInfo.topLevelDeclarations); | ||||||
| 		if (!topLevelDeclarations) | 		if (!topLevelDeclarations) | ||||||
| 			return "it doesn't tell about top level declarations."; | 			return "it doesn't tell about top level declarations."; | ||||||
| 		const fullNameResolved = this._getResolvedFullName( | 		const fullNameResolved = this._getResolvedFullName( | ||||||
|  |  | ||||||
|  | @ -822,10 +822,6 @@ class ConcatenatedModule extends Module { | ||||||
| 				const topLevelDeclarations = this.buildInfo.topLevelDeclarations; | 				const topLevelDeclarations = this.buildInfo.topLevelDeclarations; | ||||||
| 				if (topLevelDeclarations !== undefined) { | 				if (topLevelDeclarations !== undefined) { | ||||||
| 					for (const decl of m.buildInfo.topLevelDeclarations) { | 					for (const decl of m.buildInfo.topLevelDeclarations) { | ||||||
| 						// reserved names will always be renamed
 |  | ||||||
| 						if (RESERVED_NAMES.has(decl)) continue; |  | ||||||
| 						// TODO actually this is incorrect since with renaming there could be more
 |  | ||||||
| 						// We should do the renaming during build
 |  | ||||||
| 						topLevelDeclarations.add(decl); | 						topLevelDeclarations.add(decl); | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  | @ -1113,6 +1109,8 @@ class ConcatenatedModule extends Module { | ||||||
| 
 | 
 | ||||||
| 		// List of all used names to avoid conflicts
 | 		// List of all used names to avoid conflicts
 | ||||||
| 		const allUsedNames = new Set(RESERVED_NAMES); | 		const allUsedNames = new Set(RESERVED_NAMES); | ||||||
|  | 		// Updated Top level declarations are created by renaming
 | ||||||
|  | 		const topLevelDeclarations = new Set(); | ||||||
| 
 | 
 | ||||||
| 		// List of additional names in scope for module references
 | 		// List of additional names in scope for module references
 | ||||||
| 		/** @type {Map<string, { usedNames: Set<string>, alreadyCheckedScopes: Set<TODO> }>} */ | 		/** @type {Map<string, { usedNames: Set<string>, alreadyCheckedScopes: Set<TODO> }>} */ | ||||||
|  | @ -1257,6 +1255,7 @@ class ConcatenatedModule extends Module { | ||||||
| 							); | 							); | ||||||
| 							allUsedNames.add(newName); | 							allUsedNames.add(newName); | ||||||
| 							info.internalNames.set(name, newName); | 							info.internalNames.set(name, newName); | ||||||
|  | 							topLevelDeclarations.add(newName); | ||||||
| 							const source = info.source; | 							const source = info.source; | ||||||
| 							const allIdentifiers = new Set( | 							const allIdentifiers = new Set( | ||||||
| 								references.map(r => r.identifier).concat(variable.identifiers) | 								references.map(r => r.identifier).concat(variable.identifiers) | ||||||
|  | @ -1283,6 +1282,7 @@ class ConcatenatedModule extends Module { | ||||||
| 						} else { | 						} else { | ||||||
| 							allUsedNames.add(name); | 							allUsedNames.add(name); | ||||||
| 							info.internalNames.set(name, name); | 							info.internalNames.set(name, name); | ||||||
|  | 							topLevelDeclarations.add(name); | ||||||
| 						} | 						} | ||||||
| 					} | 					} | ||||||
| 					let namespaceObjectName; | 					let namespaceObjectName; | ||||||
|  | @ -1300,6 +1300,7 @@ class ConcatenatedModule extends Module { | ||||||
| 						allUsedNames.add(namespaceObjectName); | 						allUsedNames.add(namespaceObjectName); | ||||||
| 					} | 					} | ||||||
| 					info.namespaceObjectName = namespaceObjectName; | 					info.namespaceObjectName = namespaceObjectName; | ||||||
|  | 					topLevelDeclarations.add(namespaceObjectName); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 				case "external": { | 				case "external": { | ||||||
|  | @ -1311,6 +1312,7 @@ class ConcatenatedModule extends Module { | ||||||
| 					); | 					); | ||||||
| 					allUsedNames.add(externalName); | 					allUsedNames.add(externalName); | ||||||
| 					info.name = externalName; | 					info.name = externalName; | ||||||
|  | 					topLevelDeclarations.add(externalName); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | @ -1323,6 +1325,7 @@ class ConcatenatedModule extends Module { | ||||||
| 				); | 				); | ||||||
| 				allUsedNames.add(externalNameInterop); | 				allUsedNames.add(externalNameInterop); | ||||||
| 				info.interopNamespaceObjectName = externalNameInterop; | 				info.interopNamespaceObjectName = externalNameInterop; | ||||||
|  | 				topLevelDeclarations.add(externalNameInterop); | ||||||
| 			} | 			} | ||||||
| 			if ( | 			if ( | ||||||
| 				info.module.buildMeta.exportsType === "default" && | 				info.module.buildMeta.exportsType === "default" && | ||||||
|  | @ -1336,6 +1339,7 @@ class ConcatenatedModule extends Module { | ||||||
| 				); | 				); | ||||||
| 				allUsedNames.add(externalNameInterop); | 				allUsedNames.add(externalNameInterop); | ||||||
| 				info.interopNamespaceObject2Name = externalNameInterop; | 				info.interopNamespaceObject2Name = externalNameInterop; | ||||||
|  | 				topLevelDeclarations.add(externalNameInterop); | ||||||
| 			} | 			} | ||||||
| 			if ( | 			if ( | ||||||
| 				info.module.buildMeta.exportsType === "dynamic" || | 				info.module.buildMeta.exportsType === "dynamic" || | ||||||
|  | @ -1349,6 +1353,7 @@ class ConcatenatedModule extends Module { | ||||||
| 				); | 				); | ||||||
| 				allUsedNames.add(externalNameInterop); | 				allUsedNames.add(externalNameInterop); | ||||||
| 				info.interopDefaultAccessName = externalNameInterop; | 				info.interopDefaultAccessName = externalNameInterop; | ||||||
|  | 				topLevelDeclarations.add(externalNameInterop); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | @ -1618,6 +1623,7 @@ ${defineGetters}` | ||||||
| 		const data = new Map(); | 		const data = new Map(); | ||||||
| 		if (chunkInitFragments.length > 0) | 		if (chunkInitFragments.length > 0) | ||||||
| 			data.set("chunkInitFragments", chunkInitFragments); | 			data.set("chunkInitFragments", chunkInitFragments); | ||||||
|  | 		data.set("topLevelDeclarations", topLevelDeclarations); | ||||||
| 
 | 
 | ||||||
| 		/** @type {CodeGenerationResult} */ | 		/** @type {CodeGenerationResult} */ | ||||||
| 		const resultEntry = { | 		const resultEntry = { | ||||||
|  |  | ||||||
|  | @ -1129,7 +1129,7 @@ chunk (runtime: main) trees.js (trees) 215 bytes [rendered] | ||||||
| `; | `; | ||||||
| 
 | 
 | ||||||
| exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = ` | exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = ` | ||||||
| "asset main.js 1.37 KiB [emitted] (name: main) | "asset main.js 989 bytes [emitted] (name: main) | ||||||
| orphan modules 617 bytes [orphan] 9 modules | orphan modules 617 bytes [orphan] 9 modules | ||||||
| ./index.js + 9 modules 790 bytes [built] [code generated] | ./index.js + 9 modules 790 bytes [built] [code generated] | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -0,0 +1,4 @@ | ||||||
|  | const doc = console; | ||||||
|  | const setTimeout = 1; | ||||||
|  | 
 | ||||||
|  | export default 1; | ||||||
|  | @ -0,0 +1,11 @@ | ||||||
|  | import "./a"; | ||||||
|  | 
 | ||||||
|  | setTimeout(() => {}, 0); | ||||||
|  | 
 | ||||||
|  | const doc = console; | ||||||
|  | 
 | ||||||
|  | export default 1; | ||||||
|  | 
 | ||||||
|  | it("should compile and run", () => { | ||||||
|  | 	expect(doc).toBe(console); | ||||||
|  | }); | ||||||
|  | @ -0,0 +1,29 @@ | ||||||
|  | const path = require("path"); | ||||||
|  | 
 | ||||||
|  | /** @type {import("../../../../").Configuration[]} */ | ||||||
|  | module.exports = [ | ||||||
|  | 	{ | ||||||
|  | 		entry: { | ||||||
|  | 			index: path.resolve(__dirname, "./index.js") | ||||||
|  | 		}, | ||||||
|  | 		output: { | ||||||
|  | 			library: "[name]", | ||||||
|  | 			libraryExport: "default" | ||||||
|  | 		}, | ||||||
|  | 		optimization: { | ||||||
|  | 			concatenateModules: true | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		entry: { | ||||||
|  | 			index: path.resolve(__dirname, "./index.js") | ||||||
|  | 		}, | ||||||
|  | 		output: { | ||||||
|  | 			library: "[name]_doc", | ||||||
|  | 			libraryExport: "default" | ||||||
|  | 		}, | ||||||
|  | 		optimization: { | ||||||
|  | 			concatenateModules: true | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | ]; | ||||||
|  | @ -1049,6 +1049,11 @@ declare abstract class ChunkGroup { | ||||||
| } | } | ||||||
| type ChunkGroupOptions = RawChunkGroupOptions & { name?: string }; | type ChunkGroupOptions = RawChunkGroupOptions & { name?: string }; | ||||||
| declare interface ChunkHashContext { | declare interface ChunkHashContext { | ||||||
|  | 	/** | ||||||
|  | 	 * results of code generation | ||||||
|  | 	 */ | ||||||
|  | 	codeGenerationResults: CodeGenerationResults; | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * the runtime template | 	 * the runtime template | ||||||
| 	 */ | 	 */ | ||||||
|  | @ -9221,6 +9226,11 @@ declare interface RenderBootstrapContext { | ||||||
| 	 */ | 	 */ | ||||||
| 	chunk: Chunk; | 	chunk: Chunk; | ||||||
| 
 | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * results of code generation | ||||||
|  | 	 */ | ||||||
|  | 	codeGenerationResults: CodeGenerationResults; | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * the runtime template | 	 * the runtime template | ||||||
| 	 */ | 	 */ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue