| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	evaluateToIdentifier, | 
					
						
							|  |  |  | 	expressionIsUnsupported, | 
					
						
							|  |  |  | 	toConstantDependency | 
					
						
							|  |  |  | } = require("./JavascriptParserHelpers"); | 
					
						
							|  |  |  | const RuntimeGlobals = require("./RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("./RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("./Template"); | 
					
						
							|  |  |  | const ModuleDecoratorDependency = require("./dependencies/ModuleDecoratorDependency"); | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CommonJsStuffPlugin { | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"CommonJsStuffPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					ModuleDecoratorDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ModuleDecoratorDependency, | 
					
						
							|  |  |  | 					new ModuleDecoratorDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInModule | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.harmonyModuleDecorator) | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", (module, set) => { | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.module); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.require); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInModule | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.nodeModuleDecorator) | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", (module, set) => { | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.module); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.require); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.harmonyModuleDecorator) | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", (chunk, set) => { | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							new HarmonyModuleDecoratorRuntimeModule() | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.nodeModuleDecorator) | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", (chunk, set) => { | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							new NodeModuleDecoratorRuntimeModule() | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 				const handler = (parser, parserOptions) => { | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("require.main.require") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"CommonJsStuffPlugin", | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							expressionIsUnsupported( | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 								parser, | 
					
						
							|  |  |  | 								"require.main.require is not supported by webpack." | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("module.parent.require") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"CommonJsStuffPlugin", | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							expressionIsUnsupported( | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 								parser, | 
					
						
							|  |  |  | 								"module.parent.require is not supported by webpack." | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("require.main") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"CommonJsStuffPlugin", | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							toConstantDependency( | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 								parser, | 
					
						
							| 
									
										
										
										
											2019-06-13 16:51:12 +08:00
										 |  |  | 								`${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}]`, | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 								[RuntimeGlobals.moduleCache, RuntimeGlobals.entryModuleId] | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("module.loaded") | 
					
						
							|  |  |  | 						.tap("CommonJsStuffPlugin", expr => { | 
					
						
							|  |  |  | 							parser.state.module.buildMeta.moduleConcatenationBailout = | 
					
						
							|  |  |  | 								"module.loaded"; | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							return toConstantDependency( | 
					
						
							|  |  |  | 								parser, | 
					
						
							|  |  |  | 								`${RuntimeGlobals.module}.l`, | 
					
						
							|  |  |  | 								[RuntimeGlobals.module] | 
					
						
							|  |  |  | 							)(expr); | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("module.id") | 
					
						
							|  |  |  | 						.tap("CommonJsStuffPlugin", expr => { | 
					
						
							|  |  |  | 							parser.state.module.buildMeta.moduleConcatenationBailout = | 
					
						
							|  |  |  | 								"module.id"; | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							return toConstantDependency( | 
					
						
							|  |  |  | 								parser, | 
					
						
							|  |  |  | 								`${RuntimeGlobals.module}.i`, | 
					
						
							|  |  |  | 								[RuntimeGlobals.module] | 
					
						
							|  |  |  | 							)(expr); | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("module.exports") | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 						.tap("CommonJsStuffPlugin", expr => { | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 							const module = parser.state.module; | 
					
						
							|  |  |  | 							const isHarmony = | 
					
						
							|  |  |  | 								module.buildMeta && module.buildMeta.exportsType; | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							if (!isHarmony) { | 
					
						
							|  |  |  | 								return toConstantDependency( | 
					
						
							|  |  |  | 									parser, | 
					
						
							|  |  |  | 									`${RuntimeGlobals.module}.exports`, | 
					
						
							|  |  |  | 									[RuntimeGlobals.module] | 
					
						
							|  |  |  | 								)(expr); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					parser.hooks.evaluateIdentifier | 
					
						
							|  |  |  | 						.for("module.hot") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"CommonJsStuffPlugin", | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							evaluateToIdentifier("module.hot", false) | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("module") | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 						.tap("CommonJsStuffPlugin", expr => { | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 							const isHarmony = | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 								parser.state.module.buildMeta && | 
					
						
							|  |  |  | 								parser.state.module.buildMeta.exportsType; | 
					
						
							|  |  |  | 							const dep = new ModuleDecoratorDependency( | 
					
						
							|  |  |  | 								isHarmony | 
					
						
							|  |  |  | 									? RuntimeGlobals.harmonyModuleDecorator | 
					
						
							|  |  |  | 									: RuntimeGlobals.nodeModuleDecorator | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 							dep.loc = expr.loc; | 
					
						
							|  |  |  | 							parser.state.module.addDependency(dep); | 
					
						
							|  |  |  | 							return true; | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/auto") | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/dynamic") | 
					
						
							|  |  |  | 					.tap("CommonJsStuffPlugin", handler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-22 19:36:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class HarmonyModuleDecoratorRuntimeModule extends RuntimeModule { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super("harmony module decorator"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`${RuntimeGlobals.harmonyModuleDecorator} = function(module) {`, | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				"module = Object.create(module);", | 
					
						
							|  |  |  | 				"if (!module.children) module.children = [];", | 
					
						
							|  |  |  | 				"Object.defineProperty(module, 'loaded', {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"enumerable: true,", | 
					
						
							|  |  |  | 					"get: function () { return module.l; }" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"Object.defineProperty(module, 'id', {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"enumerable: true,", | 
					
						
							|  |  |  | 					"get: function () { return module.i; }" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"Object.defineProperty(module, 'exports', {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"enumerable: true,", | 
					
						
							|  |  |  | 					"set: function () {", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						"throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"return module;" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"};" | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NodeModuleDecoratorRuntimeModule extends RuntimeModule { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super("node module decorator"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`${RuntimeGlobals.nodeModuleDecorator} = function(module) {`, | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				"module.paths = [];", | 
					
						
							|  |  |  | 				"if (!module.children) module.children = [];", | 
					
						
							|  |  |  | 				"Object.defineProperty(module, 'loaded', {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"enumerable: true,", | 
					
						
							|  |  |  | 					"get: function() { return module.l; }" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"Object.defineProperty(module, 'id', {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"enumerable: true,", | 
					
						
							|  |  |  | 					"get: function() { return module.i; }" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"return module;" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"};" | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 14:03:44 +08:00
										 |  |  | module.exports = CommonJsStuffPlugin; |