Skip to content

Commit

Permalink
Merge pull request #74 from herecydev/master
Browse files Browse the repository at this point in the history
Added merge multiple that outputs an array
  • Loading branch information
bebraw committed Mar 16, 2017
2 parents c3cd11d + 9e21275 commit 6bf19b6
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Expand Up @@ -312,6 +312,43 @@ merge.smart({
}
```

## Multiple Merging

### **`merge.multiple(...configuration | [...configuration])`**

Sometimes you may need to support multiple targets, *webpack-merge* will accept an object where each key represents the target configuration. The output becomes an *array* of configurations where matching keys are merged and non-matching keys are added.

```javascript
var path = require('path');
var baseConfig = {
server: {
target: 'node',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'lib.node.js'
}
},
client: {
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'lib.js'
}
}
};

// specialized configuration
var production = {
client: {
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash].js'
}
}
}

module.exports = merge.multiple(baseConfig, production)
```

> Check out [SurviveJS - Webpack and React](http://survivejs.com/) to dig deeper into the topic.
## Development
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
@@ -1,5 +1,5 @@
import {
differenceWith, mergeWith, unionWith
differenceWith, mergeWith, unionWith, values
} from 'lodash';
import joinArrays from './join-arrays';
import { uniteRules } from './join-arrays-smart';
Expand Down Expand Up @@ -41,6 +41,8 @@ const mergeSmart = merge({
}
});

const mergeMultiple = (...sources) => values(merge(sources));

// rules: { <field>: <'append'|'prepend'|'replace'> }
// All default to append but you can override here
const mergeStrategy = (rules = {}) => merge({
Expand Down Expand Up @@ -103,6 +105,7 @@ function isRule(key) {
}

module.exports = merge;
module.exports.multiple = mergeMultiple;
module.exports.smart = mergeSmart;
module.exports.strategy = mergeStrategy;
module.exports.smartStrategy = mergeSmartStrategy;
Expand Down
169 changes: 169 additions & 0 deletions tests/merge-multiple-tests.js
@@ -0,0 +1,169 @@
/* eslint-env mocha */
const assert = require('assert');

function multipleTests(merge) {
it('should override objects', function () {
const a = {
client: {
entry: './client.js'
}
};
const b = {
client: {
entry: './replaced.js'
}
};
const result = [
{
entry: './replaced.js'
}
];

assert.deepEqual(merge(a, b), result);
});

it('should add new objects if not existing', function () {
const a = {
client: {
entry: './client.js'
},
server: {
entry: './server.js'
}
};
const b = {
client: {
entry: './replaced.js'
}
};
const result = [
{
entry: './replaced.js'
},
{
entry: './server.js'
}
];

assert.deepEqual(merge(a, b), result);
});

it('should add different configurations without merging', function () {
const a = {
client: {
entry: './client.js'
}
};
const b = {
server: {
entry: './server.js'
}
};
const result = [
{
entry: './client.js'
},
{
entry: './server.js'
}
];

assert.deepEqual(merge(a, b), result);
});

it('should work with an array of objects', function () {
const a = {
client: {
entry: ['./client.js', './client2.js']
},
server: {
entry: ['./server.js', './server2.js']
}
};
const b = {
client: {
entry: ['./replaced.js', './replaced2.js']
}
};
const result = [
{
entry: ['./client.js', './client2.js', './replaced.js', './replaced2.js']
},
{
entry: ['./server.js', './server2.js']
}
];

assert.deepEqual(merge(a, b), result);
});

it('should deeply merge objects', function () {
const a = {
client: {
entry: {
main: './client.js'
}
},
server: {
entry: {
main: './server.js'
}
}
};
const b = {
client: {
entry: {
main: './replaced.js'
}
}
};
const result = [
{
entry: {
main: './replaced.js'
}
},
{
entry: {
main: './server.js'
}
}
];

assert.deepEqual(merge(a, b), result);
});

it('should merge where keys exist and add where not', function () {
const a = {
client: {
entry: './client.js'
},
server: {
entry: './server.js'
}
};
const b = {
server: {
entry: './replaced.js'
},
test: {
entry: './test.js'
}
};
const result = [
{
entry: './client.js'
},
{
entry: './replaced.js'
},
{
entry: './test.js'
}
];

assert.deepEqual(merge(a, b), result);
});
}

module.exports = multipleTests;
9 changes: 9 additions & 0 deletions tests/test-merge-multiple.js
@@ -0,0 +1,9 @@
/* eslint-env mocha */
const webpackMerge = require('..');
const mergeMultipleTests = require('./merge-multiple-tests');

describe('Multiple merge', function () {
const merge = webpackMerge.multiple;

mergeMultipleTests(merge);
});

0 comments on commit 6bf19b6

Please sign in to comment.