1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const { OriginalSource, RawSource } = require("webpack-sources");
|
8
|
const Module = require("./Module");
|
9
|
const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
|
10
|
const Template = require("./Template");
|
11
|
|
12
|
/** @typedef {import("./util/createHash").Hash} Hash */
|
13
|
|
14
|
class 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
|
case "system":
|
141
|
return this.getSourceForAmdOrUmdExternal(
|
142
|
this.id,
|
143
|
this.optional,
|
144
|
request
|
145
|
);
|
146
|
default:
|
147
|
return this.getSourceForDefaultCase(this.optional, request);
|
148
|
}
|
149
|
}
|
150
|
|
151
|
getSource(sourceString) {
|
152
|
if (this.useSourceMap) {
|
153
|
return new OriginalSource(sourceString, this.identifier());
|
154
|
}
|
155
|
|
156
|
return new RawSource(sourceString);
|
157
|
}
|
158
|
|
159
|
source(dependencyTemplates, runtime) {
|
160
|
return this.getSource(this.getSourceString(runtime));
|
161
|
}
|
162
|
|
163
|
size() {
|
164
|
return 42;
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* @param {Hash} hash the hash used to track dependencies
|
169
|
* @returns {void}
|
170
|
*/
|
171
|
updateHash(hash) {
|
172
|
hash.update(this.externalType);
|
173
|
hash.update(JSON.stringify(this.request));
|
174
|
hash.update(JSON.stringify(Boolean(this.optional)));
|
175
|
super.updateHash(hash);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
module.exports = ExternalModule;
|