2015-05-13 06:15:01 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var ExternalModule = require("./ExternalModule");
|
|
|
|
|
|
|
|
function ExternalModuleFactoryPlugin(type, externals) {
|
|
|
|
this.type = type;
|
|
|
|
this.externals = externals;
|
|
|
|
}
|
|
|
|
module.exports = ExternalModuleFactoryPlugin;
|
|
|
|
|
|
|
|
ExternalModuleFactoryPlugin.prototype.apply = function(normalModuleFactory) {
|
|
|
|
var globalType = this.type;
|
|
|
|
normalModuleFactory.plugin("factory", function(factory) {
|
|
|
|
return function(data, callback) {
|
|
|
|
var context = data.context;
|
2016-07-20 18:51:03 +08:00
|
|
|
var dependency = data.dependencies[0];
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2015-05-13 06:15:01 +08:00
|
|
|
function handleExternal(value, type, callback) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof type === "function") {
|
2015-05-13 06:15:01 +08:00
|
|
|
callback = type;
|
|
|
|
type = undefined;
|
|
|
|
}
|
2015-07-16 06:19:23 +08:00
|
|
|
if(value === false) return factory(data, callback);
|
|
|
|
if(value === true) value = dependency.request;
|
|
|
|
if(typeof type === "undefined" && /^[a-z0-9]+ /.test(value)) {
|
2015-05-13 06:15:01 +08:00
|
|
|
var idx = value.indexOf(" ");
|
|
|
|
type = value.substr(0, idx);
|
|
|
|
value = value.substr(idx + 1);
|
|
|
|
}
|
|
|
|
callback(null, new ExternalModule(value, type || globalType));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
(function handleExternals(externals, callback) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof externals === "string") {
|
|
|
|
if(externals === dependency.request) {
|
2015-05-13 06:15:01 +08:00
|
|
|
return handleExternal(dependency.request, callback);
|
|
|
|
}
|
2015-07-16 06:19:23 +08:00
|
|
|
} else if(Array.isArray(externals)) {
|
2015-05-13 06:15:01 +08:00
|
|
|
var i = 0;
|
|
|
|
(function next() {
|
2016-10-29 17:11:44 +08:00
|
|
|
var handleExternalsAndCallback = function handleExternalsAndCallback(err, module) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
if(!module) {
|
|
|
|
if(async) {
|
|
|
|
async = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
callback(null, module);
|
|
|
|
}
|
|
|
|
|
2015-05-13 06:15:01 +08:00
|
|
|
do {
|
|
|
|
var async = true;
|
2015-07-16 06:19:23 +08:00
|
|
|
if(i >= externals.length) return callback();
|
2016-10-29 17:11:44 +08:00
|
|
|
handleExternals(externals[i++], handleExternalsAndCallback);
|
2016-12-30 00:10:41 +08:00
|
|
|
} while (!async); // eslint-disable-line keyword-spacing
|
2015-05-13 06:15:01 +08:00
|
|
|
async = false;
|
|
|
|
}());
|
|
|
|
return;
|
2015-07-16 06:19:23 +08:00
|
|
|
} else if(externals instanceof RegExp) {
|
|
|
|
if(externals.test(dependency.request)) {
|
2015-05-13 06:15:01 +08:00
|
|
|
return handleExternal(dependency.request, callback);
|
|
|
|
}
|
2015-07-16 06:19:23 +08:00
|
|
|
} else if(typeof externals === "function") {
|
2015-05-13 06:15:01 +08:00
|
|
|
externals.call(null, context, dependency.request, function(err, value, type) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(err) return callback(err);
|
|
|
|
if(typeof value !== "undefined") {
|
2015-05-13 06:15:01 +08:00
|
|
|
handleExternal(value, type, callback);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
2015-07-16 06:19:23 +08:00
|
|
|
} else if(typeof externals === "object" && Object.prototype.hasOwnProperty.call(externals, dependency.request)) {
|
2015-05-13 06:15:01 +08:00
|
|
|
return handleExternal(externals[dependency.request], callback);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}(this.externals, function(err, module) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(err) return callback(err);
|
|
|
|
if(!module) return handleExternal(false, callback);
|
2015-05-13 06:15:01 +08:00
|
|
|
return callback(null, module);
|
|
|
|
}));
|
|
|
|
}.bind(this);
|
|
|
|
}.bind(this));
|
|
|
|
};
|