Skip to content

fix(@angular-devkit/build-angular): provide option to run build-optimizer on server bundles #25070

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

Merged
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
1 change: 1 addition & 0 deletions goldens/public-api/angular_devkit/build_angular/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export interface ProtractorBuilderOptions {
// @public (undocumented)
export interface ServerBuilderOptions {
assets?: AssetPattern_3[];
buildOptimizer?: boolean;
deleteOutputPath?: boolean;
// @deprecated
deployUrl?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ async function initialize(
await generateI18nBrowserWebpackConfigFromContext(
{
...adjustedOptions,
buildOptimizer: false,
aot: true,
platform: 'server',
} as NormalizedBrowserBuilderSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
"description": "Extract all licenses in a separate file, in the case of production builds only.",
"default": true
},
"buildOptimizer": {
"type": "boolean",
"description": "Enables advanced build optimizations.",
"default": true
},
"namedChunks": {
"type": "boolean",
"description": "Use file name for lazy loaded chunks.",
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
// One for the server which will be unhashed, and other on the client which will be hashed.
const getServerOptions = (options: Record<string, JsonValue | undefined> = {}): {} => {
return {
buildOptimizer: options?.buildOptimizer,
outputHashing: options?.outputHashing === 'all' ? 'media' : options?.outputHashing,
fileReplacements: options?.fileReplacements,
optimization: options?.optimization === undefined ? undefined : !!options?.optimization,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { normalize } from 'path';
import { getGlobalVariable } from '../../../utils/env';
import { expectFileToMatch, replaceInFile, writeFile } from '../../../utils/fs';
import { installPackage } from '../../../utils/packages';
import { exec, ng } from '../../../utils/process';
import { updateJsonFile } from '../../../utils/project';

const snapshots = require('../../../ng-snapshot/package.json');

export default async function () {
await ng('generate', 'application', 'test-project-two', '--standalone', '--skip-install');
await ng('generate', 'universal', '--project', 'test-project-two');

const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
if (isSnapshotBuild) {
const packagesToInstall: string[] = [];
await updateJsonFile('package.json', (packageJson) => {
const dependencies = packageJson['dependencies'];
// Iterate over all of the packages to update them to the snapshot version.
for (const [name, version] of Object.entries<string>(snapshots.dependencies)) {
if (name in dependencies && dependencies[name] !== version) {
packagesToInstall.push(version);
}
}
});

for (const pkg of packagesToInstall) {
await installPackage(pkg);
}
}

await writeFile(
'./projects/test-project-two/server.ts',
` import 'zone.js/node';
import * as fs from 'fs';
import { renderApplication } from '@angular/platform-server';
import bootstrap from './src/main.server';

renderApplication(bootstrap, {
url: '/',
document: '<app-root></app-root>'
}).then(html => {
fs.writeFileSync('dist/test-project-two/server/index.html', html);
})
`,
);

await replaceInFile(
'./projects/test-project-two/tsconfig.server.json',
'src/main.server.ts',
'server.ts',
);
await replaceInFile('angular.json', 'src/main.server.ts', 'server.ts');

// works with build-optimizer
await ng('run', 'test-project-two:server', '--optimization', '--build-optimizer');
await exec(normalize('node'), 'dist/test-project-two/server/main.js');
await expectFileToMatch(
'dist/test-project-two/server/index.html',
/<p.*>Here are some links to help you get started:<\/p>/,
);
}