| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Joel Denning @joeldenning | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const { UsageState } = require("../ExportsInfo"); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | const ExternalModule = require("../ExternalModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | const propertyAccess = require("../util/propertyAccess"); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */ | 
					
						
							|  |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */ | 
					
						
							|  |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							|  |  |  | /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} SystemLibraryPluginOptions | 
					
						
							|  |  |  |  * @property {LibraryType} type | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} SystemLibraryPluginParsed | 
					
						
							|  |  |  |  * @property {string} name | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {SystemLibraryPluginParsed} T | 
					
						
							|  |  |  |  * @extends {AbstractLibraryPlugin<SystemLibraryPluginParsed>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class SystemLibraryPlugin extends AbstractLibraryPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {SystemLibraryPluginOptions} options the plugin options | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		super({ | 
					
						
							|  |  |  | 			pluginName: "SystemLibraryPlugin", | 
					
						
							|  |  |  | 			type: options.type | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {LibraryOptions} library normalized library option | 
					
						
							|  |  |  | 	 * @returns {T | false} preprocess as needed by overriding | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	parseOptions(library) { | 
					
						
							| 
									
										
										
										
											2020-02-27 00:20:50 +08:00
										 |  |  | 		const { name } = library; | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 		if (name && typeof name !== "string") { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							| 
									
										
										
										
											2021-02-05 06:11:11 +08:00
										 |  |  | 				`System.js library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}` | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			name: /** @type {string=} */ (name) | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Source} source source | 
					
						
							|  |  |  | 	 * @param {RenderContext} renderContext render context | 
					
						
							|  |  |  | 	 * @param {LibraryContext<T>} libraryContext context | 
					
						
							|  |  |  | 	 * @returns {Source} source with library export | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 	render(source, { chunkGraph, moduleGraph, chunk }, { options, compilation }) { | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 		const modules = chunkGraph | 
					
						
							|  |  |  | 			.getChunkModules(chunk) | 
					
						
							| 
									
										
										
										
											2021-07-08 21:07:21 +08:00
										 |  |  | 			.filter(m => m instanceof ExternalModule && m.externalType === "system"); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 		const externals = /** @type {ExternalModule[]} */ (modules); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// The name this bundle should be registered as with System
 | 
					
						
							|  |  |  | 		const name = options.name | 
					
						
							|  |  |  | 			? `${JSON.stringify(compilation.getPath(options.name, { chunk }))}, ` | 
					
						
							|  |  |  | 			: ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// The array of dependencies that are external to webpack and will be provided by System
 | 
					
						
							|  |  |  | 		const systemDependencies = JSON.stringify( | 
					
						
							|  |  |  | 			externals.map(m => | 
					
						
							|  |  |  | 				typeof m.request === "object" && !Array.isArray(m.request) | 
					
						
							|  |  |  | 					? m.request.amd | 
					
						
							|  |  |  | 					: m.request | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// The name of the variable provided by System for exporting
 | 
					
						
							|  |  |  | 		const dynamicExport = "__WEBPACK_DYNAMIC_EXPORT__"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// An array of the internal variable names for the webpack externals
 | 
					
						
							|  |  |  | 		const externalWebpackNames = externals.map( | 
					
						
							|  |  |  | 			m => | 
					
						
							|  |  |  | 				`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier( | 
					
						
							|  |  |  | 					`${chunkGraph.getModuleId(m)}` | 
					
						
							|  |  |  | 				)}__`
 | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Declaring variables for the internal variable names for the webpack externals
 | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 		const externalVarDeclarations = externalWebpackNames | 
					
						
							|  |  |  | 			.map(name => `var ${name} = {};`) | 
					
						
							|  |  |  | 			.join("\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Define __esModule flag on all internal variables and helpers
 | 
					
						
							|  |  |  | 		const externalVarInitialization = []; | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// The system.register format requires an array of setter functions for externals.
 | 
					
						
							|  |  |  | 		const setters = | 
					
						
							|  |  |  | 			externalWebpackNames.length === 0 | 
					
						
							|  |  |  | 				? "" | 
					
						
							|  |  |  | 				: Template.asString([ | 
					
						
							|  |  |  | 						"setters: [", | 
					
						
							|  |  |  | 						Template.indent( | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 							externals | 
					
						
							|  |  |  | 								.map((module, i) => { | 
					
						
							|  |  |  | 									const external = externalWebpackNames[i]; | 
					
						
							|  |  |  | 									const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							|  |  |  | 									const otherUnused = | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 										exportsInfo.otherExportsInfo.getUsed(chunk.runtime) === | 
					
						
							|  |  |  | 										UsageState.Unused; | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 									const instructions = []; | 
					
						
							|  |  |  | 									const handledNames = []; | 
					
						
							|  |  |  | 									for (const exportInfo of exportsInfo.orderedExports) { | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 										const used = exportInfo.getUsedName( | 
					
						
							|  |  |  | 											undefined, | 
					
						
							|  |  |  | 											chunk.runtime | 
					
						
							|  |  |  | 										); | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 										if (used) { | 
					
						
							|  |  |  | 											if (otherUnused || used !== exportInfo.name) { | 
					
						
							|  |  |  | 												instructions.push( | 
					
						
							|  |  |  | 													`${external}${propertyAccess([ | 
					
						
							|  |  |  | 														used | 
					
						
							|  |  |  | 													])} = module${propertyAccess([exportInfo.name])};`
 | 
					
						
							|  |  |  | 												); | 
					
						
							|  |  |  | 												handledNames.push(exportInfo.name); | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											handledNames.push(exportInfo.name); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									if (!otherUnused) { | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | 										if ( | 
					
						
							|  |  |  | 											!Array.isArray(module.request) || | 
					
						
							|  |  |  | 											module.request.length === 1 | 
					
						
							|  |  |  | 										) { | 
					
						
							|  |  |  | 											externalVarInitialization.push( | 
					
						
							|  |  |  | 												`Object.defineProperty(${external}, "__esModule", { value: true });` | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 										} | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 										if (handledNames.length > 0) { | 
					
						
							|  |  |  | 											const name = `${external}handledNames`; | 
					
						
							|  |  |  | 											externalVarInitialization.push( | 
					
						
							|  |  |  | 												`var ${name} = ${JSON.stringify(handledNames)};` | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 											instructions.push( | 
					
						
							|  |  |  | 												Template.asString([ | 
					
						
							|  |  |  | 													"Object.keys(module).forEach(function(key) {", | 
					
						
							|  |  |  | 													Template.indent([ | 
					
						
							|  |  |  | 														`if(${name}.indexOf(key) >= 0)`, | 
					
						
							|  |  |  | 														Template.indent(`${external}[key] = module[key];`) | 
					
						
							|  |  |  | 													]), | 
					
						
							|  |  |  | 													"});" | 
					
						
							|  |  |  | 												]) | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											instructions.push( | 
					
						
							|  |  |  | 												Template.asString([ | 
					
						
							|  |  |  | 													"Object.keys(module).forEach(function(key) {", | 
					
						
							|  |  |  | 													Template.indent([`${external}[key] = module[key];`]), | 
					
						
							|  |  |  | 													"});" | 
					
						
							|  |  |  | 												]) | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2020-03-09 18:03:41 +08:00
										 |  |  | 									if (instructions.length === 0) return "function() {}"; | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 									return Template.asString([ | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 										"function(module) {", | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 										Template.indent(instructions), | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 										"}" | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 									]); | 
					
						
							|  |  |  | 								}) | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 								.join(",\n") | 
					
						
							|  |  |  | 						), | 
					
						
							|  |  |  | 						"]," | 
					
						
							|  |  |  | 				  ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return new ConcatSource( | 
					
						
							|  |  |  | 			Template.asString([ | 
					
						
							| 
									
										
										
										
											2019-05-13 04:29:50 +08:00
										 |  |  | 				`System.register(${name}${systemDependencies}, function(${dynamicExport}, __system_context__) {`, | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					externalVarDeclarations, | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 					Template.asString(externalVarInitialization), | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					"return {", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						setters, | 
					
						
							|  |  |  | 						"execute: function() {", | 
					
						
							|  |  |  | 						Template.indent(`${dynamicExport}(`) | 
					
						
							|  |  |  | 					]) | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 				]), | 
					
						
							|  |  |  | 				"" | 
					
						
							|  |  |  | 			]), | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 			source, | 
					
						
							| 
									
										
										
										
											2020-03-05 20:31:58 +08:00
										 |  |  | 			Template.asString([ | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					Template.indent([Template.indent([");"]), "}"]), | 
					
						
							|  |  |  | 					"};" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"})" | 
					
						
							|  |  |  | 			]) | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Chunk} chunk the chunk | 
					
						
							|  |  |  | 	 * @param {Hash} hash hash | 
					
						
							|  |  |  | 	 * @param {ChunkHashContext} chunkHashContext chunk hash context | 
					
						
							|  |  |  | 	 * @param {LibraryContext<T>} libraryContext context | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	chunkHash(chunk, hash, chunkHashContext, { options, compilation }) { | 
					
						
							|  |  |  | 		hash.update("SystemLibraryPlugin"); | 
					
						
							|  |  |  | 		if (options.name) { | 
					
						
							|  |  |  | 			hash.update(compilation.getPath(options.name, { chunk })); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = SystemLibraryPlugin; |