Skip to content

Commit

Permalink
Support dot notation in variable names (#30)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fredriks authored and SpaceK33z committed Dec 7, 2016
1 parent 6cdd994 commit d43cecd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ module.exports = function(content, sourceMap) {
if(name === "this") {
imports.push("(function() {");
postfixes.unshift("}.call(" + value + "));");
} else if(name.indexOf(".") !== -1) {
name.split(".").reduce(function(previous, current, index, names) {
var expr = previous + current;

if(previous.length === 0) {
imports.push("var " + expr + " = (" + current + " || {});");
} else if(index < names.length-1) {
imports.push(expr + " = {};");
} else {
imports.push(expr + " = " + value + ";");
}

return previous + current + ".";
}, "");
} else {
imports.push("var " + name + " = " + value + ";");
}
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
"version": "0.6.5",
"author": "Tobias Koppers @sokra",
"description": "imports loader module for webpack",
"scripts": {
"test": "mocha"
},
"dependencies": {
"loader-utils": "0.2.x",
"source-map": "0.1.x"
},
"devDependencies": {
"mocha": "^3.1.2",
"should": "^11.1.1"
},
"license": "MIT",
"repository": {
"type": "git",
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var should = require("should");
var loader = require("../");

var HEADER = "/*** IMPORTS FROM imports-loader ***/\n";

describe("loader", function() {
it("should import nested objects", function() {
loader.call({
query: "?abc.def.ghi=>1"
}, "").should.be.eql(HEADER +
"var abc = (abc || {});\n" +
"abc.def = {};\n" +
"abc.def.ghi = 1;\n\n\n"
);
});

it("should import multiple nested objects", function() {
loader.call({
query: "?abc.def.ghi=>1,foo.bar.baz=>2"
}, "").should.be.eql(HEADER +
// First import
"var abc = (abc || {});\n" +
"abc.def = {};\n" +
"abc.def.ghi = 1;\n" +
// Second import
"var foo = (foo || {});\n" +
"foo.bar = {};\n" +
"foo.bar.baz = 2;\n\n\n"
);
});
});

0 comments on commit d43cecd

Please sign in to comment.