Skip to content

Commit 4a5eb23

Browse files
authored
fix(bundling): vite init generator supports updating vite projects to use workspace libraries (#26503)
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 #
1 parent 0a4551c commit 4a5eb23

File tree

19 files changed

+376
-6
lines changed

19 files changed

+376
-6
lines changed

docs/generated/manifests/menus.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9577,6 +9577,14 @@
95779577
"isExternal": false,
95789578
"disableCollapsible": false
95799579
},
9580+
{
9581+
"id": "setup-paths-plugin",
9582+
"path": "/nx-api/vite/generators/setup-paths-plugin",
9583+
"name": "setup-paths-plugin",
9584+
"children": [],
9585+
"isExternal": false,
9586+
"disableCollapsible": false
9587+
},
95809588
{
95819589
"id": "vitest",
95829590
"path": "/nx-api/vite/generators/vitest",

docs/generated/manifests/nx-api.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,8 +2950,17 @@
29502950
"path": "/nx-api/vite/generators/configuration",
29512951
"type": "generator"
29522952
},
2953+
"/nx-api/vite/generators/setup-paths-plugin": {
2954+
"description": "Sets up the nxViteTsPaths plugin to enable support for workspace libraries.",
2955+
"file": "generated/packages/vite/generators/setup-paths-plugin.json",
2956+
"hidden": false,
2957+
"name": "setup-paths-plugin",
2958+
"originalFilePath": "/packages/vite/src/generators/setup-paths-plugin/schema.json",
2959+
"path": "/nx-api/vite/generators/setup-paths-plugin",
2960+
"type": "generator"
2961+
},
29532962
"/nx-api/vite/generators/vitest": {
2954-
"description": "Generate a vitest configuration",
2963+
"description": "Generate a vitest configuration.",
29552964
"file": "generated/packages/vite/generators/vitest.json",
29562965
"hidden": false,
29572966
"name": "vitest",

docs/generated/packages-metadata.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,16 @@
29192919
"type": "generator"
29202920
},
29212921
{
2922-
"description": "Generate a vitest configuration",
2922+
"description": "Sets up the nxViteTsPaths plugin to enable support for workspace libraries.",
2923+
"file": "generated/packages/vite/generators/setup-paths-plugin.json",
2924+
"hidden": false,
2925+
"name": "setup-paths-plugin",
2926+
"originalFilePath": "/packages/vite/src/generators/setup-paths-plugin/schema.json",
2927+
"path": "vite/generators/setup-paths-plugin",
2928+
"type": "generator"
2929+
},
2930+
{
2931+
"description": "Generate a vitest configuration.",
29232932
"file": "generated/packages/vite/generators/vitest.json",
29242933
"hidden": false,
29252934
"name": "vitest",

docs/generated/packages/vite/documents/overview.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ In any Nx workspace, you can install `@nx/vite` by running the following command
3939
nx add @nx/vite
4040
```
4141

42+
You can also pass the `--setupPathsPlugin` flag to add [`nxViteTsPaths` plugin](/recipes/vite/configure-vite#typescript-paths), so your projects can use workspace libraries.
43+
44+
```shell {% skipRescope=true %}
45+
nx add @nx/vite --setupPathsPlugin
46+
```
47+
4248
This will install the correct version of `@nx/vite`.
4349

4450
### How @nx/vite Infers Tasks

docs/generated/packages/vite/generators/init.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
"type": "boolean",
1919
"default": false
2020
},
21+
"setupPathsPlugin": {
22+
"type": "boolean",
23+
"description": "Updates vite config files to enable support for workspace libraries via the nxViteTsPaths plugin.",
24+
"default": false
25+
},
2126
"keepExistingVersions": {
2227
"type": "boolean",
2328
"x-priority": "internal",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "setup-paths-plugin",
3+
"factory": "./src/generators/setup-paths-plugin/setup-paths-plugin",
4+
"schema": {
5+
"cli": "nx",
6+
"title": "Sets up the nxViteTsPaths plugin.",
7+
"description": "Updates vite config files to enable support for workspace libraries via the nxViteTsPaths plugin.",
8+
"$id": "setup-paths-plugin-vite-plugin",
9+
"type": "object",
10+
"properties": {
11+
"skipFormat": {
12+
"description": "Skip formatting files.",
13+
"type": "boolean",
14+
"default": false
15+
}
16+
},
17+
"presets": []
18+
},
19+
"description": "Sets up the nxViteTsPaths plugin to enable support for workspace libraries.",
20+
"implementation": "/packages/vite/src/generators/setup-paths-plugin/setup-paths-plugin.ts",
21+
"aliases": [],
22+
"hidden": false,
23+
"path": "/packages/vite/src/generators/setup-paths-plugin/schema.json",
24+
"type": "generator"
25+
}

docs/generated/packages/vite/generators/vitest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"required": ["project"],
5757
"presets": []
5858
},
59-
"description": "Generate a vitest configuration",
59+
"description": "Generate a vitest configuration.",
6060
"implementation": "/packages/vite/src/generators/vitest/vitest-generator#vitestGeneratorInternal.ts",
6161
"aliases": [],
6262
"hidden": false,

docs/shared/packages/vite/vite-plugin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ In any Nx workspace, you can install `@nx/vite` by running the following command
3939
nx add @nx/vite
4040
```
4141

42+
You can also pass the `--setupPathsPlugin` flag to add [`nxViteTsPaths` plugin](/recipes/vite/configure-vite#typescript-paths), so your projects can use workspace libraries.
43+
44+
```shell {% skipRescope=true %}
45+
nx add @nx/vite --setupPathsPlugin
46+
```
47+
4248
This will install the correct version of `@nx/vite`.
4349

4450
### How @nx/vite Infers Tasks

docs/shared/reference/sitemap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@
674674
- [generators](/nx-api/vite/generators)
675675
- [init](/nx-api/vite/generators/init)
676676
- [configuration](/nx-api/vite/generators/configuration)
677+
- [setup-paths-plugin](/nx-api/vite/generators/setup-paths-plugin)
677678
- [vitest](/nx-api/vite/generators/vitest)
678679
- [vue](/nx-api/vue)
679680
- [documents](/nx-api/vue/documents)

packages/vite/generators.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
"aliases": ["config"],
1717
"hidden": false
1818
},
19+
"setup-paths-plugin": {
20+
"factory": "./src/generators/setup-paths-plugin/setup-paths-plugin",
21+
"schema": "./src/generators/setup-paths-plugin/schema.json",
22+
"description": "Sets up the nxViteTsPaths plugin to enable support for workspace libraries."
23+
},
1924
"vitest": {
2025
"factory": "./src/generators/vitest/vitest-generator#vitestGeneratorInternal",
2126
"schema": "./src/generators/vitest/schema.json",
22-
"description": "Generate a vitest configuration"
27+
"description": "Generate a vitest configuration."
2328
}
2429
}
2530
}

