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