/
config.go
667 lines (565 loc) · 15.3 KB
/
config.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package config
import (
"fmt"
"regexp"
"strings"
"sync"
"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/compat"
"github.com/evanw/esbuild/internal/js_ast"
"github.com/evanw/esbuild/internal/logger"
)
type JSXOptions struct {
Factory DefineExpr
Fragment DefineExpr
Parse bool
Preserve bool
AutomaticRuntime bool
ImportSource string
Development bool
SideEffects bool
}
type TSJSX uint8
const (
TSJSXNone TSJSX = iota
TSJSXPreserve
TSJSXReact
TSJSXReactJSX
TSJSXReactJSXDev
)
func (jsxOptions *JSXOptions) SetOptionsFromTSJSX(tsx TSJSX) {
switch tsx {
case TSJSXPreserve:
jsxOptions.Preserve = true
case TSJSXReact:
jsxOptions.AutomaticRuntime = false
jsxOptions.Development = false
case TSJSXReactJSX:
jsxOptions.AutomaticRuntime = true
// Don't set Development = false implicitly
case TSJSXReactJSXDev:
jsxOptions.AutomaticRuntime = true
jsxOptions.Development = true
}
}
type TSOptions struct {
Parse bool
NoAmbiguousLessThan bool
}
type Platform uint8
const (
PlatformBrowser Platform = iota
PlatformNode
PlatformNeutral
)
type SourceMap uint8
const (
SourceMapNone SourceMap = iota
SourceMapInline
SourceMapLinkedWithComment
SourceMapExternalWithoutComment
SourceMapInlineAndExternal
)
type LegalComments uint8
const (
LegalCommentsInline LegalComments = iota
LegalCommentsNone
LegalCommentsEndOfFile
LegalCommentsLinkedWithComment
LegalCommentsExternalWithoutComment
)
func (lc LegalComments) HasExternalFile() bool {
return lc == LegalCommentsLinkedWithComment || lc == LegalCommentsExternalWithoutComment
}
type Loader uint8
const (
LoaderNone Loader = iota
LoaderBase64
LoaderBinary
LoaderCopy
LoaderCSS
LoaderDataURL
LoaderDefault
LoaderFile
LoaderJS
LoaderJSON
LoaderJSX
LoaderText
LoaderTS
LoaderTSNoAmbiguousLessThan // Used with ".mts" and ".cts"
LoaderTSX
)
func (loader Loader) IsTypeScript() bool {
switch loader {
case LoaderTS, LoaderTSNoAmbiguousLessThan, LoaderTSX:
return true
default:
return false
}
}
func (loader Loader) CanHaveSourceMap() bool {
switch loader {
case LoaderJS, LoaderJSX, LoaderTS, LoaderTSNoAmbiguousLessThan, LoaderTSX, LoaderCSS:
return true
default:
return false
}
}
type Format uint8
const (
// This is used when not bundling. It means to preserve whatever form the
// import or export was originally in. ES6 syntax stays ES6 syntax and
// CommonJS syntax stays CommonJS syntax.
FormatPreserve Format = iota
// IIFE stands for immediately-invoked function expression. That looks like
// this:
//
// (() => {
// ... bundled code ...
// })();
//
// If the optional GlobalName is configured, then we'll write out this:
//
// let globalName = (() => {
// ... bundled code ...
// return exports;
// })();
//
FormatIIFE
// The CommonJS format looks like this:
//
// ... bundled code ...
// module.exports = exports;
//
FormatCommonJS
// The ES module format looks like this:
//
// ... bundled code ...
// export {...};
//
FormatESModule
)
func (f Format) KeepES6ImportExportSyntax() bool {
return f == FormatPreserve || f == FormatESModule
}
func (f Format) String() string {
switch f {
case FormatIIFE:
return "iife"
case FormatCommonJS:
return "cjs"
case FormatESModule:
return "esm"
}
return ""
}
type StdinInfo struct {
Contents string
SourceFile string
AbsResolveDir string
Loader Loader
}
type WildcardPattern struct {
Prefix string
Suffix string
}
type ExternalMatchers struct {
Exact map[string]bool
Patterns []WildcardPattern
}
func (matchers ExternalMatchers) HasMatchers() bool {
return len(matchers.Exact) > 0 || len(matchers.Patterns) > 0
}
type ExternalSettings struct {
PreResolve ExternalMatchers
PostResolve ExternalMatchers
}
type Mode uint8
const (
ModePassThrough Mode = iota
ModeConvertFormat
ModeBundle
)
type MaybeBool uint8
const (
Unspecified MaybeBool = iota
True
False
)
type Options struct {
ModuleTypeData js_ast.ModuleTypeData
Defines *ProcessedDefines
TSTarget *TSTarget
TSAlwaysStrict *TSAlwaysStrict
MangleProps *regexp.Regexp
ReserveProps *regexp.Regexp
// When mangling property names, call this function with a callback and do
// the property name mangling inside the callback. The callback takes an
// argument which is the mangle cache map to mutate. These callbacks are
// serialized so mutating the map does not require extra synchronization.
//
// This is a callback for determinism reasons. We may be building multiple
// entry points in parallel that are supposed to share a single cache. We
// don't want the order that each entry point mangles properties in to cause
// the output to change, so we serialize the property mangling over all entry
// points in entry point order. However, we still want to link everything in
// parallel so only property mangling is serialized, which is implemented by
// this function blocking until the previous entry point's property mangling
// has finished.
ExclusiveMangleCacheUpdate func(cb func(mangleCache map[string]interface{}))
// This is the original information that was used to generate the
// unsupported feature sets above. It's used for error messages.
OriginalTargetEnv string
ExtensionOrder []string
MainFields []string
Conditions []string
AbsNodePaths []string // The "NODE_PATH" variable from Node.js
ExternalSettings ExternalSettings
PackageAliases map[string]string
AbsOutputFile string
AbsOutputDir string
AbsOutputBase string
OutputExtensionJS string
OutputExtensionCSS string
GlobalName []string
TsConfigOverride string
ExtensionToLoader map[string]Loader
PublicPath string
InjectAbsPaths []string
InjectedDefines []InjectedDefine
InjectedFiles []InjectedFile
JSBanner string
JSFooter string
CSSBanner string
CSSFooter string
EntryPathTemplate []PathTemplate
ChunkPathTemplate []PathTemplate
AssetPathTemplate []PathTemplate
Plugins []Plugin
SourceRoot string
Stdin *StdinInfo
JSX JSXOptions
UnsupportedJSFeatures compat.JSFeature
UnsupportedCSSFeatures compat.CSSFeature
UnsupportedJSFeatureOverrides compat.JSFeature
UnsupportedJSFeatureOverridesMask compat.JSFeature
UnsupportedCSSFeatureOverrides compat.CSSFeature
UnsupportedCSSFeatureOverridesMask compat.CSSFeature
TS TSOptions
Mode Mode
PreserveSymlinks bool
MinifyWhitespace bool
MinifyIdentifiers bool
MinifySyntax bool
ProfilerNames bool
CodeSplitting bool
WatchMode bool
AllowOverwrite bool
LegalComments LegalComments
// If true, make sure to generate a single file that can be written to stdout
WriteToStdout bool
OmitRuntimeForTests bool
OmitJSXRuntimeForTests bool
UnusedImportFlagsTS UnusedImportFlagsTS
UseDefineForClassFields MaybeBool
ASCIIOnly bool
KeepNames bool
IgnoreDCEAnnotations bool
TreeShaking bool
DropDebugger bool
MangleQuoted bool
Platform Platform
TargetFromAPI TargetFromAPI
OutputFormat Format
NeedsMetafile bool
SourceMap SourceMap
ExcludeSourcesContent bool
}
type TargetFromAPI uint8
const (
// In this state, the "target" field in "tsconfig.json" is respected
TargetWasUnconfigured TargetFromAPI = iota
// In this state, the "target" field in "tsconfig.json" is overridden
TargetWasConfigured
// In this state, "useDefineForClassFields" is true unless overridden
TargetWasConfiguredAndAtLeastES2022
)
type UnusedImportFlagsTS uint8
// With !UnusedImportKeepStmt && !UnusedImportKeepValues:
//
// "import 'foo'" => "import 'foo'"
// "import * as unused from 'foo'" => ""
// "import { unused } from 'foo'" => ""
// "import { type unused } from 'foo'" => ""
//
// With UnusedImportKeepStmt && !UnusedImportKeepValues:
//
// "import 'foo'" => "import 'foo'"
// "import * as unused from 'foo'" => "import 'foo'"
// "import { unused } from 'foo'" => "import 'foo'"
// "import { type unused } from 'foo'" => "import 'foo'"
//
// With !UnusedImportKeepStmt && UnusedImportKeepValues:
//
// "import 'foo'" => "import 'foo'"
// "import * as unused from 'foo'" => "import * as unused from 'foo'"
// "import { unused } from 'foo'" => "import { unused } from 'foo'"
// "import { type unused } from 'foo'" => ""
//
// With UnusedImportKeepStmt && UnusedImportKeepValues:
//
// "import 'foo'" => "import 'foo'"
// "import * as unused from 'foo'" => "import * as unused from 'foo'"
// "import { unused } from 'foo'" => "import { unused } from 'foo'"
// "import { type unused } from 'foo'" => "import {} from 'foo'"
const (
UnusedImportKeepStmt UnusedImportFlagsTS = 1 << iota // "importsNotUsedAsValues" != "remove"
UnusedImportKeepValues // "preserveValueImports" == true
)
func UnusedImportFlagsFromTsconfigValues(preserveImportsNotUsedAsValues bool, preserveValueImports bool) (flags UnusedImportFlagsTS) {
if preserveValueImports {
flags |= UnusedImportKeepValues
}
if preserveImportsNotUsedAsValues {
flags |= UnusedImportKeepStmt
}
return
}
type TSTarget struct {
// This information is only used for error messages
Target string
Source logger.Source
Range logger.Range
// This information can affect code transformation
UnsupportedJSFeatures compat.JSFeature
TargetIsAtLeastES2022 bool
}
type TSAlwaysStrict struct {
// This information is only used for error messages
Name string
Source logger.Source
Range logger.Range
// This information can affect code transformation
Value bool
}
type PathPlaceholder uint8
const (
NoPlaceholder PathPlaceholder = iota
// The relative path from the original parent directory to the configured
// "outbase" directory, or to the lowest common ancestor directory
DirPlaceholder
// The original name of the file, or the manual chunk name, or the name of
// the type of output file ("entry" or "chunk" or "asset")
NamePlaceholder
// A hash of the contents of this file, and the contents and output paths of
// all dependencies (except for their hash placeholders)
HashPlaceholder
// The original extension of the file, or the name of the output file
// (e.g. "css", "svg", "png")
ExtPlaceholder
)
type PathTemplate struct {
Data string
Placeholder PathPlaceholder
}
type PathPlaceholders struct {
Dir *string
Name *string
Hash *string
Ext *string
}
func (placeholders PathPlaceholders) Get(placeholder PathPlaceholder) *string {
switch placeholder {
case DirPlaceholder:
return placeholders.Dir
case NamePlaceholder:
return placeholders.Name
case HashPlaceholder:
return placeholders.Hash
case ExtPlaceholder:
return placeholders.Ext
}
return nil
}
func TemplateToString(template []PathTemplate) string {
if len(template) == 1 && template[0].Placeholder == NoPlaceholder {
// Avoid allocations in this case
return template[0].Data
}
sb := strings.Builder{}
for _, part := range template {
sb.WriteString(part.Data)
switch part.Placeholder {
case DirPlaceholder:
sb.WriteString("[dir]")
case NamePlaceholder:
sb.WriteString("[name]")
case HashPlaceholder:
sb.WriteString("[hash]")
case ExtPlaceholder:
sb.WriteString("[ext]")
}
}
return sb.String()
}
func HasPlaceholder(template []PathTemplate, placeholder PathPlaceholder) bool {
for _, part := range template {
if part.Placeholder == placeholder {
return true
}
}
return false
}
func SubstituteTemplate(template []PathTemplate, placeholders PathPlaceholders) []PathTemplate {
// Don't allocate if no substitution is possible and the template is already minimal
shouldSubstitute := false
for i, part := range template {
if placeholders.Get(part.Placeholder) != nil || (part.Placeholder == NoPlaceholder && i+1 < len(template)) {
shouldSubstitute = true
break
}
}
if !shouldSubstitute {
return template
}
// Otherwise, substitute and merge as appropriate
result := make([]PathTemplate, 0, len(template))
for _, part := range template {
if sub := placeholders.Get(part.Placeholder); sub != nil {
part.Data += *sub
part.Placeholder = NoPlaceholder
}
if last := len(result) - 1; last >= 0 && result[last].Placeholder == NoPlaceholder {
last := &result[last]
last.Data += part.Data
last.Placeholder = part.Placeholder
} else {
result = append(result, part)
}
}
return result
}
func ShouldCallRuntimeRequire(mode Mode, outputFormat Format) bool {
return mode == ModeBundle && outputFormat != FormatCommonJS
}
type InjectedDefine struct {
Data js_ast.E
Name string
Source logger.Source
}
type InjectedFile struct {
Exports []InjectableExport
DefineName string
Source logger.Source
}
type InjectableExport struct {
Alias string
Loc logger.Loc
}
var filterMutex sync.Mutex
var filterCache map[string]*regexp.Regexp
func compileFilter(filter string) (result *regexp.Regexp) {
if filter == "" {
// Must provide a filter
return nil
}
ok := false
// Cache hit?
(func() {
filterMutex.Lock()
defer filterMutex.Unlock()
if filterCache != nil {
result, ok = filterCache[filter]
}
})()
if ok {
return
}
// Cache miss
result, err := regexp.Compile(filter)
if err != nil {
return nil
}
// Cache for next time
filterMutex.Lock()
defer filterMutex.Unlock()
if filterCache == nil {
filterCache = make(map[string]*regexp.Regexp)
}
filterCache[filter] = result
return
}
func CompileFilterForPlugin(pluginName string, kind string, filter string) (*regexp.Regexp, error) {
if filter == "" {
return nil, fmt.Errorf("[%s] %q is missing a filter", pluginName, kind)
}
result := compileFilter(filter)
if result == nil {
return nil, fmt.Errorf("[%s] %q filter is not a valid Go regular expression: %q", pluginName, kind, filter)
}
return result, nil
}
func PluginAppliesToPath(path logger.Path, filter *regexp.Regexp, namespace string) bool {
return (namespace == "" || path.Namespace == namespace) && filter.MatchString(path.Text)
}
////////////////////////////////////////////////////////////////////////////////
// Plugin API
type Plugin struct {
Name string
OnStart []OnStart
OnResolve []OnResolve
OnLoad []OnLoad
}
type OnStart struct {
Callback func() OnStartResult
Name string
}
type OnStartResult struct {
ThrownError error
Msgs []logger.Msg
}
type OnResolve struct {
Filter *regexp.Regexp
Callback func(OnResolveArgs) OnResolveResult
Name string
Namespace string
}
type OnResolveArgs struct {
Path string
ResolveDir string
PluginData interface{}
Importer logger.Path
Kind ast.ImportKind
}
type OnResolveResult struct {
PluginName string
Msgs []logger.Msg
ThrownError error
AbsWatchFiles []string
AbsWatchDirs []string
PluginData interface{}
Path logger.Path
External bool
IsSideEffectFree bool
}
type OnLoad struct {
Filter *regexp.Regexp
Callback func(OnLoadArgs) OnLoadResult
Name string
Namespace string
}
type OnLoadArgs struct {
PluginData interface{}
Path logger.Path
}
type OnLoadResult struct {
PluginName string
Contents *string
AbsResolveDir string
PluginData interface{}
Msgs []logger.Msg
ThrownError error
AbsWatchFiles []string
AbsWatchDirs []string
Loader Loader
}