[6a3a178] | 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 {
|
---|
| 10 | approve,
|
---|
| 11 | evaluateToIdentifier,
|
---|
| 12 | evaluateToString,
|
---|
| 13 | toConstantDependency
|
---|
| 14 | } = require("../javascript/JavascriptParserHelpers");
|
---|
| 15 |
|
---|
| 16 | const AMDDefineDependency = require("./AMDDefineDependency");
|
---|
| 17 | const AMDDefineDependencyParserPlugin = require("./AMDDefineDependencyParserPlugin");
|
---|
| 18 | const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
|
---|
| 19 | const AMDRequireContextDependency = require("./AMDRequireContextDependency");
|
---|
| 20 | const AMDRequireDependenciesBlockParserPlugin = require("./AMDRequireDependenciesBlockParserPlugin");
|
---|
| 21 | const AMDRequireDependency = require("./AMDRequireDependency");
|
---|
| 22 | const AMDRequireItemDependency = require("./AMDRequireItemDependency");
|
---|
| 23 | const {
|
---|
| 24 | AMDDefineRuntimeModule,
|
---|
| 25 | AMDOptionsRuntimeModule
|
---|
| 26 | } = require("./AMDRuntimeModules");
|
---|
| 27 | const ConstDependency = require("./ConstDependency");
|
---|
| 28 | const LocalModuleDependency = require("./LocalModuleDependency");
|
---|
| 29 | const UnsupportedDependency = require("./UnsupportedDependency");
|
---|
| 30 |
|
---|
| 31 | /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
|
---|
| 32 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 33 |
|
---|
| 34 | class AMDPlugin {
|
---|
| 35 | /**
|
---|
| 36 | * @param {Record<string, any>} amdOptions the AMD options
|
---|
| 37 | */
|
---|
| 38 | constructor(amdOptions) {
|
---|
| 39 | this.amdOptions = amdOptions;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Apply the plugin
|
---|
| 44 | * @param {Compiler} compiler the compiler instance
|
---|
| 45 | * @returns {void}
|
---|
| 46 | */
|
---|
| 47 | apply(compiler) {
|
---|
| 48 | const amdOptions = this.amdOptions;
|
---|
| 49 | compiler.hooks.compilation.tap(
|
---|
| 50 | "AMDPlugin",
|
---|
| 51 | (compilation, { contextModuleFactory, normalModuleFactory }) => {
|
---|
| 52 | compilation.dependencyTemplates.set(
|
---|
| 53 | AMDRequireDependency,
|
---|
| 54 | new AMDRequireDependency.Template()
|
---|
| 55 | );
|
---|
| 56 |
|
---|
| 57 | compilation.dependencyFactories.set(
|
---|
| 58 | AMDRequireItemDependency,
|
---|
| 59 | normalModuleFactory
|
---|
| 60 | );
|
---|
| 61 | compilation.dependencyTemplates.set(
|
---|
| 62 | AMDRequireItemDependency,
|
---|
| 63 | new AMDRequireItemDependency.Template()
|
---|
| 64 | );
|
---|
| 65 |
|
---|
| 66 | compilation.dependencyTemplates.set(
|
---|
| 67 | AMDRequireArrayDependency,
|
---|
| 68 | new AMDRequireArrayDependency.Template()
|
---|
| 69 | );
|
---|
| 70 |
|
---|
| 71 | compilation.dependencyFactories.set(
|
---|
| 72 | AMDRequireContextDependency,
|
---|
| 73 | contextModuleFactory
|
---|
| 74 | );
|
---|
| 75 | compilation.dependencyTemplates.set(
|
---|
| 76 | AMDRequireContextDependency,
|
---|
| 77 | new AMDRequireContextDependency.Template()
|
---|
| 78 | );
|
---|
| 79 |
|
---|
| 80 | compilation.dependencyTemplates.set(
|
---|
| 81 | AMDDefineDependency,
|
---|
| 82 | new AMDDefineDependency.Template()
|
---|
| 83 | );
|
---|
| 84 |
|
---|
| 85 | compilation.dependencyTemplates.set(
|
---|
| 86 | UnsupportedDependency,
|
---|
| 87 | new UnsupportedDependency.Template()
|
---|
| 88 | );
|
---|
| 89 |
|
---|
| 90 | compilation.dependencyTemplates.set(
|
---|
| 91 | LocalModuleDependency,
|
---|
| 92 | new LocalModuleDependency.Template()
|
---|
| 93 | );
|
---|
| 94 |
|
---|
| 95 | compilation.hooks.runtimeRequirementInModule
|
---|
| 96 | .for(RuntimeGlobals.amdDefine)
|
---|
| 97 | .tap("AMDPlugin", (module, set) => {
|
---|
| 98 | set.add(RuntimeGlobals.require);
|
---|
| 99 | });
|
---|
| 100 |
|
---|
| 101 | compilation.hooks.runtimeRequirementInModule
|
---|
| 102 | .for(RuntimeGlobals.amdOptions)
|
---|
| 103 | .tap("AMDPlugin", (module, set) => {
|
---|
| 104 | set.add(RuntimeGlobals.requireScope);
|
---|
| 105 | });
|
---|
| 106 |
|
---|
| 107 | compilation.hooks.runtimeRequirementInTree
|
---|
| 108 | .for(RuntimeGlobals.amdDefine)
|
---|
| 109 | .tap("AMDPlugin", (chunk, set) => {
|
---|
| 110 | compilation.addRuntimeModule(chunk, new AMDDefineRuntimeModule());
|
---|
| 111 | });
|
---|
| 112 |
|
---|
| 113 | compilation.hooks.runtimeRequirementInTree
|
---|
| 114 | .for(RuntimeGlobals.amdOptions)
|
---|
| 115 | .tap("AMDPlugin", (chunk, set) => {
|
---|
| 116 | compilation.addRuntimeModule(
|
---|
| 117 | chunk,
|
---|
| 118 | new AMDOptionsRuntimeModule(amdOptions)
|
---|
| 119 | );
|
---|
| 120 | });
|
---|
| 121 |
|
---|
| 122 | const handler = (parser, parserOptions) => {
|
---|
| 123 | if (parserOptions.amd !== undefined && !parserOptions.amd) return;
|
---|
| 124 |
|
---|
| 125 | const tapOptionsHooks = (optionExpr, rootName, getMembers) => {
|
---|
| 126 | parser.hooks.expression
|
---|
| 127 | .for(optionExpr)
|
---|
| 128 | .tap(
|
---|
| 129 | "AMDPlugin",
|
---|
| 130 | toConstantDependency(parser, RuntimeGlobals.amdOptions, [
|
---|
| 131 | RuntimeGlobals.amdOptions
|
---|
| 132 | ])
|
---|
| 133 | );
|
---|
| 134 | parser.hooks.evaluateIdentifier
|
---|
| 135 | .for(optionExpr)
|
---|
| 136 | .tap(
|
---|
| 137 | "AMDPlugin",
|
---|
| 138 | evaluateToIdentifier(optionExpr, rootName, getMembers, true)
|
---|
| 139 | );
|
---|
| 140 | parser.hooks.evaluateTypeof
|
---|
| 141 | .for(optionExpr)
|
---|
| 142 | .tap("AMDPlugin", evaluateToString("object"));
|
---|
| 143 | parser.hooks.typeof
|
---|
| 144 | .for(optionExpr)
|
---|
| 145 | .tap(
|
---|
| 146 | "AMDPlugin",
|
---|
| 147 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
| 148 | );
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | new AMDRequireDependenciesBlockParserPlugin(parserOptions).apply(
|
---|
| 152 | parser
|
---|
| 153 | );
|
---|
| 154 | new AMDDefineDependencyParserPlugin(parserOptions).apply(parser);
|
---|
| 155 |
|
---|
| 156 | tapOptionsHooks("define.amd", "define", () => "amd");
|
---|
| 157 | tapOptionsHooks("require.amd", "require", () => ["amd"]);
|
---|
| 158 | tapOptionsHooks(
|
---|
| 159 | "__webpack_amd_options__",
|
---|
| 160 | "__webpack_amd_options__",
|
---|
| 161 | () => []
|
---|
| 162 | );
|
---|
| 163 |
|
---|
| 164 | parser.hooks.expression.for("define").tap("AMDPlugin", expr => {
|
---|
| 165 | const dep = new ConstDependency(
|
---|
| 166 | RuntimeGlobals.amdDefine,
|
---|
| 167 | expr.range,
|
---|
| 168 | [RuntimeGlobals.amdDefine]
|
---|
| 169 | );
|
---|
| 170 | dep.loc = expr.loc;
|
---|
| 171 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 172 | return true;
|
---|
| 173 | });
|
---|
| 174 | parser.hooks.typeof
|
---|
| 175 | .for("define")
|
---|
| 176 | .tap(
|
---|
| 177 | "AMDPlugin",
|
---|
| 178 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
| 179 | );
|
---|
| 180 | parser.hooks.evaluateTypeof
|
---|
| 181 | .for("define")
|
---|
| 182 | .tap("AMDPlugin", evaluateToString("function"));
|
---|
| 183 | parser.hooks.canRename.for("define").tap("AMDPlugin", approve);
|
---|
| 184 | parser.hooks.rename.for("define").tap("AMDPlugin", expr => {
|
---|
| 185 | const dep = new ConstDependency(
|
---|
| 186 | RuntimeGlobals.amdDefine,
|
---|
| 187 | expr.range,
|
---|
| 188 | [RuntimeGlobals.amdDefine]
|
---|
| 189 | );
|
---|
| 190 | dep.loc = expr.loc;
|
---|
| 191 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 192 | return false;
|
---|
| 193 | });
|
---|
| 194 | parser.hooks.typeof
|
---|
| 195 | .for("require")
|
---|
| 196 | .tap(
|
---|
| 197 | "AMDPlugin",
|
---|
| 198 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
| 199 | );
|
---|
| 200 | parser.hooks.evaluateTypeof
|
---|
| 201 | .for("require")
|
---|
| 202 | .tap("AMDPlugin", evaluateToString("function"));
|
---|
| 203 | };
|
---|
| 204 |
|
---|
| 205 | normalModuleFactory.hooks.parser
|
---|
| 206 | .for("javascript/auto")
|
---|
| 207 | .tap("AMDPlugin", handler);
|
---|
| 208 | normalModuleFactory.hooks.parser
|
---|
| 209 | .for("javascript/dynamic")
|
---|
| 210 | .tap("AMDPlugin", handler);
|
---|
| 211 | }
|
---|
| 212 | );
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | module.exports = AMDPlugin;
|
---|