From ff168ff64c924316b4ddf451835338e5864317a6 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Mon, 23 Oct 2023 23:26:03 -0500 Subject: [PATCH] feat(platform): add Vite dev-server/build and Vitest executors (#719) --- apps/docs-app/docs/features/testing/vitest.md | 10 +- package.json | 1 + .../template-angular-v16/angular.json | 4 +- packages/nx-plugin/executors.json | 36 ++++ packages/nx-plugin/project.json | 5 + .../src/executors/vite-dev-server/compat.ts | 5 + .../src/executors/vite-dev-server/schema.d.ts | 1 + .../src/executors/vite-dev-server/schema.json | 101 +++++++++++ .../vite-dev-server/vite-dev-server.impl.ts | 3 + .../nx-plugin/src/executors/vite/compat.ts | 5 + .../nx-plugin/src/executors/vite/schema.d.ts | 2 + .../nx-plugin/src/executors/vite/schema.json | 169 ++++++++++++++++++ .../nx-plugin/src/executors/vite/vite.impl.ts | 3 + .../nx-plugin/src/executors/vitest/compat.ts | 5 + .../src/executors/vitest/schema.d.ts | 1 + .../src/executors/vitest/schema.json | 68 +++++++ .../src/executors/vitest/vitest.impl.ts | 3 + .../app/lib/add-analog-project-config.ts | 6 +- packages/platform/package.json | 2 + pnpm-lock.yaml | 9 +- 20 files changed, 426 insertions(+), 13 deletions(-) create mode 100644 packages/nx-plugin/executors.json create mode 100644 packages/nx-plugin/src/executors/vite-dev-server/compat.ts create mode 100644 packages/nx-plugin/src/executors/vite-dev-server/schema.d.ts create mode 100644 packages/nx-plugin/src/executors/vite-dev-server/schema.json create mode 100644 packages/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.ts create mode 100644 packages/nx-plugin/src/executors/vite/compat.ts create mode 100644 packages/nx-plugin/src/executors/vite/schema.d.ts create mode 100644 packages/nx-plugin/src/executors/vite/schema.json create mode 100644 packages/nx-plugin/src/executors/vite/vite.impl.ts create mode 100644 packages/nx-plugin/src/executors/vitest/compat.ts create mode 100644 packages/nx-plugin/src/executors/vitest/schema.d.ts create mode 100644 packages/nx-plugin/src/executors/vitest/schema.json create mode 100644 packages/nx-plugin/src/executors/vitest/vitest.impl.ts diff --git a/apps/docs-app/docs/features/testing/vitest.md b/apps/docs-app/docs/features/testing/vitest.md index bdfe200c7..c54a1fa94 100644 --- a/apps/docs-app/docs/features/testing/vitest.md +++ b/apps/docs-app/docs/features/testing/vitest.md @@ -13,7 +13,7 @@ To add Vitest, install the necessary packages: ```shell -npm install @analogjs/vite-plugin-angular @nx/vite jsdom vite-tsconfig-paths --save-dev +npm install @analogjs/vite-plugin-angular @analogjs/platform jsdom vite-tsconfig-paths --save-dev ``` @@ -21,7 +21,7 @@ npm install @analogjs/vite-plugin-angular @nx/vite jsdom vite-tsconfig-paths --s ```shell -yarn add @analogjs/vite-plugin-angular @nx/vite jsdom vite-tsconfig-paths --dev +yarn add @analogjs/vite-plugin-angular @analogjs/platform jsdom vite-tsconfig-paths --dev ``` @@ -29,7 +29,7 @@ yarn add @analogjs/vite-plugin-angular @nx/vite jsdom vite-tsconfig-paths --dev ```shell -pnpm install -w @analogjs/vite-plugin-angular @nx/vite jsdom vite-tsconfig-paths +pnpm install -w @analogjs/vite-plugin-angular @analogjs/platform jsdom vite-tsconfig-paths ``` @@ -82,7 +82,7 @@ getTestBed().initTestEnvironment( ); ``` -Next, update the `test` target in the `angular.json` to use the `@nx/vite:test` builder: +Next, update the `test` target in the `angular.json` to use the `@analogjs/platform:vitest` builder: ```json { @@ -97,7 +97,7 @@ Next, update the `test` target in the `angular.json` to use the `@nx/vite:test` "serve": ..., "extract-i18n": ..., "test": { - "builder": "@nx/vite:test" + "builder": "@analogjs/platform:vitest" } } } diff --git a/package.json b/package.json index a2c3a9e34..6a91214d3 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "@nx/cypress": "17.0.1", "@nx/eslint-plugin": "17.0.1", "@nx/jest": "17.0.1", + "@nx/js": "17.0.1", "@nx/plugin": "17.0.1", "@nx/vite": "17.0.1", "@nx/web": "17.0.1", diff --git a/packages/create-analog/template-angular-v16/angular.json b/packages/create-analog/template-angular-v16/angular.json index 3d03e7a96..ff52427ce 100644 --- a/packages/create-analog/template-angular-v16/angular.json +++ b/packages/create-analog/template-angular-v16/angular.json @@ -10,7 +10,7 @@ "prefix": "app", "architect": { "build": { - "builder": "@nx/vite:build", + "builder": "@analogjs/platform:vite", "options": { "configFile": "vite.config.ts", "main": "src/main.ts", @@ -29,7 +29,7 @@ } }, "serve": { - "builder": "@nx/vite:dev-server", + "builder": "@analogjs/platform:vite-dev-server", "defaultConfiguration": "development", "options": { "buildTarget": "my-app:build", diff --git a/packages/nx-plugin/executors.json b/packages/nx-plugin/executors.json new file mode 100644 index 000000000..631c449e6 --- /dev/null +++ b/packages/nx-plugin/executors.json @@ -0,0 +1,36 @@ +{ + "builders": { + "vite-dev-server": { + "implementation": "./src/executors/vite-dev-server/compat", + "schema": "./src/executors/vite-dev-server/schema.json", + "description": "Vite dev server." + }, + "vite": { + "implementation": "./src/executors/vite/compat", + "schema": "./src/executors/vite/schema.json", + "description": "Build with Vite." + }, + "vitest": { + "implementation": "./src/executors/vitest/compat", + "schema": "./src/executors/vitest/schema.json", + "description": "Test with Vitest" + } + }, + "executors": { + "vite-dev-server": { + "implementation": "./src/executors/vite-dev-server/vite-dev-server.impl", + "schema": "./src/executors/vite-dev-server/schema.json", + "description": "Vite dev server." + }, + "vite": { + "implementation": "./src/executors/vite/vite.impl", + "schema": "./src/executors/vite/schema.json", + "description": "Build with Vite." + }, + "vitest": { + "implementation": "./src/executors/vitest/vitest.impl", + "schema": "./src/executors/vitest/schema.json", + "description": "Test with Vitest" + } + } +} diff --git a/packages/nx-plugin/project.json b/packages/nx-plugin/project.json index c59e4edef..f55c91913 100644 --- a/packages/nx-plugin/project.json +++ b/packages/nx-plugin/project.json @@ -27,6 +27,11 @@ "input": "./packages/nx-plugin", "glob": "generators.json", "output": "." + }, + { + "input": "./packages/nx-plugin", + "glob": "executors.json", + "output": "." } ] } diff --git a/packages/nx-plugin/src/executors/vite-dev-server/compat.ts b/packages/nx-plugin/src/executors/vite-dev-server/compat.ts new file mode 100644 index 000000000..d7ccb7bb9 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite-dev-server/compat.ts @@ -0,0 +1,5 @@ +import { convertNxExecutor } from '@nx/devkit'; + +import viteDevServerExecutor from './vite-dev-server.impl'; + +export default convertNxExecutor(viteDevServerExecutor); diff --git a/packages/nx-plugin/src/executors/vite-dev-server/schema.d.ts b/packages/nx-plugin/src/executors/vite-dev-server/schema.d.ts new file mode 100644 index 000000000..abedb0183 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite-dev-server/schema.d.ts @@ -0,0 +1 @@ +export { ViteDevServerExecutorOptions } from '@nx/vite/executors'; diff --git a/packages/nx-plugin/src/executors/vite-dev-server/schema.json b/packages/nx-plugin/src/executors/vite-dev-server/schema.json new file mode 100644 index 000000000..53d5a7ca8 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite-dev-server/schema.json @@ -0,0 +1,101 @@ +{ + "version": 2, + "outputCapture": "direct-nodejs", + "title": "Vite Dev Server", + "cli": "nx", + "description": "Starts a dev server using Vite.", + "type": "object", + "presets": [ + { + "name": "Default minimum setup", + "keys": ["buildTarget"] + }, + { + "name": "Using a Different Port", + "keys": ["buildTarget", "port"] + } + ], + "properties": { + "buildTarget": { + "type": "string", + "description": "Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.", + "x-priority": "important" + }, + "buildLibsFromSource": { + "type": "boolean", + "description": "Read buildable libraries from source instead of building them separately.", + "default": true + }, + "proxyConfig": { + "type": "string", + "description": "Path to the proxy configuration file.", + "x-completion-type": "file" + }, + "port": { + "type": "number", + "description": "Port to listen on.", + "x-priority": "important" + }, + "host": { + "description": "Specify which IP addresses the server should listen on.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "https": { + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "object" + } + ], + "description": "Serve using HTTPS. https://vitejs.dev/config/server-options.html#server-https" + }, + "hmr": { + "description": "Enable hot module replacement. For more options, use the 'hmr' option in the Vite configuration file.", + "type": "boolean" + }, + "open": { + "description": "Automatically open the app in the browser on server start. When the value is a string, it will be used as the URL's pathname.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "cors": { + "description": "Configure CORS for the dev server.", + "type": "boolean" + }, + "logLevel": { + "type": "string", + "description": "Adjust console output verbosity.", + "enum": ["info", "warn", "error", "silent"] + }, + "mode": { + "type": "string", + "description": "Mode to run the server in." + }, + "clearScreen": { + "description": "Set to false to prevent Vite from clearing the terminal screen when logging certain messages.", + "type": "boolean" + }, + "force": { + "description": "Force the optimizer to ignore the cache and re-bundle", + "type": "boolean" + } + }, + "definitions": {}, + "required": ["buildTarget"], + "examplesFile": "../../../docs/dev-server-examples.md" +} diff --git a/packages/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.ts b/packages/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.ts new file mode 100644 index 000000000..2b9c22906 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.ts @@ -0,0 +1,3 @@ +import { viteDevServerExecutor } from '@nx/vite/executors'; + +export default viteDevServerExecutor; diff --git a/packages/nx-plugin/src/executors/vite/compat.ts b/packages/nx-plugin/src/executors/vite/compat.ts new file mode 100644 index 000000000..755b62bbb --- /dev/null +++ b/packages/nx-plugin/src/executors/vite/compat.ts @@ -0,0 +1,5 @@ +import { convertNxExecutor } from '@nx/devkit'; + +import viteBuildExecutor from './vite.impl'; + +export default convertNxExecutor(viteBuildExecutor); diff --git a/packages/nx-plugin/src/executors/vite/schema.d.ts b/packages/nx-plugin/src/executors/vite/schema.d.ts new file mode 100644 index 000000000..2467b94ce --- /dev/null +++ b/packages/nx-plugin/src/executors/vite/schema.d.ts @@ -0,0 +1,2 @@ +import type { FileReplacement } from '@nx/vite/plugins/rollup-replace-files.plugin'; +export { ViteBuildExecutorOptions } from '@nx/vite/executors'; diff --git a/packages/nx-plugin/src/executors/vite/schema.json b/packages/nx-plugin/src/executors/vite/schema.json new file mode 100644 index 000000000..12bbaff65 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite/schema.json @@ -0,0 +1,169 @@ +{ + "version": 2, + "outputCapture": "direct-nodejs", + "title": "Vite Prod Builder", + "cli": "nx", + "description": "Builds a Vite.js application for production.", + "type": "object", + "presets": [ + { + "name": "Default minimum setup", + "keys": [] + } + ], + "properties": { + "outputPath": { + "type": "string", + "description": "The output path of the generated files.", + "x-completion-type": "directory", + "x-priority": "important" + }, + "buildLibsFromSource": { + "type": "boolean", + "description": "Read buildable libraries from source instead of building them separately.", + "default": true + }, + "skipTypeCheck": { + "type": "boolean", + "description": "Skip type-checking via TypeScript. Skipping type-checking speeds up the build but type errors are not caught.", + "default": false + }, + "base": { + "type": "string", + "description": "Base public path when served in development or production.", + "alias": "baseHref" + }, + "configFile": { + "type": "string", + "description": "The name of the Vite.js configuration file.", + "x-completion-type": "file", + "x-completion-glob": "vite.config.@(js|ts)" + }, + "fileReplacements": { + "description": "Replace files with other files in the build.", + "type": "array", + "items": { + "type": "object", + "properties": { + "replace": { + "type": "string", + "description": "The file to be replaced.", + "x-completion-type": "file" + }, + "with": { + "type": "string", + "description": "The file to replace with.", + "x-completion-type": "file" + } + }, + "additionalProperties": false, + "required": ["replace", "with"] + }, + "default": [] + }, + "emptyOutDir": { + "description": "When set to false, outputPath will not be emptied during the build process.", + "type": "boolean", + "default": true + }, + "sourcemap": { + "description": "Output sourcemaps. Use 'hidden' for use with error reporting tools without generating sourcemap comment.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "target": { + "description": "Browser compatibility target for the final bundle. For more info: https://vitejs.dev/config/build-options.html#build-target", + "type": "string" + }, + "minify": { + "description": "Output sourcemaps. Use 'hidden' for use with error reporting tools without generating sourcemap comment.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "manifest": { + "description": "Output sourcemaps. Use 'hidden' for use with error reporting tools without generating sourcemap comment.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "ssrManifest": { + "description": "When set to true, the build will also generate an SSR manifest for determining style links and asset preload directives in production. When the value is a string, it will be used as the manifest file name.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "ssr": { + "description": "Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or true, which requires specifying the SSR entry via rollupOptions.input.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, + "logLevel": { + "type": "string", + "description": "Adjust console output verbosity.", + "enum": ["info", "warn", "error", "silent"] + }, + "mode": { + "type": "string", + "description": "Mode to run the build in." + }, + "force": { + "description": "Force the optimizer to ignore the cache and re-bundle", + "type": "boolean" + }, + "cssCodeSplit": { + "description": "Enable/disable CSS code splitting. When enabled, CSS imported in async chunks will be inlined into the async chunk itself and inserted when the chunk is loaded.", + "type": "boolean" + }, + "watch": { + "description": "Enable re-building when files change.", + "oneOf": [ + { + "type": "boolean" + }, + { + "type": "object" + } + ], + "default": false + }, + "generatePackageJson": { + "description": "Generate a package.json for the build output.", + "type": "boolean" + }, + "includeDevDependenciesInPackageJson": { + "description": "Include devDependencies in the generated package.json.", + "type": "boolean" + } + }, + "definitions": {}, + "required": [], + "examplesFile": "../../../docs/build-examples.md" +} diff --git a/packages/nx-plugin/src/executors/vite/vite.impl.ts b/packages/nx-plugin/src/executors/vite/vite.impl.ts new file mode 100644 index 000000000..ffaf2b4c6 --- /dev/null +++ b/packages/nx-plugin/src/executors/vite/vite.impl.ts @@ -0,0 +1,3 @@ +import { viteBuildExecutor } from '@nx/vite/executors'; + +export default viteBuildExecutor; diff --git a/packages/nx-plugin/src/executors/vitest/compat.ts b/packages/nx-plugin/src/executors/vitest/compat.ts new file mode 100644 index 000000000..968ab3c52 --- /dev/null +++ b/packages/nx-plugin/src/executors/vitest/compat.ts @@ -0,0 +1,5 @@ +import { convertNxExecutor } from '@nx/devkit'; + +import vitestExecutor from './vitest.impl'; + +export default convertNxExecutor(vitestExecutor); diff --git a/packages/nx-plugin/src/executors/vitest/schema.d.ts b/packages/nx-plugin/src/executors/vitest/schema.d.ts new file mode 100644 index 000000000..0e3fa3058 --- /dev/null +++ b/packages/nx-plugin/src/executors/vitest/schema.d.ts @@ -0,0 +1 @@ +export { VitestExecutorOptions } from '@nx/vite/executors'; diff --git a/packages/nx-plugin/src/executors/vitest/schema.json b/packages/nx-plugin/src/executors/vitest/schema.json new file mode 100644 index 000000000..d730f7033 --- /dev/null +++ b/packages/nx-plugin/src/executors/vitest/schema.json @@ -0,0 +1,68 @@ +{ + "$schema": "http://json-schema.org/schema", + "version": 2, + "cli": "nx", + "title": "Vitest executor", + "description": "Test using Vitest.", + "type": "object", + "properties": { + "config": { + "type": "string", + "description": "The path to the local vitest config", + "x-completion-type": "file", + "x-completion-glob": "@(vitest|vite).config@(.js|.ts)" + }, + "passWithNoTests": { + "type": "boolean", + "default": true, + "description": "Pass the test even if no tests are found" + }, + "testNamePattern": { + "type": "string", + "description": "Run tests with full names matching the pattern" + }, + "mode": { + "type": "string", + "enum": ["test", "benchmark", "typecheck"], + "default": "test", + "description": "The mode that vitest will run on", + "x-priority": "important" + }, + "watch": { + "type": "boolean", + "default": false, + "description": "Enable watch mode" + }, + "reporters": { + "type": "array", + "items": { + "type": "string" + }, + "description": "An array of reporters to pass to vitest" + }, + "update": { + "type": "boolean", + "default": false, + "alias": "u", + "description": "Update snapshots", + "x-priority": "important" + }, + "coverage": { + "type": "boolean", + "default": false, + "description": "Enable coverage report", + "x-priority": "important" + }, + "reportsDirectory": { + "type": "string", + "description": "Directory to write coverage report to." + }, + "testFiles": { + "aliases": ["testFile"], + "type": "array", + "items": { "type": "string" } + } + }, + "required": [], + "examplesFile": "../../../docs/test-examples.md" +} diff --git a/packages/nx-plugin/src/executors/vitest/vitest.impl.ts b/packages/nx-plugin/src/executors/vitest/vitest.impl.ts new file mode 100644 index 000000000..e5680870f --- /dev/null +++ b/packages/nx-plugin/src/executors/vitest/vitest.impl.ts @@ -0,0 +1,3 @@ +import { vitestExecutor } from '@nx/vite/executors'; + +export default vitestExecutor; diff --git a/packages/nx-plugin/src/generators/app/lib/add-analog-project-config.ts b/packages/nx-plugin/src/generators/app/lib/add-analog-project-config.ts index 330d8d3f4..858732f95 100644 --- a/packages/nx-plugin/src/generators/app/lib/add-analog-project-config.ts +++ b/packages/nx-plugin/src/generators/app/lib/add-analog-project-config.ts @@ -18,7 +18,7 @@ export function addAnalogProjectConfig( sourceRoot: `${projectRoot}/src`, targets: { build: { - executor: `${nxPackageNamespace}/vite:build`, + executor: `@analogjs/platform:vite`, outputs: [ '{options.outputPath}', `{workspaceRoot}/dist/${workspaceAppsDir}${projectName}/.nitro`, @@ -43,7 +43,7 @@ export function addAnalogProjectConfig( }, }, serve: { - executor: `${nxPackageNamespace}/vite:dev-server`, + executor: `@analogjs/platform:vite-dev-server`, defaultConfiguration: 'development', options: { buildTarget: `${projectName}:build`, @@ -66,7 +66,7 @@ export function addAnalogProjectConfig( }, }, test: { - executor: `${nxPackageNamespace}/vite:test`, + executor: `@analogjs/platform:vitest`, outputs: [`{projectRoot}/coverage`], }, }, diff --git a/packages/platform/package.json b/packages/platform/package.json index da3060018..49050c7ec 100644 --- a/packages/platform/package.json +++ b/packages/platform/package.json @@ -29,6 +29,8 @@ "@analogjs/vite-plugin-nitro": "^0.2.14", "@nx/devkit": "^16.0.0 || ^17.0.0" }, + "builders": "./src/lib/nx-plugin/executors.json", + "executors": "./src/lib/nx-plugin/executors.json", "generators": "./src/lib/nx-plugin/generators.json", "schematics": "./src/lib/nx-plugin/generators.json", "ng-update": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d0a733e2c..e59ecb5b4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -168,6 +168,9 @@ importers: '@nx/jest': specifier: 17.0.1 version: 17.0.1(@swc-node/register@1.6.2)(@swc/core@1.3.42)(@types/node@18.11.18)(nx@17.0.1)(ts-node@10.9.1)(typescript@5.2.2) + '@nx/js': + specifier: 17.0.1 + version: 17.0.1(@swc-node/register@1.6.2)(@swc/core@1.3.42)(@types/node@18.11.18)(nx@17.0.1)(typescript@5.2.2) '@nx/plugin': specifier: 17.0.1 version: 17.0.1(@swc-node/register@1.6.2)(@swc/core@1.3.42)(@types/node@18.11.18)(eslint@8.46.0)(nx@17.0.1)(ts-node@10.9.1)(typescript@5.2.2) @@ -1566,7 +1569,7 @@ packages: '@babel/core': ^7.0.0 dependencies: '@babel/core': 7.21.8 - '@babel/helper-environment-visitor': 7.22.5 + '@babel/helper-environment-visitor': 7.22.20 '@babel/helper-member-expression-to-functions': 7.22.5 '@babel/helper-optimise-call-expression': 7.22.5 dev: false @@ -1578,7 +1581,7 @@ packages: '@babel/core': ^7.0.0 dependencies: '@babel/core': 7.23.2 - '@babel/helper-environment-visitor': 7.22.5 + '@babel/helper-environment-visitor': 7.22.20 '@babel/helper-member-expression-to-functions': 7.22.5 '@babel/helper-optimise-call-expression': 7.22.5 @@ -15348,7 +15351,7 @@ packages: resolution: {integrity: sha512-pzqtp31nLv/XFOzXGuvhCb8qhjmTVo5vjVk19XE4CRlSWz0KoeJ3bw9XsA7nOp9YBf4qHjwBxkDzKcME/J29Yg==} engines: {node: '>=8'} dependencies: - '@babel/core': 7.22.15 + '@babel/core': 7.23.2 '@babel/parser': 7.22.11 '@istanbuljs/schema': 0.1.3 istanbul-lib-coverage: 3.2.0