Skip to content

Commit e295b8c

Browse files
committed
feat: support resolving jsxImportSource by referrer
1 parent 383c1a6 commit e295b8c

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

internal/api/server.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
CallbackGetAccessibleEntries
6969
CallbackReadFile
7070
CallbackRealpath
71+
CallbackResolveJsxImportSource
7172
CallbackResolveModuleName
7273
CallbackResolveTypeReferenceDirective
7374
CallbackGetPackageJsonScopeIfApplicable
@@ -290,6 +291,24 @@ func (r *resolverWrapper) GetPackageScopeForPath(directory string) *packagejson.
290291
return r.inner.GetPackageScopeForPath(directory)
291292
}
292293

294+
// ResolveJsxImportSource implements module.ResolverInterface.
295+
func (r *resolverWrapper) ResolveJsxImportSource(referrerPath string) string {
296+
if r.server.CallbackEnabled(CallbackResolveJsxImportSource) {
297+
result, err := r.server.call("resolveJsxImportSource", referrerPath)
298+
if err != nil {
299+
panic(err)
300+
}
301+
if len(result) > 0 {
302+
var res string
303+
if err := json.Unmarshal(result, &res); err != nil {
304+
panic(err)
305+
}
306+
return res
307+
}
308+
}
309+
return r.inner.ResolveJsxImportSource(referrerPath)
310+
}
311+
293312
// ResolveModuleName implements module.ResolverInterface.
294313
func (r *resolverWrapper) ResolveModuleName(moduleName string, containingFile string, importAttributeType *string, resolutionMode core.ResolutionMode, redirectedReference module.ResolvedProjectReference) (*module.ResolvedModule, []module.DiagAndArgs) {
295314
if r.server.CallbackEnabled(CallbackResolveModuleName) {
@@ -534,6 +553,8 @@ func (s *Server) enableCallback(callback string) error {
534553
s.enabledCallbacks |= CallbackReadFile
535554
case "realpath":
536555
s.enabledCallbacks |= CallbackRealpath
556+
case "resolveJsxImportSource":
557+
s.enabledCallbacks |= CallbackResolveJsxImportSource
537558
case "resolveModuleName":
538559
s.enabledCallbacks |= CallbackResolveModuleName
539560
case "resolveTypeReferenceDirective":

internal/ast/utilities.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,12 +2657,19 @@ func IsRequireVariableStatement(node *Node) bool {
26572657
return false
26582658
}
26592659

2660-
func GetJSXImplicitImportBase(compilerOptions *core.CompilerOptions, file *SourceFile) string {
2660+
func GetJSXImplicitImportBase(compilerOptions *core.CompilerOptions, file *SourceFile, resolveJsxImportSource func(referrer string) string) string {
26612661
jsxImportSourcePragma := GetPragmaFromSourceFile(file, "jsximportsource")
26622662
jsxRuntimePragma := GetPragmaFromSourceFile(file, "jsxruntime")
26632663
if GetPragmaArgument(jsxRuntimePragma, "factory") == "classic" {
26642664
return ""
26652665
}
2666+
// deno: resolve the jsxImportSource based on the referrer
2667+
if file != nil && resolveJsxImportSource != nil {
2668+
resolvedJsxImportSource := resolveJsxImportSource(file.FileName())
2669+
if resolvedJsxImportSource != "" {
2670+
return resolvedJsxImportSource
2671+
}
2672+
}
26662673
if compilerOptions.Jsx == core.JsxEmitReactJSX ||
26672674
compilerOptions.Jsx == core.JsxEmitReactJSXDev ||
26682675
compilerOptions.JsxImportSource != "" ||

internal/compiler/fileloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
530530
t.importHelpersImportSpecifier = specifier
531531
}
532532

533-
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(optionsForFile, file), optionsForFile)
533+
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(optionsForFile, file, p.resolver.ResolveJsxImportSource), optionsForFile)
534534
if jsxImport != "" {
535535
specifier := p.createSyntheticImport(jsxImport, file)
536536
moduleNames = append(moduleNames, specifier)

internal/module/resolver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func GetCompilerOptionsWithRedirect(compilerOptions *core.CompilerOptions, redir
150150
type ResolverInterface interface {
151151
ResolveModuleName(moduleName string, containingFile string, importAttributeType *string, resolutionMode core.ResolutionMode, redirectedReference ResolvedProjectReference) (*ResolvedModule, []DiagAndArgs)
152152
ResolveTypeReferenceDirective(typeReferenceDirectiveName string, containingFile string, resolutionMode core.ResolutionMode, redirectedReference ResolvedProjectReference) (*ResolvedTypeReferenceDirective, []DiagAndArgs)
153+
ResolveJsxImportSource(referrer string) string
153154
GetPackageJsonScopeIfApplicable(path string) *packagejson.InfoCacheEntry
154155
GetPackageScopeForPath(directory string) *packagejson.InfoCacheEntry
155156
GetImpliedNodeFormatForFile(path string, packageJsonType string) core.ModuleKind
@@ -285,6 +286,10 @@ func (r *Resolver) GetImpliedNodeFormatForFile(path string, packageJsonType stri
285286
return ast.GetImpliedNodeFormatForFile(path, packageJsonType)
286287
}
287288

289+
func (r *Resolver) ResolveJsxImportSource(referrer string) string {
290+
return ""
291+
}
292+
288293
func (r *Resolver) tryResolveFromTypingsLocation(moduleName string, containingDirectory string, originalResult *ResolvedModule, traceBuilder *tracer) *ResolvedModule {
289294
if r.typingsLocation == "" ||
290295
tspath.IsExternalModuleNameRelative(moduleName) ||

internal/transformers/jsxtransforms/jsx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (tx *JSXTransformer) visitSourceFile(file *ast.SourceFile) *ast.Node {
211211
}
212212

213213
tx.currentSourceFile = file
214-
tx.importSpecifier = ast.GetJSXImplicitImportBase(tx.compilerOptions, file)
214+
tx.importSpecifier = ast.GetJSXImplicitImportBase(tx.compilerOptions, file, nil)
215215
tx.filenameDeclaration = nil
216216
tx.utilizedImplicitRuntimeImports = make(map[string]map[string]*ast.Node)
217217

0 commit comments

Comments
 (0)