Skip to content

Commit

Permalink
fix: defer setting default mode to core (#2095)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Nov 11, 2020
1 parent 2d6e5c6 commit 3eb410e
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -112,8 +112,6 @@ class WebpackCLI {
finalMode = configMode;
} else if (NODE_ENV && (NODE_ENV === PRODUCTION || NODE_ENV === DEVELOPMENT)) {
finalMode = NODE_ENV;
} else {
finalMode = PRODUCTION;
}

return finalMode;
Expand Down Expand Up @@ -208,10 +206,16 @@ class WebpackCLI {
// Todo - handle multi config for all flags
finalOptions.options = configOptions.map(() => ({ ...finalOptions.options }));
configOptions.forEach((configObject, index) => {
finalOptions.options[index].mode = assignMode(mode, configObject);
const resolvedMode = assignMode(mode, configObject);
if (resolvedMode) {
finalOptions.options[index].mode = resolvedMode;
}
});
} else {
finalOptions.options.mode = assignMode(mode, configOptions);
const resolvedMode = assignMode(mode, configOptions);
if (resolvedMode) {
finalOptions.options.mode = resolvedMode;
}
}

return finalOptions;
Expand Down
4 changes: 2 additions & 2 deletions test/build-warnings/warnings.test.js
Expand Up @@ -21,7 +21,7 @@ describe('warnings', () => {
const json = JSON.parse(stdout);

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

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

Expand Down
1 change: 1 addition & 0 deletions test/colors/colors-false.webpack.config.js
Expand Up @@ -2,4 +2,5 @@ module.exports = {
stats: {
colors: false,
},
mode: 'production',
};
1 change: 1 addition & 0 deletions test/colors/colors-true.webpack.config.js
Expand Up @@ -2,4 +2,5 @@ module.exports = {
stats: {
colors: true,
},
mode: 'production',
};
2 changes: 2 additions & 0 deletions test/colors/multiple-configs.js
Expand Up @@ -3,10 +3,12 @@ module.exports = [
name: 'first-config',
entry: './src/first.js',
stats: 'normal',
mode: 'production',
},
{
name: 'second-config',
entry: './src/second.js',
stats: 'normal',
mode: 'production',
},
];
1 change: 1 addition & 0 deletions test/colors/no-stats.webpack.config.js
@@ -1,3 +1,4 @@
module.exports = {
name: 'test',
mode: 'production',
};
1 change: 1 addition & 0 deletions test/colors/stats-boolean.webpack.config.js
@@ -1,3 +1,4 @@
module.exports = {
stats: true,
mode: 'production',
};
1 change: 1 addition & 0 deletions test/colors/stats-string.webpack.config.js
@@ -1,3 +1,4 @@
module.exports = {
stats: 'verbose',
mode: 'production',
};
3 changes: 3 additions & 0 deletions test/colors/webpack.config.js
@@ -0,0 +1,3 @@
module.exports = {
mode: 'production',
};
4 changes: 2 additions & 2 deletions test/defaults/output-defaults.test.js
Expand Up @@ -9,8 +9,8 @@ describe('output flag defaults', () => {

expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
// Should not print warning about config fallback, as we have production as default
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
// Should print warning about config fallback
expect(stdout).toContain('option has not been set, webpack will fallback to');
stat(resolve(__dirname, './binary/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion test/merge/config/merge-config.test.js
Expand Up @@ -8,7 +8,7 @@ const { run } = require('../../utils/test-utils');
describe('merge flag configuration', () => {
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');
expect(stdout).toContain('option has not been set, webpack will fallback to');
expect(existsSync(resolve(__dirname, './dist/merged.js'))).toBeTruthy();
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
Expand Down
5 changes: 3 additions & 2 deletions test/mode/mode-single-arg/mode-single-arg.test.js
Expand Up @@ -2,12 +2,13 @@
const { run } = require('../../utils/test-utils');

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

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

it('should load a development config when --mode=development is passed', () => {
Expand Down
1 change: 1 addition & 0 deletions test/stats/watch/webpack.config.js
@@ -1,4 +1,5 @@
module.exports = {
watch: true,
stats: 'none',
mode: 'production',
};
4 changes: 2 additions & 2 deletions test/watch/watch-flag.test.js
Expand Up @@ -10,7 +10,7 @@ const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];

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

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

0 comments on commit 3eb410e

Please sign in to comment.