mirror of https://github.com/webpack/webpack.git
				
				
				
			remove unused methods, improve code coverage
This commit is contained in:
		
							parent
							
								
									216ab35823
								
							
						
					
					
						commit
						77cdce7393
					
				
							
								
								
									
										43
									
								
								lib/Chunk.js
								
								
								
								
							
							
						
						
									
										43
									
								
								lib/Chunk.js
								
								
								
								
							|  | @ -11,14 +11,14 @@ const { intersect } = require("./util/SetHelpers"); | ||||||
| const SortableSet = require("./util/SortableSet"); | const SortableSet = require("./util/SortableSet"); | ||||||
| const { | const { | ||||||
| 	compareModulesByIdentifier, | 	compareModulesByIdentifier, | ||||||
| 	compareChunkGroupsByIndex | 	compareChunkGroupsByIndex, | ||||||
|  | 	compareModulesById | ||||||
| } = require("./util/comparators"); | } = require("./util/comparators"); | ||||||
| const { createArrayToSetDeprecationSet } = require("./util/deprecation"); | const { createArrayToSetDeprecationSet } = require("./util/deprecation"); | ||||||
| const { mergeRuntime } = require("./util/runtime"); | const { mergeRuntime } = require("./util/runtime"); | ||||||
| 
 | 
 | ||||||
| /** @typedef {import("webpack-sources").Source} Source */ | /** @typedef {import("webpack-sources").Source} Source */ | ||||||
| /** @typedef {import("./ChunkGraph").ChunkFilterPredicate} ChunkFilterPredicate */ | /** @typedef {import("./ChunkGraph").ChunkFilterPredicate} ChunkFilterPredicate */ | ||||||
| /** @typedef {import("./ChunkGraph").ChunkModuleMaps} ChunkModuleMaps */ |  | ||||||
| /** @typedef {import("./ChunkGraph").ChunkSizeOptions} ChunkSizeOptions */ | /** @typedef {import("./ChunkGraph").ChunkSizeOptions} ChunkSizeOptions */ | ||||||
| /** @typedef {import("./ChunkGraph").ModuleFilterPredicate} ModuleFilterPredicate */ | /** @typedef {import("./ChunkGraph").ModuleFilterPredicate} ModuleFilterPredicate */ | ||||||
| /** @typedef {import("./ChunkGroup")} ChunkGroup */ | /** @typedef {import("./ChunkGroup")} ChunkGroup */ | ||||||
|  | @ -45,6 +45,13 @@ const ChunkFilesSet = createArrayToSetDeprecationSet("chunk.files"); | ||||||
|  * @property {Record<string|number, string>} name |  * @property {Record<string|number, string>} name | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * @deprecated | ||||||
|  |  * @typedef {Object} ChunkModuleMaps | ||||||
|  |  * @property {Record<string|number, (string|number)[]>} id | ||||||
|  |  * @property {Record<string|number, string>} hash | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
| let debugId = 1000; | let debugId = 1000; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | @ -332,7 +339,37 @@ class Chunk { | ||||||
| 			"Chunk.getChunkModuleMaps", | 			"Chunk.getChunkModuleMaps", | ||||||
| 			"DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS" | 			"DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS" | ||||||
| 		); | 		); | ||||||
| 		return chunkGraph.getChunkModuleMaps(this, filterFn); | 		/** @type {Record<string|number, (string|number)[]>} */ | ||||||
|  | 		const chunkModuleIdMap = Object.create(null); | ||||||
|  | 		/** @type {Record<string|number, string>} */ | ||||||
|  | 		const chunkModuleHashMap = Object.create(null); | ||||||
|  | 
 | ||||||
