Files
wallet/apps/extension/config/getTsconfigAliases.ts
T
Hanzo AI 78b18797d5 init: extract wallet from lux/exchange monorepo
Copies apps/extension, apps/mobile and 10 pkgs (wallet, ui, config,
utilities, sessions, gating, notifications, analytics, eslint-config,
prices) into a standalone pnpm workspace.  Resolves merge conflict
markers in extension and mobile package.json files, strips build
artifacts (.wxt, .output, Pods).
2026-04-11 01:59:19 -07:00

53 lines
1.8 KiB
TypeScript

/* oxlint-disable import/no-unused-modules */
import fs from 'fs'
import path from 'path'
/**
* Dynamically generates Vite aliases from TypeScript path mappings in tsconfig.base.json.
* This ensures Vite understands monorepo package paths without manual configuration.
*
* @param tsconfigPath - Path to the tsconfig file. Defaults to ../../tsconfig.base.json relative to this file.
* @returns Record of alias names to resolved absolute paths
*/
export function getTsconfigAliases(
tsconfigPath: string = path.resolve(__dirname, '../../../tsconfig.base.json'),
): Record<string, string> {
const aliases: Record<string, string> = {}
// Validate tsconfig file exists
if (!fs.existsSync(tsconfigPath)) {
throw new Error(`tsconfig file not found at: ${tsconfigPath}`)
}
// Read and parse tsconfig file
let tsconfig: { compilerOptions?: { paths?: Record<string, string[]> } }
try {
const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf-8')
tsconfig = JSON.parse(tsconfigContent)
} catch (error) {
throw new Error(`Failed to read or parse tsconfig at ${tsconfigPath}`, { cause: error })
}
// Extract paths from compilerOptions
const paths: Record<string, string[]> | undefined = tsconfig.compilerOptions?.paths
if (!paths || typeof paths !== 'object') {
return aliases
}
const tsconfigDir = path.dirname(tsconfigPath)
for (const [alias, pathMapping] of Object.entries(paths)) {
if (!Array.isArray(pathMapping) || !pathMapping[0]) {
continue
}
const targetPath = pathMapping[0]
// Strip wildcard /* from both alias and target if present
const aliasBase = alias.replace(/\/\*$/, '')
const targetPathBase = targetPath.replace(/\/\*$/, '')
// Resolve to absolute path relative to tsconfig directory
aliases[aliasBase] = path.resolve(tsconfigDir, targetPathBase)
}
return aliases
}