perf(core): add option to remove blank text nodes from compiled templates

This commit is contained in:
Pawel Kozlowski
2017-07-28 15:58:28 +02:00
committed by Hans
parent 088532bf2e
commit d2c0d986d4
27 changed files with 450 additions and 46 deletions

View File

@ -107,6 +107,7 @@ export type CompilerOptions = {
// Whether to support the `<template>` tag and the `template` attribute to define angular
// templates. They have been deprecated in 4.x, `<ng-template>` should be used instead.
enableLegacyTemplate?: boolean,
preserveWhitespaces?: boolean,
};
/**

View File

@ -675,6 +675,16 @@ export interface Component extends Directive {
* {@link ComponentFactoryResolver}.
*/
entryComponents?: Array<Type<any>|any[]>;
/**
* If preserveWhitespaces is set to `false` potentially superfluous blank characters (space, tab,
* new line) will be removed from compiled templates. This can greatly reduce generated code size
* as well as speed up components' creation. The whitespace removal algorithm will drop all
* the blank text nodes and collapse series of whitespaces to just one space.
* Those transformations can potentially influence layout of the generated markup so
* the `preserveWhitespaces` should be used with care.
*/
preserveWhitespaces?: boolean;
}
/**

View File

@ -1758,6 +1758,51 @@ function declareTests({useJit}: {useJit: boolean}) {
});
});
describe('whitespaces in templates', () => {
it('should not remove whitespaces by default', async(() => {
@Component({
selector: 'comp',
template: '<span>foo</span> <span>bar</span>',
})
class MyCmp {
}
const f = TestBed.configureTestingModule({declarations: [MyCmp]}).createComponent(MyCmp);
f.detectChanges();
expect(f.nativeElement.childNodes.length).toBe(3);
}));
it('should not remove whitespaces when explicitly requested not to do so', async(() => {
@Component({
selector: 'comp',
template: '<span>foo</span> <span>bar</span>',
preserveWhitespaces: true,
})
class MyCmp {
}
const f = TestBed.configureTestingModule({declarations: [MyCmp]}).createComponent(MyCmp);
f.detectChanges();
expect(f.nativeElement.childNodes.length).toBe(3);
}));
it('should remove whitespaces when explicitly requested to do so', async(() => {
@Component({
selector: 'comp',
template: '<span>foo</span> <span>bar</span>',
preserveWhitespaces: false,
})
class MyCmp {
}
const f = TestBed.configureTestingModule({declarations: [MyCmp]}).createComponent(MyCmp);
f.detectChanges();
expect(f.nativeElement.childNodes.length).toBe(2);
}));
});
if (getDOM().supportsDOMEvents()) {
describe('svg', () => {