/
cli_impl.go
449 lines (395 loc) · 12.2 KB
/
cli_impl.go
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package cli
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/evanw/esbuild/internal/logging"
"github.com/evanw/esbuild/pkg/api"
)
func newBuildOptions() api.BuildOptions {
return api.BuildOptions{
Loaders: make(map[string]api.Loader),
Defines: make(map[string]string),
}
}
func newTransformOptions() api.TransformOptions {
return api.TransformOptions{
Defines: make(map[string]string),
}
}
func parseOptionsImpl(osArgs []string, buildOpts *api.BuildOptions, transformOpts *api.TransformOptions) error {
hasBareSourceMapFlag := false
// Parse the arguments now that we know what we're parsing
for _, arg := range osArgs {
switch {
case arg == "--bundle" && buildOpts != nil:
buildOpts.Bundle = true
case arg == "--splitting" && buildOpts != nil:
buildOpts.Splitting = true
case arg == "--minify":
if buildOpts != nil {
buildOpts.MinifySyntax = true
buildOpts.MinifyWhitespace = true
buildOpts.MinifyIdentifiers = true
} else {
transformOpts.MinifySyntax = true
transformOpts.MinifyWhitespace = true
transformOpts.MinifyIdentifiers = true
}
case arg == "--minify-syntax":
if buildOpts != nil {
buildOpts.MinifySyntax = true
} else {
transformOpts.MinifySyntax = true
}
case arg == "--minify-whitespace":
if buildOpts != nil {
buildOpts.MinifyWhitespace = true
} else {
transformOpts.MinifyWhitespace = true
}
case arg == "--minify-identifiers":
if buildOpts != nil {
buildOpts.MinifyIdentifiers = true
} else {
transformOpts.MinifyIdentifiers = true
}
case arg == "--sourcemap":
if buildOpts != nil {
buildOpts.Sourcemap = api.SourceMapLinked
} else {
transformOpts.Sourcemap = api.SourceMapInline
}
hasBareSourceMapFlag = true
case arg == "--sourcemap=external":
if buildOpts != nil {
buildOpts.Sourcemap = api.SourceMapExternal
} else {
transformOpts.Sourcemap = api.SourceMapExternal
}
hasBareSourceMapFlag = false
case arg == "--sourcemap=inline":
if buildOpts != nil {
buildOpts.Sourcemap = api.SourceMapInline
} else {
transformOpts.Sourcemap = api.SourceMapInline
}
hasBareSourceMapFlag = false
case strings.HasPrefix(arg, "--sourcefile=") && transformOpts != nil:
transformOpts.Sourcefile = arg[len("--sourcefile="):]
case strings.HasPrefix(arg, "--resolve-extensions=") && buildOpts != nil:
buildOpts.ResolveExtensions = strings.Split(arg[len("--resolve-extensions="):], ",")
case strings.HasPrefix(arg, "--global-name=") && buildOpts != nil:
buildOpts.GlobalName = arg[len("--global-name="):]
case strings.HasPrefix(arg, "--metafile=") && buildOpts != nil:
buildOpts.Metafile = arg[len("--metafile="):]
case strings.HasPrefix(arg, "--outfile=") && buildOpts != nil:
buildOpts.Outfile = arg[len("--outfile="):]
case strings.HasPrefix(arg, "--outdir=") && buildOpts != nil:
buildOpts.Outdir = arg[len("--outdir="):]
case strings.HasPrefix(arg, "--define:"):
value := arg[len("--define:"):]
equals := strings.IndexByte(value, '=')
if equals == -1 {
return fmt.Errorf("Missing \"=\": %q", value)
}
if buildOpts != nil {
buildOpts.Defines[value[:equals]] = value[equals+1:]
} else {
transformOpts.Defines[value[:equals]] = value[equals+1:]
}
case strings.HasPrefix(arg, "--pure:"):
value := arg[len("--pure:"):]
if buildOpts != nil {
buildOpts.PureFunctions = append(buildOpts.PureFunctions, value)
} else {
transformOpts.PureFunctions = append(transformOpts.PureFunctions, value)
}
case strings.HasPrefix(arg, "--loader:") && buildOpts != nil:
value := arg[len("--loader:"):]
equals := strings.IndexByte(value, '=')
if equals == -1 {
return fmt.Errorf("Missing \"=\": %q", value)
}
ext, text := value[:equals], value[equals+1:]
loader, err := parseLoader(text)
if err != nil {
return err
}
buildOpts.Loaders[ext] = loader
case strings.HasPrefix(arg, "--loader=") && transformOpts != nil:
value := arg[len("--loader="):]
loader, err := parseLoader(value)
if err != nil {
return err
}
if loader == api.LoaderFile {
return fmt.Errorf("Cannot transform using the \"file\" loader")
}
transformOpts.Loader = loader
case strings.HasPrefix(arg, "--target="):
value := arg[len("--target="):]
var target api.Target
switch value {
case "esnext":
target = api.ESNext
case "es6", "es2015":
target = api.ES2015
case "es2016":
target = api.ES2016
case "es2017":
target = api.ES2017
case "es2018":
target = api.ES2018
case "es2019":
target = api.ES2019
case "es2020":
target = api.ES2020
default:
return fmt.Errorf("Invalid target: %q (valid: "+
"esnext, es6, es2015, es2016, es2017, es2018, es2019, es2020)", value)
}
if buildOpts != nil {
buildOpts.Target = target
} else {
transformOpts.Target = target
}
case arg == "--strict":
value := api.StrictOptions{
NullishCoalescing: true,
ClassFields: true,
}
if buildOpts != nil {
buildOpts.Strict = value
} else {
transformOpts.Strict = value
}
case strings.HasPrefix(arg, "--strict:"):
var value *api.StrictOptions
if buildOpts != nil {
value = &buildOpts.Strict
} else {
value = &transformOpts.Strict
}
name := arg[len("--strict:"):]
switch name {
case "nullish-coalescing":
value.NullishCoalescing = true
case "class-fields":
value.ClassFields = true
default:
return fmt.Errorf("Invalid strict value: %q (valid: nullish-coalescing, class-fields)", name)
}
case strings.HasPrefix(arg, "--platform=") && buildOpts != nil:
value := arg[len("--platform="):]
switch value {
case "browser":
buildOpts.Platform = api.PlatformBrowser
case "node":
buildOpts.Platform = api.PlatformNode
default:
return fmt.Errorf("Invalid platform: %q (valid: browser, node)", value)
}
case strings.HasPrefix(arg, "--format=") && buildOpts != nil:
value := arg[len("--format="):]
switch value {
case "iife":
buildOpts.Format = api.FormatIIFE
case "cjs":
buildOpts.Format = api.FormatCommonJS
case "esm":
buildOpts.Format = api.FormatESModule
default:
return fmt.Errorf("Invalid format: %q (valid: iife, cjs, esm)", value)
}
case strings.HasPrefix(arg, "--external:") && buildOpts != nil:
buildOpts.Externals = append(buildOpts.Externals, arg[len("--external:"):])
case strings.HasPrefix(arg, "--jsx-factory="):
value := arg[len("--jsx-factory="):]
if buildOpts != nil {
buildOpts.JSXFactory = value
} else {
transformOpts.JSXFactory = value
}
case strings.HasPrefix(arg, "--jsx-fragment="):
value := arg[len("--jsx-fragment="):]
if buildOpts != nil {
buildOpts.JSXFragment = value
} else {
transformOpts.JSXFragment = value
}
case strings.HasPrefix(arg, "--error-limit="):
value := arg[len("--error-limit="):]
limit, err := strconv.Atoi(value)
if err != nil || limit < 0 {
return fmt.Errorf("Invalid error limit: %q", value)
}
if buildOpts != nil {
buildOpts.ErrorLimit = limit
} else {
transformOpts.ErrorLimit = limit
}
// Make sure this stays in sync with "PrintErrorToStderr"
case strings.HasPrefix(arg, "--color="):
value := arg[len("--color="):]
var color api.StderrColor
switch value {
case "false":
color = api.ColorNever
case "true":
color = api.ColorAlways
default:
return fmt.Errorf("Invalid color: %q (valid: false, true)", value)
}
if buildOpts != nil {
buildOpts.Color = color
} else {
transformOpts.Color = color
}
// Make sure this stays in sync with "PrintErrorToStderr"
case strings.HasPrefix(arg, "--log-level="):
value := arg[len("--log-level="):]
var logLevel api.LogLevel
switch value {
case "info":
logLevel = api.LogLevelInfo
case "warning":
logLevel = api.LogLevelWarning
case "error":
logLevel = api.LogLevelError
case "silent":
logLevel = api.LogLevelSilent
default:
return fmt.Errorf("Invalid log level: %q (valid: info, warning, error, silent)", arg)
}
if buildOpts != nil {
buildOpts.LogLevel = logLevel
} else {
transformOpts.LogLevel = logLevel
}
case !strings.HasPrefix(arg, "-") && buildOpts != nil:
buildOpts.EntryPoints = append(buildOpts.EntryPoints, arg)
default:
if buildOpts != nil {
return fmt.Errorf("Invalid build flag: %q", arg)
} else {
return fmt.Errorf("Invalid transform flag: %q", arg)
}
}
}
// If we're building, the last source map flag is "--sourcemap", and there
// is no output path, change the source map option to "inline" because we're
// going to be writing to stdout which can only represent a single file.
if buildOpts != nil && hasBareSourceMapFlag && buildOpts.Outfile == "" && buildOpts.Outdir == "" {
buildOpts.Sourcemap = api.SourceMapInline
}
return nil
}
func parseLoader(text string) (api.Loader, error) {
switch text {
case "js":
return api.LoaderJS, nil
case "jsx":
return api.LoaderJSX, nil
case "ts":
return api.LoaderTS, nil
case "tsx":
return api.LoaderTSX, nil
case "json":
return api.LoaderJSON, nil
case "text":
return api.LoaderText, nil
case "base64":
return api.LoaderBase64, nil
case "dataurl":
return api.LoaderDataURL, nil
case "file":
return api.LoaderFile, nil
default:
return ^api.Loader(0), fmt.Errorf("Invalid loader: %q (valid: "+
"js, jsx, ts, tsx, json, text, base64, dataurl, file)", text)
}
}
// This returns either BuildOptions, TransformOptions, or an error
func parseOptionsForRun(osArgs []string) (*api.BuildOptions, *api.TransformOptions, error) {
// If there's an entry point, then we're building
for _, arg := range osArgs {
if !strings.HasPrefix(arg, "-") {
options := newBuildOptions()
// Apply defaults appropriate for the CLI
options.ErrorLimit = 10
options.LogLevel = api.LogLevelInfo
err := parseOptionsImpl(osArgs, &options, nil)
if err != nil {
return nil, nil, err
}
return &options, nil, nil
}
}
// Otherwise, we're transforming
options := newTransformOptions()
// Apply defaults appropriate for the CLI
options.ErrorLimit = 10
options.LogLevel = api.LogLevelInfo
err := parseOptionsImpl(osArgs, nil, &options)
if err != nil {
return nil, nil, err
}
if options.Sourcemap != api.SourceMapNone && options.Sourcemap != api.SourceMapInline {
return nil, nil, fmt.Errorf("Must use \"inline\" source map when transforming stdin")
}
return nil, &options, nil
}
func runImpl(osArgs []string) int {
buildOptions, transformOptions, err := parseOptionsForRun(osArgs)
switch {
case buildOptions != nil:
// Run the build and stop if there were errors
result := api.Build(*buildOptions)
if len(result.Errors) > 0 {
return 1
}
// Special-case writing to stdout
if buildOptions.Outfile == "" && buildOptions.Outdir == "" {
if len(result.OutputFiles) != 1 {
logging.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Internal error: did not expect to generate %d files when writing to stdout", len(result.OutputFiles)))
} else if _, err := os.Stdout.Write(result.OutputFiles[0].Contents); err != nil {
logging.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write to stdout: %s", err.Error()))
}
} else {
for _, outputFile := range result.OutputFiles {
if err := os.MkdirAll(filepath.Dir(outputFile.Path), 0755); err != nil {
result.Errors = append(result.Errors, api.Message{Text: fmt.Sprintf(
"Failed to create output directory: %s", err.Error())})
} else if err := ioutil.WriteFile(outputFile.Path, outputFile.Contents, 0644); err != nil {
logging.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
}
}
case transformOptions != nil:
// Read the input from stdin
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
logging.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Could not read from stdin: %s", err.Error()))
return 1
}
// Run the transform and stop if there were errors
result := api.Transform(string(bytes), *transformOptions)
if len(result.Errors) > 0 {
return 1
}
// Write the output to stdout
os.Stdout.Write(result.JS)
case err != nil:
logging.PrintErrorToStderr(osArgs, err.Error())
return 1
}
return 0
}