Skip to content

Commit

Permalink
fix: support baseUrl for ts source
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 12, 2023
1 parent 15ac781 commit 03b824b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/config/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function registerTSNode(root: string): TSConfig | undefined {

tsNode.register(conf)
REGISTERED.add(root)

debug('%O', tsconfig)
return tsconfig
}

Expand Down Expand Up @@ -154,9 +154,19 @@ function cannotUseTsNode(root: string, plugin: Plugin | undefined, isProduction:
function determinePath(root: string, orig: string): string {
const tsconfig = registerTSNode(root)
if (!tsconfig) return orig
const {outDir, rootDir, rootDirs} = tsconfig.compilerOptions
const rootDirPath = rootDir || (rootDirs || [])[0]
if (!rootDirPath || !outDir) return orig
debug(`determining path for ${orig}`)
const {baseUrl, outDir, rootDir, rootDirs} = tsconfig.compilerOptions
const rootDirPath = rootDir || (rootDirs || [])[0] || baseUrl
if (!rootDirPath) {
debug(`no rootDir, rootDirs, or baseUrl specified in tsconfig.json. Returning default path ${orig}`)
return orig
}

if (!outDir) {
debug(`no outDir specified in tsconfig.json. Returning default path ${orig}`)
return orig
}

// rewrite path from ./lib/foo to ./src/foo
const lib = join(root, outDir) // ./lib
const src = join(root, rootDirPath) // ./src
Expand All @@ -168,7 +178,18 @@ function determinePath(root: string, orig: string): string {
// For hooks, it might point to a module, not a file. Something like "./hooks/myhook"
// That file doesn't exist, and the real file is "./hooks/myhook.ts"
// In that case we attempt to resolve to the filename. If it fails it will revert back to the lib path
if (existsSync(out) || existsSync(out + '.ts')) return out

debug(`lib dir: ${lib}`)
debug(`src dir: ${src}`)
debug(`src commands dir: ${out}`)
if (existsSync(out) || existsSync(out + '.ts')) {
debug(`Found source file for ${orig} at ${out}`)
return out
}

debug(`No source file found. Returning default path ${orig}`)
if (!isProd()) memoizedWarn(`Could not find source for ${orig} based on tsconfig. Defaulting to compiled source.`)

return orig
}

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ts-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface TSConfig {
compilerOptions: {
baseUrl?: string
emitDecoratorMetadata?: boolean
esModuleInterop?: boolean
experimentalDecorators?: boolean
Expand Down

0 comments on commit 03b824b

Please sign in to comment.