Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for merging multiple configurations (#1768)
- Loading branch information
Showing
8 changed files
with
81 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/webpack-cli/__tests__/ConfigGroup/ConfigGroup.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const ConfigGroup = require('../../lib/groups/ConfigGroup'); | ||
const { resolve } = require('path'); | ||
|
||
describe('ConfigGroup', function () { | ||
it('should handle merge properly', async () => { | ||
const group = new ConfigGroup([ | ||
{ | ||
merge: true, | ||
}, | ||
{ | ||
config: [resolve(__dirname, './webpack.config.cjs')], | ||
}, | ||
]); | ||
|
||
const result = await group.run(); | ||
const expectedOptions = { | ||
output: { filename: './dist-commonjs.js', libraryTarget: 'commonjs' }, | ||
entry: './a.js', | ||
name: 'amd', | ||
mode: 'production', | ||
devtool: 'eval-cheap-module-source-map', | ||
target: 'node', | ||
}; | ||
expect(result.options).toEqual(expectedOptions); | ||
expect(result.outputOptions).toEqual({}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/webpack-cli/__tests__/ConfigGroup/webpack.config.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = [ | ||
{ | ||
output: { | ||
filename: './dist-amd.js', | ||
libraryTarget: 'amd', | ||
}, | ||
entry: './a.js', | ||
name: 'amd', | ||
mode: 'development', | ||
devtool: 'eval-cheap-module-source-map', | ||
}, | ||
{ | ||
output: { | ||
filename: './dist-commonjs.js', | ||
libraryTarget: 'commonjs', | ||
}, | ||
entry: './a.js', | ||
mode: 'production', | ||
target: 'node', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
'use strict'; | ||
|
||
const { stat } = require('fs'); | ||
const { existsSync } = require('fs'); | ||
const { resolve } = require('path'); | ||
|
||
const { run } = require('../../utils/test-utils'); | ||
|
||
describe('merge flag configuration', () => { | ||
it('merges two configurations together', (done) => { | ||
const { stdout } = run(__dirname, ['--config', './1.js', '--merge', './2.js'], false); | ||
it('merges two configurations together', () => { | ||
const { stdout, stderr, exitCode } = run(__dirname, ['--config', './1.js', '-c', './2.js', '--merge'], false); | ||
expect(stdout).not.toContain('option has not been set, webpack will fallback to'); | ||
stat(resolve(__dirname, './dist/merged.js'), (err, stats) => { | ||
expect(err).toBe(null); | ||
expect(stats.isFile()).toBe(true); | ||
done(); | ||
}); | ||
expect(existsSync(resolve(__dirname, './dist/merged.js'))).toBeTruthy(); | ||
expect(stderr).toBeFalsy(); | ||
expect(exitCode).toBe(0); | ||
}); | ||
it('merges two configurations together with flag alias', (done) => { | ||
const { stdout } = run(__dirname, ['--config', './1.js', '-m', './2.js'], false); | ||
it('merges two configurations together with flag alias', () => { | ||
const { stdout, stderr, exitCode } = run(__dirname, ['--config', './1.js', '--config', './2.js', '-m'], false); | ||
expect(stdout).toContain('merged.js'); | ||
stat(resolve(__dirname, './dist/merged.js'), (err, stats) => { | ||
expect(err).toBe(null); | ||
expect(stats.isFile()).toBe(true); | ||
done(); | ||
}); | ||
expect(existsSync(resolve(__dirname, './dist/merged.js'))).toBeTruthy(); | ||
expect(stderr).toBeFalsy(); | ||
expect(exitCode).toBe(0); | ||
}); | ||
it('fails when there are less than 2 configurations to merge', () => { | ||
const { stdout, stderr, exitCode } = run(__dirname, ['--config', './1.js', '-m'], false); | ||
expect(stderr).toContain(`MergeError: Atleast two configurations are required for merge.`); | ||
expect(stdout).toBeFalsy(); | ||
expect(exitCode).toBe(2); | ||
}); | ||
}); |