2013-02-11 03:37:30 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var ModuleParserHelpers = require("./ModuleParserHelpers");
|
2014-10-17 04:31:20 +08:00
|
|
|
var ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
|
|
|
|
var NullFactory = require("./NullFactory");
|
2013-02-11 03:37:30 +08:00
|
|
|
|
2013-12-18 06:21:49 +08:00
|
|
|
function ProvidePlugin(definitions) {
|
|
|
|
this.definitions = definitions;
|
2013-02-11 03:37:30 +08:00
|
|
|
}
|
|
|
|
module.exports = ProvidePlugin;
|
|
|
|
ProvidePlugin.prototype.apply = function(compiler) {
|
2015-04-24 05:55:50 +08:00
|
|
|
compiler.plugin("compilation", function(compilation) {
|
2014-10-17 04:31:20 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
|
|
|
});
|
2013-12-18 06:21:49 +08:00
|
|
|
Object.keys(this.definitions).forEach(function(name) {
|
|
|
|
var request = this.definitions[name];
|
2015-02-28 07:26:44 +08:00
|
|
|
var splittedName = name.split(".");
|
|
|
|
if(splittedName.length > 0) {
|
|
|
|
splittedName.slice(1).forEach(function(_, i) {
|
2015-04-24 05:55:50 +08:00
|
|
|
var name = splittedName.slice(0, i + 1).join(".");
|
2015-02-28 07:26:44 +08:00
|
|
|
compiler.parser.plugin("can-rename " + name, function() { return true; });
|
|
|
|
});
|
|
|
|
}
|
2013-12-18 06:21:49 +08:00
|
|
|
compiler.parser.plugin("expression " + name, function(expr) {
|
2014-10-17 04:31:20 +08:00
|
|
|
var nameIdentifier = name;
|
|
|
|
var scopedName = name.indexOf(".") >= 0;
|
|
|
|
if(scopedName) {
|
|
|
|
nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_");
|
|
|
|
}
|
|
|
|
if(!ModuleParserHelpers.addParsedVariable(this, nameIdentifier, "require(" + JSON.stringify(request) + ")")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(scopedName) {
|
|
|
|
nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_");
|
|
|
|
var dep = new ConstDependency(nameIdentifier, expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
this.state.current.addDependency(dep);
|
|
|
|
}
|
|
|
|
return true;
|
2013-12-18 06:21:49 +08:00
|
|
|
});
|
|
|
|
}, this);
|
2015-07-08 20:39:02 +08:00
|
|
|
};
|