Skip to content

Commit

Permalink
feat(@schematics/angular): enable scripts optimization for server bundle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 authored and vikerman committed Sep 23, 2019
1 parent c681c9d commit 8ea892c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Migration to version 9', () => {
tree,
)
.toPromise();

tree = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export function updateWorkspaceConfig(): Rule {
updateStyleOrScriptOption('scripts', recorder, target);
}

for (const { target } of getTargets(workspace, 'server', Builders.Server)) {
updateOptimizationOption(recorder, target);
}

tree.commitUpdate(recorder);

return tree;
Expand Down Expand Up @@ -139,3 +143,22 @@ function addAnyComponentStyleBudget(recorder: UpdateRecorder, builderConfig: Jso
}
}
}

function updateOptimizationOption(recorder: UpdateRecorder, builderConfig: JsonAstObject) {
const options = getAllOptions(builderConfig, true);

for (const option of options) {
const optimizationOption = findPropertyInAstObject(option, 'optimization');
if (!optimizationOption) {
// add
insertPropertyInAstObjectInOrder(recorder, option, 'optimization', true, 14);
continue;
}

if (optimizationOption.kind !== 'true') {
const { start, end } = optimizationOption;
recorder.remove(start.offset, end.offset - start.offset);
recorder.insertLeft(start.offset, 'true');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,53 @@ describe('Migration to version 9', () => {
expect(config.configurations.production.aot).toBeUndefined();
});
});

describe('server optimization option', () => {
beforeEach(async () => {
tree = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
'universal',
{
clientProject: 'migration-test',
},
tree,
)
.toPromise();
});

it('should add optimization option when not defined', async () => {
let config = getWorkspaceTargets(tree);
config.server.configurations.production.optimization = undefined;
updateWorkspaceTargets(tree, config);

const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
config = getWorkspaceTargets(tree2).server.configurations;
expect(config.production.optimization).toBe(true);
});

it('should set optimization to true when false', async () => {
let config = getWorkspaceTargets(tree);
config.server.configurations.production.optimization = false;
updateWorkspaceTargets(tree, config);

const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
config = getWorkspaceTargets(tree2).server.configurations;
expect(config.production.optimization).toBe(true);
});

it('should set optimization to true when optimization is fine grained', async () => {
let config = getWorkspaceTargets(tree);
config.server.configurations.production.optimization = {
scripts: false,
styles: true,
};
updateWorkspaceTargets(tree, config);

const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
config = getWorkspaceTargets(tree2).server.configurations;
expect(config.production.optimization).toBe(true);
});
});
});
});
5 changes: 1 addition & 4 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
production: {
fileReplacements,
sourceMap: false,
optimization: {
scripts: false,
styles: true,
},
optimization: true,
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/tests/build/platform-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default async function () {
}


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

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

0 comments on commit 8ea892c

Please sign in to comment.