Skip to content

Commit 5e9639f

Browse files
chore(cli): better group handling (#1204)
* chore: refactored how configuration is created * chore(cli): refactor how the CLI handles configuration and cli arguments * chore(cli): added devltool inside the zero configs * fix: update lib/groups/StatsGroup.js Co-Authored-By: Rishabh Chawla <rishabh31121999@gmail.com> * fix: update lib/groups/ZeroConfigGroup.js Co-Authored-By: Rishabh Chawla <rishabh31121999@gmail.com> * fix: update lib/groups/ZeroConfigGroup.js Co-Authored-By: Rishabh Chawla <rishabh31121999@gmail.com> * chore(cli): applied suggestions Co-Authored-By: Rishabh Chawla <rishabh31121999@gmail.com> * chore(cli): post code review * chore(cli): grammar and code styling Co-authored-by: Rishabh Chawla <rishabh31121999@gmail.com>
1 parent 861e9f8 commit 5e9639f

26 files changed

+391
-142
lines changed

.prettierrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2-
"singleQuote": true,
3-
"trailingComma": "all"
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 140
45
}

bin/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const version = packageJson.engines.node;
3636

3737
if (!semver.satisfies(process.version, version)) {
3838
const rawVersion = version.replace(/[^\d\.]*/, '');
39-
logger.error('webpack CLI requires at least Node v' + rawVersion + '. ' + 'You have ' + process.version + '.\n' + 'See https://webpack.js.org/ ' + 'for migration help and similar.');
39+
logger.error(`webpack CLI requires at least Node v ${rawVersion}. You have ${process.version}.`);
40+
logger.error('See https://webpack.js.org/ for migration help and similar.');
4041
process.exit(1);
4142
}
4243

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const GroupHelper = require('../utils/group-helper');
1+
const GroupHelper = require('../utils/GroupHelper');
22
const logger = require('../utils/logger');
33

