Skip to content

Commit 4e58eaf

Browse files
authored
Fix transform api (esm-dev#1333)
1 parent d57f609 commit 4e58eaf

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

server/router.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ func esmRouter(esmStorage storage.Storage, logger *log.Logger) rex.Handle {
130130
h.Write([]byte(options.Lang))
131131
h.Write([]byte(options.Code))
132132
h.Write([]byte(options.Target))
133-
h.Write(options.ImportMap)
134-
h.Write([]byte(options.JsxImportSource))
133+
h.Write(options.ImportMapRaw)
134+
h.Write([]byte(options.JSXImportSource))
135135
h.Write([]byte(options.SourceMap))
136136
fmt.Fprintf(h, "%v", options.Minify)
137137
hash := hex.EncodeToString(h.Sum(nil))
@@ -159,8 +159,8 @@ func esmRouter(esmStorage storage.Storage, logger *log.Logger) rex.Handle {
159159
}
160160

161161
var importMap *importmap.ImportMap
162-
if len(options.ImportMap) > 0 {
163-
importMap, err = importmap.Parse(nil, options.ImportMap)
162+
if len(options.ImportMapRaw) > 0 {
163+
importMap, err = importmap.Parse(nil, options.ImportMapRaw)
164164
if err != nil {
165165
return rex.Err(400, "Invalid ImportMap")
166166
}

server/transform.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
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

2726
type ResolvedTransformOptions struct {
2827
TransformOptions
29-
importMap *importmap.ImportMap
30-
globalVersion string
28+
importMap *importmap.ImportMap
3129
}
3230

3331
type 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

test/transform/transform.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { assertEquals, assertStringIncludes } from "jsr:@std/assert";
22

33
Deno.test("transform API", async () => {
44
const options = {
5-
lang: "jsx",
5+
lang: "tsx",
66
code: `
77
import { renderToString } from "preact-render-to-string";
8-
export default () => renderToString(<h1>esm.sh</h1>);
8+
const render: (() => string) = () => renderToString(<h1>esm.sh</h1>);
9+
export default render;
910
`,
1011
target: "es2022",
1112
importMap: {
@@ -27,7 +28,9 @@ Deno.test("transform API", async () => {
2728
});
2829
assertEquals(res1.status, 200);
2930
const transformOut = await res1.json();
30-
assertStringIncludes(transformOut.code, `"preact/jsx-runtime"`);
31+
assertStringIncludes(transformOut.code, `"https://esm.sh/preact-render-to-string@6.0.2"`);
32+
assertStringIncludes(transformOut.code, `"https://esm.sh/preact@10.13.2/jsx-runtime"`);
33+
assertStringIncludes(transformOut.code, `jsx as`);
3134
assertStringIncludes(transformOut.code, `("h1"`);
3235
assertStringIncludes(transformOut.code, `//# sourceMappingURL=+${hash}.mjs.map`);
3336
assertStringIncludes(transformOut.map, `"mappings":`);

0 commit comments

Comments
 (0)