44 "encoding/json"
55 "errors"
66 "fmt"
7- "net/url"
87 "strings"
98
109 "github.com/esm-dev/esm.sh/internal/importmap"
@@ -17,17 +16,16 @@ type TransformOptions struct {
1716 Filename string `json:"filename"`
1817 Lang string `json:"lang"`
1918 Code string `json:"code"`
20- ImportMap json.RawMessage `json:"importMap"`
21- JsxImportSource string `json:"jsxImportSource"`
19+ ImportMapRaw json.RawMessage `json:"importMap"`
20+ JSXImportSource string `json:"jsxImportSource"`
2221 Target string `json:"target"`
2322 SourceMap string `json:"sourceMap"`
2423 Minify bool `json:"minify"`
2524}
2625
2726type ResolvedTransformOptions struct {
2827 TransformOptions
29- importMap * importmap.ImportMap
30- globalVersion string
28+ importMap * importmap.ImportMap
3129}
3230
3331type TransformOutput struct {
@@ -49,7 +47,8 @@ func transform(options *ResolvedTransformOptions) (out *TransformOutput, err err
4947
5048 loader := esbuild .LoaderJS
5149 sourceCode := options .Code
52- jsxImportSource := options .JsxImportSource
50+ jsxImportSource := options .JSXImportSource
51+ importMap := options .importMap
5352
5453 if options .Lang == "" && options .Filename != "" {
5554 filename , _ := utils .SplitByFirstByte (options .Filename , '?' )
@@ -72,16 +71,16 @@ func transform(options *ResolvedTransformOptions) (out *TransformOutput, err err
7271 return
7372 }
7473
75- if jsxImportSource == "" && (loader == esbuild .LoaderJSX || loader == esbuild .LoaderTSX ) && options . importMap != nil {
76- for _ , key := range options . importMap .Imports .Keys () {
74+ if jsxImportSource == "" && (loader == esbuild .LoaderJSX || loader == esbuild .LoaderTSX ) && importMap != nil {
75+ for _ , key := range importMap .Imports .Keys () {
7776 if before , ok := strings .CutSuffix (key , "/jsx-runtime" ); ok {
7877 jsxImportSource = before
7978 break
8079 }
8180 }
8281 if jsxImportSource == "" {
8382 for _ , key := range []string {"react/" , "preact/" , "solid-js/" , "mono-jsx/dom/" , "mono-jsx/" , "vue/" } {
84- if options . importMap .Imports .Has (key ) {
83+ if importMap .Imports .Has (key ) {
8584 jsxImportSource = strings .TrimSuffix (key , "/" )
8685 break
8786 }
@@ -121,18 +120,18 @@ func transform(options *ResolvedTransformOptions) (out *TransformOutput, err err
121120 MinifyIdentifiers : options .Minify ,
122121 Sourcemap : sourceMap ,
123122 Bundle : true ,
123+ TreeShaking : esbuild .TreeShakingTrue ,
124124 Outdir : "/esbuild" ,
125125 Write : false ,
126126 Plugins : []esbuild.Plugin {
127127 {
128128 Name : "resolver" ,
129129 Setup : func (build esbuild.PluginBuild ) {
130130 build .OnResolve (esbuild.OnResolveOptions {Filter : ".*" }, func (args esbuild.OnResolveArgs ) (esbuild.OnResolveResult , error ) {
131- importerUrl , _ := url .Parse (args .Importer )
132- if options .importMap != nil {
133- path , ok := options .importMap .Resolve (args .Path , importerUrl )
131+ if importMap != nil {
132+ path , ok := importMap .Resolve (args .Path , nil )
134133 if ok && isHttpSpecifier (path ) {
135- return esbuild.OnResolveResult {Path : args . Path , External : true }, nil
134+ return esbuild.OnResolveResult {Path : path , External : true }, nil
136135 }
137136 }
138137 return esbuild.OnResolveResult {Path : args .Path , External : true }, nil
0 commit comments