From 08600febb9e8f33fa058c9c582a2a7c9826dfdca Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 2 Aug 2024 10:43:58 +0100 Subject: [PATCH] fix(web): generate serve-static targets --- .../__snapshots__/webpack.legacy.test.ts.snap | 8 +++++++ e2e/webpack/src/webpack.legacy.test.ts | 6 +++--- .../src/generators/application/lib/add-e2e.ts | 21 +++++++++---------- .../application/application.legacy.spec.ts | 20 ++++++++++++++++++ .../src/generators/application/application.ts | 13 +++++++++--- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap b/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap index 9852a63a9406b6..31219273146df9 100644 --- a/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap +++ b/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap @@ -88,6 +88,14 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi "lint": { "executor": "@nx/eslint:lint" }, + "serve-static": { + "executor": "@nx/web:file-server", + "dependsOn": ["build"], + "options": { + "buildTarget": "app3224373:build", + "spa": true + } + }, "test": { "executor": "@nx/jest:jest", "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], diff --git a/e2e/webpack/src/webpack.legacy.test.ts b/e2e/webpack/src/webpack.legacy.test.ts index 080ea696740c45..1c9a8c020a861a 100644 --- a/e2e/webpack/src/webpack.legacy.test.ts +++ b/e2e/webpack/src/webpack.legacy.test.ts @@ -113,17 +113,17 @@ describe('Webpack Plugin (legacy)', () => { updateFile( `${appName}/src/main.ts`, ` - document.querySelector('proj-root').innerHTML = '

Welcome

'; + document.querySelector('proj-root')!.innerHTML = '

Welcome

'; ` ); updateFile( `${appName}/webpack.config.js`, ` const { join } = require('path'); - const {NxWebpackPlugin} = require('@nx/webpack'); + const {NxAppWebpackPlugin} = require('@nx/webpack/app-plugin'); module.exports = { output: { - path: join(__dirname, '../dist/app9524918'), + path: join(__dirname, '../dist/${appName}'), }, plugins: [ new NxAppWebpackPlugin({ diff --git a/packages/react/src/generators/application/lib/add-e2e.ts b/packages/react/src/generators/application/lib/add-e2e.ts index a6f4617d0d2e10..5da77e12ce49a2 100644 --- a/packages/react/src/generators/application/lib/add-e2e.ts +++ b/packages/react/src/generators/application/lib/add-e2e.ts @@ -16,19 +16,18 @@ export async function addE2e( tree: Tree, options: NormalizedSchema ): Promise { + const hasNxBuildPlugin = + (options.bundler === 'webpack' && hasWebpackPlugin(tree)) || + (options.bundler === 'vite' && hasVitePlugin(tree)); + if (!hasNxBuildPlugin) { + await webStaticServeGenerator(tree, { + buildTarget: `${options.projectName}:build`, + targetName: 'serve-static', + spa: true, + }); + } switch (options.e2eTestRunner) { case 'cypress': { - const hasNxBuildPlugin = - (options.bundler === 'webpack' && hasWebpackPlugin(tree)) || - (options.bundler === 'vite' && hasVitePlugin(tree)); - if (!hasNxBuildPlugin) { - await webStaticServeGenerator(tree, { - buildTarget: `${options.projectName}:build`, - targetName: 'serve-static', - spa: true, - }); - } - const { configurationGenerator } = ensurePackage< typeof import('@nx/cypress') >('@nx/cypress', nxVersion); diff --git a/packages/web/src/generators/application/application.legacy.spec.ts b/packages/web/src/generators/application/application.legacy.spec.ts index a541524603e598..55616211d8b51a 100644 --- a/packages/web/src/generators/application/application.legacy.spec.ts +++ b/packages/web/src/generators/application/application.legacy.spec.ts @@ -106,6 +106,16 @@ describe('web app generator (legacy)', () => { "buildTarget": "my-app:build", }, }, + "serve-static": { + "dependsOn": [ + "build", + ], + "executor": "@nx/web:file-server", + "options": { + "buildTarget": "my-app:build", + "spa": true, + }, + }, "test": { "executor": "@nx/jest:jest", "options": { @@ -204,6 +214,16 @@ describe('web app generator (legacy)', () => { "buildTarget": "my-vite-app:build", }, }, + "serve-static": { + "dependsOn": [ + "build", + ], + "executor": "@nx/web:file-server", + "options": { + "buildTarget": "my-vite-app:build", + "spa": true, + }, + }, "test": { "executor": "@nx/jest:jest", "options": { diff --git a/packages/web/src/generators/application/application.ts b/packages/web/src/generators/application/application.ts index 0d161a2a559a6e..5b0df944b12fc8 100644 --- a/packages/web/src/generators/application/application.ts +++ b/packages/web/src/generators/application/application.ts @@ -42,6 +42,7 @@ import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-com import { VitePluginOptions } from '@nx/vite/src/plugins/plugin'; import { WebpackPluginOptions } from '@nx/webpack/src/plugins/plugin'; import { hasVitePlugin } from '../../utils/has-vite-plugin'; +import staticServeConfiguration from '../static-serve/static-serve-configuration'; interface NormalizedSchema extends Schema { projectName: string; @@ -367,10 +368,16 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) { tasks.push(lintTask); } + const hasNxBuildPlugin = + (options.bundler === 'webpack' && hasWebpackPlugin(host)) || + (options.bundler === 'vite' && hasVitePlugin(host)); + if (!hasNxBuildPlugin) { + await staticServeConfiguration(host, { + buildTarget: `${options.projectName}:build`, + spa: true, + }); + } if (options.e2eTestRunner === 'cypress') { - const hasNxBuildPlugin = - (options.bundler === 'webpack' && hasWebpackPlugin(host)) || - (options.bundler === 'vite' && hasVitePlugin(host)); const { configurationGenerator } = ensurePackage< typeof import('@nx/cypress') >('@nx/cypress', nxVersion);