UNPKG

4.37 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const { OriginalSource, RawSource } = require("webpack-sources");
8const Module = require("./Module");
9const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
10const Template = require("./Template");
11
12/** @typedef {import("./util/createHash").Hash} Hash */
13
14class ExternalModule extends Module {
15 constructor(request, type, userRequest) {
16 super("javascript/dynamic", null);
17
18 // Info from Factory
19 this.request = request;
20 this.externalType = type;
21 this.userRequest = userRequest;
22 this.external = true;
23 }
24
25 libIdent() {
26 return this.userRequest;
27 }
28
29 chunkCondition(chunk) {
30 return chunk.hasEntryModule();
31 }
32
33 identifier() {
34 return "external " + JSON.stringify(this.request);
35 }
36
37 readableIdentifier() {
38 return "external " + JSON.stringify(this.request);
39 }
40
41 needRebuild() {
42 return false;
43 }
44
45 build(options, compilation, resolver, fs, callback) {
46 this.built = true;
47 this.buildMeta = {};
48 this.buildInfo = {};
49 callback();
50 }
51
52 getSourceForGlobalVariableExternal(variableName, type) {
53 if (!Array.isArray(variableName)) {
54 // make it an array as the look up works the same basically
55 variableName = [variableName];
56 }
57
58 // needed for e.g. window["some"]["thing"]
59 const objectLookup = variableName
60 .map(r => `[${JSON.stringify(r)}]`)
61 .join("");
62 return `(function() { module.exports = ${type}${objectLookup}; }());`;
63 }
64
65 getSourceForCommonJsExternal(moduleAndSpecifiers) {
66 if (!Array.isArray(moduleAndSpecifiers)) {
67 return `module.exports = require(${JSON.stringify(
68 moduleAndSpecifiers
69 )});`;
70 }
71
72 const moduleName = moduleAndSpecifiers[0];
73 const objectLookup = moduleAndSpecifiers
74 .slice(1)
75 .map(r => `[${JSON.stringify(r)}]`)
76 .join("");
77 return `module.exports = require(${JSON.stringify(
78 moduleName
79 )})${objectLookup};`;
80 }
81
82 checkExternalVariable(variableToCheck, request) {
83 return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(
84 request
85 )}}\n`;
86 }
87
88 getSourceForAmdOrUmdExternal(id, optional, request) {
89 const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
90 `${id}`
91 )}__`;
92 const missingModuleError = optional
93 ? this.checkExternalVariable(externalVariable, request)
94 : "";
95 return `${missingModuleError}module.exports = ${externalVariable};`;
96 }
97
98 getSourceForDefaultCase(optional, request) {
99 if (!Array.isArray(request)) {
100 // make it an array as the look up works the same basically
101 request = [request];
102 }
103
104 const variableName = request[0];
105 const missingModuleError = optional
106 ? this.checkExternalVariable(variableName, request.join("."))
107 : "";
108 const objectLookup = request
109 .slice(1)
110 .map(r => `[${JSON.stringify(r)}]`)
111 .join("");
112 return `${missingModuleError}module.exports = ${variableName}${objectLookup};`;
113 }
114
115 getSourceString(runtime) {
116 const request =
117 typeof this.request === "object" && !Array.isArray(this.request)
118 ? this.request[this.externalType]
119 : this.request;
120 switch (this.externalType) {
121 case "this":
122 case "window":
123 case "self":
124 return this.getSourceForGlobalVariableExternal(
125 request,
126 this.externalType
127 );
128 case "global":
129 return this.getSourceForGlobalVariableExternal(
130 request,
131 runtime.outputOptions.globalObject
132 );
133 case "commonjs":
134 case "commonjs2":
135 return this.getSourceForCommonJsExternal(request);
136 case "amd":
137 case "amd-require":
138 case "umd":
139 case "umd2":
140 return this.getSourceForAmdOrUmdExternal(
141 this.id,
142 this.optional,
143 request
144 );
145 default:
146 return this.getSourceForDefaultCase(this.optional, request);
147 }
148 }
149
150 getSource(sourceString) {
151 if (this.useSourceMap) {
152 return new OriginalSource(sourceString, this.identifier());
153 }
154
155 return new RawSource(sourceString);
156 }
157
158 source(dependencyTemplates, runtime) {
159 return this.getSource(this.getSourceString(runtime));
160 }
161
162 size() {
163 return 42;
164 }
165
166 /**
167 * @param {Hash} hash the hash used to track dependencies
168 * @returns {void}
169 */
170 updateHash(hash) {
171 hash.update(this.externalType);
172 hash.update(JSON.stringify(this.request));
173 hash.update(JSON.stringify(Boolean(this.optional)));
174 super.updateHash(hash);
175 }
176}
177
178module.exports = ExternalModule;