fix(vite): tsconfig paths plugin should resolve file extensions (#27774)
This PR updates the tsconfig paths plugin for Vite such that it resolves paths with file extensions as well. <!-- 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 #22868
This commit is contained in:
parent
dbf7c2030c
commit
eb7ac2c57a
@ -7,6 +7,7 @@ import {
|
||||
runCommandUntil,
|
||||
uniq,
|
||||
updateFile,
|
||||
updateJson,
|
||||
} from '@nx/e2e/utils';
|
||||
import { ChildProcess } from 'child_process';
|
||||
import { names } from '@nx/devkit';
|
||||
@ -158,6 +159,32 @@ describe('@nx/vite/plugin', () => {
|
||||
await killProcessAndPorts(process.pid, port);
|
||||
}
|
||||
});
|
||||
|
||||
it('should support importing .js and .css files in tsconfig path', () => {
|
||||
const mylib = uniq('mylib');
|
||||
runCLI(
|
||||
`generate @nx/react:library ${mylib} --bundler=none --unitTestRunner=vitest --directory=libs/${mylib} --project-name-and-root-format=as-provided`
|
||||
);
|
||||
updateFile(`libs/${mylib}/src/styles.css`, `.foo {}`);
|
||||
updateFile(`libs/${mylib}/src/foo.mts`, `export const foo = 'foo';`);
|
||||
updateFile(
|
||||
`libs/${mylib}/src/foo.spec.ts`,
|
||||
`
|
||||
import styles from '~/styles.css?inline';
|
||||
import { foo } from '~/foo.mjs';
|
||||
test('should work', () => {
|
||||
expect(styles).toBeDefined();
|
||||
expect(foo).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', () => {
|
||||
@ -5,7 +5,7 @@ import {
|
||||
workspaceRoot,
|
||||
} from '@nx/devkit';
|
||||
import { copyFileSync, existsSync } from 'node:fs';
|
||||
import { relative, join, resolve } from 'node:path';
|
||||
import { join, parse, relative, resolve } from 'node:path';
|
||||
import {
|
||||
loadConfig,
|
||||
createMatchPath,
|
||||
@ -57,8 +57,13 @@ export function nxViteTsPaths(options: nxViteTsPathsOptions = {}) {
|
||||
'.js',
|
||||
'.jsx',
|
||||
'.json',
|
||||
'.mts',
|
||||
'.mjs',
|
||||
'.cts',
|
||||
'.cjs',
|
||||
'.css',
|
||||
'.scss',
|
||||
'.less',
|
||||
];
|
||||
options.mainFields ??= [['exports', '.', 'import'], 'module', 'main'];
|
||||
options.buildLibsFromSource ??= true;
|
||||
@ -246,7 +251,9 @@ 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) {
|
||||
const resolvedPath = resolve(path + ext);
|
||||
// Support file extensions such as .css and .js in the import path.
|
||||
const { dir, name } = parse(path);
|
||||
const resolvedPath = resolve(dir, name + ext);
|
||||
if (existsSync(resolvedPath)) {
|
||||
return resolvedPath;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user