Skip to content

Commit 669df70

Browse files
gkalpakAndrewKushnir
authored andcommitted
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
1 parent c8eb164 commit 669df70

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

aio/scripts/_payload-limits.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"master": {
1313
"uncompressed": {
1414
"runtime-es2015": 2987,
15-
"main-es2015": 456604,
15+
"main-es2015": 448956,
1616
"polyfills-es2015": 52487
1717
}
1818
}
@@ -21,7 +21,7 @@
2121
"master": {
2222
"uncompressed": {
2323
"runtime-es2015": 3097,
24-
"main-es2015": 426978,
24+
"main-es2015": 427017,
2525
"polyfills-es2015": 52487
2626
}
2727
}

integration/_payload-limits.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"master": {
4040
"uncompressed": {
4141
"runtime-es2015": 2289,
42-
"main-es2015": 254657,
42+
"main-es2015": 247225,
4343
"polyfills-es2015": 36808,
4444
"5-es2015": 751
4545
}

packages/core/src/core_private_export.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {defaultIterableDiffers as ɵdefaultIterableDiffers, defaultKeyValueDiffe
1212
export {devModeEqual as ɵdevModeEqual, isListLikeIterable as ɵisListLikeIterable} from './change_detection/change_detection_util';
1313
export {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants';
1414
export {Console as ɵConsole} from './console';
15+
export {getDebugNodeR2 as ɵgetDebugNodeR2} from './debug/debug_node';
1516
export {inject, setCurrentInjector as ɵsetCurrentInjector, ɵɵinject} from './di/injector_compatibility';
1617
export {getInjectableDef as ɵgetInjectableDef, ɵɵInjectableDef, ɵɵInjectorDef} from './di/interface/defs';
1718
export {INJECTOR_SCOPE as ɵINJECTOR_SCOPE} from './di/scope';

packages/core/src/debug/debug_node.ts

+12
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,18 @@ export function getDebugNode__POST_R3__(nativeNode: any): DebugNode|null {
727727
*/
728728
export const getDebugNode: (nativeNode: any) => DebugNode | null = getDebugNode__PRE_R3__;
729729

730+
731+
export function getDebugNodeR2__PRE_R3__(nativeNode: any): DebugNode|null {
732+
return getDebugNode__PRE_R3__(nativeNode);
733+
}
734+
735+
export function getDebugNodeR2__POST_R3__(_nativeNode: any): DebugNode|null {
736+
return null;
737+
}
738+
739+
export const getDebugNodeR2: (nativeNode: any) => DebugNode | null = getDebugNodeR2__PRE_R3__;
740+
741+
730742
export function getAllDebugNodes(): DebugNode[] {
731743
return Array.from(_nativeNodeToDebugNode.values());
732744
}

packages/platform-browser/src/dom/debug/ng_probe.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {APP_INITIALIZER, ApplicationRef, DebugNode, NgProbeToken, NgZone, Optional, Provider, getDebugNode} from '@angular/core';
9+
import {APP_INITIALIZER, ApplicationRef, DebugNode, NgProbeToken, NgZone, Optional, Provider, ɵgetDebugNodeR2} from '@angular/core';
1010

1111
import {exportNgVar} from '../util';
1212

@@ -23,14 +23,14 @@ const CORE_TOKENS_GLOBAL_NAME = 'coreTokens';
2323
* null if the given native element does not have an Angular view associated
2424
* with it.
2525
*/
26-
export function inspectNativeElement(element: any): DebugNode|null {
27-
return getDebugNode(element);
26+
export function inspectNativeElementR2(element: any): DebugNode|null {
27+
return ɵgetDebugNodeR2(element);
2828
}
2929

30-
export function _createNgProbe(coreTokens: NgProbeToken[]): any {
31-
exportNgVar(INSPECT_GLOBAL_NAME, inspectNativeElement);
30+
export function _createNgProbeR2(coreTokens: NgProbeToken[]): any {
31+
exportNgVar(INSPECT_GLOBAL_NAME, inspectNativeElementR2);
3232
exportNgVar(CORE_TOKENS_GLOBAL_NAME, {...CORE_TOKENS, ..._ngProbeTokensToMap(coreTokens || [])});
33-
return () => inspectNativeElement;
33+
return () => inspectNativeElementR2;
3434
}
3535

3636
function _ngProbeTokensToMap(tokens: NgProbeToken[]): {[name: string]: any} {
@@ -52,7 +52,7 @@ export const ELEMENT_PROBE_PROVIDERS__POST_R3__ = [];
5252
export const ELEMENT_PROBE_PROVIDERS__PRE_R3__: Provider[] = [
5353
{
5454
provide: APP_INITIALIZER,
55-
useFactory: _createNgProbe,
55+
useFactory: _createNgProbeR2,
5656
deps: [
5757
[NgProbeToken, new Optional()],
5858
],

0 commit comments

Comments
 (0)