Skip to content

Commit

Permalink
fix(web): generate serve-static targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Aug 2, 2024
1 parent ecbd181 commit 08600fe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
8 changes: 8 additions & 0 deletions e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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}"],
Expand Down
6 changes: 3 additions & 3 deletions e2e/webpack/src/webpack.legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ describe('Webpack Plugin (legacy)', () => {
updateFile(
`${appName}/src/main.ts`,
`
document.querySelector('proj-root').innerHTML = '<h1>Welcome</h1>';
document.querySelector('proj-root')!.innerHTML = '<h1>Welcome</h1>';
`
);
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({
Expand Down
21 changes: 10 additions & 11 deletions packages/react/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ export async function addE2e(
tree: Tree,
options: NormalizedSchema
): Promise<GeneratorCallback> {
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);
Expand Down
20 changes: 20 additions & 0 deletions packages/web/src/generators/application/application.legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
13 changes: 10 additions & 3 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 08600fe

Please sign in to comment.