Skip to content

Commit 1cc87e9

Browse files
committed
added support for nested var in the ProvidePlugin
1 parent 2471e40 commit 1cc87e9

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎lib/ProvidePlugin.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ModuleParserHelpers = require("./ModuleParserHelpers");
6+
var ConstDependency = require("./dependencies/ConstDependency");
7+
8+
var NullFactory = require("./NullFactory");
69

710
function ProvidePlugin(definitions) {
811
this.definitions = definitions;
912
}
1013
module.exports = ProvidePlugin;
1114
ProvidePlugin.prototype.apply = function(compiler) {
15+
compiler.plugin("compilation", function(compilation, params) {
16+
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
17+
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
18+
});
1219
Object.keys(this.definitions).forEach(function(name) {
1320
var request = this.definitions[name];
1421
compiler.parser.plugin("expression " + name, function(expr) {
15-
return ModuleParserHelpers.addParsedVariable(this, name, "require(" + JSON.stringify(request) + ")");
22+
var nameIdentifier = name;
23+
var scopedName = name.indexOf(".") >= 0;
24+
if(scopedName) {
25+
nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_");
26+
}
27+
if(!ModuleParserHelpers.addParsedVariable(this, nameIdentifier, "require(" + JSON.stringify(request) + ")")) {
28+
return false;
29+
}
30+
if(scopedName) {
31+
nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_");
32+
var dep = new ConstDependency(nameIdentifier, expr.range);
33+
dep.loc = expr.loc;
34+
this.state.current.addDependency(dep);
35+
}
36+
return true;
1637
});
1738
}, this);
1839
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "aaa";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "bbbccc";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
it("should provide a module for a simple free var", function() {
2+
aaa.should.be.eql("aaa");
3+
});
4+
5+
it("should provide a module for a nested var", function() {
6+
(bbb.ccc).should.be.eql("bbbccc");
7+
var x = bbb.ccc;
8+
x.should.be.eql("bbbccc");
9+
});
10+
11+
it("should not provide a module for a part of a var", function() {
12+
(typeof bbb).should.be.eql("undefined");
13+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var ProvidePlugin = require("../../../../lib/ProvidePlugin");
2+
module.exports = {
3+
plugins: [
4+
new ProvidePlugin({
5+
aaa: "./aaa",
6+
"bbb.ccc": "./bbbccc",
7+
})
8+
]
9+
};

0 commit comments

Comments
 (0)