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 { ConcatSource, OriginalSource } = require("webpack-sources");
|
---|
9 | const ExternalModule = require("../ExternalModule");
|
---|
10 | const Template = require("../Template");
|
---|
11 | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
---|
12 |
|
---|
13 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
14 | /** @typedef {import("../../declarations/WebpackOptions").LibraryCustomUmdCommentObject} LibraryCustomUmdCommentObject */
|
---|
15 | /** @typedef {import("../../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
|
---|
16 | /** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
|
---|
17 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
18 | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
|
---|
19 | /** @typedef {import("../Compiler")} Compiler */
|
---|
20 | /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
|
---|
21 | /** @typedef {import("../util/Hash")} Hash */
|
---|
22 | /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @param {string[]} accessor the accessor to convert to path
|
---|
26 | * @returns {string} the path
|
---|
27 | */
|
---|
28 | const accessorToObjectAccess = accessor => {
|
---|
29 | return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
|
---|
30 | };
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @param {string|undefined} base the path prefix
|
---|
34 | * @param {string|string[]} accessor the accessor
|
---|
35 | * @param {string=} joinWith the element separator
|
---|
36 | * @returns {string} the path
|
---|
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 | /** @typedef {string | string[] | LibraryCustomUmdObject} UmdLibraryPluginName */
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * @typedef {Object} UmdLibraryPluginOptions
|
---|
57 | * @property {LibraryType} type
|
---|
58 | * @property {boolean=} optionalAmdExternalAsGlobal
|
---|
59 | */
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * @typedef {Object} UmdLibraryPluginParsed
|
---|
63 | * @property {string | string[]} name
|
---|
64 | * @property {LibraryCustomUmdObject} names
|
---|
65 | * @property {string | LibraryCustomUmdCommentObject} auxiliaryComment
|
---|
66 | * @property {boolean} namedDefine
|
---|
67 | */
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @typedef {UmdLibraryPluginParsed} T
|
---|
71 | * @extends {AbstractLibraryPlugin<UmdLibraryPluginParsed>}
|
---|
72 | */
|
---|
73 | class UmdLibraryPlugin extends AbstractLibraryPlugin {
|
---|
74 | /**
|
---|
75 | * @param {UmdLibraryPluginOptions} options the plugin option
|
---|
76 | */
|
---|
77 | constructor(options) {
|
---|
78 | super({
|
---|
79 | pluginName: "UmdLibraryPlugin",
|
---|
80 | type: options.type
|
---|
81 | });
|
---|
82 |
|
---|
83 | this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @param {LibraryOptions} library normalized library option
|
---|
88 | * @returns {T | false} preprocess as needed by overriding
|
---|
89 | */
|
---|
90 | parseOptions(library) {
|
---|
91 | /** @type {LibraryName} */
|
---|
92 | let name;
|
---|
93 | /** @type {LibraryCustomUmdObject} */
|
---|
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 | * @param {Source} source source
|
---|
117 | * @param {RenderContext} renderContext render context
|
---|
118 | * @param {LibraryContext<T>} libraryContext context
|
---|
119 | * @returns {Source} source with library export
|
---|
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 = /** @type {ExternalModule[]} */ (modules);
|
---|
134 | /** @type {ExternalModule[]} */
|
---|
135 | const optionalExternals = [];
|
---|
136 | /** @type {ExternalModule[]} */
|
---|
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;
|
---|