webpack/lib/ProvidePlugin.js

47 lines
1.6 KiB
JavaScript
Raw Normal View History

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");
var ConstDependency = require("./dependencies/ConstDependency");
var NullFactory = require("./NullFactory");
2013-02-11 03:37:30 +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) {
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
});
Object.keys(this.definitions).forEach(function(name) {
var request = this.definitions[name];
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(".");
compiler.parser.plugin("can-rename " + name, function() { return true; });
});
}
compiler.parser.plugin("expression " + name, function(expr) {
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;
});
}, this);
};