From 375e7d239943f867ad5a76272560a56ed1504804 Mon Sep 17 00:00:00 2001 From: Tim Sebastian Date: Sun, 19 Feb 2017 00:06:23 +1100 Subject: [PATCH] refactor source method of ExternalModule --- lib/ExternalModule.js | 88 +++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 33 deletions(-) diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index 4e61d3bca..7b94b216c 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -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() {