X Tutup
The Wayback Machine - https://web.archive.org/web/20250520130538/https://github.com/angular/angular/issues/44119
Skip to content

Convert ViewEncapsulation to a const enum #44119

Closed
@dario-piotrowicz

Description

@dario-piotrowicz

Which @angular/* package(s) are relevant/releated to the feature request?

core

Description

The ViewEncapsulation is an enum it could be converted to a const enum as such it would be tree-shakable and produce less transpiled js code.

Proposed solution

Apply the same approach used for enum InjectFlags and const enum InternalInjectFlags:

/**
* Injection flags for DI.
*
* @publicApi
*/
export enum InjectFlags {
// TODO(alxhub): make this 'const' (and remove `InternalInjectFlags` enum) when ngc no longer
// writes exports of it into ngfactory files.
/** Check self and check parent injector if needed */
Default = 0b0000,
/**
* Specifies that an injector should retrieve a dependency from any injector until reaching the
* host element of the current component. (Only used with Element Injector)
*/
Host = 0b0001,
/** Don't ascend to ancestors of the node requesting injection. */
Self = 0b0010,
/** Skip the node that is requesting injection. */
SkipSelf = 0b0100,
/** Inject `defaultValue` instead if token not found. */
Optional = 0b1000,
}
/**
* This enum is an exact copy of the `InjectFlags` enum above, but the difference is that this is a
* const enum, so actual enum values would be inlined in generated code. The `InjectFlags` enum can
* be turned into a const enum when ViewEngine is removed (see TODO at the `InjectFlags` enum
* above). The benefit of inlining is that we can use these flags at the top level without affecting
* tree-shaking (see "no-toplevel-property-access" tslint rule for more info).
* Keep this enum in sync with `InjectFlags` enum above.
*/
export const enum InternalInjectFlags {
/** Check self and check parent injector if needed */
Default = 0b0000,
/**
* Specifies that an injector should retrieve a dependency from any injector until reaching the
* host element of the current component. (Only used with Element Injector)
*/
Host = 0b0001,
/** Don't ascend to ancestors of the node requesting injection. */
Self = 0b0010,
/** Skip the node that is requesting injection. */
SkipSelf = 0b0100,
/** Inject `defaultValue` instead if token not found. */
Optional = 0b1000,
/**
* This token is being injected into a pipe.
*
* This flag is intentionally not in the public facing `InjectFlags` because it is only added by
* the compiler and is not a developer applicable flag.
*/
ForPipe = 0b10000,
}

Meaning: having two enums, one standard used internally and the other const which can be used by user code and tree shaken.

Other information

Metadata

Metadata

Assignees

No one assigned

    Labels

    P5The team acknowledges the request but does not plan to address it, it remains open for discussionarea: coreIssues related to the framework runtimearea: performanceIssues related to performancecore: CSS encapsulationrefactoringIssue that involves refactoring or code-cleanuprisk: medium

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup