Skip to content

Commit

Permalink
fix(ts-transform-paths): use fileExists, remove getSourceFile #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Mcfly committed Apr 4, 2020
1 parent d074f93 commit 0c97e90
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
1 change: 0 additions & 1 deletion packages/ts-helpers/src/ImportPathsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class ImportPathsResolver {
}
}


function isRelative(fileName: string) {
return fileName === '.' || fileName.startsWith('./') || fileName.startsWith('../')
}
49 changes: 23 additions & 26 deletions packages/ts-transform-paths/src/ImportPathInternalResolver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import ts from 'typescript'
import path from 'path'
import { ImportPathsResolver } from '@zerollup/ts-helpers'
import { Config, EmitHost, TransformationContext } from './Types'
import { Config, EmitHost, Program, TransformationContext } from './Types'

const fileExistsParts = ['.min.js', '.js', ''] as const
const fileExistsParts = ['.min.js', '.js'] as const

const tsParts = ['.d.ts','.ts', '.tsx', '/index.ts', '/index.tsx', '/index.d.ts', ''] as const
const tsParts = ['.ts', '.d.ts', '.tsx', '/index.ts', '/index.tsx', '/index.d.ts', ''] as const

export class ImportPathInternalResolver {
protected resolver: ImportPathsResolver
protected emitHost: EmitHost | undefined

constructor(
protected program: ts.Program | undefined,
protected program: Program | undefined,
transformationContext: TransformationContext,
protected config: Config
) {
Expand All @@ -27,36 +26,34 @@ export class ImportPathInternalResolver {
: undefined
}

fileExists(file: string) {
const { program, emitHost } = this
if (program?.fileExists) return program.fileExists(file)
if (emitHost?.fileExists) return emitHost.fileExists(file)

return true
}

resolveImport(oldImport: string, currentDir: string): string | undefined {
const { emitHost, config } = this
const config = this.config
const newImports = this.resolver.getImportSuggestions(oldImport, currentDir)

if (!newImports) return

for (let newImport of newImports) {
const host = this.program ?? emitHost

if (host) {
let newImportPath = path.join(currentDir, newImport)
if (newImportPath[0] === '.') newImportPath = newImportPath.substring(2)
for (let part of tsParts) {
if (host.getSourceFile(`${newImportPath}${part}`)) return newImport
}
const newImportPath = path.join(currentDir, newImport)

for (let part of tsParts) {
if (this.fileExists(`${newImportPath}${part}`)) return newImport
}

if (emitHost && emitHost.fileExists) {
if (config.tryLoadJs) {
for (let ext of fileExistsParts) {
const importWithExtension = `${newImport}${ext}`
if (emitHost.fileExists(path.join(currentDir, importWithExtension)))
return importWithExtension
}
}
if (config.fileCkeckExists) {
if (emitHost.fileExists(path.join(currentDir, newImport))) return newImport
if (config.tryLoadJs) {
for (let ext of fileExistsParts) {
if (this.fileExists(`${newImportPath}${ext}`))
return `${newImport}${ext}`
}
}

if (!host) return newImport

}
}
}
16 changes: 6 additions & 10 deletions packages/ts-transform-paths/src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ export interface Config {
* @default false
*/
tryLoadJs?: boolean

/**
* Use emitHost.fileExists to detect if import file exists. Usable for imports like some/Button.css
* @default true
*/
fileCkeckExists?: boolean
}

export const defaultConfig: Config = {
fileCkeckExists: true
}
export const defaultConfig: Config = {}

type FileExists = Partial<Pick<ts.ModuleResolutionHost, 'fileExists'>>

export type EmitHost = FileExists

export type EmitHost = ts.ModuleResolutionHost & Pick<ts.Program, 'getSourceFile'>
export type Program = ts.Program & FileExists

export type TransformationContext = ts.TransformationContext & {
getEmitHost?: () => EmitHost
Expand Down

0 comments on commit 0c97e90

Please sign in to comment.