MCPcopy
hub / github.com/esm-dev/esm.sh / transform

Function transform

server/transform.go:37–161  ·  view source on GitHub ↗

transform transforms the given code with the given options.

(options *ResolvedTransformOptions)

Source from the content-addressed store, hash-verified

35
36// transform transforms the given code with the given options.
37func transform(options *ResolvedTransformOptions) (out *TransformOutput, err error) {
38 target := esbuild.ESNext
39 if options.Target != "" {
40 if t, ok := targets[options.Target]; ok {
41 target = t
42 } else {
43 err = errors.New("invalid target")
44 return
45 }
46 }
47
48 loader := esbuild.LoaderJS
49 sourceCode := options.Code
50 jsxImportSource := options.JSXImportSource
51 importMap := options.importMap
52
53 if options.Lang == "" && options.Filename != "" {
54 filename, _ := utils.SplitByFirstByte(options.Filename, '?')
55 _, basename := utils.SplitByLastByte(filename, '/')
56 _, options.Lang = utils.SplitByLastByte(basename, '.')
57 }
58 switch options.Lang {
59 case "js":
60 loader = esbuild.LoaderJS
61 case "jsx":
62 loader = esbuild.LoaderJSX
63 case "ts":
64 loader = esbuild.LoaderTS
65 case "tsx":
66 loader = esbuild.LoaderTSX
67 case "css":
68 loader = esbuild.LoaderCSS
69 default:
70 err = errors.New("unsupported language:" + options.Lang)
71 return
72 }
73
74 if jsxImportSource == "" && (loader == esbuild.LoaderJSX || loader == esbuild.LoaderTSX) && importMap != nil {
75 for _, key := range importMap.Imports.Keys() {
76 if before, ok := strings.CutSuffix(key, "/jsx-runtime"); ok {
77 jsxImportSource = before
78 break
79 }
80 }
81 if jsxImportSource == "" {
82 for _, key := range []string{"react/", "preact/", "solid-js/", "mono-jsx/dom/", "mono-jsx/", "vue/"} {
83 if importMap.Imports.Has(key) {
84 jsxImportSource = strings.TrimSuffix(key, "/")
85 break
86 }
87 }
88 }
89 if jsxImportSource == "" {
90 jsxImportSource = "react"
91 }
92 }
93
94 sourceMap := esbuild.SourceMapNone

Callers 6

transformSolidFunction · 0.85
transformSolidFunction · 0.85
esmRouterFunction · 0.85
runFunction · 0.85
transformVueFunction · 0.85

Calls 5

isHttpSpecifierFunction · 0.85
HasMethod · 0.80
ResolveMethod · 0.80
BuildMethod · 0.80
KeysMethod · 0.45

Tested by

no test coverage detected