Skip to content
Open
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
8 changes: 6 additions & 2 deletions packages/compiler/src/jit_compiler_facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import {makeBindingParser, parseTemplate} from './render3/view/template';
import {ResourceLoader} from './resource_loader';
import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry';
import {compileService} from './service_compiler';
import {getJitStandaloneDefaultForVersion} from './util';
import {getJitOnPushDefaultForVersion, getJitStandaloneDefaultForVersion} from './util';

export class CompilerFacadeImpl implements CompilerFacade {
FactoryTarget = FactoryTarget;
Expand Down Expand Up @@ -707,6 +707,8 @@ function convertDeclareComponentFacadeToMetadata(
kind === R3TemplateDependencyKind.Directive || kind === R3TemplateDependencyKind.NgModule,
);

const hasOnPushByDefault = getJitOnPushDefaultForVersion(decl.version);

return {
...convertDeclareDirectiveFacadeToMetadata(decl, typeSourceSpan),
template,
Expand All @@ -716,7 +718,9 @@ function convertDeclareComponentFacadeToMetadata(
decl.viewProviders !== undefined ? new WrappedNodeExpr(decl.viewProviders) : null,
animations: decl.animations !== undefined ? new WrappedNodeExpr(decl.animations) : null,
defer,
changeDetection: decl.changeDetection ?? ChangeDetectionStrategy.OnPush,
changeDetection:
decl.changeDetection ??
(hasOnPushByDefault ? ChangeDetectionStrategy.OnPush : ChangeDetectionStrategy.Default),
encapsulation: decl.encapsulation ?? ViewEncapsulation.Emulated,
declarationListEmitMode: DeclarationListEmitMode.ClosureResolved,
relativeContextFilePath: '',
Expand Down
15 changes: 11 additions & 4 deletions packages/compiler/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@ const _global: {[name: string]: any} = globalThis;
export {_global as global};

const V1_TO_18 = /^([1-9]|1[0-8])\./;
const V1_TO_21 = /^([1-9]|1[0-9]|2[0-1])\./;

export function getJitStandaloneDefaultForVersion(version: string): boolean {
if (version.startsWith('0.')) {
// 0.0.0 is always "latest", default is true.
return true;
}
if (V1_TO_18.test(version)) {
// Angular v2 - v18 default is false.
return false;
Expand All @@ -150,6 +147,16 @@ export function getJitStandaloneDefaultForVersion(version: string): boolean {
return true;
}

export function getJitOnPushDefaultForVersion(version: string): boolean {
if (V1_TO_21.test(version)) {
// Angular v2 - v21 default is false.
return false;
}

// All other Angular versions (v22+) default to true.
return true;
}

/**
* Namespaces a CSS variable name and validates its syntax.
*
Expand Down
44 changes: 43 additions & 1 deletion packages/compiler/test/util_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {escapeRegExp, splitAtColon, stringify, utf8Encode} from '../src/util';
import {
escapeRegExp,
getJitOnPushDefaultForVersion,
getJitStandaloneDefaultForVersion,
splitAtColon,
stringify,
utf8Encode,
} from '../src/util';

describe('util', () => {
describe('splitAtColon', () => {
Expand Down Expand Up @@ -86,4 +93,39 @@ describe('util', () => {
expect(stringify(Object.create(null))).toEqual('object');
});
});

describe('getJitStandaloneDefaultForVersion', () => {
it('should return true for 0.0.0-PLACEHOLDER (starts with 0.)', () => {
expect(getJitStandaloneDefaultForVersion('0.0.0-PLACEHOLDER')).toBe(true);
});

it('should return false for versions 1 through 18', () => {
expect(getJitStandaloneDefaultForVersion('14.2.0')).toBe(false);
expect(getJitStandaloneDefaultForVersion('18.0.0')).toBe(false);
expect(getJitStandaloneDefaultForVersion('18.2.0')).toBe(false);
});

it('should return true for versions 19 and above', () => {
expect(getJitStandaloneDefaultForVersion('19.0.0')).toBe(true);
expect(getJitStandaloneDefaultForVersion('20.0.0')).toBe(true);
});
});

describe('getJitOnPushDefaultForVersion', () => {
it('should return true for 0.0.0-PLACEHOLDER (starts with 0.)', () => {
expect(getJitOnPushDefaultForVersion('0.0.0-PLACEHOLDER')).toBe(true);
});

it('should return false for versions 1 through 21', () => {
expect(getJitOnPushDefaultForVersion('14.2.0')).toBe(false);
expect(getJitOnPushDefaultForVersion('20.0.0')).toBe(false);
expect(getJitOnPushDefaultForVersion('21.0.0')).toBe(false);
expect(getJitOnPushDefaultForVersion('21.2.0')).toBe(false);
});

it('should return true for versions 22 and above', () => {
expect(getJitOnPushDefaultForVersion('22.0.0')).toBe(true);
expect(getJitOnPushDefaultForVersion('23.0.0')).toBe(true);
});
});
});
3 changes: 2 additions & 1 deletion packages/core/test/render3/jit/declare_component_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import {core} from '@angular/compiler';
import {
AbstractType,
ChangeDetectionStrategy,
Component,
Directive,
ElementRef,
forwardRef,
Pipe,
Type,
AbstractType,
ViewEncapsulation,
ɵɵngDeclareComponent,
} from '../../../src/core';
Expand Down Expand Up @@ -306,6 +306,7 @@ describe('component declaration jit compilation', () => {
version: '18.0.0',
type: TestClass,
template: '<div></div>',
changeDetection: ChangeDetectionStrategy.OnPush,
}) as ComponentDef<TestClass>;

expectComponentDef(def, {
Expand Down
Loading