packages/vite/src/generators/init/init.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {
44
ProjectGraph,
55
readJson,
66
readNxJson,
7+
stripIndents,
78
Tree,
89
updateJson,
910
} from '@nx/devkit';
1011
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
1112
import { nxVersion } from '../../utils/versions';
13+
import { initGenerator } from './init';
1214

1315
let projectGraph: ProjectGraph;
1416
jest.mock('@nx/devkit', () => ({
@@ -18,8 +20,6 @@ jest.mock('@nx/devkit', () => ({
1820
}),
1921
}));
2022

21-
import { initGenerator } from './init';
22-
2323
describe('@nx/vite:init', () => {
2424
let tree: Tree;
2525

@@ -99,4 +99,31 @@ describe('@nx/vite:init', () => {
9999
`);
100100
});
101101
});
102+
103+
it('should add nxViteTsPaths plugin to vite config files when setupPathsPlugin is set to true', async () => {
104+
tree.write(
105+
'proj/vite.config.ts',
106+
stripIndents`
107+
import { defineConfig } from 'vite'
108+
import react from '@vitejs/plugin-react'
109+
export default defineConfig({
110+
plugins: [react()],
111+
})`
112+
);
113+
114+
await initGenerator(tree, {
115+
addPlugin: true,
116+
setupPathsPlugin: true,
117+
});
118+
119+
expect(tree.read('proj/vite.config.ts').toString()).toMatchInlineSnapshot(`
120+
"import { defineConfig } from 'vite';
121+
import react from '@vitejs/plugin-react';
122+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
123+
export default defineConfig({
124+
plugins: [react(), nxViteTsPaths()],
125+
});
126+
"
127+
`);
128+
});
102129
});

packages/vite/src/generators/init/init.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@nx/devkit';
1010
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
1111

12+
import { setupPathsPlugin } from '../setup-paths-plugin/setup-paths-plugin';
1213
import { createNodes } from '../../plugins/plugin';
1314
import { InitGeneratorSchema } from './schema';
1415
import { checkDependenciesInstalled, moveToDevDependencies } from './lib/utils';
@@ -82,6 +83,10 @@ export async function initGeneratorInternal(
8283

8384
updateNxJsonSettings(tree);
8485

86+
if (schema.setupPathsPlugin) {
87+
await setupPathsPlugin(tree, { skipFormat: true });
88+
}
89+
8590
const tasks: GeneratorCallback[] = [];
8691
if (!schema.skipPackageJson) {
8792
tasks.push(moveToDevDependencies(tree));

packages/vite/src/generators/init/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface InitGeneratorSchema {
22
skipFormat?: boolean;
3+
setupPathsPlugin?: boolean;
34
skipPackageJson?: boolean;
45
keepExistingVersions?: boolean;
56
updatePackageScripts?: boolean;

packages/vite/src/generators/init/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"type": "boolean",
1616
"default": false
1717
},
18+
"setupPathsPlugin": {
19+
"type": "boolean",
20+
"description": "Updates vite config files to enable support for workspace libraries via the nxViteTsPaths plugin.",
21+
"default": false
22+
},
1823
"keepExistingVersions": {
1924
"type": "boolean",
2025
"x-priority": "internal",

packages/vite/src/generators/setup-paths-plugin/lib/utils.ts

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface InitGeneratorSchema {
2+
skipFormat?: boolean;
3+
addTsPathsPlugin?: boolean;
4+
skipPackageJson?: boolean;
5+
keepExistingVersions?: boolean;
6+
updatePackageScripts?: boolean;
7+
addPlugin?: boolean;
8+
vitestOnly?: boolean;
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"cli": "nx",
3+
"title": "Sets up the nxViteTsPaths plugin.",
4+
"description": "Updates vite config files to enable support for workspace libraries via the nxViteTsPaths plugin.",
5+
"$id": "setup-paths-plugin-vite-plugin",
6+
"type": "object",
7+
"properties": {
8+
"skipFormat": {
9+
"description": "Skip formatting files.",
10+
"type": "boolean",
11+
"default": false
12+
}
13+
}
14+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { ProjectGraph, stripIndents, Tree } from '@nx/devkit';
2+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
3+
import { setupPathsPlugin } from './setup-paths-plugin';
4+
5+
let projectGraph: ProjectGraph;
6+
jest.mock('@nx/devkit', () => ({
7+
...jest.requireActual<any>('@nx/devkit'),
8+
createProjectGraphAsync: jest.fn().mockImplementation(async () => {
9+
return projectGraph;
10+
}),
11+
}));
12+
13+
describe('@nx/vite:init', () => {
14+
let tree: Tree;
15+
16+
beforeEach(() => {
17+
tree = createTreeWithEmptyWorkspace();
18+
projectGraph = {
19+
nodes: {},
20+
dependencies: {},
21+
};
22+
});
23+
24+
it('should add nxViteTsPaths plugin to vite config files', async () => {
25+
tree.write(
26+
'proj1/vite.config.ts',
27+
stripIndents`
28+
import { defineConfig } from 'vite';
29+
export default defineConfig({});`
30+
);
31+
tree.write(
32+
'proj2/vite.config.ts',
33+
stripIndents`
34+
import { defineConfig } from 'vite'
35+
import react from '@vitejs/plugin-react'
36+
export default defineConfig({
37+
plugins: [react()],
38+
})`
39+
);
40+
tree.write(
41+
'proj3/vite.config.cts',
42+
stripIndents`
43+
const { defineConfig } = require('vite');
44+
const react = require('@vitejs/plugin-react');
45+
module.exports = defineConfig({
46+
plugins: [react()],
47+
});
48+
`
49+
);
50+
51+
await setupPathsPlugin(tree, {});
52+
53+
expect(tree.read('proj1/vite.config.ts').toString()).toMatchInlineSnapshot(`
54+
"import { defineConfig } from 'vite';
55+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
56+
export default defineConfig({ plugins: [nxViteTsPaths()] });
57+
"
58+
`);
59+
expect(tree.read('proj2/vite.config.ts').toString()).toMatchInlineSnapshot(`
60+
"import { defineConfig } from 'vite';
61+
import react from '@vitejs/plugin-react';
62+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
63+
export default defineConfig({
64+
plugins: [react(), nxViteTsPaths()],
65+
});
66+
"
67+
`);
68+
expect(tree.read('proj3/vite.config.cts').toString())
69+
.toMatchInlineSnapshot(`
70+
"const { nxViteTsPaths } = require('@nx/vite/plugins/nx-tsconfig-paths.plugin');
71+
const { defineConfig } = require('vite');
72+
const react = require('@vitejs/plugin-react');
73+
module.exports = defineConfig({
74+
plugins: [react(), nxViteTsPaths()],
75+
});
76+
"
77+
`);
78+
});
79+
});

0 commit comments

Comments
 (0)