This PR adds a `@nx/vite:setup-paths-plugin` generator to add
`nxViteTsPaths` plugin to all vite config files in the workspace. This
can also be used with init/add as follows:
```shell
nx add @nx/vite --setupPathsPlugin
nx g @nx/vite:init --setupPathsPlugin
```
Which takes a config such as:
```ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})
```
And updates it to:
```ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
export default defineConfig({
plugins: [react(), nxViteTsPaths()],
})
```
Taking into account ESM (default) and CJS (deprecated).
<!-- 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` -->
## 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 #
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { ProjectGraph, stripIndents, Tree } from '@nx/devkit';
|
|
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
|
import { setupPathsPlugin } from './setup-paths-plugin';
|
|
|
|
let projectGraph: ProjectGraph;
|
|
jest.mock('@nx/devkit', () => ({
|
|
...jest.requireActual<any>('@nx/devkit'),
|
|
createProjectGraphAsync: jest.fn().mockImplementation(async () => {
|
|
return projectGraph;
|
|
}),
|
|
}));
|
|
|
|
describe('@nx/vite:init', () => {
|
|
let tree: Tree;
|
|
|
|
beforeEach(() => {
|
|
tree = createTreeWithEmptyWorkspace();
|
|
projectGraph = {
|
|
nodes: {},
|
|
dependencies: {},
|
|
};
|
|
});
|
|
|
|
it('should add nxViteTsPaths plugin to vite config files', async () => {
|
|
tree.write(
|
|
'proj1/vite.config.ts',
|
|
stripIndents`
|
|
import { defineConfig } from 'vite';
|
|
export default defineConfig({});`
|
|
);
|
|
tree.write(
|
|
'proj2/vite.config.ts',
|
|
stripIndents`
|
|
import { defineConfig } from 'vite'
|
|
import react from '@vitejs/plugin-react'
|
|
export default defineConfig({
|
|
plugins: [react()],
|
|
})`
|
|
);
|
|
tree.write(
|
|
'proj3/vite.config.cts',
|
|
stripIndents`
|
|
const { defineConfig } = require('vite');
|
|
const react = require('@vitejs/plugin-react');
|
|
module.exports = defineConfig({
|
|
plugins: [react()],
|
|
});
|
|
`
|
|
);
|
|
|
|
await setupPathsPlugin(tree, {});
|
|
|
|
expect(tree.read('proj1/vite.config.ts').toString()).toMatchInlineSnapshot(`
|
|
"import { defineConfig } from 'vite';
|
|
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
export default defineConfig({ plugins: [nxViteTsPaths()] });
|
|
"
|
|
`);
|
|
expect(tree.read('proj2/vite.config.ts').toString()).toMatchInlineSnapshot(`
|
|
"import { defineConfig } from 'vite';
|
|
import react from '@vitejs/plugin-react';
|
|
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
export default defineConfig({
|
|
plugins: [react(), nxViteTsPaths()],
|
|
});
|
|
"
|
|
`);
|
|
expect(tree.read('proj3/vite.config.cts').toString())
|
|
.toMatchInlineSnapshot(`
|
|
"const { nxViteTsPaths } = require('@nx/vite/plugins/nx-tsconfig-paths.plugin');
|
|
const { defineConfig } = require('vite');
|
|
const react = require('@vitejs/plugin-react');
|
|
module.exports = defineConfig({
|
|
plugins: [react(), nxViteTsPaths()],
|
|
});
|
|
"
|
|
`);
|
|
});
|
|
});
|