Skip to content

Commit 3eb410e

Browse files
authored
fix: defer setting default mode to core (#2095)
1 parent 2d6e5c6 commit 3eb410e

14 files changed

+29
-13
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ class WebpackCLI {
112112
finalMode = configMode;
113113
} else if (NODE_ENV && (NODE_ENV === PRODUCTION || NODE_ENV === DEVELOPMENT)) {
114114
finalMode = NODE_ENV;
115-
} else {
116-
finalMode = PRODUCTION;
117115
}
118116

119117
return finalMode;
@@ -208,10 +206,16 @@ class WebpackCLI {
208206
// Todo - handle multi config for all flags
209207
finalOptions.options = configOptions.map(() => ({ ...finalOptions.options }));
210208
configOptions.forEach((configObject, index) => {
211-
finalOptions.options[index].mode = assignMode(mode, configObject);
209+
const resolvedMode = assignMode(mode, configObject);
210+
if (resolvedMode) {
211+
finalOptions.options[index].mode = resolvedMode;
212+
}
212213
});
213214
} else {
214-
finalOptions.options.mode = assignMode(mode, configOptions);
215+
const resolvedMode = assignMode(mode, configOptions);
216+
if (resolvedMode) {
217+
finalOptions.options.mode = resolvedMode;
218+
}
215219
}
216220

217221
return finalOptions;

test/build-warnings/warnings.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('warnings', () => {
2121
const json = JSON.parse(stdout);
2222

2323
expect(json['hash']).toBeDefined();
24-
expect(json['warnings']).toHaveLength(1);
24+
expect(json['warnings']).toHaveLength(2);
2525
// `message` for `webpack@5`
2626
expect(json['warnings'][0].message ? json['warnings'][0].message : json['warnings'][0]).toMatch(/Can't resolve/);
2727
});
@@ -43,7 +43,7 @@ describe('warnings', () => {
4343
const json = JSON.parse(data);
4444

4545
expect(json['hash']).toBeDefined();
46-
expect(json['warnings']).toHaveLength(1);
46+
expect(json['warnings']).toHaveLength(2);
4747
// `message` for `webpack@5`
4848
expect(json['warnings'][0].message ? json['warnings'][0].message : json['warnings'][0]).toMatch(/Can't resolve/);
4949

test/colors/colors-false.webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module.exports = {
22
stats: {
33
colors: false,
44
},
5+
mode: 'production',
56
};

test/colors/colors-true.webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module.exports = {
22
stats: {
33
colors: true,
44
},
5+
mode: 'production',
56
};

test/colors/multiple-configs.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ module.exports = [
33
name: 'first-config',
44
entry: './src/first.js',
55
stats: 'normal',
6+
mode: 'production',
67
},
78
{
89
name: 'second-config',
910
entry: './src/second.js',
1011
stats: 'normal',
12+
mode: 'production',
1113
},
1214
];
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
name: 'test',
3+
mode: 'production',
34
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
stats: true,
3+
mode: 'production',
34
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
stats: 'verbose',
3+
mode: 'production',
34
};

test/colors/webpack.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mode: 'production',
3+
};

test/defaults/output-defaults.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe('output flag defaults', () => {
99

1010
expect(stderr).toBeFalsy();
1111
expect(exitCode).toBe(0);
12-
// Should not print warning about config fallback, as we have production as default
13-
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
12+
// Should print warning about config fallback
13+
expect(stdout).toContain('option has not been set, webpack will fallback to');
1414
stat(resolve(__dirname, './binary/main.js'), (err, stats) => {
1515
expect(err).toBe(null);
1616
expect(stats.isFile()).toBe(true);

test/merge/config/merge-config.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { run } = require('../../utils/test-utils');
88
describe('merge flag configuration', () => {
99
it('merges two configurations together', () => {
1010
const { stdout, stderr, exitCode } = run(__dirname, ['--config', './1.js', '-c', './2.js', '--merge'], false);
11-
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
11+
expect(stdout).toContain('option has not been set, webpack will fallback to');
1212
expect(existsSync(resolve(__dirname, './dist/merged.js'))).toBeTruthy();
1313
expect(stderr).toBeFalsy();
1414
expect(exitCode).toBe(0);

test/mode/mode-single-arg/mode-single-arg.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
const { run } = require('../../utils/test-utils');
33

44
describe('mode flags', () => {
5-
it('should set mode=production by default', () => {
5+
it('should not set mode=production by default', () => {
66
const { stderr, stdout, exitCode } = run(__dirname);
77

88
expect(exitCode).toBe(0);
99
expect(stderr).toBeFalsy();
10-
expect(stdout).toContain(`mode: 'production'`);
10+
expect(stdout).not.toContain(`mode: 'production'`);
11+
expect(stdout).toContain(`The 'mode' option has not been set, webpack will fallback to 'production' for this value.`);
1112
});
1213

1314
it('should load a development config when --mode=development is passed', () => {

test/stats/watch/webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
watch: true,
33
stats: 'none',
4+
mode: 'production',
45
};

test/watch/watch-flag.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
1010

1111
describe('--watch flag', () => {
1212
it('should recompile upon file change', (done) => {
13-
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
13+
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'production'], false, '', true);
1414
let semaphore = 0;
1515
proc.stdout.on('data', (chunk) => {
1616
const data = stripAnsi(chunk.toString());
@@ -45,7 +45,7 @@ describe('--watch flag', () => {
4545
});
4646

4747
it('should print compilation lifecycle', (done) => {
48-
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
48+
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'production'], false, '', true);
4949
let semaphore = 0;
5050
proc.stdout.on('data', (chunk) => {
5151
const data = stripAnsi(chunk.toString());

0 commit comments

Comments
 (0)