fix(vite): tsconfig paths plugin should resolve file with dot in the name (#28701)

closed #28615

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #28615
This commit is contained in:
Egor Kuzin 2024-11-04 18:00:02 +03:00 committed by GitHub
parent 39b0a6c959
commit 7ba7f3e798
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -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', () => {

View File

@ -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;
}