Skip to content

Commit d7559d2

Browse files
authored
migrate output group (#1832)
* refactor: migrate output group * refactor: remove unused fn, simplify resolveOutput * chore: only assign filename when ext present
1 parent f7ec953 commit d7559d2

8 files changed

Lines changed: 46 additions & 87 deletions

File tree

packages/webpack-cli/__tests__/OutputGroup.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const resolveOutput = require('../lib/groups/resolveOutput');
2+
3+
describe('OutputGroup', function () {
4+
it('should handle the output option', () => {
5+
const result = resolveOutput({
6+
output: './bundle.js',
7+
});
8+
expect(result.options.output.filename).toEqual('bundle.js');
9+
});
10+
});

packages/webpack-cli/lib/groups/OutputGroup.js

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const path = require('path');
2+
3+
/**
4+
* Resolves the output flag
5+
* @param {args} args - Parsed arguments passed to the CLI
6+
*/
7+
const resolveOutput = (args) => {
8+
const { output } = args;
9+
const finalOptions = {
10+
options: { output: {} },
11+
outputOptions: {},
12+
};
13+
if (output) {
14+
const { dir, base, ext } = path.parse(output);
15+
finalOptions.options.output.path = ext.length === 0 ? path.resolve(dir, base) : path.resolve(dir);
16+
if (ext.length > 0) finalOptions.options.output.filename = base;
17+
}
18+
return finalOptions;
19+
};
20+
21+
module.exports = resolveOutput;

packages/webpack-cli/lib/utils/cli-flags.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ const cli = packageExists('webpack') ? require('webpack').cli : undefined;
33

44
const HELP_GROUP = 'help';
55
const BASIC_GROUP = 'basic';
6-
const OUTPUT_GROUP = 'output';
76
const ADVANCED_GROUP = 'advanced';
87

98
const groups = {
109
HELP_GROUP,
1110
BASIC_GROUP,
12-
OUTPUT_GROUP,
1311
ADVANCED_GROUP,
1412
};
1513

@@ -55,7 +53,6 @@ const commands = [
5553
{
5654
name: 'output',
5755
type: String,
58-
group: OUTPUT_GROUP,
5956
description: 'To get the output in specified format ( accept json or markdown )',
6057
},
6158
],
@@ -123,7 +120,6 @@ const core = [
123120
name: 'output',
124121
usage: '--output <path to output directory>',
125122
alias: 'o',
126-
group: OUTPUT_GROUP,
127123
type: String,
128124
description: 'Output location of the file generated by webpack e.g. ./dist/',
129125
link: 'https://webpack.js.org/concepts/#output',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const outputStrategy = {
2+
'output.filename': 'prepend',
3+
'output.path': 'prepend',
4+
};
5+
6+
module.exports = {
7+
outputStrategy,
8+
};

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ const GroupHelper = require('./utils/GroupHelper');
33
const handleConfigResolution = require('./groups/ConfigGroup');
44
const resolveMode = require('./groups/resolveMode');
55
const resolveStats = require('./groups/resolveStats');
6+
const resolveOutput = require('./groups/resolveOutput');
67
const { Compiler } = require('./utils/Compiler');
78
const { groups, core } = require('./utils/cli-flags');
89
const webpackMerge = require('webpack-merge');
910
const { toKebabCase } = require('./utils/helpers');
1011
const argParser = require('./utils/arg-parser');
12+
const { outputStrategy } = require('./utils/merge-strategies');
1113

1214
class WebpackCLI extends GroupHelper {
1315
constructor() {
1416
super();
1517
this.groupMap = new Map();
16-
this.groups = [];
1718
this.args = {};
1819
this.compilation = new Compiler();
1920
this.defaultEntry = 'index';
@@ -120,12 +121,6 @@ class WebpackCLI extends GroupHelper {
120121
this.helpGroup = new HelpGroup();
121122
break;
122123
}
123-
case groups.OUTPUT_GROUP: {
124-
const OutputGroup = require('./groups/OutputGroup');
125-
this.outputGroup = new OutputGroup(value);
126-
this.groups.push(this.outputGroup);
127-
break;
128-
}
129124
}
130125
}
131126
}
@@ -243,7 +238,7 @@ class WebpackCLI extends GroupHelper {
243238
.then(() => this._handleDefaultEntry())
244239
.then(() => this._handleConfig(parsedArgs))
245240
.then(() => this._baseResolver(resolveMode, parsedArgs, this.compilerConfiguration))
246-
.then(() => this._handleGroupHelper(this.outputGroup))
241+
.then(() => this._baseResolver(resolveOutput, parsedArgs, {}, outputStrategy))
247242
.then(() => this._handleCoreFlags())
248243
.then(() => this._handleGroupHelper(this.basicGroup))
249244
.then(() => this._handleGroupHelper(this.advancedGroup))

test/output/named-bundles/output-named-bundles.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ describe('output flag named bundles', () => {
4545
});
4646

4747
it('should output file in bin directory using default webpack config with warning for empty output value', () => {
48-
const { stderr } = run(__dirname, ['--output='], false);
49-
expect(stderr).toContain(
50-
"You provided an empty output value. Falling back to the output value of your webpack config file, or './dist/' if none was provided",
51-
);
52-
53-
const stats = statSync(resolve(__dirname, './bin/bundle.js'));
54-
expect(stats.isFile()).toBe(true);
48+
const { stdout, stderr, exitCode } = run(__dirname, ['--output'], false);
49+
expect(stderr).toEqual("error: option '-o, --output <value>' argument missing");
50+
expect(exitCode).toEqual(1);
51+
expect(stdout).toBeFalsy();
5552
});
5653
});

0 commit comments

Comments
 (0)