fix(ivy): ensure `DebugNode`/`DebugElement` are tree-shakeable in Ivy (#35003)

There are different `DebugNode`/`DebugElement` implementations (and
associated helper functions) for ViewEngine and Ivy. Additionally, these
classes/functions, which are defined inside the `core` package, are
imported by the `platform-browser` package.

Previously, this code was not tree-shaken as expected in Ivy. #30130
partially addressed the issue, but only for the case where `core` and
`platform-browser` end up in the same closure after webpack's scope
hoisting. In cases where this is not the case, our webpack/terser based
tooling is not capable of tree-shaking it.

This commit fixes the problem, by ensuring that the code retained in Ivy
mode (due to the cross-package import) does not unnecessarily reference
`DebugNode`/`DebugElement`, allowing the code to be tree-shaken away.
This results in a 7.6KB reduction in the size of the main angular.io
bundle.

Jira issue: [FW-1802](https://angular-team.atlassian.net/browse/FW-1802)

PR Close #35003
This commit is contained in:
George Kalpakas 2020-01-28 11:13:23 +02:00 committed by Andrew Kushnir
parent c8eb164f01
commit 669df70da5
5 changed files with 23 additions and 10 deletions

View File

@ -12,7 +12,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 2987,
"main-es2015": 456604,
"main-es2015": 448956,
"polyfills-es2015": 52487
}
}
@ -21,7 +21,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 3097,
"main-es2015": 426978,
"main-es2015": 427017,
"polyfills-es2015": 52487
}
}

View File

@ -39,7 +39,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 2289,
"main-es2015": 254657,
"main-es2015": 247225,
"polyfills-es2015": 36808,
"5-es2015": 751
}

View File

@ -12,6 +12,7 @@ export {defaultIterableDiffers as ɵdefaultIterableDiffers, defaultKeyValueDiffe
export {devModeEqual as ɵdevModeEqual, isListLikeIterable as ɵisListLikeIterable} from './change_detection/change_detection_util';
export {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants';
export {Console as ɵConsole} from './console';
export {getDebugNodeR2 as ɵgetDebugNodeR2} from './debug/debug_node';
export {inject, setCurrentInjector as ɵsetCurrentInjector, ɵɵinject} from './di/injector_compatibility';
export {getInjectableDef as ɵgetInjectableDef, ɵɵInjectableDef, ɵɵInjectorDef} from './di/interface/defs';
export {INJECTOR_SCOPE as ɵINJECTOR_SCOPE} from './di/scope';

View File

@ -727,6 +727,18 @@ export function getDebugNode__POST_R3__(nativeNode: any): DebugNode|null {
*/
export const getDebugNode: (nativeNode: any) => DebugNode | null = getDebugNode__PRE_R3__;
export function getDebugNodeR2__PRE_R3__(nativeNode: any): DebugNode|null {
return getDebugNode__PRE_R3__(nativeNode);
}
export function getDebugNodeR2__POST_R3__(_nativeNode: any): DebugNode|null {
return null;
}
export const getDebugNodeR2: (nativeNode: any) => DebugNode | null = getDebugNodeR2__PRE_R3__;
export function getAllDebugNodes(): DebugNode[] {
return Array.from(_nativeNodeToDebugNode.values());
}

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {APP_INITIALIZER, ApplicationRef, DebugNode, NgProbeToken, NgZone, Optional, Provider, getDebugNode} from '@angular/core';
import {APP_INITIALIZER, ApplicationRef, DebugNode, NgProbeToken, NgZone, Optional, Provider, ɵgetDebugNodeR2} from '@angular/core';
import {exportNgVar} from '../util';
@ -23,14 +23,14 @@ const CORE_TOKENS_GLOBAL_NAME = 'coreTokens';
* null if the given native element does not have an Angular view associated
* with it.
*/
export function inspectNativeElement(element: any): DebugNode|null {
return getDebugNode(element);
export function inspectNativeElementR2(element: any): DebugNode|null {
return ɵgetDebugNodeR2(element);
}
export function _createNgProbe(coreTokens: NgProbeToken[]): any {
exportNgVar(INSPECT_GLOBAL_NAME, inspectNativeElement);
export function _createNgProbeR2(coreTokens: NgProbeToken[]): any {
exportNgVar(INSPECT_GLOBAL_NAME, inspectNativeElementR2);
exportNgVar(CORE_TOKENS_GLOBAL_NAME, {...CORE_TOKENS, ..._ngProbeTokensToMap(coreTokens || [])});
return () => inspectNativeElement;
return () => inspectNativeElementR2;
}
function _ngProbeTokensToMap(tokens: NgProbeToken[]): {[name: string]: any} {
@ -52,7 +52,7 @@ export const ELEMENT_PROBE_PROVIDERS__POST_R3__ = [];
export const ELEMENT_PROBE_PROVIDERS__PRE_R3__: Provider[] = [
{
provide: APP_INITIALIZER,
useFactory: _createNgProbe,
useFactory: _createNgProbeR2,
deps: [
[NgProbeToken, new Optional()],
],