1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ConcatSource, OriginalSource } = require("webpack-sources");
|
9 | const ExternalModule = require("../ExternalModule");
|
10 | const Template = require("../Template");
|
11 | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const accessorToObjectAccess = accessor => {
|
29 | return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | const accessorAccess = (base, accessor, joinWith = ", ") => {
|
39 | const accessors = Array.isArray(accessor) ? accessor : [accessor];
|
40 | return accessors
|
41 | .map((_, idx) => {
|
42 | const a = base
|
43 | ? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
|
44 | : accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
|
45 | if (idx === accessors.length - 1) return a;
|
46 | if (idx === 0 && base === undefined)
|
47 | return `${a} = typeof ${a} === "object" ? ${a} : {}`;
|
48 | return `${a} = ${a} || {}`;
|
49 | })
|
50 | .join(joinWith);
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | class UmdLibraryPlugin extends AbstractLibraryPlugin {
|
74 | |
75 |
|
76 |
|
77 | constructor(options) {
|
78 | super({
|
79 | pluginName: "UmdLibraryPlugin",
|
80 | type: options.type
|
81 | });
|
82 |
|
83 | this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 | parseOptions(library) {
|
91 |
|
92 | let name;
|
93 |
|
94 | let names;
|
95 | if (typeof library.name === "object" && !Array.isArray(library.name)) {
|
96 | name = library.name.root || library.name.amd || library.name.commonjs;
|
97 | names = library.name;
|
98 | } else {
|
99 | name = library.name;
|
100 | const singleName = Array.isArray(name) ? name[0] : name;
|
101 | names = {
|
102 | commonjs: singleName,
|
103 | root: library.name,
|
104 | amd: singleName
|
105 | };
|
106 | }
|
107 | return {
|
108 | name,
|
109 | names,
|
110 | auxiliaryComment: library.auxiliaryComment,
|
111 | namedDefine: library.umdNamedDefine
|
112 | };
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | render(
|
122 | source,
|
123 | { chunkGraph, runtimeTemplate, chunk, moduleGraph },
|
124 | { options, compilation }
|
125 | ) {
|
126 | const modules = chunkGraph
|
127 | .getChunkModules(chunk)
|
128 | .filter(
|
129 | m =>
|
130 | m instanceof ExternalModule &&
|
131 | (m.externalType === "umd" || m.externalType === "umd2")
|
132 | );
|
133 | let externals = (modules);
|
134 |
|
135 | const optionalExternals = [];
|
136 |
|
137 | let requiredExternals = [];
|
138 | if (this.optionalAmdExternalAsGlobal) {
|
139 | for (const m of externals) {
|
140 | if (m.isOptional(moduleGraph)) {
|
141 | optionalExternals.push(m);
|
142 | } else {
|
143 | requiredExternals.push(m);
|
144 | }
|
145 | }
|
146 | externals = requiredExternals.concat(optionalExternals);
|
147 | } else {
|
148 | requiredExternals = externals;
|
149 | }
|
150 |
|
151 | const replaceKeys = str => {
|
152 | return compilation.getPath(str, {
|
153 | chunk
|
154 | });
|
155 | };
|
156 |
|
157 | const externalsDepsArray = modules => {
|
158 | return `[${replaceKeys(
|
159 | modules
|
160 | .map(m =>
|
161 | JSON.stringify(
|
162 | typeof m.request === "object" ? m.request.amd : m.request
|
163 | )
|
164 | )
|
165 | .join(", ")
|
166 | )}]`;
|
167 | };
|
168 |
|
169 | const externalsRootArray = modules => {
|
170 | return replaceKeys(
|
171 | modules
|
172 | .map(m => {
|
173 | let request = m.request;
|
174 | if (typeof request === "object") request = request.root;
|
175 | return `root${accessorToObjectAccess([].concat(request))}`;
|
176 | })
|
177 | .join(", ")
|
178 | );
|
179 | };
|
180 |
|
181 | const externalsRequireArray = type => {
|
182 | return replaceKeys(
|
183 | externals
|
184 | .map(m => {
|
185 | let expr;
|
186 | let request = m.request;
|
187 | if (typeof request === "object") {
|
188 | request = request[type];
|
189 | }
|
190 | if (request === undefined) {
|
191 | throw new Error(
|
192 | "Missing external configuration for type:" + type
|
193 | );
|
194 | }
|
195 | if (Array.isArray(request)) {
|
196 | expr = `require(${JSON.stringify(
|
197 | request[0]
|
198 | )})${accessorToObjectAccess(request.slice(1))}`;
|
199 | } else {
|
200 | expr = `require(${JSON.stringify(request)})`;
|
201 | }
|
202 | if (m.isOptional(moduleGraph)) {
|
203 | expr = `(function webpackLoadOptionalExternalModule() { try { return ${expr}; } catch(e) {} }())`;
|
204 | }
|
205 | return expr;
|
206 | })
|
207 | .join(", ")
|
208 | );
|
209 | };
|
210 |
|
211 | const externalsArguments = modules => {
|
212 | return modules
|
213 | .map(
|
214 | m =>
|
215 | `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
|
216 | `${chunkGraph.getModuleId(m)}`
|
217 | )}__`
|
218 | )
|
219 | .join(", ");
|
220 | };
|
221 |
|
222 | const libraryName = library => {
|
223 | return JSON.stringify(replaceKeys([].concat(library).pop()));
|
224 | };
|
225 |
|
226 | let amdFactory;
|
227 | if (optionalExternals.length > 0) {
|
228 | const wrapperArguments = externalsArguments(requiredExternals);
|
229 | const factoryArguments =
|
230 | requiredExternals.length > 0
|
231 | ? externalsArguments(requiredExternals) +
|
232 | ", " +
|
233 | externalsRootArray(optionalExternals)
|
234 | : externalsRootArray(optionalExternals);
|
235 | amdFactory =
|
236 | `function webpackLoadOptionalExternalModuleAmd(${wrapperArguments}) {\n` +
|
237 | ` return factory(${factoryArguments});\n` +
|
238 | " }";
|
239 | } else {
|
240 | amdFactory = "factory";
|
241 | }
|
242 |
|
243 | const { auxiliaryComment, namedDefine, names } = options;
|
244 |
|
245 | const getAuxiliaryComment = type => {
|
246 | if (auxiliaryComment) {
|
247 | if (typeof auxiliaryComment === "string")
|
248 | return "\t//" + auxiliaryComment + "\n";
|
249 | if (auxiliaryComment[type])
|
250 | return "\t//" + auxiliaryComment[type] + "\n";
|
251 | }
|
252 | return "";
|
253 | };
|
254 |
|
255 | return new ConcatSource(
|
256 | new OriginalSource(
|
257 | "(function webpackUniversalModuleDefinition(root, factory) {\n" +
|
258 | getAuxiliaryComment("commonjs2") +
|
259 | " if(typeof exports === 'object' && typeof module === 'object')\n" +
|
260 | " module.exports = factory(" +
|
261 | externalsRequireArray("commonjs2") +
|
262 | ");\n" +
|
263 | getAuxiliaryComment("amd") +
|
264 | " else if(typeof define === 'function' && define.amd)\n" +
|
265 | (requiredExternals.length > 0
|
266 | ? names.amd && namedDefine === true
|
267 | ? " define(" +
|
268 | libraryName(names.amd) +
|
269 | ", " +
|
270 | externalsDepsArray(requiredExternals) +
|
271 | ", " +
|
272 | amdFactory +
|
273 | ");\n"
|
274 | : " define(" +
|
275 | externalsDepsArray(requiredExternals) +
|
276 | ", " +
|
277 | amdFactory +
|
278 | ");\n"
|
279 | : names.amd && namedDefine === true
|
280 | ? " define(" +
|
281 | libraryName(names.amd) +
|
282 | ", [], " +
|
283 | amdFactory +
|
284 | ");\n"
|
285 | : " define([], " + amdFactory + ");\n") +
|
286 | (names.root || names.commonjs
|
287 | ? getAuxiliaryComment("commonjs") +
|
288 | " else if(typeof exports === 'object')\n" +
|
289 | " exports[" +
|
290 | libraryName(names.commonjs || names.root) +
|
291 | "] = factory(" +
|
292 | externalsRequireArray("commonjs") +
|
293 | ");\n" +
|
294 | getAuxiliaryComment("root") +
|
295 | " else\n" +
|
296 | " " +
|
297 | replaceKeys(
|
298 | accessorAccess("root", names.root || names.commonjs)
|
299 | ) +
|
300 | " = factory(" +
|
301 | externalsRootArray(externals) +
|
302 | ");\n"
|
303 | : " else {\n" +
|
304 | (externals.length > 0
|
305 | ? " var a = typeof exports === 'object' ? factory(" +
|
306 | externalsRequireArray("commonjs") +
|
307 | ") : factory(" +
|
308 | externalsRootArray(externals) +
|
309 | ");\n"
|
310 | : " var a = factory();\n") +
|
311 | " for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
|
312 | " }\n") +
|
313 | `})(${
|
314 | runtimeTemplate.outputOptions.globalObject
|
315 | }, function(${externalsArguments(externals)}) {\nreturn `,
|
316 | "webpack/universalModuleDefinition"
|
317 | ),
|
318 | source,
|
319 | ";\n})"
|
320 | );
|
321 | }
|
322 | }
|
323 |
|
324 | module.exports = UmdLibraryPlugin;
|