Skip to content

Commit

Permalink
fix(angular): should support filereplacements for apps that already h…
Browse files Browse the repository at this point in the history
…ave them (#13247)
  • Loading branch information
Coly010 committed Nov 18, 2022
1 parent b2b161c commit b4fb074
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
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",
},
}
`;
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
@@ -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();
});
});

0 comments on commit b4fb074

Please sign in to comment.