-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite): add tsconfig paths resolution plugin
- Loading branch information
1 parent
7d15e0c
commit 948e075
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { workspaceRoot } from '@nx/devkit'; | ||
import { relative, join, resolve } from 'node:path'; | ||
import { loadConfig, createMatchPath, MatchPath } from 'tsconfig-paths'; | ||
|
||
export function nxViteTsPaths() { | ||
let matchTsPath: MatchPath; | ||
|
||
return { | ||
name: 'nx-vite-ts-paths', | ||
configResolved(config: any) { | ||
const projectRoot = config.root; | ||
const tsConfigGeneratedPath = join( | ||
'tmp', | ||
relative(workspaceRoot, projectRoot) | ||
); | ||
|
||
const tmpRoot = resolve(workspaceRoot, tsConfigGeneratedPath); | ||
// TODO(caleb): fallback to root level tsconfig file if generated doesn't not exist | ||
const tsConfigPath = resolve(tmpRoot, 'tsconfig.generated.json'); | ||
const parsed = loadConfig(tsConfigPath); | ||
|
||
if (parsed.resultType === 'failed') { | ||
throw new Error('unable to load tsconfig'); | ||
} | ||
matchTsPath = createMatchPath(parsed.absoluteBaseUrl, parsed.paths, [ | ||
// TODO(caleb): should there be any other places to match? | ||
['exports', '.', 'import'], | ||
]); | ||
}, | ||
resolveId(source: string) { | ||
const resolvedFile = matchTsPath(source); | ||
return resolvedFile; | ||
}, | ||
}; | ||
} |