Skip to content

Commit

Permalink
fix: --config-name behaviour for fuctional configs
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 1, 2020
1 parent f13346e commit 6284a55
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
@@ -1,13 +1,13 @@
const ConfigGroup = require('../../lib/groups/ConfigGroup');
const resolveConfig = require('../../lib/groups/resolveConfig.js');
const { resolve } = require('path');
const config1 = require('./webpack.config1.cjs');
const config2 = require('./webpack.config2.cjs');
const arrayConfig = require('./webpack.config.cjs');
const promiseConfig = require('./webpack.promise.config.cjs');

describe('ConfigGroup', function () {
describe('resolveConfig', function () {
it('should handle merge properly', async () => {
const result = await ConfigGroup({
const result = await resolveConfig({
merge: true,
config: [resolve(__dirname, './webpack.config.cjs')],
});
Expand All @@ -25,7 +25,7 @@ describe('ConfigGroup', function () {
});

it('should return array for multiple config', async () => {
const result = await ConfigGroup({
const result = await resolveConfig({
config: [resolve(__dirname, './webpack.config1.cjs'), resolve(__dirname, './webpack.config2.cjs')],
});
const expectedOptions = [config1, config2];
Expand All @@ -34,20 +34,20 @@ describe('ConfigGroup', function () {
});

it('should return config object for single config', async () => {
const result = await ConfigGroup({ config: [resolve(__dirname, './webpack.config1.cjs')] });
const result = await resolveConfig({ config: [resolve(__dirname, './webpack.config1.cjs')] });
expect(result.options).toEqual(config1);
expect(result.outputOptions).toEqual({});
});

it('should return resolved config object for promise config', async () => {
const result = await ConfigGroup({ config: [resolve(__dirname, './webpack.promise.config.cjs')] });
const result = await resolveConfig({ config: [resolve(__dirname, './webpack.promise.config.cjs')] });
const expectedOptions = await promiseConfig();
expect(result.options).toEqual(expectedOptions);
expect(result.outputOptions).toEqual({});
});

it('should handle configs returning different types', async () => {
const result = await ConfigGroup({
const result = await resolveConfig({
config: [resolve(__dirname, './webpack.promise.config.cjs'), resolve(__dirname, './webpack.config.cjs')],
});
const resolvedPromiseConfig = await promiseConfig();
Expand Down
Expand Up @@ -156,6 +156,8 @@ const finalize = async (moduleObj, args) => {
const isMultiCompilerMode = Array.isArray(config);
const rawConfigs = isMultiCompilerMode ? config : [config];

let isFunctionalConfig = false;

let configs = await Promise.all(
rawConfigs.map(async (rawConfig) => {
const isPromise = typeof rawConfig.then === 'function';
Expand All @@ -166,6 +168,7 @@ const finalize = async (moduleObj, args) => {

// `Promise` may return `Function`
if (typeof rawConfig === 'function') {
isFunctionalConfig = true;
// when config is a function, pass the env from args to the config function
rawConfig = await rawConfig(env, args);
}
Expand All @@ -176,8 +179,9 @@ const finalize = async (moduleObj, args) => {

if (configName) {
const foundConfigNames = [];
const configsToFilter = isFunctionalConfig ? configs[0] : configs;

configs = configs.filter((options) => {
configs = configsToFilter.filter((options) => {
const found = configName.includes(options.name);

if (found) {
Expand All @@ -204,7 +208,7 @@ const finalize = async (moduleObj, args) => {
process.exit(2);
}

newOptionsObject['options'] = isMultiCompilerMode ? configs : configs[0];
newOptionsObject['options'] = isMultiCompilerMode || isFunctionalConfig ? configs : configs[0];

return newOptionsObject;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -13,7 +13,7 @@ const { options: coloretteOptions } = require('colorette');
const WebpackCLIPlugin = require('./plugins/WebpackCLIPlugin');

// CLI arg resolvers
const handleConfigResolution = require('./groups/ConfigGroup');
const handleConfigResolution = require('./groups/resolveConfig');
const resolveMode = require('./groups/resolveMode');
const resolveStats = require('./groups/resolveStats');
const resolveOutput = require('./groups/resolveOutput');
Expand Down
47 changes: 47 additions & 0 deletions test/config-name/config-name.test.js
Expand Up @@ -81,4 +81,51 @@ describe('--config-name flag', () => {
expect(stdout).toBeFalsy();
expect(exitCode).toBe(2);
});

it('should work with config as a function', (done) => {
const { stderr, stdout, exitCode } = run(__dirname, ['--config', 'function-config.js', '--config-name', 'first'], false);
expect(stderr).toBeFalsy();
expect(stdout).toContain('first');
expect(stdout).not.toContain('second');
expect(stdout).not.toContain('third');
expect(exitCode).toBe(0);

stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should work with multiple values for --config-name when the config is a function', (done) => {
const { stderr, stdout, exitCode } = run(
__dirname,
['--config', 'function-config.js', '--config-name', 'first', '--config-name', 'third'],
false,
);
expect(stderr).toBeFalsy();
expect(stdout).toContain('first');
expect(stdout).not.toContain('second');
expect(stdout).toContain('third');
expect(exitCode).toBe(0);

stat(resolve(__dirname, './dist/dist-third.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);

stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});

it('should log error if invalid config name is provided ', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--config', 'function-config.js', '--config-name', 'test'], false);

expect(stderr).toContain('Configuration with name "test" was not found.');
expect(stdout).toBeFalsy();
expect(exitCode).toBe(2);
});
});
29 changes: 29 additions & 0 deletions test/config-name/function-config.js
@@ -0,0 +1,29 @@
module.exports = () => [
{
output: {
filename: './dist-first.js',
},
name: 'first',
entry: './src/first.js',
mode: 'development',
stats: 'minimal',
},
{
output: {
filename: './dist-second.js',
},
name: 'second',
entry: './src/second.js',
mode: 'production',
stats: 'minimal',
},
{
output: {
filename: './dist-third.js',
},
name: 'third',
entry: './src/third.js',
mode: 'none',
stats: 'minimal',
},
];

0 comments on commit 6284a55

Please sign in to comment.