2014-03-05 16:58:51 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-02-18 20:17:16 +08:00
|
|
|
"use strict";
|
|
|
|
const Module = require("./Module");
|
|
|
|
const OriginalSource = require("webpack-sources").OriginalSource;
|
|
|
|
const RawSource = require("webpack-sources").RawSource;
|
|
|
|
const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
class ExternalModule extends Module {
|
|
|
|
constructor(request, type) {
|
|
|
|
super();
|
|
|
|
this.chunkCondition = function(chunk) {
|
|
|
|
return chunk.hasEntryModule();
|
|
|
|
};
|
|
|
|
this.request = request;
|
|
|
|
this.type = type;
|
|
|
|
this.built = false;
|
|
|
|
this.external = true;
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
identifier() {
|
|
|
|
return "external " + JSON.stringify(this.request);
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
readableIdentifier() {
|
|
|
|
return "external " + JSON.stringify(this.request);
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
needRebuild() {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
build(options, compilation, resolver, fs, callback) {
|
|
|
|
this.builtTime = new Date().getTime();
|
|
|
|
callback();
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 21:06:23 +08:00
|
|
|
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;
|
2017-02-18 20:17:16 +08:00
|
|
|
switch(this.type) {
|
|
|
|
case "this":
|
|
|
|
case "window":
|
|
|
|
case "global":
|
2017-02-18 21:06:23 +08:00
|
|
|
return this.getSourceForGlobalVariableExternal(request, this.type);
|
2017-02-18 20:17:16 +08:00
|
|
|
case "commonjs":
|
|
|
|
case "commonjs2":
|
2017-02-18 21:06:23 +08:00
|
|
|
return this.getSourceForCommonJsExternal(request);
|
2017-02-18 20:17:16 +08:00
|
|
|
case "amd":
|
|
|
|
case "umd":
|
|
|
|
case "umd2":
|
2017-02-18 21:06:23 +08:00
|
|
|
return this.getSourceForAmdOrUmdExternal(request, this.id, this.optional);
|
2017-02-18 20:17:16 +08:00
|
|
|
default:
|
2017-02-18 21:06:23 +08:00
|
|
|
return this.getSourceForDefaultCase(request, this.optional);
|
2017-02-18 20:17:16 +08:00
|
|
|
}
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getSource(sourceString) {
|
2017-02-18 20:17:16 +08:00
|
|
|
if(this.useSourceMap) {
|
2017-02-18 21:06:23 +08:00
|
|
|
return new OriginalSource(sourceString, this.identifier());
|
2017-02-18 20:17:16 +08:00
|
|
|
}
|
2017-02-18 21:06:23 +08:00
|
|
|
|
|
|
|
return new RawSource(sourceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
source() {
|
|
|
|
return this.getSource(
|
|
|
|
this.getSourceString()
|
|
|
|
);
|
2014-03-05 16:58:51 +08:00
|
|
|
}
|
2017-02-18 20:17:16 +08:00
|
|
|
|
|
|
|
size() {
|
|
|
|
return 42;
|
2014-03-05 16:58:51 +08:00
|
|
|
}
|
2017-02-18 20:17:16 +08:00
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
module.exports = ExternalModule;
|