at main 1.8 kB view raw
1package transform 2 3import ( 4 "errors" 5 6 "github.com/evanw/esbuild/pkg/api" 7) 8 9type Format int 10 11const ( 12 FormatCommonJS Format = iota 13 FormatIIFE 14) 15 16func Transform(file, src string, format Format) ([]byte, []error) { 17 var fmt api.Format 18 switch format { 19 case FormatCommonJS: 20 fmt = api.FormatCommonJS 21 case FormatIIFE: 22 fmt = api.FormatIIFE 23 default: 24 return nil, []error{errors.New("unknown format")} 25 } 26 27 minify := true 28 result := api.Transform(src, api.TransformOptions{ 29 Color: 0, 30 LogLevel: 0, 31 LogLimit: 0, 32 LogOverride: nil, 33 Sourcemap: api.SourceMapInline, 34 SourceRoot: "", 35 SourcesContent: 0, 36 Target: api.ESNext, 37 Engines: nil, 38 Supported: nil, 39 Platform: api.PlatformNode, 40 Format: fmt, 41 GlobalName: "__export__", 42 MangleProps: "", 43 ReserveProps: "", 44 MangleQuoted: 0, 45 MangleCache: nil, 46 Drop: 0, 47 DropLabels: nil, 48 MinifyWhitespace: minify, 49 MinifyIdentifiers: minify, 50 MinifySyntax: minify, 51 LineLimit: 0, 52 Charset: 0, 53 TreeShaking: api.TreeShakingDefault, 54 IgnoreAnnotations: false, 55 LegalComments: 0, 56 JSX: api.JSXAutomatic, 57 JSXFactory: "h", 58 JSXFragment: "b", 59 JSXImportSource: "react", 60 JSXDev: false, 61 JSXSideEffects: false, 62 TsconfigRaw: "", 63 Banner: "", 64 Footer: "__export__", 65 Define: nil, 66 Pure: nil, 67 KeepNames: false, 68 Sourcefile: file, 69 Loader: api.LoaderJSX, 70 }) 71 72 if len(result.Errors) > 0 { 73 errs := []error{} 74 for _, err := range result.Errors { 75 errs = append(errs, errors.New(err.Text)) 76 } 77 return nil, errs 78 } 79 80 return result.Code, nil 81}