From 0c97e900fa171a52425c4d4410c30b2df109038e Mon Sep 17 00:00:00 2001 From: Marty Mcfly Date: Sat, 4 Apr 2020 18:32:38 +0300 Subject: [PATCH] fix(ts-transform-paths): use fileExists, remove getSourceFile #30 --- .../ts-helpers/src/ImportPathsResolver.ts | 1 - .../src/ImportPathInternalResolver.ts | 49 +++++++++---------- packages/ts-transform-paths/src/Types.ts | 16 +++--- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/packages/ts-helpers/src/ImportPathsResolver.ts b/packages/ts-helpers/src/ImportPathsResolver.ts index 83229ae..e21c283 100644 --- a/packages/ts-helpers/src/ImportPathsResolver.ts +++ b/packages/ts-helpers/src/ImportPathsResolver.ts @@ -58,7 +58,6 @@ export class ImportPathsResolver { } } - function isRelative(fileName: string) { return fileName === '.' || fileName.startsWith('./') || fileName.startsWith('../') } diff --git a/packages/ts-transform-paths/src/ImportPathInternalResolver.ts b/packages/ts-transform-paths/src/ImportPathInternalResolver.ts index ee8fbff..9f1129e 100644 --- a/packages/ts-transform-paths/src/ImportPathInternalResolver.ts +++ b/packages/ts-transform-paths/src/ImportPathInternalResolver.ts @@ -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 ) { @@ -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 - } } } diff --git a/packages/ts-transform-paths/src/Types.ts b/packages/ts-transform-paths/src/Types.ts index b9aa563..f9bc16d 100644 --- a/packages/ts-transform-paths/src/Types.ts +++ b/packages/ts-transform-paths/src/Types.ts @@ -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> + +export type EmitHost = FileExists -export type EmitHost = ts.ModuleResolutionHost & Pick +export type Program = ts.Program & FileExists export type TransformationContext = ts.TransformationContext & { getEmitHost?: () => EmitHost