Skip to content

Commit 2f36b9d

Browse files
authored
fix: only set output path on passing flag (#1855)
* fix: only set output path on passing flag
1 parent ef14a7c commit 2f36b9d

File tree

16 files changed

+36
-49
lines changed

16 files changed

+36
-49
lines changed

packages/webpack-cli/__tests__/arg-parser.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe('arg-parser', () => {
368368
expect(res.unknownArgs.length).toEqual(0);
369369
expect(res.opts.entry).toEqual(['test.js']);
370370
expect(res.opts.hot).toBeTruthy();
371-
expect(res.opts.output).toEqual('./dist/');
371+
expect(res.opts.outputPath).toEqual('./dist/');
372372
expect(res.opts.stats).toEqual(true);
373373
expect(warnMock.mock.calls.length).toEqual(0);
374374
});
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
const { resolve } = require('path');
12
const resolveOutput = require('../lib/groups/resolveOutput');
23

34
describe('OutputGroup', function () {
45
it('should handle the output option', () => {
56
const result = resolveOutput({
6-
output: './bundle.js',
7+
outputPath: './bundle',
78
});
8-
expect(result.options.output.filename).toEqual('bundle.js');
9+
expect(result.options.output.path).toEqual(resolve('bundle'));
910
});
1011
});

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ const path = require('path');
55
* @param {args} args - Parsed arguments passed to the CLI
66
*/
77
const resolveOutput = (args) => {
8-
const { output } = args;
8+
const { outputPath } = args;
99
const finalOptions = {
1010
options: { output: {} },
1111
outputOptions: {},
1212
};
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;
13+
if (outputPath) {
14+
finalOptions.options.output.path = path.resolve(outputPath);
1715
}
1816
return finalOptions;
1917
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ const core = [
117117
description: 'Outputs list of supported flags',
118118
},
119119
{
120-
name: 'output',
121-
usage: '--output <path to output directory>',
120+
name: 'output-path',
121+
usage: '--output-path <path to output directory>',
122122
alias: 'o',
123123
type: String,
124124
description: 'Output location of the file generated by webpack e.g. ./dist/',
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
'use strict';
2-
const { stat } = require('fs');
2+
const { existsSync } = require('fs');
33
const { resolve } = require('path');
44
const { run } = require('../../utils/test-utils');
55

66
describe('basic config file', () => {
7-
it('is able to understand and parse a very basic configuration file', (done) => {
8-
const { stdout, stderr } = run(
9-
__dirname,
10-
['-c', resolve(__dirname, 'webpack.config.js'), '--output', './binary/a.bundle.js'],
11-
false,
12-
);
7+
it('is able to understand and parse a very basic configuration file', () => {
8+
const { stdout, stderr } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js'), '--output-path', './binary'], false);
139
expect(stderr).toBeFalsy();
14-
expect(stdout).not.toBe(undefined);
15-
stat(resolve(__dirname, './binary/a.bundle.js'), (err, stats) => {
16-
expect(err).toBe(null);
17-
expect(stats.isFile()).toBe(true);
18-
done();
19-
});
10+
expect(stdout).toBeTruthy();
11+
expect(existsSync(resolve(__dirname, './binary/a.bundle.js'))).toBeTruthy();
2012
});
2113
});

test/defaults/output-defaults.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { run } = require('../utils/test-utils');
55

66
describe('output flag defaults', () => {
77
it('should create default file for a given directory', (done) => {
8-
const { stdout } = run(__dirname, ['--entry', './a.js', '--output', './binary'], false);
8+
const { stdout } = run(__dirname, ['--entry', './a.js', '--output-path', './binary'], false);
99
// Should not print warning about config fallback, as we have production as default
1010
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
1111
stat(resolve(__dirname, './binary/main.js'), (err, stats) => {
@@ -26,7 +26,7 @@ describe('output flag defaults', () => {
2626
});
2727

2828
it('throw error on empty output flag', () => {
29-
const { stderr } = run(__dirname, ['--entry', './a.js', '--output'], false);
30-
expect(stderr).toContain("error: option '-o, --output <value>' argument missing");
29+
const { stderr } = run(__dirname, ['--entry', './a.js', '--output-path'], false);
30+
expect(stderr).toContain("error: option '-o, --output-path <value>' argument missing");
3131
});
3232
});

test/devtool/array/source-map-array.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('source-map object', () => {
1414
});
1515
});
1616
it('should override entire array on flag', (done) => {
17-
const { stderr } = run(__dirname, ['--devtool', 'source-map', '--output', './binary'], false);
17+
const { stderr } = run(__dirname, ['--devtool', 'source-map', '--output-path', './binary'], false);
1818
expect(stderr).toBe('');
1919
readdir(resolve(__dirname, 'binary'), (err, files) => {
2020
expect(err).toBe(null);

test/devtool/object/source-map-object.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('source-map object', () => {
2424
});
2525

2626
it('should override config with source-map', (done) => {
27-
run(__dirname, ['-c', './webpack.eval.config.js', '--devtool', 'source-map', '-o', './binary/dist-amd.js'], false);
27+
run(__dirname, ['-c', './webpack.eval.config.js', '--devtool', 'source-map', '-o', './binary'], false);
2828
stat(resolve(__dirname, 'binary/dist-amd.js.map'), (err, stats) => {
2929
expect(err).toBe(null);
3030
expect(stats.isFile()).toBe(true);

test/entry/defaults-index/entry-multi-args.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('single entry flag index present', () => {
1818
});
1919

2020
it('finds default index file, compiles and overrides with flags successfully', (done) => {
21-
const { stderr } = run(__dirname, ['--output', 'bin/main.js']);
21+
const { stderr } = run(__dirname, ['--output-path', 'bin']);
2222
expect(stderr).toBeFalsy();
2323

2424
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {

test/node/node.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { run } = require('../utils/test-utils');
88
// throws different error from what we manually see
99
describe('node flags', () => {
1010
it('is able to pass the options flags to node js', async (done) => {
11-
const { stdout, stderr } = await run(__dirname, ['--output', './bin/[name].bundle.js'], false, [
11+
const { stdout, stderr } = await run(__dirname, ['--output-path', './bin'], false, [
1212
`--require=${resolve(__dirname, 'bootstrap.js')}`,
1313
`--require=${resolve(__dirname, 'bootstrap2.js')}`,
1414
]);

0 commit comments

Comments
 (0)