From 719801ae7268e1d5e63347835d6530ba8cd701ed Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Thu, 30 Nov 2023 18:28:47 +0200 Subject: [PATCH] feat(vite): remove migration --- docs/generated/manifests/menus.json | 8 - docs/generated/manifests/nx-api.json | 11 - docs/generated/packages-metadata.json | 11 - .../vite/documents/vite-nodes-plugin.md | 64 ----- docs/map.json | 7 - .../shared/packages/vite/vite-nodes-plugin.md | 64 ----- docs/shared/reference/sitemap.md | 1 - .../src/generators/application/lib/add-e2e.ts | 7 +- .../__snapshots__/library.spec.ts.snap | 2 - packages/react/src/utils/has-vite-plugin.ts | 10 + packages/vite/migrations.json | 5 - .../__snapshots__/configuration.spec.ts.snap | 3 - .../vite/src/generators/init/lib/utils.ts | 11 - .../vite/src/generators/vitest/vitest.spec.ts | 3 + .../update-17-2-0/add-vite-plugin.spec.ts | 223 ------------------ .../update-17-2-0/add-vite-plugin.ts | 31 --- .../plugins/__snapshots__/plugin.spec.ts.snap | 40 +++- packages/vite/src/plugins/plugin.ts | 55 +++-- .../__snapshots__/library.spec.ts.snap | 1 - 19 files changed, 77 insertions(+), 480 deletions(-) delete mode 100644 docs/generated/packages/vite/documents/vite-nodes-plugin.md delete mode 100644 docs/shared/packages/vite/vite-nodes-plugin.md create mode 100644 packages/react/src/utils/has-vite-plugin.ts delete mode 100644 packages/vite/src/migrations/update-17-2-0/add-vite-plugin.spec.ts delete mode 100644 packages/vite/src/migrations/update-17-2-0/add-vite-plugin.ts diff --git a/docs/generated/manifests/menus.json b/docs/generated/manifests/menus.json index 0b472f729ee967..ee82e17be6e89c 100644 --- a/docs/generated/manifests/menus.json +++ b/docs/generated/manifests/menus.json @@ -9034,14 +9034,6 @@ "isExternal": false, "children": [], "disableCollapsible": false - }, - { - "name": "Overview of the @nx/vite/plugin", - "path": "/nx-api/vite/documents/vite-nodes-plugin", - "id": "vite-nodes-plugin", - "isExternal": false, - "children": [], - "disableCollapsible": false } ], "isExternal": false, diff --git a/docs/generated/manifests/nx-api.json b/docs/generated/manifests/nx-api.json index c4cedba50ce9bb..08352ad373a345 100644 --- a/docs/generated/manifests/nx-api.json +++ b/docs/generated/manifests/nx-api.json @@ -2524,17 +2524,6 @@ "path": "/nx-api/vite/documents/overview", "tags": [], "originalFilePath": "shared/packages/vite/vite-plugin" - }, - "/nx-api/vite/documents/vite-nodes-plugin": { - "id": "vite-nodes-plugin", - "name": "Overview of the @nx/vite/plugin", - "description": "The @nx/vite/plugin adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a vite.config.ts file without creating a project.json file.", - "file": "generated/packages/vite/documents/vite-nodes-plugin", - "itemList": [], - "isExternal": false, - "path": "/nx-api/vite/documents/vite-nodes-plugin", - "tags": [], - "originalFilePath": "shared/packages/vite/vite-nodes-plugin" } }, "root": "/packages/vite", diff --git a/docs/generated/packages-metadata.json b/docs/generated/packages-metadata.json index 072255cac4443c..4c115308198280 100644 --- a/docs/generated/packages-metadata.json +++ b/docs/generated/packages-metadata.json @@ -2497,17 +2497,6 @@ "path": "vite/documents/overview", "tags": [], "originalFilePath": "shared/packages/vite/vite-plugin" - }, - { - "id": "vite-nodes-plugin", - "name": "Overview of the @nx/vite/plugin", - "description": "The @nx/vite/plugin adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a vite.config.ts file without creating a project.json file.", - "file": "generated/packages/vite/documents/vite-nodes-plugin", - "itemList": [], - "isExternal": false, - "path": "vite/documents/vite-nodes-plugin", - "tags": [], - "originalFilePath": "shared/packages/vite/vite-nodes-plugin" } ], "executors": [ diff --git a/docs/generated/packages/vite/documents/vite-nodes-plugin.md b/docs/generated/packages/vite/documents/vite-nodes-plugin.md deleted file mode 100644 index 3d3b5efbc7cd70..00000000000000 --- a/docs/generated/packages/vite/documents/vite-nodes-plugin.md +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: Overview of the @nx/vite/plugin -description: The @nx/vite/plugin adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a vite.config.ts file without creating a project.json file. ---- - -{% callout type="note" title="Still in beta" %} -This feature is still in beta. To use it you must have the `NX_PCV3` environment variable set to `"true"`. -{% /callout %} - -## What is the @nx/vite/plugin? - -The `@nx/vite/plugin` adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a `vite.config.ts` file without creating a `project.json` file. - -This is an effort to reduce duplicate code and configurations for your projects that use Vite. - -## How it works - -The `@nx/vite:init` generator (which is called through the `@nx/vite:configuration` generator, and the `@nx/react:app|lib`, `@nx/vue:app|lib` and `@nx/web:app|lib` generators) the following entry is added in the `plugins` array of your `nx.json`: - -```json -... -plugins: [ - { - "plugin": "@nx/vite/plugin", - "options": { - "buildTargetName": "build", - "previewTargetName": "preview", - "serveTargetName": "serve", - "testTargetName": "test", - "serveStaticTargetName": "static-serve", - }, - }, - ... -] -``` - -This will create targets for `build`, `preview`, `test` and `serve` in your project graph, so that you can run `nx build my-app`, `nx test my-app` etc, without manually defining these targets in each of your projects' `project.json` files. This will work for every single project in your workspace that has a `vite.config.ts|js` file. - -## How to configure - -The plugin generates these targets with bare minimum (no extra) options configuration. Any options you need for your Vite app, you can add in your project's `vite.config.ts`. - -If you want to add some universal options for all your `build` targets, for example, you can still do so in the [`targetDefaults`](/recipes/running-tasks/reduce-repetitive-configuration#reduce-configuration-with-targetdefaults). - -You can edit the name of your targets if you want to use a different name. For example, you can have this configuration: - -```json -... -plugins: [ - { - "plugin": "@nx/vite/plugin", - "options": { - "buildTargetName": "build-core", - "previewTargetName": "preview", - "serveTargetName": "serve", - "testTargetName": "test", - "serveStaticTargetName": "static-serve", - }, - }, - ... -] -``` - -and this will create `build-core`, `preview`, `test` and `serve` targets in your project graph. You can then call `nx build-core my-app` for all your projects that have a `vite.config.ts|js` file. diff --git a/docs/map.json b/docs/map.json index 77a548d973fa52..438451c1840606 100644 --- a/docs/map.json +++ b/docs/map.json @@ -2151,13 +2151,6 @@ "name": "Overview of the Nx Vite Plugin", "description": "The Nx Plugin for Vite contains executors and generators that support building applications using Vite. This page also explains how to configure Vite on your Nx workspace.", "file": "shared/packages/vite/vite-plugin" - }, - { - "id": "vite-nodes-plugin", - "path": "/nx-api/vite-nodes-plugin", - "name": "Overview of the @nx/vite/plugin", - "description": "The @nx/vite/plugin adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a vite.config.ts file without creating a project.json file.", - "file": "shared/packages/vite/vite-nodes-plugin" } ] }, diff --git a/docs/shared/packages/vite/vite-nodes-plugin.md b/docs/shared/packages/vite/vite-nodes-plugin.md deleted file mode 100644 index 3d3b5efbc7cd70..00000000000000 --- a/docs/shared/packages/vite/vite-nodes-plugin.md +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: Overview of the @nx/vite/plugin -description: The @nx/vite/plugin adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a vite.config.ts file without creating a project.json file. ---- - -{% callout type="note" title="Still in beta" %} -This feature is still in beta. To use it you must have the `NX_PCV3` environment variable set to `"true"`. -{% /callout %} - -## What is the @nx/vite/plugin? - -The `@nx/vite/plugin` adds Vite targets/projects to the Nx project graph. This allows you to run Vite for projects that have a `vite.config.ts` file without creating a `project.json` file. - -This is an effort to reduce duplicate code and configurations for your projects that use Vite. - -## How it works - -The `@nx/vite:init` generator (which is called through the `@nx/vite:configuration` generator, and the `@nx/react:app|lib`, `@nx/vue:app|lib` and `@nx/web:app|lib` generators) the following entry is added in the `plugins` array of your `nx.json`: - -```json -... -plugins: [ - { - "plugin": "@nx/vite/plugin", - "options": { - "buildTargetName": "build", - "previewTargetName": "preview", - "serveTargetName": "serve", - "testTargetName": "test", - "serveStaticTargetName": "static-serve", - }, - }, - ... -] -``` - -This will create targets for `build`, `preview`, `test` and `serve` in your project graph, so that you can run `nx build my-app`, `nx test my-app` etc, without manually defining these targets in each of your projects' `project.json` files. This will work for every single project in your workspace that has a `vite.config.ts|js` file. - -## How to configure - -The plugin generates these targets with bare minimum (no extra) options configuration. Any options you need for your Vite app, you can add in your project's `vite.config.ts`. - -If you want to add some universal options for all your `build` targets, for example, you can still do so in the [`targetDefaults`](/recipes/running-tasks/reduce-repetitive-configuration#reduce-configuration-with-targetdefaults). - -You can edit the name of your targets if you want to use a different name. For example, you can have this configuration: - -```json -... -plugins: [ - { - "plugin": "@nx/vite/plugin", - "options": { - "buildTargetName": "build-core", - "previewTargetName": "preview", - "serveTargetName": "serve", - "testTargetName": "test", - "serveStaticTargetName": "static-serve", - }, - }, - ... -] -``` - -and this will create `build-core`, `preview`, `test` and `serve` targets in your project graph. You can then call `nx build-core my-app` for all your projects that have a `vite.config.ts|js` file. diff --git a/docs/shared/reference/sitemap.md b/docs/shared/reference/sitemap.md index b70c1e0cd6bb43..0aceb122cae4dc 100644 --- a/docs/shared/reference/sitemap.md +++ b/docs/shared/reference/sitemap.md @@ -626,7 +626,6 @@ - [vite](/nx-api/vite) - [documents](/nx-api/vite/documents) - [Overview of the Nx Vite Plugin](/nx-api/vite/documents/overview) - - [Overview of the @nx/vite/plugin](/nx-api/vite/documents/vite-nodes-plugin) - [executors](/nx-api/vite/executors) - [dev-server](/nx-api/vite/executors/dev-server) - [build](/nx-api/vite/executors/build) diff --git a/packages/react/src/generators/application/lib/add-e2e.ts b/packages/react/src/generators/application/lib/add-e2e.ts index b699f60384164e..2b58d966331d6b 100644 --- a/packages/react/src/generators/application/lib/add-e2e.ts +++ b/packages/react/src/generators/application/lib/add-e2e.ts @@ -4,12 +4,12 @@ import { ensurePackage, getPackageManagerCommand, joinPathFragments, - readNxJson, } from '@nx/devkit'; import { webStaticServeGenerator } from '@nx/web'; import { nxVersion } from '../../../utils/versions'; import { hasWebpackPlugin } from '../../../utils/has-webpack-plugin'; +import { hasVitePlugin } from '../../../utils/has-vite-plugin'; import { NormalizedSchema } from '../schema'; export async function addE2e( @@ -18,7 +18,10 @@ export async function addE2e( ): Promise { switch (options.e2eTestRunner) { case 'cypress': { - if (!hasWebpackPlugin(tree)) { + if ( + (options.bundler === 'webpack' && !hasWebpackPlugin(tree)) || + (options.bundler === 'vite' && !hasVitePlugin(tree)) + ) { webStaticServeGenerator(tree, { buildTarget: `${options.projectName}:build`, targetName: 'serve-static', diff --git a/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap index b6537f9624b706..4d4970fca14d7d 100644 --- a/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap @@ -17,7 +17,6 @@ export default defineConfig({ // }, test: { - reportsDirectory: '../coverage/my-lib', globals: true, cache: { dir: '../node_modules/.vitest' }, environment: 'jsdom', @@ -72,7 +71,6 @@ export default defineConfig({ // External packages that should not be bundled into your library. external: ['react', 'react-dom', 'react/jsx-runtime'], }, - outDir: 'dist/my-lib', }, test: { diff --git a/packages/react/src/utils/has-vite-plugin.ts b/packages/react/src/utils/has-vite-plugin.ts new file mode 100644 index 00000000000000..9430f5030ea075 --- /dev/null +++ b/packages/react/src/utils/has-vite-plugin.ts @@ -0,0 +1,10 @@ +import { readNxJson, Tree } from '@nx/devkit'; + +export function hasVitePlugin(tree: Tree) { + const nxJson = readNxJson(tree); + return !!nxJson.plugins?.some((p) => + typeof p === 'string' + ? p === '@nx/vite/plugin' + : p.plugin === '@nx/vite/plugin' + ); +} diff --git a/packages/vite/migrations.json b/packages/vite/migrations.json index 2c4928c3d57e40..bbca74289a12ee 100644 --- a/packages/vite/migrations.json +++ b/packages/vite/migrations.json @@ -45,11 +45,6 @@ "version": "17.2.0-beta.10", "description": "Update vite config.", "implementation": "./src/migrations/update-17-2-0/update-vite-config" - }, - "add-vite-plugin": { - "version": "17.3.0-beta.0", - "description": "Add @nx/vite/plugin", - "implementation": "./src/migrations/update-17-2-0/add-vite-plugin" } }, "packageJsonUpdates": { diff --git a/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap b/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap index 14596cdabcf733..b5ffd6de1c1b45 100644 --- a/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap +++ b/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap @@ -44,7 +44,6 @@ export default defineConfig({ // External packages that should not be bundled into your library. external: ['react', 'react-dom', 'react/jsx-runtime'], }, - outDir: 'dist/react-lib-nonb-jest', }, }); " @@ -94,7 +93,6 @@ export default defineConfig({ // External packages that should not be bundled into your library. external: ['react', 'react-dom', 'react/jsx-runtime'], }, - outDir: 'dist/react-lib-nonb-jest', }, test: { @@ -170,7 +168,6 @@ export default defineConfig({ // External packages that should not be bundled into your library. external: ['react', 'react-dom', 'react/jsx-runtime'], }, - outDir: 'dist/react-lib-nonb-vitest', }, plugins: [ nxViteTsPaths(), diff --git a/packages/vite/src/generators/init/lib/utils.ts b/packages/vite/src/generators/init/lib/utils.ts index 3450adf9f197e4..428693b440f018 100644 --- a/packages/vite/src/generators/init/lib/utils.ts +++ b/packages/vite/src/generators/init/lib/utils.ts @@ -91,17 +91,6 @@ export function createVitestConfig(tree: Tree) { nxJson.namedInputs.production = Array.from(new Set(productionFileSet)); } - nxJson.targetDefaults ??= {}; - nxJson.targetDefaults['@nx/vite:test'] ??= {}; - nxJson.targetDefaults['@nx/vite:test'].cache ??= true; - nxJson.targetDefaults['@nx/vite:test'].inputs ??= [ - 'default', - productionFileSet ? '^production' : '^default', - ]; - nxJson.targetDefaults['@nx/vite:test'].options ??= { - passWithNoTests: true, - }; - updateNxJson(tree, nxJson); } diff --git a/packages/vite/src/generators/vitest/vitest.spec.ts b/packages/vite/src/generators/vitest/vitest.spec.ts index 36b9eb9a42b985..80336aa433bf28 100644 --- a/packages/vite/src/generators/vitest/vitest.spec.ts +++ b/packages/vite/src/generators/vitest/vitest.spec.ts @@ -54,6 +54,9 @@ describe('vitest generator', () => { expect(config.targets['test']).toMatchInlineSnapshot(` { "executor": "@nx/vite:test", + "options": { + "reportsDirectory": "../../coverage/apps/my-test-react-app", + }, "outputs": [ "{options.reportsDirectory}", ], diff --git a/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.spec.ts b/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.spec.ts deleted file mode 100644 index 594340403d9f48..00000000000000 --- a/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.spec.ts +++ /dev/null @@ -1,223 +0,0 @@ -import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; -import { - readProjectConfiguration, - Tree, - updateProjectConfiguration, -} from '@nx/devkit'; -import update from './add-vite-plugin'; -import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; - -jest.mock('vite', () => ({ - loadConfigFromFile: jest.fn().mockImplementation(() => { - return Promise.resolve({ - path: 'vite.config.ts', - config: {}, - dependencies: [], - }); - }), -})); -describe('add-vite-plugin migration', () => { - let tree: Tree; - let tempFs: TempFs; - - describe('not root project', () => { - beforeEach(async () => { - tempFs = new TempFs('test'); - tree = createTreeWithEmptyWorkspace(); - tree.root = tempFs.tempDir; - await tempFs.createFiles({ - 'apps/my-app/vite.config.ts': '', - 'apps/my-app/project.json': '{ "name": "my-app" }', - }); - tree.write('apps/my-app/vite.config.ts', `console.log('hi');`); - }); - - afterEach(() => { - jest.resetModules(); - tempFs.cleanup(); - }); - - it('should remove the serve target', async () => { - updateProjectConfiguration(tree, 'my-app', { - root: 'apps/my-app', - targets: { - serve: { - executor: '@nx/vite:dev-server', - defaultConfiguration: 'development', - options: { - buildTarget: 'build', - }, - configurations: { - development: { - buildTarget: 'build:development', - hmr: true, - }, - production: { - buildTarget: 'build:production', - hmr: false, - }, - }, - }, - }, - }); - - await update(tree); - - expect( - readProjectConfiguration(tree, 'my-app').targets.serve - ).toBeUndefined(); - }); - - it('should remove the serve, serve-static and build target, but keep other targets', async () => { - updateProjectConfiguration(tree, 'my-app', { - root: 'apps/my-app', - targets: { - build: { - executor: '@nx/vite:build', - outputs: ['{options.outputPath}'], - defaultConfiguration: 'production', - options: { - outputPath: 'dist/apps/my-app', - }, - configurations: { - development: { - mode: 'development', - }, - production: { - mode: 'production', - }, - }, - }, - serve: { - executor: '@nx/vite:dev-server', - defaultConfiguration: 'development', - options: { - buildTarget: 'build', - }, - configurations: { - development: { - buildTarget: 'build:development', - hmr: true, - }, - production: { - buildTarget: 'build:production', - hmr: false, - }, - }, - }, - 'preview-custom': { - executor: '@nx/vite:preview-server', - defaultConfiguration: 'development', - options: { - buildTarget: 'build', - }, - configurations: { - development: { - buildTarget: 'build:development', - }, - production: { - buildTarget: 'build:production', - }, - }, - }, - 'test-custom': { - executor: '@nx/vite:test', - outputs: ['{options.reportsDirectory}'], - options: { - passWithNoTests: true, - reportsDirectory: '../../coverage/apps/my-app', - }, - }, - lint: { - executor: '@nx/eslint:lint', - outputs: ['{options.outputFile}'], - options: { - lintFilePatterns: ['apps/my-app/**/*.{ts,tsx,js,jsx}'], - }, - }, - 'serve-static': { - executor: '@nx/web:file-server', - options: { - buildTarget: 'my-app:build', - }, - }, - }, - }); - - await update(tree); - - expect( - readProjectConfiguration(tree, 'my-app').targets.serve - ).toBeUndefined(); - - expect( - readProjectConfiguration(tree, 'my-app').targets.build - ).toBeUndefined(); - - expect( - readProjectConfiguration(tree, 'my-app').targets['preview-custom'] - ).toBeDefined(); - - expect( - readProjectConfiguration(tree, 'my-app').targets['lint'] - ).toBeDefined(); - - expect( - readProjectConfiguration(tree, 'my-app').targets['test-custom'] - ).toBeDefined(); - - expect( - readProjectConfiguration(tree, 'my-app').targets['serve-static'] - ).toBeUndefined(); - }); - }); - - describe('root project', () => { - beforeEach(async () => { - tempFs = new TempFs('test'); - tree = createTreeWithEmptyWorkspace(); - tree.root = tempFs.tempDir; - await tempFs.createFiles({ - 'vite.config.ts': '', - 'project.json': '{ "name": "my-app" }', - }); - tree.write('vite.config.ts', `console.log('hi');`); - }); - - afterEach(() => { - jest.resetModules(); - tempFs.cleanup(); - }); - - it('should remove the serve target', async () => { - updateProjectConfiguration(tree, 'my-app', { - root: '.', - targets: { - serve: { - executor: '@nx/vite:dev-server', - defaultConfiguration: 'development', - options: { - buildTarget: 'build', - }, - configurations: { - development: { - buildTarget: 'build:development', - hmr: true, - }, - production: { - buildTarget: 'build:production', - hmr: false, - }, - }, - }, - }, - }); - - await update(tree); - - expect( - readProjectConfiguration(tree, 'my-app').targets.serve - ).toBeUndefined(); - }); - }); -}); diff --git a/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.ts b/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.ts deleted file mode 100644 index 3bb9b1916cba7f..00000000000000 --- a/packages/vite/src/migrations/update-17-2-0/add-vite-plugin.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { formatFiles, getProjects, Tree } from '@nx/devkit'; -import { createNodes } from '../../plugins/plugin'; - -import { createProjectRootMappingsFromProjectConfigurations } from 'nx/src/project-graph/utils/find-project-for-path'; -import { replaceProjectConfigurationsWithPlugin } from '@nx/devkit/src/utils/replace-project-configuration-with-plugin'; - -export default async function update(tree: Tree) { - const proj = Object.fromEntries(getProjects(tree).entries()); - - const rootMappings = createProjectRootMappingsFromProjectConfigurations(proj); - - // add function here to remove - // passWithNoTests: true, - // reportsDirectory: '../../coverage/apps/my-app', - - await replaceProjectConfigurationsWithPlugin( - tree, - rootMappings, - '@nx/vite/plugin', - createNodes, - { - buildTargetName: 'build', - serveTargetName: 'serve', - previewTargetName: 'preview', - testTargetName: 'test', - serveStaticTargetName: 'serve-static', - } - ); - - await formatFiles(tree); -} diff --git a/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap b/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap index da0769cf83861b..d35229bffcd73d 100644 --- a/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap +++ b/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap @@ -8,10 +8,15 @@ exports[`@nx/vite/plugin not root project should create nodes 1`] = ` "targets": { "build-something": { "cache": true, - "command": "pnpm exec vite build", + "command": "vite build", "inputs": [ "production", "^production", + { + "externalDependencies": [ + "vite", + ], + }, ], "options": { "cwd": "my-app", @@ -21,13 +26,13 @@ exports[`@nx/vite/plugin not root project should create nodes 1`] = ` ], }, "my-serve": { - "command": "pnpm exec vite serve", + "command": "vite serve", "options": { "cwd": "my-app", }, }, "preview-site": { - "command": "pnpm exec vite preview", + "command": "vite preview", "options": { "cwd": "my-app", }, @@ -40,10 +45,15 @@ exports[`@nx/vite/plugin not root project should create nodes 1`] = ` }, "vitest": { "cache": true, - "command": "pnpm exec vitest run", + "command": "vitest run", "inputs": [ - "default", + "production", "^production", + { + "externalDependencies": [ + "vitest", + ], + }, ], "options": { "cwd": "my-app", @@ -66,10 +76,15 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = ` "targets": { "build": { "cache": true, - "command": "pnpm exec vite build", + "command": "vite build", "inputs": [ "production", "^production", + { + "externalDependencies": [ + "vite", + ], + }, ], "options": { "cwd": ".", @@ -79,13 +94,13 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = ` ], }, "preview": { - "command": "pnpm exec vite preview", + "command": "vite preview", "options": { "cwd": ".", }, }, "serve": { - "command": "pnpm exec vite serve", + "command": "vite serve", "options": { "cwd": ".", }, @@ -98,10 +113,15 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = ` }, "test": { "cache": true, - "command": "pnpm exec vitest run", + "command": "vitest run", "inputs": [ - "default", + "production", "^production", + { + "externalDependencies": [ + "vitest", + ], + }, ], "options": { "cwd": ".", diff --git a/packages/vite/src/plugins/plugin.ts b/packages/vite/src/plugins/plugin.ts index b6589421867e6f..4d07ed9b2bd618 100644 --- a/packages/vite/src/plugins/plugin.ts +++ b/packages/vite/src/plugins/plugin.ts @@ -4,12 +4,12 @@ import { CreateNodesContext, TargetConfiguration, detectPackageManager, - getPackageManagerCommand, joinPathFragments, readJsonFile, + workspaceRoot, writeJsonFile, } from '@nx/devkit'; -import { dirname, join } from 'path'; +import { dirname, isAbsolute, join, relative, resolve } from 'path'; import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils'; import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs'; @@ -17,6 +17,7 @@ import { UserConfig, loadConfigFromFile } from 'vite'; import { existsSync, readdirSync } from 'fs'; import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes'; import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory'; +import { getLockFileName } from '@nx/js'; export interface VitePluginOptions { buildTargetName?: string; testTargetName?: string; @@ -66,7 +67,9 @@ export const createNodes: CreateNodes = [ options = normalizeOptions(options); - const hash = calculateHashForCreateNodes(projectRoot, options, context); + const hash = calculateHashForCreateNodes(projectRoot, options, context, [ + getLockFileName(detectPackageManager(context.workspaceRoot)), + ]); const targets = targetsCache[hash] ? targetsCache[hash] : await buildViteTargets(configFilePath, projectRoot, options, context); @@ -143,14 +146,11 @@ async function buildTarget( ) { const targetDefaults = readTargetDefaultsForTarget( options.buildTargetName, - context.nxJsonConfiguration.targetDefaults, - '@nx/vite:build' + context.nxJsonConfiguration.targetDefaults ); const targetConfig: TargetConfiguration = { - command: `${ - getPackageManagerCommand(detectPackageManager()).exec - } vite build`, + command: `vite build`, options: { cwd: joinPathFragments(projectRoot), }, @@ -165,10 +165,14 @@ async function buildTarget( } if (targetDefaults?.inputs === undefined) { - targetConfig.inputs = - 'production' in namedInputs + targetConfig.inputs = [ + ...('production' in namedInputs ? ['production', '^production'] - : ['default', '^default']; + : ['default', '^default']), + { + externalDependencies: ['vite'], + }, + ]; } return targetConfig; @@ -176,9 +180,7 @@ async function buildTarget( function serveTarget(projectRoot: string) { const targetConfig: TargetConfiguration = { - command: `${ - getPackageManagerCommand(detectPackageManager()).exec - } vite serve`, + command: `vite serve`, options: { cwd: joinPathFragments(projectRoot), }, @@ -189,9 +191,7 @@ function serveTarget(projectRoot: string) { function previewTarget(projectRoot: string) { const targetConfig: TargetConfiguration = { - command: `${ - getPackageManagerCommand(detectPackageManager()).exec - } vite preview`, + command: `vite preview`, options: { cwd: joinPathFragments(projectRoot), }, @@ -211,14 +211,11 @@ async function testTarget( ) { const targetDefaults = readTargetDefaultsForTarget( options.testTargetName, - context.nxJsonConfiguration.targetDefaults, - '@nx/vite:test' + context.nxJsonConfiguration.targetDefaults ); const targetConfig: TargetConfiguration = { - command: `${ - getPackageManagerCommand(detectPackageManager()).exec - } vitest run`, + command: `vitest run`, options: { cwd: joinPathFragments(projectRoot), }, @@ -233,10 +230,14 @@ async function testTarget( } if (targetDefaults?.inputs === undefined) { - targetConfig.inputs = - 'production' in namedInputs - ? ['default', '^production'] - : ['default', '^default']; + targetConfig.inputs = [ + ...('production' in namedInputs + ? ['production', '^production'] + : ['default', '^default']), + { + externalDependencies: ['vitest'], + }, + ]; } return targetConfig; } @@ -266,6 +267,8 @@ function getOutputs( function getOutput(path: string, projectRoot: string): string { if (path.startsWith('..')) { return join('{workspaceRoot}', join(projectRoot, path)); + } else if (isAbsolute(resolve(path))) { + return `{workspaceRoot}/${relative(workspaceRoot, path)}`; } else { return join('{projectRoot}', path); } diff --git a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap index b477b8007ed937..dc9843b800d76a 100644 --- a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap @@ -71,7 +71,6 @@ export default defineConfig({ // External packages that should not be bundled into your library. external: [], }, - outDir: 'dist/my-lib', }, test: {