Browse Source

Processes path mappings that are arrays.

Herbert Vojčík 9 years ago
parent
commit
e166b47f31

+ 15 - 3
lib/config-builder.js

@@ -32,14 +32,26 @@ function transformShimDeps(json) {
 }
 
 function transformPaths(json, root, file, dirs) {
-    if (json.paths) {
+   if (json.paths) {
         var specifier = path.basename(file).replace(/\.amd\.json$/, "");
         var modulePath = specifier === "local" ? path.dirname(file) : dirs[specifier];
         var dir = (path.relative(root, modulePath).split(path.sep).join('/') || ".") + '/';
-        var paths = Object.keys(json.paths);
+        transformPathArray(json.paths);
+    }
+
+    function transformPathArray(array) {
+        var paths = Object.keys(array);
         for (var i = 0; i < paths.length; i++) {
             var key = paths[i];
-            json.paths[key] = url.resolve(dir, json.paths[key]) || ".";
+            var pathValue = array[key];
+            switch (typeof pathValue) {
+                case "string":
+                    array[key] = url.resolve(dir, pathValue) || ".";
+                    break;
+                case "object":
+                    Array.isArray(pathValue) && transformPathArray(pathValue);
+                    break;
+            }
         }
     }
 }

+ 6 - 0
test/arrays-in-paths/.amd.json

@@ -0,0 +1,6 @@
+{
+    "paths": {
+        "fooDot": "fooDot",
+        "fooDotArray": [ "fooDotArray"]
+    }
+}

+ 1 - 0
test/arrays-in-paths/a3/3/other/local.amd.json

@@ -0,0 +1 @@
+{}

+ 6 - 0
test/arrays-in-paths/amd.json

@@ -0,0 +1,6 @@
+{
+    "paths": {
+        "fooStripped": "fooStripped",
+        "fooStrippedArray": [ "fooStrippedArray" ]
+    }
+}

+ 16 - 0
test/arrays-in-paths/deep/local.amd.json

@@ -0,0 +1,16 @@
+{
+    "paths": {
+        "dot1": ".",
+        "rel1": "relative",
+        "sibling1": "../sibling",
+        "abs1": "/absolute",
+        "uri1": "http://example.com/uri",
+        "arr1": [
+            ".",
+            "relative",
+            "../sibling",
+            "/absolute",
+            "http://example.com/uri"
+        ]
+    }
+}

+ 16 - 0
test/arrays-in-paths/local.amd.json

@@ -0,0 +1,16 @@
+{
+    "paths": {
+        "dot0": ".",
+        "rel0": "relative",
+        "sibling0": "../sibling",
+        "abs0": "/absolute",
+        "uri0": "http://example.com/uri",
+        "arr0": [
+            ".",
+            "relative",
+            "../sibling",
+            "/absolute",
+            "http://example.com/uri"
+        ]
+    }
+}

+ 16 - 0
test/arrays-in-paths/other.amd.json

@@ -0,0 +1,16 @@
+{
+    "paths": {
+        "dot2": ".",
+        "rel2": "relative",
+        "sibling2": "../sibling",
+        "abs2": "/absolute",
+        "uri2": "http://example.com/uri",
+        "arr2": [
+            ".",
+            "relative",
+            "../sibling",
+            "/absolute",
+            "http://example.com/uri"
+        ]
+    }
+}

+ 1 - 0
test/arrays-in-paths/z2/other/local.amd.json

@@ -0,0 +1 @@
+{}

+ 19 - 2
test/produce.js

@@ -70,7 +70,7 @@ describe('#produceConfigObject merging', function () {
         builder.produceConfigObject(fixture('two-locals-two-others'), function (err, result) {
             assert.ifError(err);
             assert.deepEqual(result, {
-                config: {a:2, b:2, c:2, d:2, e:1, f:1}
+                config: {a: 2, b: 2, c: 2, d: 2, e: 1, f: 1}
             });
             done();
         });
@@ -93,7 +93,7 @@ describe('#produceConfigObject knows to deal with shims', function () {
         builder.produceConfigObject(fixture('two-locals-two-others-shim'), function (err, result) {
             assert.ifError(err);
             assert.deepEqual(result, {
-                config: {a:2, b:2, c:2, d:2, e:1, f:1},
+                config: {a: 2, b: 2, c: 2, d: 2, e: 1, f: 1},
                 shim: {
                     a: {deps: ["deeplocal", "other"], exports: "deep"},
                     b: {deps: ["rootlocal", "deep"], exports: "other"}
@@ -136,4 +136,21 @@ describe('#produceConfigObject paths', function () {
         });
     });
 
+    it('should works with array paths correctly', function (done) {
+        builder.produceConfigObject(fixture('arrays-in-paths'), function (err, result) {
+            assert.ifError(err);
+            assert.deepEqual(result, {
+                paths: {
+                    dot0: ".", rel0: "relative", sibling0: "../sibling", abs0: "/absolute", uri0: "http://example.com/uri",
+                    arr0: [".", "relative", "../sibling", "/absolute", "http://example.com/uri"],
+                    dot1: "deep", rel1: "deep/relative", sibling1: "sibling", abs1: "/absolute", uri1: "http://example.com/uri",
+                    arr1: ["deep", "deep/relative", "sibling", "/absolute", "http://example.com/uri"],
+                    dot2: "z2/other", rel2: "z2/other/relative", sibling2: "z2/sibling", abs2: "/absolute", uri2: "http://example.com/uri",
+                    arr2: ["z2/other", "z2/other/relative", "z2/sibling", "/absolute", "http://example.com/uri"]
+                }
+            });
+            done();
+        });
+    });
+
 });