forked from babel/babel
/
options.js
339 lines (301 loc) 路 9.23 KB
/
options.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// @flow
import fs from "fs";
import commander from "commander";
import { version } from "@babel/core";
import uniq from "lodash/uniq";
import glob from "glob";
import pkg from "../../package.json";
// Standard Babel input configs.
commander.option(
"-f, --filename [filename]",
"The filename to use when reading from stdin. This will be used in source-maps, errors etc.",
);
commander.option(
"--presets [list]",
"A comma-separated list of preset names.",
collect,
);
commander.option(
"--plugins [list]",
"A comma-separated list of plugin names.",
collect,
);
commander.option("--config-file [path]", "Path to a .babelrc file to use.");
commander.option(
"--env-name [name]",
"The name of the 'env' to use when loading configs and plugins. " +
"Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'.",
);
commander.option(
"--root-mode [mode]",
"The project-root resolution mode. " +
"One of 'root' (the default), 'upward', or 'upward-optional'.",
);
// Basic file input configuration.
commander.option("--source-type [script|module]", "");
commander.option(
"--no-babelrc",
"Whether or not to look up .babelrc and .babelignore files.",
);
commander.option(
"--ignore [list]",
"List of glob paths to **not** compile.",
collect,
);
commander.option(
"--only [list]",
"List of glob paths to **only** compile.",
collect,
);
// Misc babel config.
commander.option(
"--no-highlight-code",
"Enable or disable ANSI syntax highlighting of code frames. (on by default)",
);
// General output formatting.
commander.option(
"--no-comments",
"Write comments to generated output. (true by default)",
);
commander.option(
"--retain-lines",
"Retain line numbers. This will result in really ugly code.",
);
commander.option(
"--compact [true|false|auto]",
"Do not include superfluous whitespace characters and line terminators.",
booleanify,
);
commander.option(
"--minified",
"Save as many bytes when printing. (false by default)",
);
commander.option(
"--auxiliary-comment-before [string]",
"Print a comment before any injected non-user code.",
);
commander.option(
"--auxiliary-comment-after [string]",
"Print a comment after any injected non-user code.",
);
// General source map formatting.
commander.option("-s, --source-maps [true|false|inline|both]", "", booleanify);
commander.option(
"--source-map-target [string]",
"Set `file` on returned source map.",
);
commander.option(
"--source-file-name [string]",
"Set `sources[0]` on returned source map.",
);
commander.option(
"--source-root [filename]",
"The root from which all sources are relative.",
);
// Config params for certain module output formats.
commander.option(
"--module-root [filename]",
// eslint-disable-next-line max-len
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.",
);
commander.option("-M, --module-ids", "Insert an explicit id for modules.");
commander.option(
"--module-id [string]",
"Specify a custom name for module ids.",
);
// "babel" command specific arguments that are not passed to @babel/core.
commander.option(
"-x, --extensions [extensions]",
"List of extensions to compile when a directory has been the input. [.es6,.js,.es,.jsx,.mjs]",
collect,
);
commander.option(
"--keep-file-extension",
"Preserve the file extensions of the input files.",
);
commander.option("-w, --watch", "Recompile files on changes.");
commander.option(
"--skip-initial-build",
"Do not compile files before watching.",
);
commander.option(
"-o, --out-file [out]",
"Compile all input files into a single file.",
);
commander.option(
"-d, --out-dir [out]",
"Compile an input directory of modules into an output directory.",
);
commander.option(
"--relative",
"Compile into an output directory relative to input directory or file. Requires --out-dir [out]",
);
commander.option(
"-D, --copy-files",
"When compiling a directory copy over non-compilable files.",
);
commander.option(
"--include-dotfiles",
"Include dotfiles when compiling and copying non-compilable files.",
);
commander.option(
"--verbose",
"Log everything. This option conflicts with --quiet",
);
commander.option(
"--quiet",
"Don't log anything. This option conflicts with --verbose",
);
commander.option(
"--delete-dir-on-start",
"Delete the out directory before compilation.",
);
commander.option(
"--copy-ignored",
"Include ignored files when copying non-compilable files.",
);
commander.version(pkg.version + " (@babel/core " + version + ")");
commander.usage("[options] <files ...>");
// register an empty action handler so that commander.js can throw on
// unknown options _after_ args
// see https://github.com/tj/commander.js/issues/561#issuecomment-522209408
commander.action(() => {});
export type CmdOptions = {
babelOptions: Object,
cliOptions: Object,
};
export default function parseArgv(args: Array<string>): CmdOptions | null {
//
commander.parse(args);
const errors = [];
let filenames = commander.args.reduce(function(globbed, input) {
let files = glob.sync(input);
if (!files.length) files = [input];
return globbed.concat(files);
}, []);
filenames = uniq(filenames);
filenames.forEach(function(filename) {
if (!fs.existsSync(filename)) {
errors.push(filename + " does not exist");
}
});
if (commander.outDir && !filenames.length) {
errors.push("--out-dir requires filenames");
}
if (commander.outFile && commander.outDir) {
errors.push("--out-file and --out-dir cannot be used together");
}
if (commander.relative && !commander.outDir) {
errors.push("--relative requires --out-dir usage");
}
if (commander.watch) {
if (!commander.outFile && !commander.outDir) {
errors.push("--watch requires --out-file or --out-dir");
}
if (!filenames.length) {
errors.push("--watch requires filenames");
}
}
if (commander.skipInitialBuild && !commander.watch) {
errors.push("--skip-initial-build requires --watch");
}
if (commander.deleteDirOnStart && !commander.outDir) {
errors.push("--delete-dir-on-start requires --out-dir");
}
if (commander.verbose && commander.quiet) {
errors.push("--verbose and --quiet cannot be used together");
}
if (
!commander.outDir &&
filenames.length === 0 &&
typeof commander.filename !== "string" &&
commander.babelrc !== false
) {
errors.push(
"stdin compilation requires either -f/--filename [filename] or --no-babelrc",
);
}
if (errors.length) {
console.error("babel:");
errors.forEach(function(e) {
console.error(" " + e);
});
return null;
}
const opts = commander.opts();
const babelOptions = {
presets: opts.presets,
plugins: opts.plugins,
rootMode: opts.rootMode,
configFile: opts.configFile,
envName: opts.envName,
sourceType: opts.sourceType,
ignore: opts.ignore,
only: opts.only,
retainLines: opts.retainLines,
compact: opts.compact,
minified: opts.minified,
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
sourceMaps: opts.sourceMaps,
sourceFileName: opts.sourceFileName,
sourceRoot: opts.sourceRoot,
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,
// Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the
// default-assignment logic on its own.
babelrc: opts.babelrc === true ? undefined : opts.babelrc,
highlightCode: opts.highlightCode === true ? undefined : opts.highlightCode,
comments: opts.comments === true ? undefined : opts.comments,
};
// If the @babel/cli version is newer than the @babel/core version, and we have added
// new options for @babel/core, we'll potentially get option validation errors from
// @babel/core. To avoid that, we delete undefined options, so @babel/core will only
// give the error if users actually pass an unsupported CLI option.
for (const key of Object.keys(babelOptions)) {
if (babelOptions[key] === undefined) {
delete babelOptions[key];
}
}
return {
babelOptions,
cliOptions: {
filename: opts.filename,
filenames,
extensions: opts.extensions,
keepFileExtension: opts.keepFileExtension,
watch: opts.watch,
skipInitialBuild: opts.skipInitialBuild,
outFile: opts.outFile,
outDir: opts.outDir,
relative: opts.relative,
copyFiles: opts.copyFiles,
includeDotfiles: opts.includeDotfiles,
verbose: opts.verbose,
quiet: opts.quiet,
deleteDirOnStart: opts.deleteDirOnStart,
sourceMapTarget: opts.sourceMapTarget,
copyIgnored: opts.copyIgnored,
},
};
}
function booleanify(val: any): boolean | any {
if (val === "true" || val == 1) {
return true;
}
if (val === "false" || val == 0 || !val) {
return false;
}
return val;
}
function collect(
value: string | any,
previousValue: Array<string>,
): Array<string> {
// If the user passed the option with no value, like "babel file.js --presets", do nothing.
if (typeof value !== "string") return previousValue;
const values = value.split(",");
return previousValue ? previousValue.concat(values) : values;
}