44
class AdvancedGroup extends GroupHelper {
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
const GroupHelper = require('../utils/group-helper');
1+
const GroupHelper = require('../utils/GroupHelper');
2+
const { core, groups } = require('../utils/cli-flags');
23

34
class BasicGroup extends GroupHelper {
45
constructor(options) {
56
super(options);
6-
this.WEBPACK_OPTION_FLAGS = ['prod', 'dev', 'watch', 'w', 'prod', 'p', 'interactive', 'i', 'defaults', 'progress'];
7+
this.WEBPACK_OPTION_FLAGS = core
8+
.filter(coreFlag => {
9+
return coreFlag.group === groups.BASIC_GROUP;
10+
})
11+
.reduce((result, flagObject) => {
12+
result.push(flagObject.name);
13+
if (flagObject.alias) {
14+
result.push(flagObject.alias);
15+
}
16+
return result;
17+
}, []);
718
}
819
resolveFlags() {
920
const { args } = this;
@@ -16,11 +27,11 @@ class BasicGroup extends GroupHelper {
1627
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
1728
// this.opts.options.plugins = [new BundleAnalyzerPlugin()];
1829
// }
19-
if (arg == 'sourcemap') {
30+
if (arg === 'sourcemap') {
2031
this.opts.options.devtool = args[arg] || 'eval';
2132
this.opts.outputOptions.devtool = args[arg];
2233
}
23-
if (arg == 'entry') {
34+
if (arg === 'entry') {
2435
this.opts.options[arg] = this.resolveFilePath(args[arg], 'index.js');
2536
}
2637
});
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ const { existsSync } = require('fs');
22
const { resolve, sep, dirname, parse } = require('path');
33
const { extensions } = require('interpret');
44

5-
const GroupHelper = require('../utils/group-helper');
6-
7-
const DEFAULT_CONFIG_LOC = ['.webpack/webpack.config', '.webpack/webpack.config.dev', '.webpack/webpack.config.prod', '.webpack/webpackfile', 'webpack.config'];
5+
const GroupHelper = require('../utils/GroupHelper');
6+
7+
const DEFAULT_CONFIG_LOC = [
8+
'.webpack/webpack.config',
9+
'.webpack/webpack.config.dev',
10+
'.webpack/webpack.config.prod',
11+
'.webpack/webpackfile',
12+
'webpack.config',
13+
];
814

915
const getDefaultConfigFiles = () => {
1016
return DEFAULT_CONFIG_LOC.map(filename => {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const path = require('path');
2-
const GroupHelper = require('../utils/group-helper');
3-
4-
const DEFAULT_FILENAME = 'main.js';
2+
const GroupHelper = require('../utils/GroupHelper');
53

64
class OutputGroup extends GroupHelper {
75
constructor(options) {
@@ -11,11 +9,16 @@ class OutputGroup extends GroupHelper {
119
output: {},
1210
},
1311
};
12+
13+
this.strategy = {
14+
'output.filename': 'prepend',
15+
'output.path': 'prepend',
16+
};
1417
}
1518

1619
parseDirectory(metaData) {
1720
return {
18-
filename: DEFAULT_FILENAME,
21+
// filename: DEFAULT_FILENAME,
1922
path: path.resolve(metaData.dir, metaData.name),
2023
};
2124
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const GroupHelper = require('../utils/group-helper');
1+
const GroupHelper = require('../utils/GroupHelper');
22

3+
/**
4+
* StatsGroup gathers information about the stats options
5+
*/
36
class StatsGroup extends GroupHelper {
47
constructor(options) {
58
super(options);

lib/groups/ZeroConfigGroup.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const GroupHelper = require('../utils/GroupHelper');
2+
3+
/**
4+
* ZeroConfigGroup creates a zero configuration based on the environment
5+
*/
6+
class ZeroConfigGroup extends GroupHelper {
7+
constructor(options) {
8+
super(options);
9+
}
10+
11+
/**
12+
* It determines the mode to pass to webpack compiler
13+
* @returns {string} The mode
14+
*/
15+
getEnvFromOptionsAndMode() {
16+
if (process.env.NODE_ENV && (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'development')) {
17+
return process.env.NODE_ENV;
18+
} else if (this.args.prod) {
19+
return 'production';
20+
} else if (this.args.dev) {
21+
return 'development';
22+
}
23+
return 'production';
24+
}
25+
26+
resolveZeroConfig() {
27+
const defaultConfigType = this.getEnvFromOptionsAndMode();
28+
const defaultConfig = require(`./${defaultConfigType}-config`)();
29+
30+
const isEntryObject = defaultConfig.entry && defaultConfig.entry instanceof Object;
31+
const isOutputDefined = defaultConfig.output && defaultConfig.output.filename;
32+
const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'bundle.js';
33+
if (isConflictingOutput) {
34+
defaultConfig.output.filename = '[name].bundle.js';
35+
}
36+
this.opts.options = defaultConfig;
37+
}
38+
39+
run() {
40+
this.resolveZeroConfig();
41+
return this.opts;
42+
}
43+
}
44+
45+
module.exports = ZeroConfigGroup;
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class GroupHelper {
99
options: {},
1010
processingMessageBuffer: [],
1111
};
12+
this.strategy = undefined;
1213
}
1314

1415
hyphenToUpperCase(name) {
@@ -24,14 +25,11 @@ class GroupHelper {
2425
if (!arr) {
2526
return;
2627
}
27-
const result = {};
28-
const arrLength = arr.length;
29-
for (let i = 0; i < arrLength; i++) {
30-
const key = Object.keys(arr[i])[0];
31-
const val = arr[i][key];
32-
result[this.hyphenToUpperCase(key)] = val;
33-
}
34-
return result;
28+
return arr.reduce((result, currentItem, index) => {
29+
const key = Object.keys(currentItem)[0];
30+
result[this.hyphenToUpperCase(key)] = currentItem[key];
31+
return result;
32+
}, {});
3533
}
3634

3735
// TODO: to re implement
@@ -100,6 +98,10 @@ class GroupHelper {
10098
}
10199
return configPathExists ? predefinedConfigPath : configPath;
102100
}
101+
102+
run() {
103+
throw new Error('You must implement the "run" function');
104+
}
103105
}
104106

105107
module.exports = GroupHelper;

0 commit comments

Comments
 (0)