Skip to content

Commit a416b4e

Browse files
alan-agius4angular-robot[bot]
authored andcommitted
fix(@angular-devkit/build-angular): provide option to run build-optimizer on server bundles
This commit adds the option to run build-optimizer on the server bundles. This is needed as in some cases, there can be restrictions on the size of server bundles that can be executed. One of these cases is CloudFlare workers which by default does not work with an `ng-new` application due to the size of the bundle mainly due to the retention of the `@angular/compiler`.
1 parent 96249ce commit a416b4e

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

goldens/public-api/angular_devkit/build_angular/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export interface ProtractorBuilderOptions {
267267
// @public (undocumented)
268268
export interface ServerBuilderOptions {
269269
assets?: AssetPattern_3[];
270+
buildOptimizer?: boolean;
270271
deleteOutputPath?: boolean;
271272
// @deprecated
272273
deployUrl?: string;

packages/angular_devkit/build_angular/src/builders/server/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ async function initialize(
203203
await generateI18nBrowserWebpackConfigFromContext(
204204
{
205205
...adjustedOptions,
206-
buildOptimizer: false,
207206
aot: true,
208207
platform: 'server',
209208
} as NormalizedBrowserBuilderSchema,

packages/angular_devkit/build_angular/src/builders/server/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@
189189
"description": "Extract all licenses in a separate file, in the case of production builds only.",
190190
"default": true
191191
},
192+
"buildOptimizer": {
193+
"type": "boolean",
194+
"description": "Enables advanced build optimizations.",
195+
"default": true
196+
},
192197
"namedChunks": {
193198
"type": "boolean",
194199
"description": "Use file name for lazy loaded chunks.",

packages/schematics/angular/universal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
4646
// One for the server which will be unhashed, and other on the client which will be hashed.
4747
const getServerOptions = (options: Record<string, JsonValue | undefined> = {}): {} => {
4848
return {
49+
buildOptimizer: options?.buildOptimizer,
4950
outputHashing: options?.outputHashing === 'all' ? 'media' : options?.outputHashing,
5051
fileReplacements: options?.fileReplacements,
5152
optimization: options?.optimization === undefined ? undefined : !!options?.optimization,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { normalize } from 'path';
2+
import { getGlobalVariable } from '../../../utils/env';
3+
import { expectFileToMatch, replaceInFile, writeFile } from '../../../utils/fs';
4+
import { installPackage } from '../../../utils/packages';
5+
import { exec, ng } from '../../../utils/process';
6+
import { updateJsonFile } from '../../../utils/project';
7+
8+
const snapshots = require('../../../ng-snapshot/package.json');
9+
10+
export default async function () {
11+
await ng('generate', 'application', 'test-project-two', '--standalone', '--skip-install');
12+
await ng('generate', 'universal', '--project', 'test-project-two');
13+
14+
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
15+
if (isSnapshotBuild) {
16+
const packagesToInstall: string[] = [];
17+
await updateJsonFile('package.json', (packageJson) => {
18+
const dependencies = packageJson['dependencies'];
19+
// Iterate over all of the packages to update them to the snapshot version.
20+
for (const [name, version] of Object.entries<string>(snapshots.dependencies)) {
21+
if (name in dependencies && dependencies[name] !== version) {
22+
packagesToInstall.push(version);
23+
}
24+
}
25+
});
26+
27+
for (const pkg of packagesToInstall) {
28+
await installPackage(pkg);
29+
}
30+
}
31+
32+
await writeFile(
33+
'./projects/test-project-two/server.ts',
34+
` import 'zone.js/node';
35+
import * as fs from 'fs';
36+
import { renderApplication } from '@angular/platform-server';
37+
import bootstrap from './src/main.server';
38+
39+
renderApplication(bootstrap, {
40+
url: '/',
41+
document: '<app-root></app-root>'
42+
}).then(html => {
43+
fs.writeFileSync('dist/test-project-two/server/index.html', html);
44+
})
45+
`,
46+
);
47+
48+
await replaceInFile(
49+
'./projects/test-project-two/tsconfig.server.json',
50+
'src/main.server.ts',
51+
'server.ts',
52+
);
53+
await replaceInFile('angular.json', 'src/main.server.ts', 'server.ts');
54+
55+
// works with build-optimizer
56+
await ng('run', 'test-project-two:server', '--optimization', '--build-optimizer');
57+
await exec(normalize('node'), 'dist/test-project-two/server/main.js');
58+
await expectFileToMatch(
59+
'dist/test-project-two/server/index.html',
60+
/<p.*>Here are some links to help you get started:<\/p>/,
61+
);
62+
}

0 commit comments

Comments
 (0)