fix(angular): add serve static target more intentionally #27854 (#27924)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
The `serve-static` target is being added in the `add-e2e` file, however,
it has uses beyond e2e.



## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The `serve-static` target should be added with more intention

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27854
This commit is contained in:
Colum Ferry 2024-09-16 13:45:46 +01:00 committed by GitHub
parent 9c218e7606
commit d7233268d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 31 deletions

View File

@ -16,6 +16,7 @@ import {
addE2e,
addLinting,
addProxyConfig,
addServeStaticTarget,
addUnitTestRunner,
createFiles,
createProject,
@ -74,7 +75,12 @@ export async function applicationGeneratorInternal(
await addLinting(tree, options);
await addUnitTestRunner(tree, options);
await addE2e(tree, options);
const e2ePort = await addE2e(tree, options);
addServeStaticTarget(
tree,
options,
options.e2eTestRunner !== 'none' ? e2ePort : options.port
);
updateEditorTsConfig(tree, options);
setGeneratorDefaults(tree, options);

View File

@ -27,8 +27,6 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
options.name,
options.port
);
// TODO: This can call `@nx/web:static-config` generator when ready
addFileServerTarget(tree, options, 'serve-static', e2eWebServerInfo.e2ePort);
if (options.e2eTestRunner === 'cypress') {
const { configurationGenerator } = ensurePackage<
@ -101,35 +99,8 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
);
}
}
}
function addFileServerTarget(
tree: Tree,
options: NormalizedSchema,
targetName: string,
e2ePort: number
) {
if (!options.skipPackageJson) {
addDependenciesToPackageJson(tree, {}, { '@nx/web': nxVersion });
}
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
const isUsingApplicationBuilder =
angularMajorVersion >= 17 && options.bundler === 'esbuild';
const projectConfig = readProjectConfiguration(tree, options.name);
projectConfig.targets[targetName] = {
executor: '@nx/web:file-server',
options: {
buildTarget: `${options.name}:build`,
port: e2ePort,
staticFilePath: isUsingApplicationBuilder
? joinPathFragments(options.outputPath, 'browser')
: undefined,
spa: true,
},
};
updateProjectConfiguration(tree, options.name, projectConfig);
return e2eWebServerInfo.e2ePort;
}
function getAngularE2EWebServerInfo(

View File

@ -0,0 +1,47 @@
import { Tree } from '@nx/devkit';
import type { NormalizedSchema } from './normalized-schema';
import {
addDependenciesToPackageJson,
joinPathFragments,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nx/devkit';
import { nxVersion } from '../../../utils/versions';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
export function addServeStaticTarget(
tree: Tree,
options: NormalizedSchema,
port: number
) {
addFileServerTarget(tree, options, 'serve-static', port);
}
function addFileServerTarget(
tree: Tree,
options: NormalizedSchema,
targetName: string,
e2ePort: number
) {
if (!options.skipPackageJson) {
addDependenciesToPackageJson(tree, {}, { '@nx/web': nxVersion });
}
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
const isUsingApplicationBuilder =
angularMajorVersion >= 17 && options.bundler === 'esbuild';
const projectConfig = readProjectConfiguration(tree, options.name);
projectConfig.targets[targetName] = {
executor: '@nx/web:file-server',
options: {
buildTarget: `${options.name}:build`,
port: e2ePort,
staticFilePath: isUsingApplicationBuilder
? joinPathFragments(options.outputPath, 'browser')
: undefined,
spa: true,
},
};
updateProjectConfiguration(tree, options.name, projectConfig);
}

View File

@ -10,3 +10,4 @@ export * from './normalized-schema';
export * from './set-app-strict-default';
export * from './set-generator-defaults';
export * from './update-editor-tsconfig';
export * from './add-serve-static-target';