|  | 		for (const asyncChunk of this.getAllAsyncChunks()) { | ||||||
|  | 			/** @type {(string|number)[]} */ | ||||||
|  | 			let array; | ||||||
|  | 			for (const module of chunkGraph.getOrderedChunkModulesIterable( | ||||||
|  | 				asyncChunk, | ||||||
|  | 				compareModulesById(chunkGraph) | ||||||
|  | 			)) { | ||||||
|  | 				if (filterFn(module)) { | ||||||
|  | 					if (array === undefined) { | ||||||
|  | 						array = []; | ||||||
|  | 						chunkModuleIdMap[asyncChunk.id] = array; | ||||||
|  | 					} | ||||||
|  | 					const moduleId = chunkGraph.getModuleId(module); | ||||||
|  | 					array.push(moduleId); | ||||||
|  | 					chunkModuleHashMap[moduleId] = chunkGraph.getRenderedModuleHash( | ||||||
|  | 						module, | ||||||
|  | 						undefined | ||||||
|  | 					); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		return { | ||||||
|  | 			id: chunkModuleIdMap, | ||||||
|  | 			hash: chunkModuleHashMap | ||||||
|  | 		}; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
|  |  | ||||||
|  | @ -46,12 +46,6 @@ const compareModuleIterables = compareIterables(compareModulesByIdentifier); | ||||||
|  * @property {number=} entryChunkMultiplicator multiplicator for initial chunks |  * @property {number=} entryChunkMultiplicator multiplicator for initial chunks | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /** |  | ||||||
|  * @typedef {Object} ChunkModuleMaps |  | ||||||
|  * @property {Record<string|number, (string|number)[]>} id |  | ||||||
|  * @property {Record<string|number, string>} hash |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * @typedef {Object} ModuleHashInfo |  * @typedef {Object} ModuleHashInfo | ||||||
|  * @property {string} hash |  * @property {string} hash | ||||||
|  | @ -584,48 +578,6 @@ class ChunkGraph { | ||||||
| 		return cgc.modules.getFromUnorderedCache(arrayFunction); | 		return cgc.modules.getFromUnorderedCache(arrayFunction); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** |  | ||||||
| 	 * @param {Chunk} chunk the chunk |  | ||||||
| 	 * @param {ModuleFilterPredicate} filterFn function used to filter modules |  | ||||||
| 	 * @param {boolean} includeAllChunks all chunks or only async chunks |  | ||||||
| 	 * @returns {ChunkModuleMaps} module map information |  | ||||||
| 	 */ |  | ||||||
| 	getChunkModuleMaps(chunk, filterFn, includeAllChunks = false) { |  | ||||||
| 		/** @type {Record<string|number, (string|number)[]>} */ |  | ||||||
| 		const chunkModuleIdMap = Object.create(null); |  | ||||||
| 		/** @type {Record<string|number, string>} */ |  | ||||||
| 		const chunkModuleHashMap = Object.create(null); |  | ||||||
| 
 |  | ||||||
| 		for (const asyncChunk of includeAllChunks |  | ||||||
| 			? chunk.getAllReferencedChunks() |  | ||||||
| 			: chunk.getAllAsyncChunks()) { |  | ||||||
| 			/** @type {(string|number)[]} */ |  | ||||||
| 			let array; |  | ||||||
| 			for (const module of this.getOrderedChunkModulesIterable( |  | ||||||
| 				asyncChunk, |  | ||||||
| 				compareModulesById(this) |  | ||||||
| 			)) { |  | ||||||
| 				if (filterFn(module)) { |  | ||||||
| 					if (array === undefined) { |  | ||||||
| 						array = []; |  | ||||||
| 						chunkModuleIdMap[asyncChunk.id] = array; |  | ||||||
| 					} |  | ||||||
| 					const moduleId = this.getModuleId(module); |  | ||||||
| 					array.push(moduleId); |  | ||||||
| 					chunkModuleHashMap[moduleId] = this.getRenderedModuleHash( |  | ||||||
| 						module, |  | ||||||
| 						undefined |  | ||||||
| 					); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		return { |  | ||||||
| 			id: chunkModuleIdMap, |  | ||||||
| 			hash: chunkModuleHashMap |  | ||||||
| 		}; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @param {Chunk} chunk the chunk | 	 * @param {Chunk} chunk the chunk | ||||||
| 	 * @param {ModuleFilterPredicate} filterFn function used to filter modules | 	 * @param {ModuleFilterPredicate} filterFn function used to filter modules | ||||||
|  | @ -715,20 +667,6 @@ class ChunkGraph { | ||||||
| 		return map; | 		return map; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** |  | ||||||
| 	 * @param {Chunk} chunk the chunk |  | ||||||
| 	 * @param {ModuleFilterPredicate} filterFn function used to filter modules |  | ||||||
| 	 * @returns {boolean} true, if the chunk contains at least one module matching the filter |  | ||||||
| 	 */ |  | ||||||
| 	hasModuleInChunk(chunk, filterFn) { |  | ||||||
| 		for (const module of this.getChunkModulesIterable(chunk)) { |  | ||||||
| 			if (filterFn(module)) { |  | ||||||
| 				return true; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		return false; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @param {Chunk} chunk the chunk | 	 * @param {Chunk} chunk the chunk | ||||||
| 	 * @param {ModuleFilterPredicate} filterFn predicate function used to filter modules | 	 * @param {ModuleFilterPredicate} filterFn predicate function used to filter modules | ||||||
|  |  | ||||||
|  | @ -1318,6 +1318,7 @@ class FileSystemInfo { | ||||||
| 				const cache = this._snapshotCache.get(child); | 				const cache = this._snapshotCache.get(child); | ||||||
| 				if (cache !== undefined) { | 				if (cache !== undefined) { | ||||||
| 					if (cache !== undefined) { | 					if (cache !== undefined) { | ||||||
|  | 						/* istanbul ignore else */ | ||||||
| 						if (typeof cache === "boolean") { | 						if (typeof cache === "boolean") { | ||||||
| 							if (cache === false) { | 							if (cache === false) { | ||||||
| 								invalid(); | 								invalid(); | ||||||
|  |  | ||||||
|  | @ -815,18 +815,6 @@ class NormalModule extends Module { | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** |  | ||||||
| 	 * @param {ChunkGraph} chunkGraph the chunk graph |  | ||||||
| 	 * @param {DependencyTemplates} dependencyTemplates dependency templates |  | ||||||
| 	 * @param {RuntimeSpec} runtime the runtime |  | ||||||
| 	 * @returns {string} hash |  | ||||||
| 	 */ |  | ||||||
| 	_getHashDigest(chunkGraph, dependencyTemplates, runtime) { |  | ||||||
| 		const hash = chunkGraph.getModuleHash(this, runtime); |  | ||||||
| 		const dtHash = dependencyTemplates.getHash(); |  | ||||||
| 		return `${hash}-${dtHash}`; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @returns {Set<string>} types available (do not mutate) | 	 * @returns {Set<string>} types available (do not mutate) | ||||||
| 	 */ | 	 */ | ||||||
|  |  | ||||||
|  | @ -163,6 +163,7 @@ class AsyncQueue { | ||||||
| 	 */ | 	 */ | ||||||
| 	increaseParallelism() { | 	increaseParallelism() { | ||||||
| 		this._parallelism++; | 		this._parallelism++; | ||||||
|  | 		/* istanbul ignore next */ | ||||||
| 		if (this._willEnsureProcessing === false && this._queued.length > 0) { | 		if (this._willEnsureProcessing === false && this._queued.length > 0) { | ||||||
| 			this._willEnsureProcessing = true; | 			this._willEnsureProcessing = true; | ||||||
| 			setImmediate(this._ensureProcessing); | 			setImmediate(this._ensureProcessing); | ||||||
|  |  | ||||||
|  | @ -2,6 +2,6 @@ import png from "../_images/file.png"; | ||||||
| import svg from "../_images/file.svg"; | import svg from "../_images/file.svg"; | ||||||
| 
 | 
 | ||||||
| it("should change filenames", () => { | it("should change filenames", () => { | ||||||
| 	expect(png).toEqual("images/success-png.png"); | 	expect(png).toEqual("images/[ext]/success-png.png"); | ||||||
| 	expect(svg).toEqual("images/success-svg.svg"); | 	expect(svg).toEqual("images/success-svg.svg"); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ module.exports = { | ||||||
| 	output: { | 	output: { | ||||||
| 		assetModuleFilename: ({ filename }) => { | 		assetModuleFilename: ({ filename }) => { | ||||||
| 			if (/.png$/.test(filename)) { | 			if (/.png$/.test(filename)) { | ||||||
| 				return "images/success-png[ext]"; | 				return "images/[\\ext\\]/success-png[ext]"; | ||||||
| 			} | 			} | ||||||
| 			if (/.svg$/.test(filename)) { | 			if (/.svg$/.test(filename)) { | ||||||
| 				return "images/success-svg[ext]"; | 				return "images/success-svg[ext]"; | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ module.exports = [ | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_ADD_MODULE/ }, | 	{ code: /DEP_WEBPACK_CHUNK_ADD_MODULE/ }, | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_CONTAINS_MODULE/ }, | 	{ code: /DEP_WEBPACK_CHUNK_CONTAINS_MODULE/ }, | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_ENTRY_MODULE/ }, | 	{ code: /DEP_WEBPACK_CHUNK_ENTRY_MODULE/ }, | ||||||
|  | 	{ code: /DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS/ }, | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_GET_MODULES/ }, | 	{ code: /DEP_WEBPACK_CHUNK_GET_MODULES/ }, | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_GET_NUMBER_OF_MODULES/ }, | 	{ code: /DEP_WEBPACK_CHUNK_GET_NUMBER_OF_MODULES/ }, | ||||||
| 	{ code: /DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE/ }, | 	{ code: /DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE/ }, | ||||||
|  | @ -27,5 +28,6 @@ module.exports = [ | ||||||
| 	{ code: /DEP_WEBPACK_MODULE_PROFILE/ }, | 	{ code: /DEP_WEBPACK_MODULE_PROFILE/ }, | ||||||
| 	{ code: /DEP_WEBPACK_MODULE_REMOVE_CHUNK/ }, | 	{ code: /DEP_WEBPACK_MODULE_REMOVE_CHUNK/ }, | ||||||
| 	{ code: /DEP_WEBPACK_MODULE_RENDERED_HASH/ }, | 	{ code: /DEP_WEBPACK_MODULE_RENDERED_HASH/ }, | ||||||
|  | 	{ code: /DEP_WEBPACK_MODULE_SOURCE/ }, | ||||||
| 	{ code: /DEP_WEBPACK_MODULE_USED_EXPORTS/ } | 	{ code: /DEP_WEBPACK_MODULE_USED_EXPORTS/ } | ||||||
| ]; | ]; | ||||||
|  |  | ||||||
|  | @ -27,8 +27,20 @@ module.exports = { | ||||||
| 					expect(chunk.isEmpty()).toBe(false); | 					expect(chunk.isEmpty()).toBe(false); | ||||||
| 					expect(chunk.modulesSize()).toBeTypeOf("number"); | 					expect(chunk.modulesSize()).toBeTypeOf("number"); | ||||||
| 					expect(chunk.size()).toBe(chunk.modulesSize() * 10 + 10000); | 					expect(chunk.size()).toBe(chunk.modulesSize() * 10 + 10000); | ||||||
|  | 					expect(chunk.getChunkModuleMaps(m => true)).toEqual({ | ||||||
|  | 						id: {}, | ||||||
|  | 						hash: {} | ||||||
|  | 					}); | ||||||
| 
 | 
 | ||||||
| 					const m = chunk.entryModule; | 					const m = chunk.entryModule; | ||||||
|  | 					expect( | ||||||
|  | 						m | ||||||
|  | 							.source( | ||||||
|  | 								compilation.dependencyTemplates, | ||||||
|  | 								compilation.runtimeTemplate | ||||||
|  | 							) | ||||||
|  | 							.source() | ||||||
|  | 					).toMatch(/should compile with deprecations/); | ||||||
| 					expect(m.hash).toMatch(/^[0-9a-f]{32}$/); | 					expect(m.hash).toMatch(/^[0-9a-f]{32}$/); | ||||||
| 					expect(m.renderedHash).toMatch(/^[0-9a-f]{20}$/); | 					expect(m.renderedHash).toMatch(/^[0-9a-f]{20}$/); | ||||||
| 					expect(m.profile).toBe(undefined); | 					expect(m.profile).toBe(undefined); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | module.exports = [ | ||||||
|  | 	{ code: /DEP_WEBPACK_CONFIGURATION_OPTIMIZATION_NO_EMIT_ON_ERRORS/ } | ||||||
|  | ]; | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | it("should compile with deprecations", () => {}); | ||||||
|  | @ -0,0 +1,6 @@ | ||||||
|  | /** @type {import("../../../../").Configuration} */ | ||||||
|  | module.exports = { | ||||||
|  | 	optimization: { | ||||||
|  | 		noEmitOnErrors: true | ||||||
|  | 	} | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | import { a } from "./module"; | ||||||
|  | 
 | ||||||
|  | console.log(a); | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | import { b } from "./module"; | ||||||
|  | 
 | ||||||
|  | console.log(b); | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | module.exports = [{ code: /DEP_WEBPACK_MODULE_HASH/ }]; | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | it("should compile fine", () => {}); | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | export const a = 1; | ||||||
|  | export const b = 2; | ||||||
|  | @ -0,0 +1,33 @@ | ||||||
|  | /** @type {import("../../../../").Configuration} */ | ||||||
|  | module.exports = { | ||||||
|  | 	entry: { | ||||||
|  | 		bundle0: "./index", | ||||||
|  | 		a: "./a", | ||||||
|  | 		b: "./b" | ||||||
|  | 	}, | ||||||
|  | 	output: { | ||||||
|  | 		filename: "[name].js" | ||||||
|  | 	}, | ||||||
|  | 	optimization: { | ||||||
|  | 		usedExports: true, | ||||||
|  | 		concatenateModules: false | ||||||
|  | 	}, | ||||||
|  | 	plugins: [ | ||||||
|  | 		compiler => { | ||||||
|  | 			compiler.hooks.compilation.tap("Test", compilation => { | ||||||
|  | 				compilation.hooks.afterModuleHash.tap("Test", () => { | ||||||
|  | 					const hashes = []; | ||||||
|  | 					expect(() => { | ||||||
|  | 						for (const module of compilation.chunkGraph.getChunkModulesIterable( | ||||||
|  | 							compilation.namedChunks.get("a") | ||||||
|  | 						)) { | ||||||
|  | 							hashes.push(module.hash); | ||||||
|  | 						} | ||||||
|  | 					}).toThrowError( | ||||||
|  | 						/No unique hash info entry for unspecified runtime .+ \(existing runtimes: a, b\)\.\n.+opt-out via optimization\.usedExports: "global"/ | ||||||
|  | 					); | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		} | ||||||
|  | 	] | ||||||
|  | }; | ||||||
|  | @ -14,6 +14,9 @@ module.exports = { | ||||||
| 			} | 			} | ||||||
| 		] | 		] | ||||||
| 	}, | 	}, | ||||||
|  | 	output: { | ||||||
|  | 		webassemblyModuleFilename: "[id].[hash].wasm" | ||||||
|  | 	}, | ||||||
| 	experiments: { | 	experiments: { | ||||||
| 		asyncWebAssembly: true, | 		asyncWebAssembly: true, | ||||||
| 		importAwait: true | 		importAwait: true | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| /** @type {import("../../../../").Configuration} */ | /** @type {import("../../../../").Configuration} */ | ||||||
| module.exports = { | module.exports = { | ||||||
| 	entry: "./index", | 	entry: "./index", | ||||||
|  | 	output: { | ||||||
|  | 		webassemblyModuleFilename: "[id].[hash:3].wasm" | ||||||
|  | 	}, | ||||||
| 	module: { | 	module: { | ||||||
| 		rules: [ | 		rules: [ | ||||||
| 			{ | 			{ | ||||||
|  |  | ||||||
|  | @ -633,11 +633,6 @@ declare class ChunkGraph { | ||||||
| 		chunk: Chunk, | 		chunk: Chunk, | ||||||
| 		comparator: (arg0: Module, arg1: Module) => 0 | 1 | -1 | 		comparator: (arg0: Module, arg1: Module) => 0 | 1 | -1 | ||||||
| 	): Module[]; | 	): Module[]; | ||||||
| 	getChunkModuleMaps( |  | ||||||
| 		chunk: Chunk, |  | ||||||
| 		filterFn: (m: Module) => boolean, |  | ||||||
| 		includeAllChunks?: boolean |  | ||||||
| 	): ChunkModuleMaps; |  | ||||||
| 	getChunkModuleIdMap( | 	getChunkModuleIdMap( | ||||||
| 		chunk: Chunk, | 		chunk: Chunk, | ||||||
| 		filterFn: (m: Module) => boolean, | 		filterFn: (m: Module) => boolean, | ||||||
|  | @ -653,7 +648,6 @@ declare class ChunkGraph { | ||||||
| 		chunk: Chunk, | 		chunk: Chunk, | ||||||
| 		filterFn: (c: Chunk, chunkGraph: ChunkGraph) => boolean | 		filterFn: (c: Chunk, chunkGraph: ChunkGraph) => boolean | ||||||
| 	): Record<string | number, boolean>; | 	): Record<string | number, boolean>; | ||||||
| 	hasModuleInChunk(chunk: Chunk, filterFn: (m: Module) => boolean): boolean; |  | ||||||
| 	hasModuleInGraph( | 	hasModuleInGraph( | ||||||
| 		chunk: Chunk, | 		chunk: Chunk, | ||||||
| 		filterFn: (m: Module) => boolean, | 		filterFn: (m: Module) => boolean, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue