Skip to content

Commit e4d27e3

Browse files
committed
feat: support resolving jsxImportSource by referrer
1 parent 07d4196 commit e4d27e3

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
@@ -257,6 +258,24 @@ func (r *resolverWrapper) GetPackageScopeForPath(directory string) *packagejson.
257258
return r.inner.GetPackageScopeForPath(directory)
258259
}
259260

261+
// ResolveJsxImportSource implements module.ResolverInterface.
262+
func (r *resolverWrapper) ResolveJsxImportSource(referrerPath string) string {
263+
if r.server.CallbackEnabled(CallbackResolveJsxImportSource) {
264+
result, err := r.server.call("resolveJsxImportSource", referrerPath)
265+
if err != nil {
266+
panic(err)
267+
}
268+
if len(result) > 0 {
269+
var res string
270+
if err := json.Unmarshal(result, &res); err != nil {
271+
panic(err)
272+
}
273+
return res
274+
}
275+
}
276+
return r.inner.ResolveJsxImportSource(referrerPath)
277+
}
278+
260279
// ResolveModuleName implements module.ResolverInterface.
261280
func (r *resolverWrapper) ResolveModuleName(moduleName string, containingFile string, importAttributeType *string, resolutionMode core.ResolutionMode, redirectedReference module.ResolvedProjectReference) (*module.ResolvedModule, []module.DiagAndArgs) {
262281
if r.server.CallbackEnabled(CallbackResolveModuleName) {
@@ -524,6 +543,8 @@ func (s *Server) enableCallback(callback string) error {
524543
s.enabledCallbacks |= CallbackReadFile
525544
case "realpath":
526545
s.enabledCallbacks |= CallbackRealpath
546+
case "resolveJsxImportSource":
547+
s.enabledCallbacks |= CallbackResolveJsxImportSource
527548
case "resolveModuleName":
528549
s.enabledCallbacks |= CallbackResolveModuleName
529550
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
@@ -419,7 +419,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
419419
t.importHelpersImportSpecifier = specifier
420420
}
421421

422-
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(optionsForFile, file), optionsForFile)
422+
jsxImport := ast.GetJSXRuntimeImport(ast.GetJSXImplicitImportBase(optionsForFile, file, p.resolver.ResolveJsxImportSource), optionsForFile)
423423
if jsxImport != "" {
424424
specifier := p.createSyntheticImport(jsxImport, file)
425425
moduleNames = append(moduleNames, specifier)

internal/module/resolver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ type ResolverInterface interface {
152152
ResolveModuleName(moduleName string, containingFile string, importAttributeType *string, resolutionMode core.ResolutionMode, redirectedReference ResolvedProjectReference) (*ResolvedModule, []DiagAndArgs)
153153
ResolvePackageDirectory(moduleName string, containingFile string, resolutionMode core.ResolutionMode, redirectedReference ResolvedProjectReference) *ResolvedModule
154154
ResolveTypeReferenceDirective(typeReferenceDirectiveName string, containingFile string, resolutionMode core.ResolutionMode, redirectedReference ResolvedProjectReference) (*ResolvedTypeReferenceDirective, []DiagAndArgs)
155+
ResolveJsxImportSource(referrer string) string
155156
GetPackageScopeForPath(directory string) *packagejson.InfoCacheEntry
156157
GetImpliedNodeFormatForFile(path string, packageJsonType string) core.ModuleKind
157158
}
@@ -284,6 +285,10 @@ func (r *Resolver) GetImpliedNodeFormatForFile(path string, packageJsonType stri
284285
return ast.GetImpliedNodeFormatForFile(path, packageJsonType)
285286
}
286287

288+
func (r *Resolver) ResolveJsxImportSource(referrer string) string {
289+
return ""
290+
}
291+
287292
func (r *Resolver) tryResolveFromTypingsLocation(moduleName string, containingDirectory string, originalResult *ResolvedModule, traceBuilder *tracer) *ResolvedModule {
288293
if r.typingsLocation == "" ||
289294
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)