| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | const util = require("util"); | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | const { ConcatSource, RawSource, ReplaceSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | const Generator = require("./Generator"); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("./DependenciesBlock")} DependenciesBlock */ | 
					
						
							|  |  |  | /** @typedef {import("./Dependency")} Dependency */ | 
					
						
							|  |  |  | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | /** @typedef {import("./Generator").GenerateContext} GenerateContext */ | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  | /** @typedef {import("./InitFragment")} InitFragment */ | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | /** @typedef {import("./NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | // TODO: clean up this file
 | 
					
						
							|  |  |  | // replace with newer constructs
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-18 19:37:08 +08:00
										 |  |  | const deprecatedGetInitFragments = util.deprecate( | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 	(template, dependency, templateContext) => | 
					
						
							|  |  |  | 		template.getInitFragments(dependency, templateContext), | 
					
						
							|  |  |  | 	"DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)" | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  |  * @param {InitFragment} fragment the init fragment | 
					
						
							|  |  |  |  * @param {number} index index | 
					
						
							|  |  |  |  * @returns {[InitFragment, number]} tuple with both | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | const extractFragmentIndex = (fragment, index) => [fragment, index]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {[InitFragment, number]} a first pair | 
					
						
							|  |  |  |  * @param {[InitFragment, number]} b second pair | 
					
						
							|  |  |  |  * @returns {number} sort value | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-30 16:15:18 +08:00
										 |  |  | const sortFragmentWithIndex = ([a, i], [b, j]) => { | 
					
						
							|  |  |  | 	const stageCmp = a.stage - b.stage; | 
					
						
							|  |  |  | 	if (stageCmp !== 0) return stageCmp; | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 	const positionCmp = a.position - b.position; | 
					
						
							|  |  |  | 	if (positionCmp !== 0) return positionCmp; | 
					
						
							| 
									
										
										
										
											2018-07-30 16:15:18 +08:00
										 |  |  | 	return i - j; | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | const TYPES = new Set(["javascript"]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | class JavascriptGenerator extends Generator { | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getTypes() { | 
					
						
							|  |  |  | 		return TYPES; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSize(module, type) { | 
					
						
							|  |  |  | 		const originalSource = module.originalSource(); | 
					
						
							|  |  |  | 		if (!originalSource) { | 
					
						
							|  |  |  | 			return 39; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return originalSource.size(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-07-18 00:57:03 +08:00
										 |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	generate(module, generateContext) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		const originalSource = module.originalSource(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (!originalSource) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 			return new RawSource("throw new Error('No source available');"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const source = new ReplaceSource(originalSource); | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 		const initFragments = []; | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 		this.sourceBlock(module, module, initFragments, source, generateContext); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 		if (initFragments.length > 0) { | 
					
						
							|  |  |  | 			// Sort fragments by position. If 2 fragments have the same position,
 | 
					
						
							| 
									
										
										
										
											2018-07-25 01:56:35 +08:00
										 |  |  | 			// use their index.
 | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 			const sortedFragments = initFragments | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 				.map(extractFragmentIndex) | 
					
						
							| 
									
										
										
										
											2018-07-30 16:15:18 +08:00
										 |  |  | 				.sort(sortFragmentWithIndex); | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 01:56:35 +08:00
										 |  |  | 			// Deduplicate fragments. If a fragment has no key, it is always included.
 | 
					
						
							|  |  |  | 			const keyedFragments = new Map(); | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 			for (const [fragment] of sortedFragments) { | 
					
						
							| 
									
										
										
										
											2018-07-25 01:56:35 +08:00
										 |  |  | 				keyedFragments.set(fragment.key || Symbol(), fragment); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const concatSource = new ConcatSource(); | 
					
						
							|  |  |  | 			for (const fragment of keyedFragments.values()) { | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 				concatSource.add(fragment.content); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			concatSource.add(source); | 
					
						
							|  |  |  | 			return concatSource; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return source; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Module} module the module to generate | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 	 * @param {DependenciesBlock} block the dependencies block which will be processed | 
					
						
							|  |  |  | 	 * @param {InitFragment[]} initFragments mutable list of init fragments | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	 * @param {ReplaceSource} source the current replace source which can be modified | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	 * @param {GenerateContext} generateContext the generateContext | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	sourceBlock(module, block, initFragments, source, generateContext) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const dependency of block.dependencies) { | 
					
						
							|  |  |  | 			this.sourceDependency( | 
					
						
							| 
									
										
										
										
											2018-07-31 03:39:17 +08:00
										 |  |  | 				module, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				dependency, | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 				initFragments, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				source, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				generateContext | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const childBlock of block.blocks) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 			this.sourceBlock( | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				childBlock, | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 				initFragments, | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 				source, | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 				generateContext | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-07-31 03:39:17 +08:00
										 |  |  | 	 * @param {Module} module the current module | 
					
						
							| 
									
										
										
										
											2018-07-18 01:38:42 +08:00
										 |  |  | 	 * @param {Dependency} dependency the dependency to generate | 
					
						
							| 
									
										
										
										
											2018-07-30 18:09:42 +08:00
										 |  |  | 	 * @param {InitFragment[]} initFragments mutable list of init fragments | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	 * @param {ReplaceSource} source the current replace source which can be modified | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	 * @param {GenerateContext} generateContext the render context | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 	sourceDependency(module, dependency, initFragments, source, generateContext) { | 
					
						
							| 
									
										
										
										
											2018-07-18 01:38:42 +08:00
										 |  |  | 		const constructor = | 
					
						
							|  |  |  | 			/** @type {new (...args: any[]) => Dependency} */ (dependency.constructor); | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 		const template = generateContext.dependencyTemplates.get(constructor); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (!template) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				"No template for dependency: " + dependency.constructor.name | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 		const templateContext = { | 
					
						
							|  |  |  | 			runtimeTemplate: generateContext.runtimeTemplate, | 
					
						
							|  |  |  | 			dependencyTemplates: generateContext.dependencyTemplates, | 
					
						
							|  |  |  | 			moduleGraph: generateContext.moduleGraph, | 
					
						
							| 
									
										
										
										
											2018-08-23 02:17:49 +08:00
										 |  |  | 			chunkGraph: generateContext.chunkGraph, | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 			module, | 
					
						
							| 
									
										
										
										
											2018-11-15 00:31:32 +08:00
										 |  |  | 			runtimeRequirements: generateContext.runtimeRequirements, | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 			initFragments | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template.apply(dependency, source, templateContext); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 		// TODO remove in webpack 6
 | 
					
						
							|  |  |  | 		if ("getInitFragments" in template) { | 
					
						
							| 
									
										
										
										
											2018-11-18 19:37:08 +08:00
										 |  |  | 			const fragments = deprecatedGetInitFragments( | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 				template, | 
					
						
							|  |  |  | 				dependency, | 
					
						
							|  |  |  | 				templateContext | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:57:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 23:40:03 +08:00
										 |  |  | 			if (fragments) { | 
					
						
							|  |  |  | 				for (const fragment of fragments) { | 
					
						
							|  |  |  | 					initFragments.push(fragment); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JavascriptGenerator; |