package transform import ( "errors" "github.com/evanw/esbuild/pkg/api" ) type Format int const ( FormatCommonJS Format = iota FormatIIFE ) func Transform(file, src string, format Format) ([]byte, []error) { var fmt api.Format switch format { case FormatCommonJS: fmt = api.FormatCommonJS case FormatIIFE: fmt = api.FormatIIFE default: return nil, []error{errors.New("unknown format")} } minify := true result := api.Transform(src, api.TransformOptions{ Color: 0, LogLevel: 0, LogLimit: 0, LogOverride: nil, Sourcemap: api.SourceMapInline, SourceRoot: "", SourcesContent: 0, Target: api.ESNext, Engines: nil, Supported: nil, Platform: api.PlatformNode, Format: fmt, GlobalName: "__export__", MangleProps: "", ReserveProps: "", MangleQuoted: 0, MangleCache: nil, Drop: 0, DropLabels: nil, MinifyWhitespace: minify, MinifyIdentifiers: minify, MinifySyntax: minify, LineLimit: 0, Charset: 0, TreeShaking: api.TreeShakingDefault, IgnoreAnnotations: false, LegalComments: 0, JSX: api.JSXAutomatic, JSXFactory: "h", JSXFragment: "b", JSXImportSource: "react", JSXDev: false, JSXSideEffects: false, TsconfigRaw: "", Banner: "", Footer: "__export__", Define: nil, Pure: nil, KeepNames: false, Sourcefile: file, Loader: api.LoaderJSX, }) if len(result.Errors) > 0 { errs := []error{} for _, err := range result.Errors { errs = append(errs, errors.New(err.Text)) } return nil, errs } return result.Code, nil }