Skip to content

Commit 4ca25bc

Browse files
authored
fix: 🐛 do not apply own defaults while setting mode (#1565)
1 parent 16c55f3 commit 4ca25bc

File tree

11 files changed

+162
-79
lines changed

11 files changed

+162
-79
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const ZeroConfigGroup = require('../lib/groups/ZeroConfigGroup');
22

3-
describe('GroupHelper', function () {
4-
it('should load the dev zero config', () => {
3+
describe('ZeroConfigGroup', function () {
4+
it('should load the dev zero config', () => {
55
const group = new ZeroConfigGroup([
66
{
77
dev: true,
@@ -27,8 +27,9 @@ describe('GroupHelper', function () {
2727
mode: 'production',
2828
},
2929
]);
30-
3130
const result = group.run();
31+
// ensure no other properties are added
32+
expect(result.options).toMatchObject({ mode: 'production' });
3233
expect(result.options.mode).toEqual('production');
3334
});
3435

@@ -40,6 +41,8 @@ describe('GroupHelper', function () {
4041
]);
4142

4243
const result = group.run();
44+
// ensure no other properties are added
45+
expect(result.options).toMatchObject({ mode: 'development' });
4346
expect(result.options.mode).toEqual('development');
4447
});
4548

@@ -51,6 +54,8 @@ describe('GroupHelper', function () {
5154
]);
5255

5356
const result = group.run();
57+
// ensure no other properties are added
58+
expect(result.options).toMatchObject({ mode: 'none' });
5459
expect(result.options.mode).toEqual('none');
5560
});
5661
});

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ZeroConfigGroup extends GroupHelper {
1616
* It determines the mode to pass to webpack compiler
1717
* @returns {string} The mode
1818
*/
19-
getEnvFromOptionsAndMode() {
19+
resolveMode() {
2020
if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) {
2121
return process.env.NODE_ENV;
2222
} else {
@@ -44,28 +44,8 @@ class ZeroConfigGroup extends GroupHelper {
4444
}
4545
}
4646

47-
resolveZeroConfig() {
48-
const defaultConfigType = this.getEnvFromOptionsAndMode();
49-
let defaultConfig;
50-
if (defaultConfigType === PRODUCTION) {
51-
defaultConfig = require('../utils/production-config')();
52-
} else if (defaultConfigType === DEVELOPMENT) {
53-
defaultConfig = require('../utils/development-config')();
54-
} else {
55-
defaultConfig = require('../utils/none-config')();
56-
}
57-
58-
const isEntryObject = defaultConfig.entry && defaultConfig.entry instanceof Object;
59-
const isOutputDefined = defaultConfig.output && defaultConfig.output.filename;
60-
const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'main.js';
61-
if (isConflictingOutput) {
62-
defaultConfig.output.filename = '[name].main.js';
63-
}
64-
this.opts.options = defaultConfig;
65-
}
66-
6747
run() {
68-
this.resolveZeroConfig();
48+
this.opts.options.mode = this.resolveMode();
6949
return this.opts;
7050
}
7151
}

packages/webpack-cli/lib/utils/development-config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/webpack-cli/lib/utils/none-config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/webpack-cli/lib/utils/production-config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/prepareSuite.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ function extractFolder(folderToRead, folders = []) {
3333
return folders;
3434
}
3535

36-
{
37-
Promise.all(
38-
collectTestingFoldersWithPackage().map(async (folder) => {
39-
return execa('yarn', {
36+
(async () => {
37+
try {
38+
const folders = collectTestingFoldersWithPackage();
39+
for (const folder of folders) {
40+
await execa('yarn', {
4041
cwd: folder,
4142
stdio: 'inherit',
4243
});
43-
}),
44-
)
45-
.then(() => {
46-
console.log(chalk.inverse.green(' Successfully prepared the test suite '));
47-
})
48-
.catch((e) => {
49-
console.error(chalk.inverse.red(' Unable to prepare the test suite '));
50-
console.error(e.stack);
51-
process.exitCode = 1;
52-
});
53-
}
44+
}
45+
console.log(chalk.inverse.green(' Successfully prepared the test suite '));
46+
} catch (e) {
47+
console.error(chalk.inverse.red(' Unable to prepare the test suite '));
48+
console.error(e.stack);
49+
process.exitCode = 1;
50+
}
51+
})();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
yarn.lock
2+
package-lock.json
3+
node_modules/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require("react")
2+
console.log("Ichigo")
3+
if (process.env.NODE_ENV === "production") {
4+
console.log("production mode")
5+
} else if (process.env.NODE_ENV === "development") {
6+
console.log(console.log("development mode"))
7+
} else {
8+
console.log(console.log("none mode"))
9+
}
10+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
const { stat, readFile } = require('fs');
3+
const { resolve } = require('path');
4+
// eslint-disable-next-line node/no-unpublished-require
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe('mode flags with config', () => {
8+
it('should run in production mode when --mode=production is passed', (done) => {
9+
const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--config', './webpack.config.js']);
10+
expect(stderr).toBeFalsy();
11+
expect(stdout).toBeTruthy();
12+
13+
// Should generate the appropriate files
14+
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
15+
expect(err).toBe(null);
16+
expect(stats.isFile()).toBe(true);
17+
});
18+
19+
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
20+
expect(err).toBe(null);
21+
expect(stats.isFile()).toBe(true);
22+
});
23+
24+
// Should not generate source maps when not specified
25+
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
26+
expect(err).toBeTruthy();
27+
});
28+
29+
// Correct mode should be propagated to the compiler
30+
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
31+
expect(err).toBe(null);
32+
expect(data).toContain('"production mode"');
33+
done();
34+
});
35+
});
36+
37+
it('should run in development mode when --mode=development is passed', (done) => {
38+
const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--config', './webpack.config.js']);
39+
expect(stderr).toBeFalsy();
40+
expect(stdout).toBeTruthy();
41+
42+
// Should generate the appropriate files
43+
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
44+
expect(err).toBe(null);
45+
expect(stats.isFile()).toBe(true);
46+
});
47+
48+
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
49+
expect(err).toBe(null);
50+
expect(stats.isFile()).toBe(true);
51+
});
52+
53+
// Should not generate source maps when not specified
54+
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
55+
expect(err).toBeTruthy();
56+
});
57+
58+
// Correct mode should be propagated to the compiler
59+
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
60+
expect(err).toBe(null);
61+
expect(data).toContain('development mode');
62+
done();
63+
});
64+
});
65+
66+
it('should run in none mode when --mode=none is passed', (done) => {
67+
const { stderr, stdout } = run(__dirname, ['--mode', 'none', '--config', './webpack.config.js']);
68+
expect(stderr).toBeFalsy();
69+
expect(stdout).toBeTruthy();
70+
71+
// Should generate the appropriate files
72+
stat(resolve(__dirname, './bin/main.js.OTHER.LICENSE.txt'), (err, stats) => {
73+
expect(err).toBe(null);
74+
expect(stats.isFile()).toBe(true);
75+
});
76+
77+
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
78+
expect(err).toBe(null);
79+
expect(stats.isFile()).toBe(true);
80+
});
81+
82+
// Should not generate source maps when not specified
83+
stat(resolve(__dirname, './bin/main.js.map'), (err) => {
84+
expect(err).toBeTruthy();
85+
});
86+
87+
// Correct mode should be propagated to the compiler
88+
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
89+
expect(err).toBe(null);
90+
expect(data).toContain('none mode');
91+
done();
92+
});
93+
});
94+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"terser-webpack-plugin": "^3.0.1",
4+
"react": "^16.13.0"
5+
}
6+
}

0 commit comments

Comments
 (0)