Skip to content

Commit 8ea892c

Browse files
alan-agius4vikerman
authored andcommitted
feat(@schematics/angular): enable scripts optimization for server bundle
The optimizations are suggested to; 1) disables ngDevMode via terser 2) helps with cold server starts the same way as client by lowering JS parse times
1 parent c681c9d commit 8ea892c

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

packages/schematics/angular/migrations/update-9/ivy-libraries_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('Migration to version 9', () => {
4949
tree,
5050
)
5151
.toPromise();
52+
5253
tree = await schematicRunner
5354
.runExternalSchematicAsync(
5455
require.resolve('../../collection.json'),

packages/schematics/angular/migrations/update-9/update-workspace-config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export function updateWorkspaceConfig(): Rule {
4040
updateStyleOrScriptOption('scripts', recorder, target);
4141
}
4242

43+
for (const { target } of getTargets(workspace, 'server', Builders.Server)) {
44+
updateOptimizationOption(recorder, target);
45+
}
46+
4347
tree.commitUpdate(recorder);
4448

4549
return tree;
@@ -139,3 +143,22 @@ function addAnyComponentStyleBudget(recorder: UpdateRecorder, builderConfig: Jso
139143
}
140144
}
141145
}
146+
147+
function updateOptimizationOption(recorder: UpdateRecorder, builderConfig: JsonAstObject) {
148+
const options = getAllOptions(builderConfig, true);
149+
150+
for (const option of options) {
151+
const optimizationOption = findPropertyInAstObject(option, 'optimization');
152+
if (!optimizationOption) {
153+
// add
154+
insertPropertyInAstObjectInOrder(recorder, option, 'optimization', true, 14);
155+
continue;
156+
}
157+
158+
if (optimizationOption.kind !== 'true') {
159+
const { start, end } = optimizationOption;
160+
recorder.remove(start.offset, end.offset - start.offset);
161+
recorder.insertLeft(start.offset, 'true');
162+
}
163+
}
164+
}

packages/schematics/angular/migrations/update-9/update-workspace-config_spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,53 @@ describe('Migration to version 9', () => {
248248
expect(config.configurations.production.aot).toBeUndefined();
249249
});
250250
});
251+
252+
describe('server optimization option', () => {
253+
beforeEach(async () => {
254+
tree = await schematicRunner
255+
.runExternalSchematicAsync(
256+
require.resolve('../../collection.json'),
257+
'universal',
258+
{
259+
clientProject: 'migration-test',
260+
},
261+
tree,
262+
)
263+
.toPromise();
264+
});
265+
266+
it('should add optimization option when not defined', async () => {
267+
let config = getWorkspaceTargets(tree);
268+
config.server.configurations.production.optimization = undefined;
269+
updateWorkspaceTargets(tree, config);
270+
271+
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
272+
config = getWorkspaceTargets(tree2).server.configurations;
273+
expect(config.production.optimization).toBe(true);
274+
});
275+
276+
it('should set optimization to true when false', async () => {
277+
let config = getWorkspaceTargets(tree);
278+
config.server.configurations.production.optimization = false;
279+
updateWorkspaceTargets(tree, config);
280+
281+
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
282+
config = getWorkspaceTargets(tree2).server.configurations;
283+
expect(config.production.optimization).toBe(true);
284+
});
285+
286+
it('should set optimization to true when optimization is fine grained', async () => {
287+
let config = getWorkspaceTargets(tree);
288+
config.server.configurations.production.optimization = {
289+
scripts: false,
290+
styles: true,
291+
};
292+
updateWorkspaceTargets(tree, config);
293+
294+
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
295+
config = getWorkspaceTargets(tree2).server.configurations;
296+
expect(config.production.optimization).toBe(true);
297+
});
298+
});
251299
});
252300
});

packages/schematics/angular/universal/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
6262
production: {
6363
fileReplacements,
6464
sourceMap: false,
65-
optimization: {
66-
scripts: false,
67-
styles: true,
68-
},
65+
optimization: true,
6966
},
7067
},
7168
});

tests/legacy-cli/e2e/tests/build/platform-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default async function () {
5454
}
5555

5656

57-
await ng('run', 'test-project:server:production');
57+
await ng('run', 'test-project:server:production', '--optimization', 'false');
5858

5959
await expectFileToMatch('dist/server/main.js', veEnabled ? /exports.*AppServerModuleNgFactory/ : /exports.*AppServerModule/);
6060
await exec(normalize('node'), 'index.js');

0 commit comments

Comments
 (0)