/
cli-flags.js
202 lines (185 loc) · 5.48 KB
/
cli-flags.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const packageExists = require('./package-exists');
const cli = packageExists('webpack') ? require('webpack').cli : undefined;
const minimumHelpFlags = [
'config',
'config-name',
'merge',
'env',
'mode',
'watch',
'watch-options-stdin',
'stats',
'devtool',
'entry',
'target',
'progress',
'json',
'name',
'output-path',
];
const builtInFlags = [
// For configs
{
name: 'config',
alias: 'c',
type: String,
multiple: true,
description: 'Provide path to a webpack configuration file e.g. ./webpack.config.js.',
},
{
name: 'config-name',
type: String,
multiple: true,
description: 'Name of the configuration to use.',
},
{
name: 'merge',
alias: 'm',
type: Boolean,
description: "Merge two or more configurations using 'webpack-merge'.",
},
// Complex configs
{
name: 'env',
type: (value, previous = {}) => {
// This ensures we're only splitting by the first `=`
const [allKeys, val] = value.split(/=(.+)/, 2);
const splitKeys = allKeys.split(/\.(?!$)/);
let prevRef = previous;
splitKeys.forEach((someKey, index) => {
if (!prevRef[someKey]) {
prevRef[someKey] = {};
}
if (typeof prevRef[someKey] === 'string') {
prevRef[someKey] = {};
}
if (index === splitKeys.length - 1) {
prevRef[someKey] = val || true;
}
prevRef = prevRef[someKey];
});
return previous;
},
multiple: true,
description: 'Environment passed to the configuration when it is a function.',
},
// Adding more plugins
{
name: 'hot',
alias: 'h',
type: Boolean,
negative: true,
description: 'Enables Hot Module Replacement',
negatedDescription: 'Disables Hot Module Replacement.',
},
{
name: 'analyze',
type: Boolean,
multiple: false,
description: 'It invokes webpack-bundle-analyzer plugin to get bundle information.',
},
{
name: 'progress',
type: [Boolean, String],
description: 'Print compilation progress during build.',
},
{
name: 'prefetch',
type: String,
description: 'Prefetch this request.',
},
// Output options
{
name: 'json',
type: [String, Boolean],
alias: 'j',
description: 'Prints result as JSON or store it in a file.',
},
// For webpack@4
{
name: 'entry',
type: String,
multiple: true,
description: 'The entry point(s) of your application e.g. ./src/main.js.',
},
{
name: 'output-path',
alias: 'o',
type: String,
description: 'Output location of the file generated by webpack e.g. ./dist/.',
},
{
name: 'target',
alias: 't',
type: String,
multiple: cli !== undefined,
description: 'Sets the build target e.g. node.',
},
{
name: 'devtool',
type: String,
negative: true,
alias: 'd',
description: 'Determine source maps to use.',
negatedDescription: 'Do not generate source maps.',
},
{
name: 'mode',
type: String,
description: 'Defines the mode to pass to webpack.',
},
{
name: 'name',
type: String,
description: 'Name of the configuration. Used when loading multiple configurations.',
},
{
name: 'stats',
type: [String, Boolean],
negative: true,
description: 'It instructs webpack on how to treat the stats e.g. verbose.',
negatedDescription: 'Disable stats output.',
},
{
name: 'watch',
type: Boolean,
negative: true,
alias: 'w',
description: 'Watch for files changes.',
negatedDescription: 'Do not watch for file changes.',
},
{
name: 'watch-options-stdin',
type: Boolean,
negative: true,
description: 'Stop watching when stdin stream has ended.',
negatedDescription: 'Do not stop watching when stdin stream has ended.',
},
];
// Extract all the flags being exported from core.
// A list of cli flags generated by core can be found here https://github.com/webpack/webpack/blob/master/test/__snapshots__/Cli.test.js.snap
const coreFlags = cli
? Object.entries(cli.getArguments()).map(([flag, meta]) => {
if (meta.simpleType === 'string') {
meta.type = String;
} else if (meta.simpleType === 'number') {
meta.type = Number;
} else {
meta.type = Boolean;
meta.negative = !flag.endsWith('-reset');
}
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);
if (inBuiltIn) {
return { ...meta, name: flag, group: 'core', ...inBuiltIn };
}
return { ...meta, name: flag, group: 'core' };
})
: [];
const flags = []
.concat(builtInFlags.filter((builtInFlag) => !coreFlags.find((coreFlag) => builtInFlag.name === coreFlag.name)))
.concat(coreFlags)
.map((option) => {
option.help = minimumHelpFlags.includes(option.name) ? 'minimum' : 'verbose';
return option;
});
module.exports = { cli, flags };