Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): should support filereplacements for apps that already have them #13247

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,31 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {

export * from './src/main.server';"
`;

exports[`setupSSR should use fileReplacements if they already exist 1`] = `
Object {
"configurations": Object {
"development": Object {
"extractLicenses": false,
"optimization": false,
"sourceMap": true,
},
"production": Object {
"fileReplacements": Array [
Object {
"replace": "apps/app1/src/environments/environment.ts",
"with": "apps/app1/src/environments/environment.prod.ts",
},
],
"outputHashing": "media",
},
},
"defaultConfiguration": "production",
"executor": "@angular-devkit/build-angular:server",
"options": Object {
"main": "apps/app1/server.ts",
"outputPath": "dist/apps/app1/server",
"tsConfig": "apps/app1/tsconfig.server.json",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export function updateProjectConfig(tree: Tree, schema: Schema) {

projectConfig.targets.build.options.outputPath = `dist/apps/${schema.project}/browser`;

const buildTargetFileReplacements =
projectConfig.targets.build.configurations?.production?.fileReplacements;

projectConfig.targets.server = {
executor: '@angular-devkit/build-angular:server',
options: {
Expand All @@ -21,6 +24,9 @@ export function updateProjectConfig(tree: Tree, schema: Schema) {
configurations: {
production: {
outputHashing: 'media',
...(buildTargetFileReplacements
? { fileReplacements: buildTargetFileReplacements }
: {}),
},
development: {
optimization: false,
Expand Down
34 changes: 33 additions & 1 deletion packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { readJson, readProjectConfiguration } from '@nrwl/devkit';
import {
readJson,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { PackageJson } from 'nx/src/utils/package-json';
import { angularVersion, ngUniversalVersion } from '../../utils/versions';
Expand Down Expand Up @@ -126,4 +130,32 @@ describe('setupSSR', () => {
expect(packageJson.devDependencies[dep]).toEqual(version);
}
});

it('should use fileReplacements if they already exist', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();

await applicationGenerator(tree, {
name: 'app1',
});

tree.write('apps/app1/src/environments/environment.ts', '');
tree.write('apps/app1/src/environments/environment.prod.ts', '');
const project = readProjectConfiguration(tree, 'app1');
project.targets.build.configurations.production.fileReplacements = [
{
replace: 'apps/app1/src/environments/environment.ts',
with: 'apps/app1/src/environments/environment.prod.ts',
},
];
updateProjectConfiguration(tree, 'app1', project);

// ACT
await setupSsr(tree, { project: 'app1' });

// ASSERT
expect(
readProjectConfiguration(tree, 'app1').targets.server
).toMatchSnapshot();
});
});