From 8e838b5aac1cbbb9376545a3b7e14c0190dda07f Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 14 Mar 2023 19:56:04 +0200 Subject: [PATCH] fix(vite): configuration docs and optional chain targets (#15655) --- .../generated/packages/vite/generators/configuration.json | 2 +- packages/vite/docs/configuration-examples.md | 8 ++++++-- .../vite/src/generators/configuration/configuration.ts | 2 +- packages/vite/src/utils/generator-utils.ts | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/generated/packages/vite/generators/configuration.json b/docs/generated/packages/vite/generators/configuration.json index 4a5d9c4459451..b1b005366fa25 100644 --- a/docs/generated/packages/vite/generators/configuration.json +++ b/docs/generated/packages/vite/generators/configuration.json @@ -51,7 +51,7 @@ "description": "The test target of the project to be transformed to use the @nrwl/vite:test executor." } }, - "examplesFile": "---\ntitle: Examples for the Vite configuration generator\ndescription: This page contains examples for the Vite @nrwl/vite:configuration generator, which helps you set up Vite on your Nx workspace, or convert an existing project to use Vite.\n---\n\nThis generator is used for converting an existing React or Web project to use [Vite.js](https://vitejs.dev/) and the [@nrwl/vite executors](/packages/vite#executors).\n\nIt will change the `build` and `serve` targets to use the `@nrwl/vite` executors for serving and building the application. If you choose so, it will also change your `test` target to use the `@nrwl/vite:test` executor. It will create a `vite.config.ts` file at the root of your project with the correct settings, or if there's already a `vite.config.ts` file, it will modify it to include the correct settings.\n\n{% callout type=\"caution\" title=\"Your code will be modified!\" %}\nThis generator will modify your code, so make sure to commit your changes before running it.\n{% /callout %}\n\n```bash\nnx g @nrwl/vite:configuration\n```\n\nWhen running this generator, you will be prompted to provide the following:\n\n- The `project`, as the name of the project you want to generate the configuration for.\n- The `uiFramework` you want to use. Supported values are: `react` and `none`.\n\nYou must provide a `project` and a `uiFramework` for the generator to work.\n\nYou may also pass the `includeVitest` flag. This will also change your `test` target to use the `@nrwl/vite:test` executor, and configure your project for testing with [Vitest](https://vitest.dev/), by adding the `test` configuration in your `vite.config.ts` file.\n\n## Converting custom (specific) targets\n\nBy default, the `@nrwl/vite:configuration` generator will search your project's configuration to find the targets for serving, building, and testing your project, and it will attempt to convert these targets to use the `@nrwl/vite` executors.\n\nYour targets for building, serving and testing may not be named `build`, `serve` and `test`. Nx will try to infer the correct targets to convert, and it will attempt to convert the first one it finds for each of these actions if you have more than one. If you have more than one target for serving, building, or testing your project, you can pass the `--serveTarget`, `--buildTarget`, and `--testTarget` flags to the generator, to tell Nx specifically which targets to convert.\n\nNx will determine if the targets you provided (or the ones it inferred) are valid and can be converted to use the `@nrwl/vite` executors. If the targets are not valid, the generator will fail. If no targets are found - or recognized to be either supported or unsupported - Nx will ask you whether you want to convert your project anyway. If you choose to do so, Nx will configure your project to use Vite.js, creating new targets for you, and creating or modifying your `vite.config.ts` file. You can then test on your own if the result works or not, and modify the configuration as needed. It's suggested that if Nx does not recognize your targets automatically, you commit your changes before running the generator, so you can revert the changes if needed.\n\n## Projects that can be converted to use the `@nrwl/vite` executors\n\nUsually, React and Web projects generated with the `@nrwl/react` and the `@nrwl/web` generators can be converted to use the `@nrwl/vite` executors without any issues.\n\nThe list of executors for building, testing and serving that can be converted to use the `@nrwl/vite` executors is:\n\n### Supported `build` executors\n\n- `@nxext/vite:build`\n- `@nrwl/js:babel`\n- `@nrwl/js:swc`\n- `@nrwl/webpack:webpack`\n- `@nrwl/rollup:rollup`\n- `@nrwl/web:rollup`\n\n### Supported `serve` executors\n\n- `@nxext/vite:dev`\n- `@nrwl/webpack:dev-server`\n\n### Supported `test` executors\n\n- `@nrwl/jest:jest`\n- `@nxext/vitest:vitest`\n\n### Unsupported executors\n\nWe cannot guarantee that the projects using the following executors for either building, testing or serving will work correctly when converted to use the `@nrwl/vite` executors. If you have a project that uses one of these executors, you can try to [configure it to use the `@nrwl/vite` executors manually](/packages/vite/documents/set-up-vite-manually), but it may not work properly.\n\n- `@nrwl/angular:ng-packagr-lite`\n- `@nrwl/angular:package`\n- `@nrwl/angular:webpack-browser`\n- `@angular-devkit/build-angular:browser`\n- `@angular-devkit/build-angular:dev-server`\n- `@nrwl/esbuild:esbuild`\n- `@nrwl/react-native:start`\n- `@nrwl/next:build`\n- `@nrwl/next:server`\n- `@nrwl/js:tsc`\n\nYou can read more in the [Vite package overview page](/packages/vite).\n\n## Examples\n\n### Change a React app to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-react-app --uiFramework=react --includeVitest\n```\n\nThis will change the `my-react-app` project to use the `@nrwl/vite` executors for building, serving and testing the application.\n\n### Change a Web app to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-web-app --uiFramework=none --includeVitest\n```\n\nThis will change the `my-web-app` project to use the `@nrwl/vite` executors for building, serving and testing the application.\n\n### Change only my custom provided targets to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-react-app --uiFramework=react --includeVitest --buildTarget=my-build --serveTarget=my-serve --testTarget=my-test\n```\n\nThis will change the `my-build`, `my-serve` and `my-test` targets to use the `@nrwl/vite` executors for building, serving and testing the application, even if you have other targets for these actions as well.\n", + "examplesFile": "---\ntitle: Examples for the Vite configuration generator\ndescription: This page contains examples for the Vite @nrwl/vite:configuration generator, which helps you set up Vite on your Nx workspace, or convert an existing project to use Vite.\n---\n\nThis generator is used for converting an existing React or Web project to use [Vite.js](https://vitejs.dev/) and the [@nrwl/vite executors](/packages/vite#executors).\n\nIt will change the `build` and `serve` targets to use the `@nrwl/vite` executors for serving and building the application. If you choose so, it will also change your `test` target to use the `@nrwl/vite:test` executor. It will create a `vite.config.ts` file at the root of your project with the correct settings, or if there's already a `vite.config.ts` file, it will modify it to include the correct settings.\n\n{% callout type=\"caution\" title=\"Your code will be modified!\" %}\nThis generator will modify your code, so make sure to commit your changes before running it.\n{% /callout %}\n\n```bash\nnx g @nrwl/vite:configuration\n```\n\nWhen running this generator, you will be prompted to provide the following:\n\n- The `project`, as the name of the project you want to generate the configuration for.\n- The `uiFramework` you want to use. Supported values are: `react` and `none`.\n\nYou must provide a `project` and a `uiFramework` for the generator to work.\n\nYou may also pass the `includeVitest` flag. This will also change your `test` target to use the `@nrwl/vite:test` executor, and configure your project for testing with [Vitest](https://vitest.dev/), by adding the `test` configuration in your `vite.config.ts` file.\n\n## Converting custom (specific) targets\n\nBy default, the `@nrwl/vite:configuration` generator will search your project's configuration to find the targets for serving, building, and testing your project, and it will attempt to convert these targets to use the `@nrwl/vite` executors.\n\nYour targets for building, serving and testing may not be named `build`, `serve` and `test`. Nx will try to infer the correct targets to convert, and it will attempt to convert the first one it finds for each of these actions if you have more than one. If you have more than one target for serving, building, or testing your project, you can pass the `--serveTarget`, `--buildTarget`, and `--testTarget` flags to the generator, to tell Nx specifically which targets to convert.\n\nNx will determine if the targets you provided (or the ones it inferred) are valid and can be converted to use the `@nrwl/vite` executors. If the targets are not valid, the generator will fail. If no targets are found - or recognized to be either supported or unsupported - Nx will ask you whether you want to convert your project anyway. If you choose to do so, Nx will configure your project to use Vite.js, creating new targets for you, and creating or modifying your `vite.config.ts` file. You can then test on your own if the result works or not, and modify the configuration as needed. It's suggested that if Nx does not recognize your targets automatically, you commit your changes before running the generator, so you can revert the changes if needed.\n\n## Projects that can be converted to use the `@nrwl/vite` executors\n\nUsually, React and Web projects generated with the `@nrwl/react` and the `@nrwl/web` generators can be converted to use the `@nrwl/vite` executors without any issues.\n\nThe list of executors for building, testing and serving that can be converted to use the `@nrwl/vite` executors is:\n\n### Supported `build` executors\n\n- `@nxext/vite:build`\n- `@nrwl/js:babel`\n- `@nrwl/js:swc`\n- `@nrwl/webpack:webpack`\n- `@nrwl/rollup:rollup`\n- `@nrwl/web:rollup`\n\n### Supported `serve` executors\n\n- `@nxext/vite:dev`\n- `@nrwl/webpack:dev-server`\n\n### Supported `test` executors\n\n- `@nrwl/jest:jest`\n- `@nxext/vitest:vitest`\n\n### Unsupported executors\n\n- `@nrwl/angular:ng-packagr-lite`\n- `@nrwl/angular:package`\n- `@nrwl/angular:webpack-browser`\n- `@angular-devkit/build-angular:browser`\n- `@angular-devkit/build-angular:dev-server`\n- `@nrwl/esbuild:esbuild`\n- `@nrwl/react-native:start`\n- `@nrwl/next:build`\n- `@nrwl/next:server`\n- `@nrwl/js:tsc`\n- any executor _not_ listed in the lists of \"supported executors\"\n- any project that does _not_ have a target for building, serving or testing\n\nWe **cannot** guarantee that projects using unsupported executors - _or any executor that is NOT listed in the list of \"supported executors\"_ - for either building, testing or serving will work correctly when converted to use the `@nrwl/vite` executors.\n\nIf you have a project that does _not_ use one of the supported executors you can try to [configure it to use the `@nrwl/vite` executors manually](/packages/vite/documents/set-up-vite-manually), but it may not work properly.\n\nYou can read more in the [Vite package overview page](/packages/vite).\n\n## Examples\n\n### Change a React app to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-react-app --uiFramework=react --includeVitest\n```\n\nThis will change the `my-react-app` project to use the `@nrwl/vite` executors for building, serving and testing the application.\n\n### Change a Web app to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-web-app --uiFramework=none --includeVitest\n```\n\nThis will change the `my-web-app` project to use the `@nrwl/vite` executors for building, serving and testing the application.\n\n### Change only my custom provided targets to use Vite\n\n```bash\nnx g @nrwl/vite:configuration --project=my-react-app --uiFramework=react --includeVitest --buildTarget=my-build --serveTarget=my-serve --testTarget=my-test\n```\n\nThis will change the `my-build`, `my-serve` and `my-test` targets to use the `@nrwl/vite` executors for building, serving and testing the application, even if you have other targets for these actions as well.\n", "presets": [] }, "description": "Add Vite configuration to an application.", diff --git a/packages/vite/docs/configuration-examples.md b/packages/vite/docs/configuration-examples.md index 2ab32a0327d37..9b858d27bca0e 100644 --- a/packages/vite/docs/configuration-examples.md +++ b/packages/vite/docs/configuration-examples.md @@ -59,8 +59,6 @@ The list of executors for building, testing and serving that can be converted to ### Unsupported executors -We cannot guarantee that the projects using the following executors for either building, testing or serving will work correctly when converted to use the `@nrwl/vite` executors. If you have a project that uses one of these executors, you can try to [configure it to use the `@nrwl/vite` executors manually](/packages/vite/documents/set-up-vite-manually), but it may not work properly. - - `@nrwl/angular:ng-packagr-lite` - `@nrwl/angular:package` - `@nrwl/angular:webpack-browser` @@ -71,6 +69,12 @@ We cannot guarantee that the projects using the following executors for either b - `@nrwl/next:build` - `@nrwl/next:server` - `@nrwl/js:tsc` +- any executor _not_ listed in the lists of "supported executors" +- any project that does _not_ have a target for building, serving or testing + +We **cannot** guarantee that projects using unsupported executors - _or any executor that is NOT listed in the list of "supported executors"_ - for either building, testing or serving will work correctly when converted to use the `@nrwl/vite` executors. + +If you have a project that does _not_ use one of the supported executors you can try to [configure it to use the `@nrwl/vite` executors manually](/packages/vite/documents/set-up-vite-manually), but it may not work properly. You can read more in the [Vite package overview page](/packages/vite). diff --git a/packages/vite/src/generators/configuration/configuration.ts b/packages/vite/src/generators/configuration/configuration.ts index 190c6dfe6159d..8ddb7dc34b598 100644 --- a/packages/vite/src/generators/configuration/configuration.ts +++ b/packages/vite/src/generators/configuration/configuration.ts @@ -143,7 +143,7 @@ export async function viteConfigurationGenerator( deleteWebpackConfig( tree, root, - targets[buildTargetName]?.options?.webpackConfig + targets?.[buildTargetName]?.options?.webpackConfig ); editTsConfig(tree, schema); diff --git a/packages/vite/src/utils/generator-utils.ts b/packages/vite/src/utils/generator-utils.ts index 3d46c7be76f99..7f5bb97cbcbd1 100644 --- a/packages/vite/src/utils/generator-utils.ts +++ b/packages/vite/src/utils/generator-utils.ts @@ -391,10 +391,10 @@ export function moveAndEditIndexHtml( const projectConfig = readProjectConfiguration(tree, options.project); let indexHtmlPath = - projectConfig.targets[buildTarget].options?.index ?? + projectConfig.targets?.[buildTarget]?.options?.index ?? `${projectConfig.root}/src/index.html`; let mainPath = - projectConfig.targets[buildTarget].options?.main ?? + projectConfig.targets?.[buildTarget]?.options?.main ?? `${projectConfig.root}/src/main.ts${ options.uiFramework === 'react' ? 'x' : '' }`; @@ -638,7 +638,7 @@ export function getViteConfigPathForProject( let viteConfigPath: string | undefined; const { targets, root } = readProjectConfiguration(tree, projectName); if (target) { - viteConfigPath = targets[target]?.options?.configFile; + viteConfigPath = targets?.[target]?.options?.configFile; } else { const config = Object.values(targets).find( (config) => config.executor === '@nrwl/vite:build'