Skip to content

Commit 375e7d2

Browse files
committed
refactor source method of ExternalModule
1 parent 8c435f5 commit 375e7d2

1 file changed

Lines changed: 55 additions & 33 deletions

File tree

‎lib/ExternalModule.js‎

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,74 @@ class ExternalModule extends Module {
3737
callback();
3838
}
3939

40-
source() {
41-
let str = "throw new Error('Externals not supported');";
42-
let request = this.request;
43-
if(typeof request === "object") request = request[this.type];
40+
getSourceForGlobalVariableExternal(request, type) {
41+
if(!Array.isArray(request)) {
42+
return `(function() { module.exports = ${type}[${JSON.stringify(request)}]; }());`;
43+
}
44+
45+
// needed for e.g. window["some"]["thing"]
46+
const objectLookup = request.map(r => `[${JSON.stringify(r)}]`).join("");
47+
return `(function() { module.exports = ${type}${objectLookup}; }());`;
48+
}
49+
50+
getSourceForCommonJsExternal(request) {
51+
if(!Array.isArray(request)) {
52+
return `module.exports = require(${JSON.stringify(request)});`;
53+
}
54+
55+
const moduleName = request[0];
56+
const objectLookup = request.slice(1).map(r => `[${JSON.stringify(r)}]`).join("");
57+
return `module.exports = require(${moduleName})${objectLookup};`;
58+
}
59+
60+
checkExternalVariable(variableToCheck, request) {
61+
return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(request)}}`;
62+
}
63+
64+
getSourceForAmdOrUmdExternal(request, id, optional) {
65+
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${id}__`;
66+
const missingModuleError = optional ? this.checkExternalVariable(externalVariable, request) : "";
67+
return `${missingModuleError}
68+
module.exports = ${externalVariable};`;
69+
}
70+
71+
getSourceForDefaultCase(request, optional) {
72+
const missingModuleError = optional ? this.checkExternalVariable(request, request) : "";
73+
return `${missingModuleError}
74+
module.exports = ${request};`;
75+
}
76+
77+
getSourceString() {
78+
const request = typeof this.request === "object" ? this.request[this.type] : this.request;
4479
switch(this.type) {
4580
case "this":
4681
case "window":
4782
case "global":
48-
if(Array.isArray(request)) {
49-
str = "(function() { module.exports = " + this.type + request.map(function(r) {
50-
return "[" + JSON.stringify(r) + "]";
51-
}).join("") + "; }());";
52-
} else
53-
str = "(function() { module.exports = " + this.type + "[" + JSON.stringify(request) + "]; }());";
54-
break;
83+
return this.getSourceForGlobalVariableExternal(request, this.type);
5584
case "commonjs":
5685
case "commonjs2":
57-
if(Array.isArray(request)) {
58-
str = "module.exports = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) {
59-
return "[" + JSON.stringify(r) + "]";
60-
}).join("") + ";";
61-
} else
62-
str = "module.exports = require(" + JSON.stringify(request) + ");";
63-
break;
86+
return this.getSourceForCommonJsExternal(request);
6487
case "amd":
6588
case "umd":
6689
case "umd2":
67-
str = "";
68-
if(this.optional) {
69-
str += "if(typeof __WEBPACK_EXTERNAL_MODULE_" + this.id + "__ === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
70-
}
71-
str += "module.exports = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;";
72-
break;
90+
return this.getSourceForAmdOrUmdExternal(request, this.id, this.optional);
7391
default:
74-
str = "";
75-
if(this.optional) {
76-
str += "if(typeof " + request + " === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
77-
}
78-
str += "module.exports = " + request + ";";
79-
break;
92+
return this.getSourceForDefaultCase(request, this.optional);
8093
}
94+
}
95+
96+
getSource(sourceString) {
8197
if(this.useSourceMap) {
82-
return new OriginalSource(str, this.identifier());
83-
} else {
84-
return new RawSource(str);
98+
return new OriginalSource(sourceString, this.identifier());
8599
}
100+
101+
return new RawSource(sourceString);
102+
}
103+
104+
source() {
105+
return this.getSource(
106+
this.getSourceString()
107+
);
86108
}
87109

88110
size() {

0 commit comments

Comments
 (0)