[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 9 | const RuntimeModule = require("../RuntimeModule");
|
---|
| 10 | const SelfModuleFactory = require("../SelfModuleFactory");
|
---|
| 11 | const Template = require("../Template");
|
---|
| 12 | const CommonJsExportsDependency = require("./CommonJsExportsDependency");
|
---|
| 13 | const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency");
|
---|
| 14 | const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
|
---|
| 15 | const CommonJsRequireDependency = require("./CommonJsRequireDependency");
|
---|
| 16 | const CommonJsSelfReferenceDependency = require("./CommonJsSelfReferenceDependency");
|
---|
| 17 | const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
|
---|
| 18 | const RequireHeaderDependency = require("./RequireHeaderDependency");
|
---|
| 19 | const RequireResolveContextDependency = require("./RequireResolveContextDependency");
|
---|
| 20 | const RequireResolveDependency = require("./RequireResolveDependency");
|
---|
| 21 | const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
|
---|
| 22 | const RuntimeRequirementsDependency = require("./RuntimeRequirementsDependency");
|
---|
| 23 |
|
---|
| 24 | const CommonJsExportsParserPlugin = require("./CommonJsExportsParserPlugin");
|
---|
| 25 | const CommonJsImportsParserPlugin = require("./CommonJsImportsParserPlugin");
|
---|
| 26 |
|
---|
| 27 | const {
|
---|
| 28 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 29 | JAVASCRIPT_MODULE_TYPE_DYNAMIC
|
---|
| 30 | } = require("../ModuleTypeConstants");
|
---|
| 31 | const {
|
---|
| 32 | evaluateToIdentifier,
|
---|
| 33 | toConstantDependency
|
---|
| 34 | } = require("../javascript/JavascriptParserHelpers");
|
---|
| 35 | const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency");
|
---|
| 36 |
|
---|
| 37 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
| 38 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 39 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 40 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
| 41 | /** @typedef {import("../Module").BuildInfo} BuildInfo */
|
---|
| 42 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
| 43 |
|
---|
| 44 | const PLUGIN_NAME = "CommonJsPlugin";
|
---|
| 45 |
|
---|
| 46 | class CommonJsPlugin {
|
---|
| 47 | /**
|
---|
| 48 | * Apply the plugin
|
---|
| 49 | * @param {Compiler} compiler the compiler instance
|
---|
| 50 | * @returns {void}
|
---|
| 51 | */
|
---|
| 52 | apply(compiler) {
|
---|
| 53 | compiler.hooks.compilation.tap(
|
---|
| 54 | PLUGIN_NAME,
|
---|
| 55 | (compilation, { contextModuleFactory, normalModuleFactory }) => {
|
---|
| 56 | compilation.dependencyFactories.set(
|
---|
| 57 | CommonJsRequireDependency,
|
---|
| 58 | normalModuleFactory
|
---|
| 59 | );
|
---|
| 60 | compilation.dependencyTemplates.set(
|
---|
| 61 | CommonJsRequireDependency,
|
---|
| 62 | new CommonJsRequireDependency.Template()
|
---|
| 63 | );
|
---|
| 64 |
|
---|
| 65 | compilation.dependencyFactories.set(
|
---|
| 66 | CommonJsFullRequireDependency,
|
---|
| 67 | normalModuleFactory
|
---|
| 68 | );
|
---|
| 69 | compilation.dependencyTemplates.set(
|
---|
| 70 | CommonJsFullRequireDependency,
|
---|
| 71 | new CommonJsFullRequireDependency.Template()
|
---|
| 72 | );
|
---|
| 73 |
|
---|
| 74 | compilation.dependencyFactories.set(
|
---|
| 75 | CommonJsRequireContextDependency,
|
---|
| 76 | contextModuleFactory
|
---|
| 77 | );
|
---|
| 78 | compilation.dependencyTemplates.set(
|
---|
| 79 | CommonJsRequireContextDependency,
|
---|
| 80 | new CommonJsRequireContextDependency.Template()
|
---|
| 81 | );
|
---|
| 82 |
|
---|
| 83 | compilation.dependencyFactories.set(
|
---|
| 84 | RequireResolveDependency,
|
---|
| 85 | normalModuleFactory
|
---|
| 86 | );
|
---|
| 87 | compilation.dependencyTemplates.set(
|
---|
| 88 | RequireResolveDependency,
|
---|
| 89 | new RequireResolveDependency.Template()
|
---|
| 90 | );
|
---|
| 91 |
|
---|
| 92 | compilation.dependencyFactories.set(
|
---|
| 93 | RequireResolveContextDependency,
|
---|
| 94 | contextModuleFactory
|
---|
| 95 | );
|
---|
| 96 | compilation.dependencyTemplates.set(
|
---|
| 97 | RequireResolveContextDependency,
|
---|
| 98 | new RequireResolveContextDependency.Template()
|
---|
| 99 | );
|
---|
| 100 |
|
---|
| 101 | compilation.dependencyTemplates.set(
|
---|
| 102 | RequireResolveHeaderDependency,
|
---|
| 103 | new RequireResolveHeaderDependency.Template()
|
---|
| 104 | );
|
---|
| 105 |
|
---|
| 106 | compilation.dependencyTemplates.set(
|
---|
| 107 | RequireHeaderDependency,
|
---|
| 108 | new RequireHeaderDependency.Template()
|
---|
| 109 | );
|
---|
| 110 |
|
---|
| 111 | compilation.dependencyTemplates.set(
|
---|
| 112 | CommonJsExportsDependency,
|
---|
| 113 | new CommonJsExportsDependency.Template()
|
---|
| 114 | );
|
---|
| 115 |
|
---|
| 116 | compilation.dependencyFactories.set(
|
---|
| 117 | CommonJsExportRequireDependency,
|
---|
| 118 | normalModuleFactory
|
---|
| 119 | );
|
---|
| 120 | compilation.dependencyTemplates.set(
|
---|
| 121 | CommonJsExportRequireDependency,
|
---|
| 122 | new CommonJsExportRequireDependency.Template()
|
---|
| 123 | );
|
---|
| 124 |
|
---|
| 125 | const selfFactory = new SelfModuleFactory(compilation.moduleGraph);
|
---|
| 126 |
|
---|
| 127 | compilation.dependencyFactories.set(
|
---|
| 128 | CommonJsSelfReferenceDependency,
|
---|
| 129 | selfFactory
|
---|
| 130 | );
|
---|
| 131 | compilation.dependencyTemplates.set(
|
---|
| 132 | CommonJsSelfReferenceDependency,
|
---|
| 133 | new CommonJsSelfReferenceDependency.Template()
|
---|
| 134 | );
|
---|
| 135 |
|
---|
| 136 | compilation.dependencyFactories.set(
|
---|
| 137 | ModuleDecoratorDependency,
|
---|
| 138 | selfFactory
|
---|
| 139 | );
|
---|
| 140 | compilation.dependencyTemplates.set(
|
---|
| 141 | ModuleDecoratorDependency,
|
---|
| 142 | new ModuleDecoratorDependency.Template()
|
---|
| 143 | );
|
---|
| 144 |
|
---|
| 145 | compilation.hooks.runtimeRequirementInModule
|
---|
| 146 | .for(RuntimeGlobals.harmonyModuleDecorator)
|
---|
| 147 | .tap(PLUGIN_NAME, (module, set) => {
|
---|
| 148 | set.add(RuntimeGlobals.module);
|
---|
| 149 | set.add(RuntimeGlobals.requireScope);
|
---|
| 150 | });
|
---|
| 151 |
|
---|
| 152 | compilation.hooks.runtimeRequirementInModule
|
---|
| 153 | .for(RuntimeGlobals.nodeModuleDecorator)
|
---|
| 154 | .tap(PLUGIN_NAME, (module, set) => {
|
---|
| 155 | set.add(RuntimeGlobals.module);
|
---|
| 156 | set.add(RuntimeGlobals.requireScope);
|
---|
| 157 | });
|
---|
| 158 |
|
---|
| 159 | compilation.hooks.runtimeRequirementInTree
|
---|
| 160 | .for(RuntimeGlobals.harmonyModuleDecorator)
|
---|
| 161 | .tap(PLUGIN_NAME, (chunk, set) => {
|
---|
| 162 | compilation.addRuntimeModule(
|
---|
| 163 | chunk,
|
---|
| 164 | new HarmonyModuleDecoratorRuntimeModule()
|
---|
| 165 | );
|
---|
| 166 | });
|
---|
| 167 |
|
---|
| 168 | compilation.hooks.runtimeRequirementInTree
|
---|
| 169 | .for(RuntimeGlobals.nodeModuleDecorator)
|
---|
| 170 | .tap(PLUGIN_NAME, (chunk, set) => {
|
---|
| 171 | compilation.addRuntimeModule(
|
---|
| 172 | chunk,
|
---|
| 173 | new NodeModuleDecoratorRuntimeModule()
|
---|
| 174 | );
|
---|
| 175 | });
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * @param {Parser} parser parser parser
|
---|
| 179 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
| 180 | * @returns {void}
|
---|
| 181 | */
|
---|
| 182 | const handler = (parser, parserOptions) => {
|
---|
| 183 | if (parserOptions.commonjs !== undefined && !parserOptions.commonjs)
|
---|
| 184 | return;
|
---|
| 185 | parser.hooks.typeof
|
---|
| 186 | .for("module")
|
---|
| 187 | .tap(
|
---|
| 188 | PLUGIN_NAME,
|
---|
| 189 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
| 190 | );
|
---|
| 191 |
|
---|
| 192 | parser.hooks.expression
|
---|
| 193 | .for("require.main")
|
---|
| 194 | .tap(
|
---|
| 195 | PLUGIN_NAME,
|
---|
| 196 | toConstantDependency(
|
---|
| 197 | parser,
|
---|
| 198 | `${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}]`,
|
---|
| 199 | [RuntimeGlobals.moduleCache, RuntimeGlobals.entryModuleId]
|
---|
| 200 | )
|
---|
| 201 | );
|
---|
| 202 | parser.hooks.expression
|
---|
| 203 | .for(RuntimeGlobals.moduleLoaded)
|
---|
| 204 | .tap(PLUGIN_NAME, expr => {
|
---|
| 205 | /** @type {BuildInfo} */
|
---|
| 206 | (parser.state.module.buildInfo).moduleConcatenationBailout =
|
---|
| 207 | RuntimeGlobals.moduleLoaded;
|
---|
| 208 | const dep = new RuntimeRequirementsDependency([
|
---|
| 209 | RuntimeGlobals.moduleLoaded
|
---|
| 210 | ]);
|
---|
| 211 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 212 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 213 | return true;
|
---|
| 214 | });
|
---|
| 215 |
|
---|
| 216 | parser.hooks.expression
|
---|
| 217 | .for(RuntimeGlobals.moduleId)
|
---|
| 218 | .tap(PLUGIN_NAME, expr => {
|
---|
| 219 | /** @type {BuildInfo} */
|
---|
| 220 | (parser.state.module.buildInfo).moduleConcatenationBailout =
|
---|
| 221 | RuntimeGlobals.moduleId;
|
---|
| 222 | const dep = new RuntimeRequirementsDependency([
|
---|
| 223 | RuntimeGlobals.moduleId
|
---|
| 224 | ]);
|
---|
| 225 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 226 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 227 | return true;
|
---|
| 228 | });
|
---|
| 229 |
|
---|
| 230 | parser.hooks.evaluateIdentifier.for("module.hot").tap(
|
---|
| 231 | PLUGIN_NAME,
|
---|
| 232 | evaluateToIdentifier("module.hot", "module", () => ["hot"], null)
|
---|
| 233 | );
|
---|
| 234 |
|
---|
| 235 | new CommonJsImportsParserPlugin(parserOptions).apply(parser);
|
---|
| 236 | new CommonJsExportsParserPlugin(compilation.moduleGraph).apply(
|
---|
| 237 | parser
|
---|
| 238 | );
|
---|
| 239 | };
|
---|
| 240 |
|
---|
| 241 | normalModuleFactory.hooks.parser
|
---|
| 242 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 243 | .tap(PLUGIN_NAME, handler);
|
---|
| 244 | normalModuleFactory.hooks.parser
|
---|
| 245 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
| 246 | .tap(PLUGIN_NAME, handler);
|
---|
| 247 | }
|
---|
| 248 | );
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | class HarmonyModuleDecoratorRuntimeModule extends RuntimeModule {
|
---|
| 253 | constructor() {
|
---|
| 254 | super("harmony module decorator");
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * @returns {string | null} runtime code
|
---|
| 259 | */
|
---|
| 260 | generate() {
|
---|
| 261 | const { runtimeTemplate } = /** @type {Compilation} */ (this.compilation);
|
---|
| 262 | return Template.asString([
|
---|
| 263 | `${
|
---|
| 264 | RuntimeGlobals.harmonyModuleDecorator
|
---|
| 265 | } = ${runtimeTemplate.basicFunction("module", [
|
---|
| 266 | "module = Object.create(module);",
|
---|
| 267 | "if (!module.children) module.children = [];",
|
---|
| 268 | "Object.defineProperty(module, 'exports', {",
|
---|
| 269 | Template.indent([
|
---|
| 270 | "enumerable: true,",
|
---|
| 271 | `set: ${runtimeTemplate.basicFunction("", [
|
---|
| 272 | "throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);"
|
---|
| 273 | ])}`
|
---|
| 274 | ]),
|
---|
| 275 | "});",
|
---|
| 276 | "return module;"
|
---|
| 277 | ])};`
|
---|
| 278 | ]);
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | class NodeModuleDecoratorRuntimeModule extends RuntimeModule {
|
---|
| 283 | constructor() {
|
---|
| 284 | super("node module decorator");
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
| 288 | * @returns {string | null} runtime code
|
---|
| 289 | */
|
---|
| 290 | generate() {
|
---|
| 291 | const { runtimeTemplate } = /** @type {Compilation} */ (this.compilation);
|
---|
| 292 | return Template.asString([
|
---|
| 293 | `${RuntimeGlobals.nodeModuleDecorator} = ${runtimeTemplate.basicFunction(
|
---|
| 294 | "module",
|
---|
| 295 | [
|
---|
| 296 | "module.paths = [];",
|
---|
| 297 | "if (!module.children) module.children = [];",
|
---|
| 298 | "return module;"
|
---|
| 299 | ]
|
---|
| 300 | )};`
|
---|
| 301 | ]);
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | module.exports = CommonJsPlugin;
|
---|