Skip to content
Closed
Prev Previous commit
Next Next commit
refactor(compiler): dynamic default for the partial compiler.
Use `semver` in the partial compiler to decide on a default value

Co-authored-by: Alex Rickabaugh <alxhub@users.noreply.github.com>
  • Loading branch information
JeanMeche and alxhub committed Oct 14, 2024
commit 1380caaac70bdd64dd7b365b1a760c6360967166
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class PartialComponentLinkerVersion1<TStatement, TExpression>
}

return {
...toR3DirectiveMeta(metaObj, this.code, this.sourceUrl),
...toR3DirectiveMeta(metaObj, this.code, this.sourceUrl, version),
viewProviders: metaObj.has('viewProviders') ? metaObj.getOpaque('viewProviders') : null,
template: {
nodes: template.nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {AstObject, AstValue} from '../../ast/ast_value';
import {FatalLinkerError} from '../../fatal_linker_error';

import {LinkedDefinition, PartialLinker} from './partial_linker';
import {extractForwardRef, wrapReference} from './util';
import {extractForwardRef, getDefaultStandaloneValue, wrapReference} from './util';

/**
* A `PartialLinker` that is designed to process `ɵɵngDeclareDirective()` call expressions.
Expand All @@ -46,8 +46,9 @@ export class PartialDirectiveLinkerVersion1<TExpression> implements PartialLinke
linkPartialDeclaration(
constantPool: ConstantPool,
metaObj: AstObject<R3PartialDeclaration, TExpression>,
version: string,
): LinkedDefinition {
const meta = toR3DirectiveMeta(metaObj, this.code, this.sourceUrl);
const meta = toR3DirectiveMeta(metaObj, this.code, this.sourceUrl, version);
return compileDirectiveFromMetadata(meta, constantPool, makeBindingParser());
}
}
Expand All @@ -59,6 +60,7 @@ export function toR3DirectiveMeta<TExpression>(
metaObj: AstObject<R3DeclareDirectiveMetadata, TExpression>,
code: string,
sourceUrl: AbsoluteFsPath,
version: string,
): R3DirectiveMetadata {
const typeExpr = metaObj.getValue('type');
const typeName = typeExpr.getSymbolName();
Expand Down Expand Up @@ -96,7 +98,9 @@ export function toR3DirectiveMeta<TExpression>(
},
name: typeName,
usesInheritance: metaObj.has('usesInheritance') ? metaObj.getBoolean('usesInheritance') : false,
isStandalone: metaObj.has('isStandalone') ? metaObj.getBoolean('isStandalone') : false,
isStandalone: metaObj.has('isStandalone')
? metaObj.getBoolean('isStandalone')
: getDefaultStandaloneValue(version),
isSignal: metaObj.has('isSignal') ? metaObj.getBoolean('isSignal') : false,
hostDirectives: metaObj.has('hostDirectives')
? toHostDirectivesMetadata(metaObj.getValue('hostDirectives'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {AstObject} from '../../ast/ast_value';
import {FatalLinkerError} from '../../fatal_linker_error';

import {LinkedDefinition, PartialLinker} from './partial_linker';
import {wrapReference} from './util';
import {getDefaultStandaloneValue, wrapReference} from './util';

/**
* A `PartialLinker` that is designed to process `ɵɵngDeclarePipe()` call expressions.
Expand All @@ -29,8 +29,9 @@ export class PartialPipeLinkerVersion1<TExpression> implements PartialLinker<TEx
linkPartialDeclaration(
constantPool: ConstantPool,
metaObj: AstObject<R3PartialDeclaration, TExpression>,
version: string,
): LinkedDefinition {
const meta = toR3PipeMeta(metaObj);
const meta = toR3PipeMeta(metaObj, version);
return compilePipeFromMetadata(meta);
}
}
Expand All @@ -40,6 +41,7 @@ export class PartialPipeLinkerVersion1<TExpression> implements PartialLinker<TEx
*/
export function toR3PipeMeta<TExpression>(
metaObj: AstObject<R3DeclarePipeMetadata, TExpression>,
version: string,
): R3PipeMetadata {
const typeExpr = metaObj.getValue('type');
const typeName = typeExpr.getSymbolName();
Expand All @@ -51,7 +53,9 @@ export function toR3PipeMeta<TExpression>(
}

const pure = metaObj.has('pure') ? metaObj.getBoolean('pure') : true;
const isStandalone = metaObj.has('isStandalone') ? metaObj.getBoolean('isStandalone') : false;
const isStandalone = metaObj.has('isStandalone')
? metaObj.getBoolean('isStandalone')
: getDefaultStandaloneValue(version);

return {
name: typeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {

import {AstObject, AstValue} from '../../ast/ast_value';
import {FatalLinkerError} from '../../fatal_linker_error';
import semver from 'semver';

export const PLACEHOLDER_VERSION = '0.0.0-PLACEHOLDER';

Expand Down Expand Up @@ -113,3 +114,11 @@ export function extractForwardRef<TExpression>(
ForwardRefHandling.Unwrapped,
);
}

const STANDALONE_IS_DEFAULT_RANGE = new semver.Range('>= 19.0.0', {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure that 0.0.0-PLACEHOLDER is also considered to pass.

includePrerelease: true,
});

export function getDefaultStandaloneValue(version: string): boolean {
return STANDALONE_IS_DEFAULT_RANGE.test(version);
}
2 changes: 2 additions & 0 deletions packages/compiler-cli/src/ngtsc/metadata/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export class DtsMetadataReader implements MetadataReader {
const ngContentSelectors =
def.type.typeArguments.length > 6 ? readStringArrayType(def.type.typeArguments[6]) : null;

// Note: the default value is still `false` here, because only legacy .d.ts files written before
// we had so many arguments use this default.
const isStandalone =
def.type.typeArguments.length > 7 && (readBooleanType(def.type.typeArguments[7]) ?? false);

Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/compiler_facade_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export type LegacyInputPartialMapping =
export interface R3DeclareDirectiveFacade {
selector?: string;
type: Type;
version: string;
inputs?: {
[fieldName: string]:
| {
Expand Down Expand Up @@ -390,6 +391,7 @@ export interface R3DeclareNgModuleFacade {

export interface R3DeclarePipeFacade {
type: Type;
version: string;
name: string;
pure?: boolean;
isStandalone?: boolean;
Expand Down
7 changes: 5 additions & 2 deletions packages/compiler/src/jit_compiler_facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import {makeBindingParser, parseTemplate} from './render3/view/template';
import {ResourceLoader} from './resource_loader';
import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry';
import {SelectorMatcher} from './selector';
import {getJitStandaloneDefaultForVersion} from './util';

export class CompilerFacadeImpl implements CompilerFacade {
FactoryTarget = FactoryTarget;
Expand Down Expand Up @@ -567,7 +568,8 @@ function convertDeclareDirectiveFacadeToMetadata(
deps: null,
typeArgumentCount: 0,
fullInheritance: false,
isStandalone: declaration.isStandalone ?? false,
isStandalone:
declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
isSignal: declaration.isSignal ?? false,
hostDirectives,
};
Expand Down Expand Up @@ -1018,7 +1020,8 @@ function convertDeclarePipeFacadeToMetadata(declaration: R3DeclarePipeFacade): R
pipeName: declaration.name,
deps: null,
pure: declaration.pure ?? true,
isStandalone: declaration.isStandalone ?? false,
isStandalone:
declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
};
}

Expand Down
17 changes: 17 additions & 0 deletions packages/compiler/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,20 @@ export function partitionArray<T, F = T>(
}
return [truthy, falsy];
}

const SINGLE_DIGIT_VERSIONS = /^\d\./;
const V11_V18 = /1[12345678]\./;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could probably combine this into a single Regex. e.g. V1_TO_18 = ^([1-9]|1[0-8])\.


export function getJitStandaloneDefaultForVersion(version: string): boolean {
if (version.startsWith('0.')) {
// 0.0.0 is always "latest", default is true.
return true;
}
if (SINGLE_DIGIT_VERSIONS.test(version) || V11_V18.test(version)) {
// Angular v2 - v18 default is false.
return false;
}

// All other Angular versions (v19+) default to true.
return true;
}
2 changes: 2 additions & 0 deletions packages/core/src/compiler/compiler_facade_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export type LegacyInputPartialMapping =
export interface R3DeclareDirectiveFacade {
selector?: string;
type: Type;
version: string;
inputs?: {
[fieldName: string]:
| {
Expand Down Expand Up @@ -391,6 +392,7 @@ export interface R3DeclareNgModuleFacade {
export interface R3DeclarePipeFacade {
type: Type;
name: string;
version: string;
pure?: boolean;
isStandalone?: boolean;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/render3/ivy/jit_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('render3 jit', () => {
class OuterCmp {
static ɵcmp = ngDeclareComponent({
template: '<inner-cmp></inner-cmp>',
version: '18.0.0',
type: OuterCmp,
components: [
{
Expand Down Expand Up @@ -95,6 +96,7 @@ describe('render3 jit', () => {
static ɵcmp = ngDeclareComponent({
template: '<inner-cmp></inner-cmp>',
type: OuterCmp,
version: '18.0.0',
dependencies: [
{
kind: 'component',
Expand Down
Loading