Skip to content

Commit

Permalink
fix(angular): add serve static target more intentionally #27854 (#27924)
Browse files Browse the repository at this point in the history
<!-- 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
  • Loading branch information
Coly010 authored Sep 16, 2024
1 parent 9c218e7 commit d723326
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
8 changes: 7 additions & 1 deletion packages/angular/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
addE2e,
addLinting,
addProxyConfig,
addServeStaticTarget,
addUnitTestRunner,
createFiles,
createProject,
Expand Down Expand Up @@ -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);

Expand Down
31 changes: 1 addition & 30 deletions packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions packages/angular/src/generators/application/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit d723326

Please sign in to comment.