Repository: survivejs/webpack-merge
Pull request #14 (2016-04-10 10:26:10)

Allow smart merging loaders with loader string values and include/exc…
…lude options


Patch:
diff --git a/lib/index.js b/lib/index.js
index aee95d0..af414d5 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -23,15 +23,16 @@ function reduceLoaders(mergedLoaderConfigs, loaderConfig) {
     return String(l.test) === String(loaderConfig.test);
   });
 
-  // foundLoader.loader is intentionally ignored, because a string loader value should always override
-  if (foundLoader && foundLoader.loaders) {
-    var newLoaders = loaderConfig.loader ? [loaderConfig.loader] : loaderConfig.loaders || [];
-
+  if (foundLoader) {
     if (foundLoader.include || foundLoader.exclude) {
       return mergedLoaderConfigs.concat([loaderConfig]);
     }
 
-    foundLoader.loaders = mergeLoaders(foundLoader.loaders, newLoaders);
+    // foundLoader.loader is intentionally ignored, because a string loader value should always override
+    if (foundLoader.loaders) {
+      var newLoaders = loaderConfig.loader ? [loaderConfig.loader] : loaderConfig.loaders || [];
+      foundLoader.loaders = mergeLoaders(foundLoader.loaders, newLoaders);
+    }
   } else if (!foundLoader) {
     return mergedLoaderConfigs.concat([loaderConfig]);
   }
diff --git a/src/index.js b/src/index.js
index 9e56454..f15f41f 100644
--- a/src/index.js
+++ b/src/index.js
@@ -17,15 +17,16 @@ function mergeLoaders(currentLoaders, newLoaders) {
 function reduceLoaders(mergedLoaderConfigs, loaderConfig) {
   const foundLoader = find(mergedLoaderConfigs, l => String(l.test) === String(loaderConfig.test));
 
-  // foundLoader.loader is intentionally ignored, because a string loader value should always override
-  if (foundLoader && foundLoader.loaders) {
-    const newLoaders = loaderConfig.loader ? [loaderConfig.loader] : loaderConfig.loaders || [];
-
+  if (foundLoader) {
     if (foundLoader.include || foundLoader.exclude) {
       return mergedLoaderConfigs.concat([loaderConfig]);
     }
 
-    foundLoader.loaders = mergeLoaders(foundLoader.loaders, newLoaders);
+    // foundLoader.loader is intentionally ignored, because a string loader value should always override
+    if (foundLoader.loaders) {
+      const newLoaders = loaderConfig.loader ? [loaderConfig.loader] : loaderConfig.loaders || [];
+      foundLoader.loaders = mergeLoaders(foundLoader.loaders, newLoaders);
+    }
   } else if (!foundLoader) {
     return mergedLoaderConfigs.concat([loaderConfig]);
   }


Test patch:
diff --git a/test.js b/test.js
index f8f9da6..558ce61 100644
--- a/test.js
+++ b/test.js
@@ -393,6 +393,54 @@ function smartMergeTests(merge, loadersKey) {
     assert.deepEqual(merge(common, isparta), result);
   });
 
+  it('should not merge if a loader has include and string loader values for ' + loadersKey, function () {
+    // these shouldn't be merged because `include` is defined.
+    // instead, it should prepend to guarantee sane evaluation order
+    const common = {
+      module: {}
+    };
+    common.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loaders: ['eslint'],
+        include: [
+          'foo',
+          'bar'
+        ]
+      }
+    ];
+    const isparta = {
+      module: {}
+    };
+    isparta.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loader: 'isparta-instrumenter',
+        include: 'baz'
+      }
+    ];
+    const result = {
+      module: {}
+    };
+    result.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loader: 'isparta-instrumenter',
+        include: 'baz'
+      },
+      {
+        test: /\.jsx?$/,
+        loaders: ['eslint'],
+        include: [
+          'foo',
+          'bar'
+        ]
+      }
+    ];
+
+    assert.deepEqual(merge(common, isparta), result);
+  });
+
   it('should not merge if a loader has exclude for ' + loadersKey, function () {
     // these shouldn't be merged because `exclude` is defined.
     // instead, it should prepend to guarantee sane evaluation order
@@ -440,6 +488,54 @@ function smartMergeTests(merge, loadersKey) {
 
     assert.deepEqual(merge(common, isparta), result);
   });
+
+  it('should not merge if a loader has exclude and string loader values for ' + loadersKey, function () {
+    // these shouldn't be merged because `exclude` is defined.
+    // instead, it should prepend to guarantee sane evaluation order
+    const common = {
+      module: {}
+    };
+    common.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loaders: ['eslint'],
+        exclude: [
+          'foo',
+          'bar'
+        ]
+      }
+    ];
+    const isparta = {
+      module: {}
+    };
+    isparta.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loader: 'isparta-instrumenter',
+        exclude: 'baz'
+      }
+    ];
+    const result = {
+      module: {}
+    };
+    result.module[loadersKey] = [
+      {
+        test: /\.jsx?$/,
+        loader: 'isparta-instrumenter',
+        exclude: 'baz'
+      },
+      {
+        test: /\.jsx?$/,
+        loaders: ['eslint'],
+        exclude: [
+          'foo',
+          'bar'
+        ]
+      }
+    ];
+
+    assert.deepEqual(merge(common, isparta), result);
+  });
 }
 
 function mergeTests(merge) {