mirror of https://github.com/webpack/webpack.git
refactor source method of ExternalModule
This commit is contained in:
parent
8c435f58a6
commit
375e7d2399
|
|
@ -37,52 +37,74 @@ class ExternalModule extends Module {
|
|||
callback();
|
||||
}
|
||||
|
||||
source() {
|
||||
let str = "throw new Error('Externals not supported');";
|
||||
let request = this.request;
|
||||
if(typeof request === "object") request = request[this.type];
|
||||
getSourceForGlobalVariableExternal(request, type) {
|
||||
if(!Array.isArray(request)) {
|
||||
return `(function() { module.exports = ${type}[${JSON.stringify(request)}]; }());`;
|
||||
}
|
||||
|
||||
// needed for e.g. window["some"]["thing"]
|
||||
const objectLookup = request.map(r => `[${JSON.stringify(r)}]`).join("");
|
||||
return `(function() { module.exports = ${type}${objectLookup}; }());`;
|
||||
}
|
||||
|
||||
getSourceForCommonJsExternal(request) {
|
||||
if(!Array.isArray(request)) {
|
||||
return `module.exports = require(${JSON.stringify(request)});`;
|
||||
}
|
||||
|
||||
const moduleName = request[0];
|
||||
const objectLookup = request.slice(1).map(r => `[${JSON.stringify(r)}]`).join("");
|
||||
return `module.exports = require(${moduleName})${objectLookup};`;
|
||||
}
|
||||
|
||||
checkExternalVariable(variableToCheck, request) {
|
||||
return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(request)}}`;
|
||||
}
|
||||
|
||||
getSourceForAmdOrUmdExternal(request, id, optional) {
|
||||
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${id}__`;
|
||||
const missingModuleError = optional ? this.checkExternalVariable(externalVariable, request) : "";
|
||||
return `${missingModuleError}
|
||||
module.exports = ${externalVariable};`;
|
||||
}
|
||||
|
||||
getSourceForDefaultCase(request, optional) {
|
||||
const missingModuleError = optional ? this.checkExternalVariable(request, request) : "";
|
||||
return `${missingModuleError}
|
||||
module.exports = ${request};`;
|
||||
}
|
||||
|
||||
getSourceString() {
|
||||
const request = typeof this.request === "object" ? this.request[this.type] : this.request;
|
||||
switch(this.type) {
|
||||
case "this":
|
||||
case "window":
|
||||
case "global":
|
||||
if(Array.isArray(request)) {
|
||||
str = "(function() { module.exports = " + this.type + request.map(function(r) {
|
||||
return "[" + JSON.stringify(r) + "]";
|
||||
}).join("") + "; }());";
|
||||
} else
|
||||
str = "(function() { module.exports = " + this.type + "[" + JSON.stringify(request) + "]; }());";
|
||||
break;
|
||||
return this.getSourceForGlobalVariableExternal(request, this.type);
|
||||
case "commonjs":
|
||||
case "commonjs2":
|
||||
if(Array.isArray(request)) {
|
||||
str = "module.exports = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) {
|
||||
return "[" + JSON.stringify(r) + "]";
|
||||
}).join("") + ";";
|
||||
} else
|
||||
str = "module.exports = require(" + JSON.stringify(request) + ");";
|
||||
break;
|
||||
return this.getSourceForCommonJsExternal(request);
|
||||
case "amd":
|
||||
case "umd":
|
||||
case "umd2":
|
||||
str = "";
|
||||
if(this.optional) {
|
||||
str += "if(typeof __WEBPACK_EXTERNAL_MODULE_" + this.id + "__ === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
|
||||
}
|
||||
str += "module.exports = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;";
|
||||
break;
|
||||
return this.getSourceForAmdOrUmdExternal(request, this.id, this.optional);
|
||||
default:
|
||||
str = "";
|
||||
if(this.optional) {
|
||||
str += "if(typeof " + request + " === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
|
||||
}
|
||||
str += "module.exports = " + request + ";";
|
||||
break;
|
||||
return this.getSourceForDefaultCase(request, this.optional);
|
||||
}
|
||||
}
|
||||
|
||||
getSource(sourceString) {
|
||||
if(this.useSourceMap) {
|
||||
return new OriginalSource(str, this.identifier());
|
||||
} else {
|
||||
return new RawSource(str);
|
||||
return new OriginalSource(sourceString, this.identifier());
|
||||
}
|
||||
|
||||
return new RawSource(sourceString);
|
||||
}
|
||||
|
||||
source() {
|
||||
return this.getSource(
|
||||
this.getSourceString()
|
||||
);
|
||||
}
|
||||
|
||||
size() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue