refactor(core): remove module bootstrap code when using standalone. (#59208)

This commit improves tree shaking in standalone apps.

PR Close #59208
This commit is contained in:
Matthieu Riegler 2024-12-16 18:42:24 +01:00 committed by Andrew Kushnir
parent 33a336f74a
commit c2c8b75d7b
6 changed files with 28 additions and 3 deletions

View file

@ -162,7 +162,7 @@ export function bootstrap<M>(
}
return appRef;
} else {
moduleDoBootstrap(config.moduleRef, config.allPlatformModules);
moduleBootstrapImpl?.(config.moduleRef, config.allPlatformModules);
return config.moduleRef;
}
});
@ -170,7 +170,20 @@ export function bootstrap<M>(
});
}
function moduleDoBootstrap(
/**
* Having a separate symbol for the module boostrap implementation allows us to
* tree shake the module based boostrap implementation in standalone apps.
*/
let moduleBootstrapImpl: undefined | typeof _moduleDoBootstrap;
/**
* Set the implementation of the module based bootstrap.
*/
export function setModuleBootstrapImpl() {
moduleBootstrapImpl = _moduleDoBootstrap;
}
function _moduleDoBootstrap(
moduleRef: InternalNgModuleRef<any>,
allPlatformModules: NgModuleRef<unknown>[],
): void {

View file

@ -22,7 +22,7 @@ import {CompilerOptions} from '../linker';
import {NgModuleFactory, NgModuleRef} from '../linker/ng_module_factory';
import {createNgModuleRefWithProviders} from '../render3/ng_module_ref';
import {getNgZone} from '../zone/ng_zone';
import {bootstrap} from './bootstrap';
import {bootstrap, setModuleBootstrapImpl} from './bootstrap';
import {PLATFORM_DESTROY_LISTENERS} from './platform_destroy_listeners';
/**
@ -77,6 +77,7 @@ export class PlatformRef {
allAppProviders,
);
setModuleBootstrapImpl();
return bootstrap({
moduleRef,
allPlatformModules: this._modules,
@ -107,6 +108,7 @@ export class PlatformRef {
| Array<CompilerOptions & BootstrapOptions> = [],
): Promise<NgModuleRef<M>> {
const options = optionsReducer({}, compilerOptions);
setModuleBootstrapImpl();
return compileNgModuleFactory(this.injector, options, moduleType).then((moduleFactory) =>
this.bootstrapModuleFactory(moduleFactory, options),
);

View file

@ -160,6 +160,7 @@
"_isRefreshingViews",
"_keyMap",
"_locateOrCreateElementNode",
"_moduleDoBootstrap",
"_platformInjector",
"_testabilityGetter",
"_wasLastNodeCreated",
@ -345,6 +346,7 @@
"maybeWrapInNotSelector",
"mergeHostAttribute",
"mergeHostAttrs",
"moduleBootstrapImpl",
"nativeAppendChild",
"nativeAppendOrInsertBefore",
"nativeInsertBefore",
@ -399,6 +401,7 @@
"setInjectImplementation",
"setInputsFromAttrs",
"setIsRefreshingViews",
"setModuleBootstrapImpl",
"setSelectedIndex",
"setShadowStylingInputFlags",
"setupHostDirectiveInputsOrOutputs",

View file

@ -413,6 +413,7 @@
"maybeWrapInNotSelector",
"mergeHostAttribute",
"mergeHostAttrs",
"moduleBootstrapImpl",
"nativeAppendChild",
"nativeAppendOrInsertBefore",
"nativeInsertBefore",

View file

@ -229,6 +229,7 @@
"_locateOrCreateContainerAnchor",
"_locateOrCreateElementNode",
"_locateOrCreateTextNode",
"_moduleDoBootstrap",
"_platformInjector",
"_testabilityGetter",
"_wasLastNodeCreated",
@ -514,6 +515,7 @@
"mergeHostAttrs",
"mergeInputsWithTransforms",
"mergeValidators",
"moduleBootstrapImpl",
"multiFactoryAdd",
"multiProvidersFactoryResolver",
"multiResolve",
@ -594,6 +596,7 @@
"setInjectImplementation",
"setInputsFromAttrs",
"setIsRefreshingViews",
"setModuleBootstrapImpl",
"setSelectedIndex",
"setShadowStylingInputFlags",
"setTStylingRangeNext",

View file

@ -226,6 +226,7 @@
"_locateOrCreateContainerAnchor",
"_locateOrCreateElementNode",
"_locateOrCreateTextNode",
"_moduleDoBootstrap",
"_platformInjector",
"_testabilityGetter",
"_wasLastNodeCreated",
@ -503,6 +504,7 @@
"mergeInputsWithTransforms",
"mergeValidators",
"modelGroupProvider",
"moduleBootstrapImpl",
"multiFactoryAdd",
"multiProvidersFactoryResolver",
"multiResolve",
@ -588,6 +590,7 @@
"setInjectImplementation",
"setInputsFromAttrs",
"setIsRefreshingViews",
"setModuleBootstrapImpl",
"setPropertyAndInputs",
"setSelectedIndex",
"setShadowStylingInputFlags",