Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/runner/resolveFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { normalizeFilePath } from "./normalizeFsMap"
import { dirname } from "lib/utils/dirname"
import { resolveWithTsconfigPaths, type TsConfig } from "./tsconfigPaths"
import { resolveRelativePath } from "lib/utils/resolveRelativePath"
import {
resolveWithTsconfigPaths,
type TsConfig,
resolveWithBaseUrl,
} from "./tsconfigPaths"

export const resolveFilePath = (
unknownFilePath: string,
Expand Down Expand Up @@ -48,13 +51,21 @@ export const resolveFilePath = (
const tsConfig = opts.tsConfig ?? null

if (!unknownFilePath.startsWith("./") && !unknownFilePath.startsWith("../")) {
const viaTsconfig = resolveWithTsconfigPaths({
const resolvedPathFromPaths = resolveWithTsconfigPaths({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resolvedPathFromPaths = resolveWithTsconfigPaths({
const resolvedPathFromTsconfigPaths = resolveWithTsconfigPaths({

importPath: unknownFilePath,
normalizedFilePathMap,
extensions: extension,
tsConfig,
})
if (viaTsconfig) return viaTsconfig
if (resolvedPathFromPaths) return resolvedPathFromPaths

const resolvedPathFromBaseUrl = resolveWithBaseUrl({
importPath: unknownFilePath,
normalizedFilePathMap,
extensions: extension,
tsConfig,
})
if (resolvedPathFromBaseUrl) return resolvedPathFromBaseUrl
}

// Check if it's an absolute import
Expand Down
36 changes: 36 additions & 0 deletions lib/runner/tsconfigPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ export function resolveWithTsconfigPaths(opts: {
}
}

const resolvedPathFromBaseUrl = resolveWithBaseUrl({
importPath,
normalizedFilePathMap,
extensions,
tsConfig,
})

if (resolvedPathFromBaseUrl) return resolvedPathFromBaseUrl

return null
}

export function resolveWithBaseUrl(opts: {
importPath: string
normalizedFilePathMap: Map<string, string>
extensions: string[]
tsConfig: TsConfig | null
}): string | null {
const { importPath, normalizedFilePathMap, extensions, tsConfig } = opts
const baseUrl = tsConfig?.compilerOptions?.baseUrl
if (!baseUrl) return null

const filePathToResolve = `${baseUrl}/${importPath}`
const normalizedFilePath = normalizeFilePath(filePathToResolve)

if (normalizedFilePathMap.has(normalizedFilePath)) {
return normalizedFilePathMap.get(normalizedFilePath)!
}

for (const ext of extensions) {
const withExt = `${normalizedFilePath}.${ext}`
if (normalizedFilePathMap.has(withExt)) {
return normalizedFilePathMap.get(withExt)!
}
}

return null
}

Expand Down
33 changes: 33 additions & 0 deletions tests/features/tsconfig-paths-resolution.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,36 @@ test("throws error when tsconfig path alias cannot be resolved (instead of tryin
'Import "@utils/missing" matches a tsconfig path alias but could not be resolved to an existing file',
)
})

test("resolves imports using tsconfig baseUrl", async () => {
const circuitJson = await runTscircuitCode(
{
"tsconfig.json": JSON.stringify({
compilerOptions: {
baseUrl: "src",
},
}),
"src/utils/values.ts": `
export const resistorName = "RbaseUrl"
export const resistance = "2k"
`,
"src/component.tsx": `
import { resistorName, resistance } from "utils/values"
export default () => (<resistor name={resistorName} resistance={resistance} />)
`,
"user.tsx": `
import Comp from "component"
export default () => (<Comp />)
`,
},
{
mainComponentPath: "user",
},
)

const resistor = circuitJson.find(
(el) => el.type === "source_component" && el.name === "RbaseUrl",
) as any
expect(resistor).toBeDefined()
expect(resistor.resistance).toBe(2000)
})