Skip to content

Commit d43cecd

Browse files
fredriksSpaceK33z
authored andcommitted
Support dot notation in variable names (#30)
* Support dot notation in variable names import?abc.def.ghi=>1 => var abc = {}; abc.def = {}; abc.def.ghi = 1; import?window.jQuery=jquery => var window = {}; window.jQuery = require("jquery"); * Removed unnecessary append syntax * Special handling of "window" name Dot notation involving window is handled slightly different to not overwriting window properties import?window.jQuery=jquery => window = (window || {}); window.jQuery = require("jquery"); * Added mocha and tests for nested imports * Dont overwrite existing globals (removed special handling of 'window') Nested imports will not overwrite top level globals. Eg. import?abc.def=1 is compiled to var abc = (abc || {}); abc.def = 1; * Removed redundant tests
1 parent 6cdd994 commit d43cecd

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ module.exports = function(content, sourceMap) {
2525
if(name === "this") {
2626
imports.push("(function() {");
2727
postfixes.unshift("}.call(" + value + "));");
28+
} else if(name.indexOf(".") !== -1) {
29+
name.split(".").reduce(function(previous, current, index, names) {
30+
var expr = previous + current;
31+
32+
if(previous.length === 0) {
33+
imports.push("var " + expr + " = (" + current + " || {});");
34+
} else if(index < names.length-1) {
35+
imports.push(expr + " = {};");
36+
} else {
37+
imports.push(expr + " = " + value + ";");
38+
}
39+
40+
return previous + current + ".";
41+
}, "");
2842
} else {
2943
imports.push("var " + name + " = " + value + ";");
3044
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
"version": "0.6.5",
44
"author": "Tobias Koppers @sokra",
55
"description": "imports loader module for webpack",
6+
"scripts": {
7+
"test": "mocha"
8+
},
69
"dependencies": {
710
"loader-utils": "0.2.x",
811
"source-map": "0.1.x"
912
},
13+
"devDependencies": {
14+
"mocha": "^3.1.2",
15+
"should": "^11.1.1"
16+
},
1017
"license": "MIT",
1118
"repository": {
1219
"type": "git",

test/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var should = require("should");
2+
var loader = require("../");
3+
4+
var HEADER = "/*** IMPORTS FROM imports-loader ***/\n";
5+
6+
describe("loader", function() {
7+
it("should import nested objects", function() {
8+
loader.call({
9+
query: "?abc.def.ghi=>1"
10+
}, "").should.be.eql(HEADER +
11+
"var abc = (abc || {});\n" +
12+
"abc.def = {};\n" +
13+
"abc.def.ghi = 1;\n\n\n"
14+
);
15+
});
16+
17+
it("should import multiple nested objects", function() {
18+
loader.call({
19+
query: "?abc.def.ghi=>1,foo.bar.baz=>2"
20+
}, "").should.be.eql(HEADER +
21+
// First import
22+
"var abc = (abc || {});\n" +
23+
"abc.def = {};\n" +
24+
"abc.def.ghi = 1;\n" +
25+
// Second import
26+
"var foo = (foo || {});\n" +
27+
"foo.bar = {};\n" +
28+
"foo.bar.baz = 2;\n\n\n"
29+
);
30+
});
31+
});

0 commit comments

Comments
 (0)