js: Support JSX and JSXImportSourceOptions

Fixes #12118
This commit is contained in:
Benjamin Altpeter
2024-02-22 17:58:22 +01:00
committed by Bjørn Erik Pedersen
parent 36bf3cb98c
commit 554aa58db6
4 changed files with 84 additions and 0 deletions

View File

@@ -86,6 +86,14 @@ type Options struct {
// What to use instead of React.Fragment.
JSXFragment string
// What to do about JSX syntax.
// See https://esbuild.github.io/api/#jsx
JSX string
// Which library to use to automatically import JSX helper functions from. Only works if JSX is set to automatic.
// See https://esbuild.github.io/api/#jsx-import-source
JSXImportSource string
// There is/was a bug in WebKit with severe performance issue with the tracking
// of TDZ checks in JavaScriptCore.
//
@@ -375,6 +383,19 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
return
}
var jsx api.JSX
switch opts.JSX {
case "", "transform":
jsx = api.JSXTransform
case "preserve":
jsx = api.JSXPreserve
case "automatic":
jsx = api.JSXAutomatic
default:
err = fmt.Errorf("unsupported jsx type: %q", opts.JSX)
return
}
var defines map[string]string
if opts.Defines != nil {
defines = maps.ToStringMapString(opts.Defines)
@@ -416,6 +437,9 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
JSXFactory: opts.JSXFactory,
JSXFragment: opts.JSXFragment,
JSX: jsx,
JSXImportSource: opts.JSXImportSource,
Tsconfig: opts.tsConfig,
// Note: We're not passing Sourcefile to ESBuild.

View File

@@ -135,6 +135,20 @@ func TestToBuildOptions(t *testing.T) {
Loader: api.LoaderJS,
},
})
opts, err = toBuildOptions(Options{mediaType: media.Builtin.JavascriptType,
JSX: "automatic", JSXImportSource: "preact"})
c.Assert(err, qt.IsNil)
c.Assert(opts, qt.DeepEquals, api.BuildOptions{
Bundle: true,
Target: api.ESNext,
Format: api.FormatIIFE,
Stdin: &api.StdinOptions{
Loader: api.LoaderJS,
},
JSX: api.JSXAutomatic,
JSXImportSource: "preact",
})
}
func TestResolveComponentInAssets(t *testing.T) {