Browse Source

produceConfigObject deals correctly with shim deps

Herbert Vojčík 10 years ago
parent
commit
0599a3c3ed

+ 16 - 2
lib/config-builder.js

@@ -19,6 +19,17 @@ function eachConfigFile(root, callback) {
     });
 }
 
+function transformShimDeps(json) {
+    var shimmedList = json.shim && Object.keys(json.shim);
+    if (!shimmedList) return;
+    for (var i = 0; i < shimmedList.length; i++) {
+        var shim = json.shim[shimmedList[i]];
+        if (Array.isArray(shim)) {
+            json.shim[shimmedList[i]] = {deps: shim};
+        }
+    }
+}
+
 exports.produceConfigObject = function (root, callback) {
     eachConfigFile(root, function (err, filesAndDirs) {
         if (err) {
@@ -56,8 +67,11 @@ exports.produceConfigObject = function (root, callback) {
         }
         var result = {};
         for (var i = 0; i < bothPasses.length; i++) {
-            var file = bothPasses[i];
-            _.merge(result, require(file));
+            var json = require(bothPasses[i]);
+            transformShimDeps(json);
+            _.merge(result, json, function(a, b) {
+                return _.isArray(a) ? a.concat(b) : undefined;
+            });
         }
         callback(null, result);
     })

+ 27 - 0
test/produce.js

@@ -66,3 +66,30 @@ describe('#produceConfigObject merging', function () {
     });
 
 });
+
+describe('#produceConfigObject knows to deal with shims', function () {
+    it('should output default array as {deps: anArray} in shim', function (done) {
+        builder.produceConfigObject(fixture('single-local-shim'), function (err, result) {
+            assert.ifError(err);
+            assert.deepEqual(result, {
+                shim: {foo: {deps: ["bar"]}}
+            });
+            done();
+        });
+    });
+
+    it('should merge shims from {root,deep}/local.amd.json then from {foo,bar}.amd.json given {foo,bar} dir is present', function (done) {
+        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},
+                shim: {
+                    a: {deps: ["deeplocal", "other"], exports: "deep"},
+                    b: {deps: ["rootlocal", "deep"], exports: "other"}
+                }
+            });
+            done();
+        });
+    });
+
+});

+ 5 - 0
test/single-local-shim/.amd.json

@@ -0,0 +1,5 @@
+{
+    "shim": {
+        "fooDot": ["barDot"]
+    }
+}

+ 5 - 0
test/single-local-shim/amd.json

@@ -0,0 +1,5 @@
+{
+    "shim": {
+        "fooStripped": ["barStripped"]
+    }
+}

+ 5 - 0
test/single-local-shim/local.amd.json

@@ -0,0 +1,5 @@
+{
+    "shim": {
+        "foo": ["bar"]
+    }
+}

+ 9 - 0
test/two-locals-two-others-shim/deep/local.amd.json

@@ -0,0 +1,9 @@
+{
+    "config": {
+        "a": 1, "b": 1, "e": 1
+    },
+    "shim": {
+        "a": ["deeplocal"],
+        "b":{"exports": "deeplocal"}
+    }
+}

+ 9 - 0
test/two-locals-two-others-shim/local.amd.json

@@ -0,0 +1,9 @@
+{
+    "config": {
+        "c": 1, "d": 1, "f": 1
+    },
+    "shim": {
+        "a": {"exports": "rootlocal"},
+        "b": {"deps": ["rootlocal"], "exports": "rootlocal"}
+    }
+}

+ 9 - 0
test/two-locals-two-others-shim/other.amd.json

@@ -0,0 +1,9 @@
+{
+    "config": {
+        "b": 2, "c": 2
+    },
+    "shim": {
+        "a": {"deps": ["other"]},
+        "b": {"exports": "other"}
+    }
+}

+ 9 - 0
test/two-locals-two-others-shim/other/deep.amd.json

@@ -0,0 +1,9 @@
+{
+    "config": {
+        "a": 2, "d": 2
+    },
+    "shim": {
+        "a": {"exports": "deep"},
+        "b": ["deep"]
+    }
+}