Skip to content

Commit

Permalink
chore(misc): use our own nxViteTsPaths plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Feb 25, 2025
1 parent f9a5277 commit a74febe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function installDependencies(options: NormalizedOptions) {
'web-vitals',
'jest-watch-typeahead',
'vite',
'vite-tsconfig-paths',
'vitest',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function writeViteConfig(
`import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import replace from '@rollup/plugin-replace';
import tsconfigPaths from 'vite-tsconfig-paths';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
// Match CRA's environment variables.
// TODO: Replace these with VITE_ prefixed environment variables, and using import.meta.env.VITE_* instead of process.env.REACT_APP_*.
Expand Down Expand Up @@ -54,7 +54,7 @@ export default defineConfig({
plugins: [
react(),
replace({ values: craEnvVars, preventAssignment: true }),
tsconfigPaths(),
nxViteTsPaths(),
],
});
`
Expand Down
13 changes: 8 additions & 5 deletions packages/vite/plugins/nx-tsconfig-paths.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface nxViteTsPathsOptions {
}

export function nxViteTsPaths(options: nxViteTsPathsOptions = {}) {
let foundTsConfigPath: string;
let matchTsPathEsm: MatchPath;
let matchTsPathFallback: MatchPath | undefined;
let tsConfigPathsEsm: ConfigLoaderSuccessResult;
Expand Down Expand Up @@ -79,7 +80,7 @@ export function nxViteTsPaths(options: nxViteTsPathsOptions = {}) {
async configResolved(config: any) {
projectRoot = config.root;
const projectRootFromWorkspaceRoot = relative(workspaceRoot, projectRoot);
let foundTsConfigPath = getTsConfig(
foundTsConfigPath = getTsConfig(
process.env.NX_TSCONFIG_PATH ??
join(
workspaceRoot,
Expand All @@ -89,10 +90,8 @@ export function nxViteTsPaths(options: nxViteTsPathsOptions = {}) {
'tsconfig.generated.json'
)
);
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}`);
}

if (!foundTsConfigPath) return;

if (
!options.buildLibsFromSource &&
Expand Down Expand Up @@ -164,6 +163,9 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th
}
},
resolveId(importPath: string) {
// Let other resolvers handle this path.
if (!foundTsConfigPath) return null;

let resolvedFile: string;
try {
resolvedFile = matchTsPathEsm(importPath);
Expand Down Expand Up @@ -211,6 +213,7 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th
resolve(preferredTsConfigPath),
resolve(join(workspaceRoot, 'tsconfig.base.json')),
resolve(join(workspaceRoot, 'tsconfig.json')),
resolve(join(workspaceRoot, 'jsconfig.json')),
].find((tsPath) => {
if (existsSync(tsPath)) {
logIt('Found tsconfig at', tsPath);
Expand Down

0 comments on commit a74febe

Please sign in to comment.