From 7ba7f3e79838d2681d11f7dc14046eb657060836 Mon Sep 17 00:00:00 2001 From: Egor Kuzin <30974622+q2een@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:00:02 +0300 Subject: [PATCH] fix(vite): tsconfig paths plugin should resolve file with dot in the name (#28701) closed #28615 ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #28615 --- e2e/vite/src/vite.test.ts | 29 +++++++++++++++++++ .../vite/plugins/nx-tsconfig-paths.plugin.ts | 8 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/e2e/vite/src/vite.test.ts b/e2e/vite/src/vite.test.ts index 7d7dc9639e..cb8e31f368 100644 --- a/e2e/vite/src/vite.test.ts +++ b/e2e/vite/src/vite.test.ts @@ -177,6 +177,35 @@ describe('@nx/vite/plugin', () => { expect(() => runCLI(`test ${mylib}`)).not.toThrow(); }); + + it('should support importing files with "." in the name in tsconfig path', () => { + const mylib = uniq('mylib'); + runCLI( + `generate @nx/react:library libs/${mylib} --bundler=none --unitTestRunner=vitest` + ); + updateFile(`libs/${mylib}/src/styles.module.css`, `.foo {}`); + updateFile(`libs/${mylib}/src/foo.enum.ts`, `export const foo = 'foo';`); + updateFile(`libs/${mylib}/src/bar.enum.ts`, `export const bar = 'bar';`); + updateFile( + `libs/${mylib}/src/foo.spec.ts`, + ` + import styles from '~/styles.module.css'; + import { foo } from '~/foo.enum.ts'; + import { bar } from '~/bar.enum'; + test('should work', () => { + expect(styles).toBeDefined(); + expect(foo).toBeDefined(); + expect(bar).toBeDefined(); + }); + ` + ); + updateJson('tsconfig.base.json', (json) => { + json.compilerOptions.paths['~/*'] = [`libs/${mylib}/src/*`]; + return json; + }); + + expect(() => runCLI(`test ${mylib}`)).not.toThrow(); + }); }); describe('react with vitest only', () => { diff --git a/packages/vite/plugins/nx-tsconfig-paths.plugin.ts b/packages/vite/plugins/nx-tsconfig-paths.plugin.ts index 73f9937e9b..7cfc3c3a67 100644 --- a/packages/vite/plugins/nx-tsconfig-paths.plugin.ts +++ b/packages/vite/plugins/nx-tsconfig-paths.plugin.ts @@ -251,9 +251,15 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th function findFile(path: string): string { for (const ext of options.extensions) { + // Support file with "." in the name. + let resolvedPath = resolve(path + ext); + if (existsSync(resolvedPath)) { + return resolvedPath; + } + // Support file extensions such as .css and .js in the import path. const { dir, name } = parse(path); - const resolvedPath = resolve(dir, name + ext); + resolvedPath = resolve(dir, name + ext); if (existsSync(resolvedPath)) { return resolvedPath; }