@@ -46,6 +46,32 @@ type parseOptionsExtras struct {
46
46
mangleCache * string
47
47
}
48
48
49
+ func isBoolFlag (arg string , flag string ) bool {
50
+ if strings .HasPrefix (arg , flag ) {
51
+ remainder := arg [len (flag ):]
52
+ return len (remainder ) == 0 || remainder [0 ] == '='
53
+ }
54
+ return false
55
+ }
56
+
57
+ func parseBoolFlag (arg string , defaultValue bool ) (bool , * cli_helpers.ErrorWithNote ) {
58
+ equals := strings .IndexByte (arg , '=' )
59
+ if equals == - 1 {
60
+ return defaultValue , nil
61
+ }
62
+ value := arg [equals + 1 :]
63
+ switch value {
64
+ case "false" :
65
+ return false , nil
66
+ case "true" :
67
+ return true , nil
68
+ }
69
+ return false , cli_helpers .MakeErrorWithNote (
70
+ fmt .Sprintf ("Invalid value %q in %q" , value , arg ),
71
+ "Valid values are \" true\" or \" false\" ." ,
72
+ )
73
+ }
74
+
49
75
func parseOptionsImpl (
50
76
osArgs []string ,
51
77
buildOpts * api.BuildOptions ,
@@ -57,51 +83,81 @@ func parseOptionsImpl(
57
83
// Parse the arguments now that we know what we're parsing
58
84
for _ , arg := range osArgs {
59
85
switch {
60
- case arg == "--bundle" && buildOpts != nil :
61
- buildOpts .Bundle = true
86
+ case isBoolFlag (arg , "--bundle" ) && buildOpts != nil :
87
+ if value , err := parseBoolFlag (arg , true ); err != nil {
88
+ return parseOptionsExtras {}, err
89
+ } else {
90
+ buildOpts .Bundle = value
91
+ }
62
92
63
- case arg == "--preserve-symlinks" && buildOpts != nil :
64
- buildOpts .PreserveSymlinks = true
93
+ case isBoolFlag (arg , "--preserve-symlinks" ) && buildOpts != nil :
94
+ if value , err := parseBoolFlag (arg , true ); err != nil {
95
+ return parseOptionsExtras {}, err
96
+ } else {
97
+ buildOpts .PreserveSymlinks = value
98
+ }
65
99
66
- case arg == "--splitting" && buildOpts != nil :
67
- buildOpts .Splitting = true
100
+ case isBoolFlag (arg , "--splitting" ) && buildOpts != nil :
101
+ if value , err := parseBoolFlag (arg , true ); err != nil {
102
+ return parseOptionsExtras {}, err
103
+ } else {
104
+ buildOpts .Splitting = value
105
+ }
68
106
69
- case arg == "--allow-overwrite" && buildOpts != nil :
70
- buildOpts .AllowOverwrite = true
107
+ case isBoolFlag (arg , "--allow-overwrite" ) && buildOpts != nil :
108
+ if value , err := parseBoolFlag (arg , true ); err != nil {
109
+ return parseOptionsExtras {}, err
110
+ } else {
111
+ buildOpts .AllowOverwrite = value
112
+ }
71
113
72
- case arg == "--watch" && buildOpts != nil :
73
- buildOpts .Watch = & api.WatchMode {}
114
+ case isBoolFlag (arg , "--watch" ) && buildOpts != nil :
115
+ if value , err := parseBoolFlag (arg , true ); err != nil {
116
+ return parseOptionsExtras {}, err
117
+ } else if value {
118
+ buildOpts .Watch = & api.WatchMode {}
119
+ } else {
120
+ buildOpts .Watch = nil
121
+ }
74
122
75
- case arg == "--minify" :
76
- if buildOpts != nil {
77
- buildOpts .MinifySyntax = true
78
- buildOpts .MinifyWhitespace = true
79
- buildOpts .MinifyIdentifiers = true
123
+ case isBoolFlag (arg , "--minify" ):
124
+ if value , err := parseBoolFlag (arg , true ); err != nil {
125
+ return parseOptionsExtras {}, err
126
+ } else if buildOpts != nil {
127
+ buildOpts .MinifySyntax = value
128
+ buildOpts .MinifyWhitespace = value
129
+ buildOpts .MinifyIdentifiers = value
80
130
} else {
81
- transformOpts .MinifySyntax = true
82
- transformOpts .MinifyWhitespace = true
83
- transformOpts .MinifyIdentifiers = true
131
+ transformOpts .MinifySyntax = value
132
+ transformOpts .MinifyWhitespace = value
133
+ transformOpts .MinifyIdentifiers = value
84
134
}
85
135
86
- case arg == "--minify-syntax" :
87
- if buildOpts != nil {
88
- buildOpts .MinifySyntax = true
136
+ case isBoolFlag (arg , "--minify-syntax" ):
137
+ if value , err := parseBoolFlag (arg , true ); err != nil {
138
+ return parseOptionsExtras {}, err
139
+ } else if buildOpts != nil {
140
+ buildOpts .MinifySyntax = value
89
141
} else {
90
- transformOpts .MinifySyntax = true
142
+ transformOpts .MinifySyntax = value
91
143
}
92
144
93
- case arg == "--minify-whitespace" :
94
- if buildOpts != nil {
95
- buildOpts .MinifyWhitespace = true
145
+ case isBoolFlag (arg , "--minify-whitespace" ):
146
+ if value , err := parseBoolFlag (arg , true ); err != nil {
147
+ return parseOptionsExtras {}, err
148
+ } else if buildOpts != nil {
149
+ buildOpts .MinifyWhitespace = value
96
150
} else {
97
- transformOpts .MinifyWhitespace = true
151
+ transformOpts .MinifyWhitespace = value
98
152
}
99
153
100
- case arg == "--minify-identifiers" :
101
- if buildOpts != nil {
102
- buildOpts .MinifyIdentifiers = true
154
+ case isBoolFlag (arg , "--minify-identifiers" ):
155
+ if value , err := parseBoolFlag (arg , true ); err != nil {
156
+ return parseOptionsExtras {}, err
157
+ } else if buildOpts != nil {
158
+ buildOpts .MinifyIdentifiers = value
103
159
} else {
104
- transformOpts .MinifyIdentifiers = true
160
+ transformOpts .MinifyIdentifiers = value
105
161
}
106
162
107
163
case strings .HasPrefix (arg , "--mangle-props=" ):
@@ -192,38 +248,39 @@ func parseOptionsImpl(
192
248
)
193
249
}
194
250
195
- case strings .HasPrefix (arg , "--tree-shaking=" ):
196
- var value * api.TreeShaking
197
- if buildOpts != nil {
198
- value = & buildOpts .TreeShaking
251
+ case isBoolFlag (arg , "--tree-shaking" ):
252
+ if value , err := parseBoolFlag (arg , true ); err != nil {
253
+ return parseOptionsExtras {}, err
199
254
} else {
200
- value = & transformOpts .TreeShaking
201
- }
202
- name := arg [len ("--tree-shaking=" ):]
203
- switch name {
204
- case "false" :
205
- * value = api .TreeShakingFalse
206
- case "true" :
207
- * value = api .TreeShakingTrue
208
- default :
209
- return parseOptionsExtras {}, cli_helpers .MakeErrorWithNote (
210
- fmt .Sprintf ("Invalid value %q in %q" , name , arg ),
211
- "Valid values are \" true\" or \" false\" ." ,
212
- )
255
+ var treeShaking * api.TreeShaking
256
+ if buildOpts != nil {
257
+ treeShaking = & buildOpts .TreeShaking
258
+ } else {
259
+ treeShaking = & transformOpts .TreeShaking
260
+ }
261
+ if value {
262
+ * treeShaking = api .TreeShakingTrue
263
+ } else {
264
+ * treeShaking = api .TreeShakingFalse
265
+ }
213
266
}
214
267
215
- case arg == "--ignore-annotations" :
216
- if buildOpts != nil {
217
- buildOpts .IgnoreAnnotations = true
268
+ case isBoolFlag (arg , "--ignore-annotations" ):
269
+ if value , err := parseBoolFlag (arg , true ); err != nil {
270
+ return parseOptionsExtras {}, err
271
+ } else if buildOpts != nil {
272
+ buildOpts .IgnoreAnnotations = value
218
273
} else {
219
- transformOpts .IgnoreAnnotations = true
274
+ transformOpts .IgnoreAnnotations = value
220
275
}
221
276
222
- case arg == "--keep-names" :
223
- if buildOpts != nil {
224
- buildOpts .KeepNames = true
277
+ case isBoolFlag (arg , "--keep-names" ):
278
+ if value , err := parseBoolFlag (arg , true ); err != nil {
279
+ return parseOptionsExtras {}, err
280
+ } else if buildOpts != nil {
281
+ buildOpts .KeepNames = value
225
282
} else {
226
- transformOpts .KeepNames = true
283
+ transformOpts .KeepNames = value
227
284
}
228
285
229
286
case arg == "--sourcemap" :
@@ -267,24 +324,21 @@ func parseOptionsImpl(
267
324
transformOpts .SourceRoot = sourceRoot
268
325
}
269
326
270
- case strings .HasPrefix (arg , "--sources-content=" ):
271
- value := arg [len ("--sources-content=" ):]
272
- var sourcesContent api.SourcesContent
273
- switch value {
274
- case "false" :
275
- sourcesContent = api .SourcesContentExclude
276
- case "true" :
277
- sourcesContent = api .SourcesContentInclude
278
- default :
279
- return parseOptionsExtras {}, cli_helpers .MakeErrorWithNote (
280
- fmt .Sprintf ("Invalid value %q in %q" , value , arg ),
281
- "Valid values are \" true\" or \" false\" ." ,
282
- )
283
- }
284
- if buildOpts != nil {
285
- buildOpts .SourcesContent = sourcesContent
327
+ case isBoolFlag (arg , "--sources-content" ):
328
+ if value , err := parseBoolFlag (arg , true ); err != nil {
329
+ return parseOptionsExtras {}, err
286
330
} else {
287
- transformOpts .SourcesContent = sourcesContent
331
+ var sourcesContent * api.SourcesContent
332
+ if buildOpts != nil {
333
+ sourcesContent = & buildOpts .SourcesContent
334
+ } else {
335
+ sourcesContent = & transformOpts .SourcesContent
336
+ }
337
+ if value {
338
+ * sourcesContent = api .SourcesContentInclude
339
+ } else {
340
+ * sourcesContent = api .SourcesContentExclude
341
+ }
288
342
}
289
343
290
344
case strings .HasPrefix (arg , "--sourcefile=" ):
@@ -569,24 +623,21 @@ func parseOptionsImpl(
569
623
}
570
624
571
625
// Make sure this stays in sync with "PrintErrorToStderr"
572
- case strings .HasPrefix (arg , "--color=" ):
573
- value := arg [len ("--color=" ):]
574
- var color api.StderrColor
575
- switch value {
576
- case "false" :
577
- color = api .ColorNever
578
- case "true" :
579
- color = api .ColorAlways
580
- default :
581
- return parseOptionsExtras {}, cli_helpers .MakeErrorWithNote (
582
- fmt .Sprintf ("Invalid value %q in %q" , value , arg ),
583
- "Valid values are \" true\" or \" false\" ." ,
584
- )
585
- }
586
- if buildOpts != nil {
587
- buildOpts .Color = color
626
+ case isBoolFlag (arg , "--color" ):
627
+ if value , err := parseBoolFlag (arg , true ); err != nil {
628
+ return parseOptionsExtras {}, err
588
629
} else {
589
- transformOpts .Color = color
630
+ var color * api.StderrColor
631
+ if buildOpts != nil {
632
+ color = & buildOpts .Color
633
+ } else {
634
+ color = & transformOpts .Color
635
+ }
636
+ if value {
637
+ * color = api .ColorAlways
638
+ } else {
639
+ * color = api .ColorNever
640
+ }
590
641
}
591
642
592
643
// Make sure this stays in sync with "PrintErrorToStderr"
@@ -641,7 +692,6 @@ func parseOptionsImpl(
641
692
"bundle" : true ,
642
693
"ignore-annotations" : true ,
643
694
"keep-names" : true ,
644
- "metafile" : true ,
645
695
"minify-identifiers" : true ,
646
696
"minify-syntax" : true ,
647
697
"minify-whitespace" : true ,
@@ -653,8 +703,10 @@ func parseOptionsImpl(
653
703
}
654
704
655
705
equals := map [string ]bool {
706
+ "allow-overwrite" : true ,
656
707
"asset-names" : true ,
657
708
"banner" : true ,
709
+ "bundle" : true ,
658
710
"charset" : true ,
659
711
"chunk-names" : true ,
660
712
"color" : true ,
@@ -663,31 +715,41 @@ func parseOptionsImpl(
663
715
"footer" : true ,
664
716
"format" : true ,
665
717
"global-name" : true ,
718
+ "ignore-annotations" : true ,
666
719
"jsx-factory" : true ,
667
720
"jsx-fragment" : true ,
668
721
"jsx" : true ,
722
+ "keep-names" : true ,
669
723
"legal-comments" : true ,
670
724
"loader" : true ,
671
725
"log-level" : true ,
672
726
"log-limit" : true ,
673
727
"main-fields" : true ,
674
728
"mangle-cache" : true ,
675
729
"mangle-props" : true ,
730
+ "metafile" : true ,
731
+ "minify-identifiers" : true ,
732
+ "minify-syntax" : true ,
733
+ "minify-whitespace" : true ,
734
+ "minify" : true ,
676
735
"outbase" : true ,
677
736
"outdir" : true ,
678
737
"outfile" : true ,
679
738
"platform" : true ,
739
+ "preserve-symlinks" : true ,
680
740
"public-path" : true ,
681
741
"reserve-props" : true ,
682
742
"resolve-extensions" : true ,
683
743
"source-root" : true ,
684
744
"sourcefile" : true ,
685
745
"sourcemap" : true ,
686
746
"sources-content" : true ,
747
+ "splitting" : true ,
687
748
"target" : true ,
688
749
"tree-shaking" : true ,
689
750
"tsconfig-raw" : true ,
690
751
"tsconfig" : true ,
752
+ "watch" : true ,
691
753
}
692
754
693
755
colon := map [string ]bool {
0 commit comments