2014-03-05 16:58:51 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var Module = require("./Module");
|
2015-12-30 00:44:55 +08:00
|
|
|
var OriginalSource = require("webpack-sources").OriginalSource;
|
|
|
|
var RawSource = require("webpack-sources").RawSource;
|
2014-07-03 06:00:06 +08:00
|
|
|
var WebpackMissingModule = require("./dependencies/WebpackMissingModule");
|
2014-03-05 16:58:51 +08:00
|
|
|
|
|
|
|
function ExternalModule(request, type) {
|
|
|
|
Module.call(this);
|
2016-05-05 16:13:50 +08:00
|
|
|
this.chunkCondition = function(chunk) {
|
|
|
|
return chunk.hasEntryModule();
|
|
|
|
};
|
2014-03-05 16:58:51 +08:00
|
|
|
this.request = request;
|
|
|
|
this.type = type;
|
|
|
|
this.built = false;
|
|
|
|
}
|
|
|
|
module.exports = ExternalModule;
|
|
|
|
|
|
|
|
ExternalModule.prototype = Object.create(Module.prototype);
|
2016-05-20 13:39:36 +08:00
|
|
|
ExternalModule.prototype.constructor = ExternalModule;
|
2014-03-05 16:58:51 +08:00
|
|
|
|
|
|
|
ExternalModule.prototype.external = true;
|
|
|
|
|
|
|
|
ExternalModule.prototype.identifier = function() {
|
2014-03-06 02:55:53 +08:00
|
|
|
return "external " + JSON.stringify(this.request);
|
2014-03-05 16:58:51 +08:00
|
|
|
};
|
|
|
|
|
2015-04-24 05:55:50 +08:00
|
|
|
ExternalModule.prototype.readableIdentifier = function() {
|
2014-03-06 02:55:53 +08:00
|
|
|
return "external " + JSON.stringify(this.request);
|
2014-03-05 16:58:51 +08:00
|
|
|
};
|
|
|
|
|
2015-04-24 05:55:50 +08:00
|
|
|
ExternalModule.prototype.needRebuild = function() {
|
2014-03-05 16:58:51 +08:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ExternalModule.prototype.build = function(options, compilation, resolver, fs, callback) {
|
|
|
|
this.builtTime = new Date().getTime();
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
2015-04-24 05:55:50 +08:00
|
|
|
ExternalModule.prototype.source = function() {
|
2014-03-05 16:58:51 +08:00
|
|
|
var str = "throw new Error('Externals not supported');";
|
|
|
|
var request = this.request;
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof request === "object") request = request[this.type];
|
|
|
|
switch(this.type) {
|
2015-07-08 20:15:21 +08:00
|
|
|
case "this":
|
|
|
|
case "window":
|
|
|
|
case "global":
|
2015-07-16 06:19:23 +08:00
|
|
|
if(Array.isArray(request)) {
|
2016-04-22 05:50:28 +08:00
|
|
|
str = "(function() { module.exports = " + this.type + request.map(function(r) {
|
2015-07-08 20:15:21 +08:00
|
|
|
return "[" + JSON.stringify(r) + "]";
|
|
|
|
}).join("") + "; }());";
|
|
|
|
} else
|
2016-04-22 05:50:28 +08:00
|
|
|
str = "(function() { module.exports = " + this.type + "[" + JSON.stringify(request) + "]; }());";
|
2015-07-08 20:15:21 +08:00
|
|
|
break;
|
|
|
|
case "commonjs":
|
|
|
|
case "commonjs2":
|
2015-07-16 06:19:23 +08:00
|
|
|
if(Array.isArray(request)) {
|
2016-04-22 05:50:28 +08:00
|
|
|
str = "module.exports = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) {
|
2015-07-08 20:15:21 +08:00
|
|
|
return "[" + JSON.stringify(r) + "]";
|
|
|
|
}).join("") + ";";
|
|
|
|
} else
|
2016-04-22 05:50:28 +08:00
|
|
|
str = "module.exports = require(" + JSON.stringify(request) + ");";
|
2015-07-08 20:15:21 +08:00
|
|
|
break;
|
|
|
|
case "amd":
|
|
|
|
case "umd":
|
|
|
|
case "umd2":
|
|
|
|
str = "";
|
2015-07-16 06:19:23 +08:00
|
|
|
if(this.optional) {
|
2015-07-08 20:15:21 +08:00
|
|
|
str += "if(typeof __WEBPACK_EXTERNAL_MODULE_" + this.id + "__ === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
|
|
|
|
}
|
2016-04-22 05:50:28 +08:00
|
|
|
str += "module.exports = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;";
|
2015-07-08 20:15:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "";
|
2015-07-16 06:19:23 +08:00
|
|
|
if(this.optional) {
|
2015-07-08 20:15:21 +08:00
|
|
|
str += "if(typeof " + request + " === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n";
|
|
|
|
}
|
2016-04-22 05:50:28 +08:00
|
|
|
str += "module.exports = " + request + ";";
|
2015-07-08 20:15:21 +08:00
|
|
|
break;
|
2014-03-05 16:58:51 +08:00
|
|
|
}
|
2015-07-16 06:19:23 +08:00
|
|
|
if(this.useSourceMap) {
|
2014-03-05 16:58:51 +08:00
|
|
|
return new OriginalSource(str, this.identifier());
|
|
|
|
} else {
|
|
|
|
return new RawSource(str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ExternalModule.prototype.size = function() {
|
|
|
|
return 42;
|
2014-07-22 23:10:15 +08:00
|
|
|
};
|