Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: defer setting default mode to core #2095

Merged
merged 4 commits into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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