<!-- 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
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
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);
|
|
}
|