-
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 694577d
Showing
6 changed files
with
74 additions
and
10 deletions.
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,60 @@ | ||
import { stripIndents, workspaceRoot } from '@nx/devkit'; | ||
import { existsSync } from 'node:fs'; | ||
import { relative, join, resolve, posix } from 'node:path'; | ||
import { loadConfig, createMatchPath, MatchPath } from 'tsconfig-paths'; | ||
|
||
// TODO(caleb): should we provide a way to override anything for the plugin? | ||
|
||
export function nxViteTsPaths() { | ||
let matchTsPath: MatchPath; | ||
|
||
return { | ||
name: 'nx-vite-ts-paths', | ||
configResolved(config: any) { | ||
const projectRoot = config.root; | ||
// TODO(caleb): verify on windows to see what type of paths vite returns posix vs win32 | ||
const projectRootFromWorkspaceRoot = relative(workspaceRoot, projectRoot); | ||
|
||
const foundTsConfigPath = getTsConfig(projectRootFromWorkspaceRoot); | ||
if (!foundTsConfigPath) { | ||
throw new Error(stripIndents`Unable to find a tsconfig in the workspace! | ||
There should at least be a tsconfig.base.json or tsconfig.json in the root of the workspace ${workspaceRoot}`); | ||
} | ||
const parsed = loadConfig(foundTsConfigPath); | ||
|
||
if (parsed.resultType === 'failed') { | ||
throw new Error(`Failed loading tsonfig at ${foundTsConfigPath}`); | ||
} | ||
|
||
matchTsPath = createMatchPath(parsed.absoluteBaseUrl, parsed.paths, [ | ||
['exports', '.', 'import'], | ||
]); | ||
}, | ||
resolveId(source: string) { | ||
const resolvedFile = matchTsPath(source); | ||
|
||
if (!resolvedFile) { | ||
console.warn( | ||
`[Nx Vite TsPaths] Unable to resolve ${source} with tsconfig paths` | ||
); | ||
} | ||
|
||
return resolvedFile; | ||
}, | ||
}; | ||
} | ||
|
||
function getTsConfig( | ||
projectRoot: string, | ||
tmpTsConfigName = 'tsconfig.generated.json' | ||
): string { | ||
return [ | ||
resolve(join(workspaceRoot, 'tmp', projectRoot, tmpTsConfigName)), | ||
resolve(join(workspaceRoot, 'tsconfig.base.json')), | ||
resolve(join(workspaceRoot, 'tsconfig.json')), | ||
].find((tsPath) => { | ||
if (existsSync(tsPath)) { | ||
return tsPath; | ||
} | ||
}); | ||
} |
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
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