1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { OriginalSource, RawSource } = require("webpack-sources");
|
9 | const ConcatenationScope = require("./ConcatenationScope");
|
10 | const { UsageState } = require("./ExportsInfo");
|
11 | const InitFragment = require("./InitFragment");
|
12 | const Module = require("./Module");
|
13 | const RuntimeGlobals = require("./RuntimeGlobals");
|
14 | const Template = require("./Template");
|
15 | const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
|
16 | const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
|
17 | const makeSerializable = require("./util/makeSerializable");
|
18 | const propertyAccess = require("./util/propertyAccess");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | const TYPES = new Set(["javascript"]);
|
52 | const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
|
53 | const RUNTIME_REQUIREMENTS_FOR_SCRIPT = new Set([RuntimeGlobals.loadScript]);
|
54 | const RUNTIME_REQUIREMENTS_FOR_MODULE = new Set([
|
55 | RuntimeGlobals.definePropertyGetters
|
56 | ]);
|
57 | const EMPTY_RUNTIME_REQUIREMENTS = new Set([]);
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | const getSourceForGlobalVariableExternal = (variableName, type) => {
|
65 | if (!Array.isArray(variableName)) {
|
66 |
|
67 | variableName = [variableName];
|
68 | }
|
69 |
|
70 |
|
71 | const objectLookup = variableName.map(r => `[${JSON.stringify(r)}]`).join("");
|
72 | return {
|
73 | iife: type === "this",
|
74 | expression: `${type}${objectLookup}`
|
75 | };
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | const getSourceForCommonJsExternal = moduleAndSpecifiers => {
|
83 | if (!Array.isArray(moduleAndSpecifiers)) {
|
84 | return {
|
85 | expression: `require(${JSON.stringify(moduleAndSpecifiers)})`
|
86 | };
|
87 | }
|
88 | const moduleName = moduleAndSpecifiers[0];
|
89 | return {
|
90 | expression: `require(${JSON.stringify(moduleName)})${propertyAccess(
|
91 | moduleAndSpecifiers,
|
92 | 1
|
93 | )}`
|
94 | };
|
95 | };
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | const getSourceForCommonJsExternalInNodeModule = moduleAndSpecifiers => {
|
102 | const chunkInitFragments = [
|
103 | new InitFragment(
|
104 | 'import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";\n',
|
105 | InitFragment.STAGE_HARMONY_IMPORTS,
|
106 | 0,
|
107 | "external module node-commonjs"
|
108 | )
|
109 | ];
|
110 | if (!Array.isArray(moduleAndSpecifiers)) {
|
111 | return {
|
112 | expression: `__WEBPACK_EXTERNAL_createRequire(import.meta.url)(${JSON.stringify(
|
113 | moduleAndSpecifiers
|
114 | )})`,
|
115 | chunkInitFragments
|
116 | };
|
117 | }
|
118 | const moduleName = moduleAndSpecifiers[0];
|
119 | return {
|
120 | expression: `__WEBPACK_EXTERNAL_createRequire(import.meta.url)(${JSON.stringify(
|
121 | moduleName
|
122 | )})${propertyAccess(moduleAndSpecifiers, 1)}`,
|
123 | chunkInitFragments
|
124 | };
|
125 | };
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | const getSourceForImportExternal = (moduleAndSpecifiers, runtimeTemplate) => {
|
133 | const importName = runtimeTemplate.outputOptions.importFunctionName;
|
134 | if (!runtimeTemplate.supportsDynamicImport() && importName === "import") {
|
135 | throw new Error(
|
136 | "The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
|
137 | );
|
138 | }
|
139 | if (!Array.isArray(moduleAndSpecifiers)) {
|
140 | return {
|
141 | expression: `${importName}(${JSON.stringify(moduleAndSpecifiers)});`
|
142 | };
|
143 | }
|
144 | if (moduleAndSpecifiers.length === 1) {
|
145 | return {
|
146 | expression: `${importName}(${JSON.stringify(moduleAndSpecifiers[0])});`
|
147 | };
|
148 | }
|
149 | const moduleName = moduleAndSpecifiers[0];
|
150 | return {
|
151 | expression: `${importName}(${JSON.stringify(
|
152 | moduleName
|
153 | )}).then(${runtimeTemplate.returningFunction(
|
154 | `module${propertyAccess(moduleAndSpecifiers, 1)}`,
|
155 | "module"
|
156 | )});`
|
157 | };
|
158 | };
|
159 |
|
160 | class ModuleExternalInitFragment extends InitFragment {
|
161 | constructor(id, request) {
|
162 | const identifier = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
|
163 | `${id}`
|
164 | )}__`;
|
165 | super(
|
166 | `import * as ${identifier} from ${JSON.stringify(request)};\n`,
|
167 | InitFragment.STAGE_HARMONY_IMPORTS,
|
168 | 0,
|
169 | `external module import ${id}`
|
170 | );
|
171 | this._identifier = identifier;
|
172 | }
|
173 |
|
174 | getNamespaceIdentifier() {
|
175 | return this._identifier;
|
176 | }
|
177 | }
|
178 |
|
179 | const generateModuleRemapping = (input, exportsInfo, runtime) => {
|
180 | if (exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused) {
|
181 | const properties = [];
|
182 | for (const exportInfo of exportsInfo.orderedExports) {
|
183 | const used = exportInfo.getUsedName(exportInfo.name, runtime);
|
184 | if (!used) continue;
|
185 | const nestedInfo = exportInfo.getNestedExportsInfo();
|
186 | if (nestedInfo) {
|
187 | const nestedExpr = generateModuleRemapping(
|
188 | `${input}${propertyAccess([exportInfo.name])}`,
|
189 | nestedInfo
|
190 | );
|
191 | if (nestedExpr) {
|
192 | properties.push(`[${JSON.stringify(used)}]: y(${nestedExpr})`);
|
193 | continue;
|
194 | }
|
195 | }
|
196 | properties.push(
|
197 | `[${JSON.stringify(used)}]: () => ${input}${propertyAccess([
|
198 | exportInfo.name
|
199 | ])}`
|
200 | );
|
201 | }
|
202 | return `x({ ${properties.join(", ")} })`;
|
203 | }
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | const getSourceForModuleExternal = (
|
214 | id,
|
215 | moduleAndSpecifiers,
|
216 | exportsInfo,
|
217 | runtime
|
218 | ) => {
|
219 | if (!Array.isArray(moduleAndSpecifiers))
|
220 | moduleAndSpecifiers = [moduleAndSpecifiers];
|
221 | const initFragment = new ModuleExternalInitFragment(
|
222 | id,
|
223 | moduleAndSpecifiers[0]
|
224 | );
|
225 | const baseAccess = `${initFragment.getNamespaceIdentifier()}${propertyAccess(
|
226 | moduleAndSpecifiers,
|
227 | 1
|
228 | )}`;
|
229 | const moduleRemapping = generateModuleRemapping(
|
230 | baseAccess,
|
231 | exportsInfo,
|
232 | runtime
|
233 | );
|
234 | let expression = moduleRemapping || baseAccess;
|
235 | return {
|
236 | expression,
|
237 | init: `var x = y => { var x = {}; ${RuntimeGlobals.definePropertyGetters}(x, y); return x; }\nvar y = x => () => x`,
|
238 | runtimeRequirements: moduleRemapping
|
239 | ? RUNTIME_REQUIREMENTS_FOR_MODULE
|
240 | : undefined,
|
241 | chunkInitFragments: [initFragment]
|
242 | };
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | const getSourceForScriptExternal = (urlAndGlobal, runtimeTemplate) => {
|
251 | if (typeof urlAndGlobal === "string") {
|
252 | urlAndGlobal = extractUrlAndGlobal(urlAndGlobal);
|
253 | }
|
254 | const url = urlAndGlobal[0];
|
255 | const globalName = urlAndGlobal[1];
|
256 | return {
|
257 | init: "var __webpack_error__ = new Error();",
|
258 | expression: `new Promise(${runtimeTemplate.basicFunction(
|
259 | "resolve, reject",
|
260 | [
|
261 | `if(typeof ${globalName} !== "undefined") return resolve();`,
|
262 | `${RuntimeGlobals.loadScript}(${JSON.stringify(
|
263 | url
|
264 | )}, ${runtimeTemplate.basicFunction("event", [
|
265 | `if(typeof ${globalName} !== "undefined") return resolve();`,
|
266 | "var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
|
267 | "var realSrc = event && event.target && event.target.src;",
|
268 | "__webpack_error__.message = 'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';",
|
269 | "__webpack_error__.name = 'ScriptExternalLoadError';",
|
270 | "__webpack_error__.type = errorType;",
|
271 | "__webpack_error__.request = realSrc;",
|
272 | "reject(__webpack_error__);"
|
273 | ])}, ${JSON.stringify(globalName)});`
|
274 | ]
|
275 | )}).then(${runtimeTemplate.returningFunction(
|
276 | `${globalName}${propertyAccess(urlAndGlobal, 2)}`
|
277 | )})`,
|
278 | runtimeRequirements: RUNTIME_REQUIREMENTS_FOR_SCRIPT
|
279 | };
|
280 | };
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | const checkExternalVariable = (variableName, request, runtimeTemplate) => {
|
289 | return `if(typeof ${variableName} === 'undefined') { ${runtimeTemplate.throwMissingModuleErrorBlock(
|
290 | { request }
|
291 | )} }\n`;
|
292 | };
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | const getSourceForAmdOrUmdExternal = (
|
302 | id,
|
303 | optional,
|
304 | request,
|
305 | runtimeTemplate
|
306 | ) => {
|
307 | const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
|
308 | `${id}`
|
309 | )}__`;
|
310 | return {
|
311 | init: optional
|
312 | ? checkExternalVariable(
|
313 | externalVariable,
|
314 | Array.isArray(request) ? request.join(".") : request,
|
315 | runtimeTemplate
|
316 | )
|
317 | : undefined,
|
318 | expression: externalVariable
|
319 | };
|
320 | };
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
|
329 | if (!Array.isArray(request)) {
|
330 |
|
331 | request = [request];
|
332 | }
|
333 |
|
334 | const variableName = request[0];
|
335 | const objectLookup = propertyAccess(request, 1);
|
336 | return {
|
337 | init: optional
|
338 | ? checkExternalVariable(variableName, request.join("."), runtimeTemplate)
|
339 | : undefined,
|
340 | expression: `${variableName}${objectLookup}`
|
341 | };
|
342 | };
|
343 |
|
344 | class ExternalModule extends Module {
|
345 | constructor(request, type, userRequest) {
|
346 | super("javascript/dynamic", null);
|
347 |
|
348 |
|
349 |
|
350 | this.request = request;
|
351 |
|
352 | this.externalType = type;
|
353 |
|
354 | this.userRequest = userRequest;
|
355 | }
|
356 |
|
357 | |
358 |
|
359 |
|
360 | getSourceTypes() {
|
361 | return TYPES;
|
362 | }
|
363 |
|
364 | |
365 |
|
366 |
|
367 |
|
368 | libIdent(options) {
|
369 | return this.userRequest;
|
370 | }
|
371 |
|
372 | |
373 |
|
374 |
|
375 |
|
376 |
|
377 | chunkCondition(chunk, { chunkGraph }) {
|
378 | return chunkGraph.getNumberOfEntryModules(chunk) > 0;
|
379 | }
|
380 |
|
381 | |
382 |
|
383 |
|
384 | identifier() {
|
385 | return "external " + JSON.stringify(this.request);
|
386 | }
|
387 |
|
388 | |
389 |
|
390 |
|
391 |
|
392 | readableIdentifier(requestShortener) {
|
393 | return "external " + JSON.stringify(this.request);
|
394 | }
|
395 |
|
396 | |
397 |
|
398 |
|
399 |
|
400 |
|
401 | needBuild(context, callback) {
|
402 | return callback(null, !this.buildMeta);
|
403 | }
|
404 |
|
405 | |
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | build(options, compilation, resolver, fs, callback) {
|
414 | this.buildMeta = {
|
415 | async: false,
|
416 | exportsType: undefined
|
417 | };
|
418 | this.buildInfo = {
|
419 | strict: true,
|
420 | topLevelDeclarations: new Set(),
|
421 | module: compilation.outputOptions.module
|
422 | };
|
423 | const { request, externalType } = this._getRequestAndExternalType();
|
424 | this.buildMeta.exportsType = "dynamic";
|
425 | let canMangle = false;
|
426 | this.clearDependenciesAndBlocks();
|
427 | switch (externalType) {
|
428 | case "this":
|
429 | this.buildInfo.strict = false;
|
430 | break;
|
431 | case "system":
|
432 | case "module":
|
433 | if (this.buildInfo.module) {
|
434 | if (!Array.isArray(request) || request.length === 1) {
|
435 | this.buildMeta.exportsType = "namespace";
|
436 | canMangle = true;
|
437 | }
|
438 | } else {
|
439 | this.buildMeta.async = true;
|
440 | if (!Array.isArray(request) || request.length === 1) {
|
441 | this.buildMeta.exportsType = "namespace";
|
442 | canMangle = false;
|
443 | }
|
444 | }
|
445 | break;
|
446 | case "script":
|
447 | case "promise":
|
448 | this.buildMeta.async = true;
|
449 | break;
|
450 | case "import":
|
451 | this.buildMeta.async = true;
|
452 | if (!Array.isArray(request) || request.length === 1) {
|
453 | this.buildMeta.exportsType = "namespace";
|
454 | canMangle = false;
|
455 | }
|
456 | break;
|
457 | }
|
458 | this.addDependency(new StaticExportsDependency(true, canMangle));
|
459 | callback();
|
460 | }
|
461 |
|
462 | |
463 |
|
464 |
|
465 |
|
466 | getConcatenationBailoutReason({ moduleGraph }) {
|
467 | switch (this.externalType) {
|
468 | case "amd":
|
469 | case "amd-require":
|
470 | case "umd":
|
471 | case "umd2":
|
472 | case "system":
|
473 | case "jsonp":
|
474 | return `${this.externalType} externals can't be concatenated`;
|
475 | }
|
476 | return undefined;
|
477 | }
|
478 |
|
479 | _getRequestAndExternalType() {
|
480 | let { request, externalType } = this;
|
481 | if (typeof request === "object" && !Array.isArray(request))
|
482 | request = request[externalType];
|
483 | return { request, externalType };
|
484 | }
|
485 |
|
486 | _getSourceData(runtimeTemplate, moduleGraph, chunkGraph, runtime) {
|
487 | const { request, externalType } = this._getRequestAndExternalType();
|
488 | switch (externalType) {
|
489 | case "this":
|
490 | case "window":
|
491 | case "self":
|
492 | return getSourceForGlobalVariableExternal(request, this.externalType);
|
493 | case "global":
|
494 | return getSourceForGlobalVariableExternal(
|
495 | request,
|
496 | runtimeTemplate.outputOptions.globalObject
|
497 | );
|
498 | case "commonjs":
|
499 | case "commonjs2":
|
500 | case "commonjs-module":
|
501 | return getSourceForCommonJsExternal(request);
|
502 | case "node-commonjs":
|
503 | return this.buildInfo.module
|
504 | ? getSourceForCommonJsExternalInNodeModule(request)
|
505 | : getSourceForCommonJsExternal(request);
|
506 | case "amd":
|
507 | case "amd-require":
|
508 | case "umd":
|
509 | case "umd2":
|
510 | case "system":
|
511 | case "jsonp":
|
512 | return getSourceForAmdOrUmdExternal(
|
513 | chunkGraph.getModuleId(this),
|
514 | this.isOptional(moduleGraph),
|
515 | request,
|
516 | runtimeTemplate
|
517 | );
|
518 | case "import":
|
519 | return getSourceForImportExternal(request, runtimeTemplate);
|
520 | case "script":
|
521 | return getSourceForScriptExternal(request, runtimeTemplate);
|
522 | case "module":
|
523 | if (!this.buildInfo.module) {
|
524 | if (!runtimeTemplate.supportsDynamicImport()) {
|
525 | throw new Error(
|
526 | "The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script" +
|
527 | (runtimeTemplate.supportsEcmaScriptModuleSyntax()
|
528 | ? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
|
529 | : "")
|
530 | );
|
531 | }
|
532 | return getSourceForImportExternal(request, runtimeTemplate);
|
533 | }
|
534 | if (!runtimeTemplate.supportsEcmaScriptModuleSyntax()) {
|
535 | throw new Error(
|
536 | "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
|
537 | );
|
538 | }
|
539 | return getSourceForModuleExternal(
|
540 | chunkGraph.getModuleId(this),
|
541 | request,
|
542 | moduleGraph.getExportsInfo(this),
|
543 | runtime
|
544 | );
|
545 | case "var":
|
546 | case "promise":
|
547 | case "const":
|
548 | case "let":
|
549 | case "assign":
|
550 | default:
|
551 | return getSourceForDefaultCase(
|
552 | this.isOptional(moduleGraph),
|
553 | request,
|
554 | runtimeTemplate
|
555 | );
|
556 | }
|
557 | }
|
558 |
|
559 | |
560 |
|
561 |
|
562 |
|
563 | codeGeneration({
|
564 | runtimeTemplate,
|
565 | moduleGraph,
|
566 | chunkGraph,
|
567 | runtime,
|
568 | concatenationScope
|
569 | }) {
|
570 | const sourceData = this._getSourceData(
|
571 | runtimeTemplate,
|
572 | moduleGraph,
|
573 | chunkGraph,
|
574 | runtime
|
575 | );
|
576 |
|
577 | let sourceString = sourceData.expression;
|
578 | if (sourceData.iife)
|
579 | sourceString = `(function() { return ${sourceString}; }())`;
|
580 | if (concatenationScope) {
|
581 | sourceString = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
582 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
583 | } = ${sourceString};`;
|
584 | concatenationScope.registerNamespaceExport(
|
585 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
586 | );
|
587 | } else {
|
588 | sourceString = `module.exports = ${sourceString};`;
|
589 | }
|
590 | if (sourceData.init) sourceString = `${sourceData.init}\n${sourceString}`;
|
591 |
|
592 | let data = undefined;
|
593 | if (sourceData.chunkInitFragments) {
|
594 | data = new Map();
|
595 | data.set("chunkInitFragments", sourceData.chunkInitFragments);
|
596 | }
|
597 |
|
598 | const sources = new Map();
|
599 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
600 | sources.set(
|
601 | "javascript",
|
602 | new OriginalSource(sourceString, this.identifier())
|
603 | );
|
604 | } else {
|
605 | sources.set("javascript", new RawSource(sourceString));
|
606 | }
|
607 |
|
608 | let runtimeRequirements = sourceData.runtimeRequirements;
|
609 | if (!concatenationScope) {
|
610 | if (!runtimeRequirements) {
|
611 | runtimeRequirements = RUNTIME_REQUIREMENTS;
|
612 | } else {
|
613 | const set = new Set(runtimeRequirements);
|
614 | set.add(RuntimeGlobals.module);
|
615 | runtimeRequirements = set;
|
616 | }
|
617 | }
|
618 |
|
619 | return {
|
620 | sources,
|
621 | runtimeRequirements: runtimeRequirements || EMPTY_RUNTIME_REQUIREMENTS,
|
622 | data
|
623 | };
|
624 | }
|
625 |
|
626 | |
627 |
|
628 |
|
629 |
|
630 | size(type) {
|
631 | return 42;
|
632 | }
|
633 |
|
634 | |
635 |
|
636 |
|
637 |
|
638 |
|
639 | updateHash(hash, context) {
|
640 | const { chunkGraph } = context;
|
641 | hash.update(this.externalType);
|
642 | hash.update(JSON.stringify(this.request));
|
643 | hash.update(
|
644 | JSON.stringify(Boolean(this.isOptional(chunkGraph.moduleGraph)))
|
645 | );
|
646 | super.updateHash(hash, context);
|
647 | }
|
648 |
|
649 | serialize(context) {
|
650 | const { write } = context;
|
651 |
|
652 | write(this.request);
|
653 | write(this.externalType);
|
654 | write(this.userRequest);
|
655 |
|
656 | super.serialize(context);
|
657 | }
|
658 |
|
659 | deserialize(context) {
|
660 | const { read } = context;
|
661 |
|
662 | this.request = read();
|
663 | this.externalType = read();
|
664 | this.userRequest = read();
|
665 |
|
666 | super.deserialize(context);
|
667 | }
|
668 | }
|
669 |
|
670 | makeSerializable(ExternalModule, "webpack/lib/ExternalModule");
|
671 |
|
672 | module.exports = ExternalModule;
|