refactor(platform-browser): drop BROWSER_MODULE_PROVIDERS_MARKER in production (#59412)

In this commit, we switch from decorators (which also produce redundant metadata, such as in the
`declareFactory` instruction) to the `inject` function to drop the `BROWSER_MODULE_PROVIDERS_MARKER`
token in production. This token is actually provided only in development mode but is still
referenced in the constructor due to the `@Inject(BROWSER_MODULE_PROVIDERS_MARKER)` decorator.

PR Close #59412
This commit is contained in:
arturovt 2025-01-07 19:18:57 +02:00 committed by kirjs
parent e8df770141
commit 17510becb0
7 changed files with 17 additions and 24 deletions

View file

@ -33,9 +33,9 @@ export function bootstrapApplication(rootComponent: Type<unknown>, options?: App
// @public
export class BrowserModule {
constructor(providersAlreadyPresent: boolean | null);
constructor();
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<BrowserModule, [{ optional: true; skipSelf: true; }]>;
static ɵfac: i0.ɵɵFactoryDeclaration<BrowserModule, never>;
// (undocumented)
static ɵinj: i0.ɵɵInjectorDeclaration<BrowserModule>;
// (undocumented)

View file

@ -28,7 +28,6 @@
"ApplicationRef",
"BROWSER_ANIMATIONS_PROVIDERS",
"BROWSER_MODULE_PROVIDERS",
"BROWSER_MODULE_PROVIDERS_MARKER",
"BROWSER_NOOP_ANIMATIONS_PROVIDERS",
"BaseAnimationRenderer",
"BehaviorSubject",

View file

@ -9,7 +9,6 @@
"ApplicationModule",
"ApplicationRef",
"BROWSER_MODULE_PROVIDERS",
"BROWSER_MODULE_PROVIDERS_MARKER",
"BehaviorSubject",
"BrowserDomAdapter",
"BrowserModule",

View file

@ -13,7 +13,6 @@
"ApplicationModule",
"ApplicationRef",
"BROWSER_MODULE_PROVIDERS",
"BROWSER_MODULE_PROVIDERS_MARKER",
"BaseControlValueAccessor",
"BehaviorSubject",
"BrowserDomAdapter",

View file

@ -14,7 +14,6 @@
"ApplicationModule",
"ApplicationRef",
"BROWSER_MODULE_PROVIDERS",
"BROWSER_MODULE_PROVIDERS_MARKER",
"BaseControlValueAccessor",
"BehaviorSubject",
"BrowserDomAdapter",

View file

@ -9,7 +9,6 @@
"ApplicationModule",
"ApplicationRef",
"BROWSER_MODULE_PROVIDERS",
"BROWSER_MODULE_PROVIDERS_MARKER",
"BehaviorSubject",
"BrowserDomAdapter",
"BrowserModule",

View file

@ -13,25 +13,20 @@ import {
ɵPLATFORM_BROWSER_ID as PLATFORM_BROWSER_ID,
} from '@angular/common';
import {
APP_ID,
ApplicationConfig as ApplicationConfigFromCore,
ApplicationModule,
ApplicationRef,
createPlatformFactory,
ErrorHandler,
Inject,
InjectionToken,
ModuleWithProviders,
NgModule,
NgZone,
Optional,
PLATFORM_ID,
PLATFORM_INITIALIZER,
platformCore,
PlatformRef,
Provider,
RendererFactory2,
SkipSelf,
StaticProvider,
Testability,
TestabilityRegistry,
@ -42,6 +37,7 @@ import {
ɵsetDocument,
ɵTESTABILITY as TESTABILITY,
ɵTESTABILITY_GETTER as TESTABILITY_GETTER,
inject,
} from '@angular/core';
import {BrowserDomAdapter} from './browser/browser_adapter';
@ -264,18 +260,20 @@ const BROWSER_MODULE_PROVIDERS: Provider[] = [
exports: [CommonModule, ApplicationModule],
})
export class BrowserModule {
constructor(
@Optional()
@SkipSelf()
@Inject(BROWSER_MODULE_PROVIDERS_MARKER)
providersAlreadyPresent: boolean | null,
) {
if ((typeof ngDevMode === 'undefined' || ngDevMode) && providersAlreadyPresent) {
throw new RuntimeError(
RuntimeErrorCode.BROWSER_MODULE_ALREADY_LOADED,
`Providers from the \`BrowserModule\` have already been loaded. If you need access ` +
`to common directives such as NgIf and NgFor, import the \`CommonModule\` instead.`,
);
constructor() {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
const providersAlreadyPresent = inject(BROWSER_MODULE_PROVIDERS_MARKER, {
optional: true,
skipSelf: true,
});
if (providersAlreadyPresent) {
throw new RuntimeError(
RuntimeErrorCode.BROWSER_MODULE_ALREADY_LOADED,
`Providers from the \`BrowserModule\` have already been loaded. If you need access ` +
`to common directives such as NgIf and NgFor, import the \`CommonModule\` instead.`,
);
}
}
}
}