Skip to content

Commit a3cb7b9

Browse files
crisbetommalerba
authored andcommitted
refactor(core): allow InputBinding and OutputBinding to be tree shaken (#60137)
Reworks the `InputBinding` and `OutputBinding` functionality to be in object literals constructed in functions, rather than classes, because it seems like Terser was having a hard time tree shaking the classes when the functions weren't used. PR Close #60137
1 parent be44cc8 commit a3cb7b9

6 files changed

Lines changed: 82 additions & 89 deletions

File tree

‎goldens/public-api/core/index.api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ export const Input: InputDecorator;
997997
export const input: InputFunction;
998998

999999
// @public
1000-
export function inputBinding<T>(publicName: string, value: () => unknown): Binding;
1000+
export function inputBinding(publicName: string, value: () => unknown): Binding;
10011001

10021002
// @public (undocumented)
10031003
export interface InputDecorator {

‎goldens/size-tracking/integration-payloads.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"cli-hello-world": {
33
"uncompressed": {
4-
"main": 137893,
4+
"main": 132425,
55
"polyfills": 33792
66
}
77
},

‎packages/core/src/render3/dynamic_bindings.ts‎

Lines changed: 78 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {setDirectiveInput, storePropertyBindingMetadata} from './instructions/sh
1414
import {DirectiveDef} from './interfaces/definition';
1515
import {CONTEXT} from './interfaces/view';
1616
import {getCurrentTNode, getLView, getSelectedTNode, getTView, nextBindingIndex} from './state';
17+
import {stringifyForError} from './util/stringify_utils';
1718

1819
/** Symbol used to store and retrieve metadata about a binding. */
1920
export const BINDING = /* @__PURE__ */ Symbol('BINDING');
@@ -53,85 +54,6 @@ export interface DirectiveWithBindings<T> {
5354
const INPUT_BINDING_METADATA: Binding[typeof BINDING] = {kind: 'input', requiredVars: 1};
5455
const OUTPUT_BINDING_METADATA: Binding[typeof BINDING] = {kind: 'output', requiredVars: 0};
5556

56-
class InputBinding<T> implements Binding {
57-
readonly target!: DirectiveDef<T>;
58-
readonly [BINDING] = INPUT_BINDING_METADATA;
59-
60-
constructor(
61-
private readonly publicName: string,
62-
private readonly value: () => unknown,
63-
) {}
64-
65-
update(): void {
66-
const lView = getLView();
67-
const bindingIndex = nextBindingIndex();
68-
const value = this.value();
69-
if (bindingUpdated(lView, bindingIndex, value)) {
70-
const tView = getTView();
71-
const tNode = getSelectedTNode();
72-
73-
if (!this.target && ngDevMode) {
74-
throw new RuntimeError(
75-
RuntimeErrorCode.NO_BINDING_TARGET,
76-
`Input binding to property "${this.publicName}" does not have a target.`,
77-
);
78-
}
79-
80-
const hasSet = setDirectiveInput(tNode, tView, lView, this.target, this.publicName, value);
81-
82-
if (ngDevMode) {
83-
if (!hasSet) {
84-
throw new RuntimeError(
85-
RuntimeErrorCode.NO_BINDING_TARGET,
86-
`${this.target.type.name} does not have an input with a public name of "${this.publicName}".`,
87-
);
88-
}
89-
storePropertyBindingMetadata(tView.data, tNode, this.publicName, bindingIndex);
90-
}
91-
}
92-
}
93-
}
94-
95-
class OutputBinding<T> implements Binding {
96-
readonly target!: DirectiveDef<unknown>;
97-
readonly [BINDING] = OUTPUT_BINDING_METADATA;
98-
99-
constructor(
100-
private readonly eventName: string,
101-
private readonly listener: (event: T) => unknown,
102-
) {}
103-
104-
create(): void {
105-
if (!this.target && ngDevMode) {
106-
throw new RuntimeError(
107-
RuntimeErrorCode.NO_BINDING_TARGET,
108-
`Output binding to "${this.eventName}" does not have a target.`,
109-
);
110-
}
111-
112-
const lView = getLView<{} | null>();
113-
const tView = getTView();
114-
const tNode = getCurrentTNode()!;
115-
const context = lView[CONTEXT];
116-
const wrappedListener = wrapListener(tNode, lView, context, this.listener);
117-
const hasBound = listenToDirectiveOutput(
118-
tNode,
119-
tView,
120-
lView,
121-
this.target,
122-
this.eventName,
123-
wrappedListener,
124-
);
125-
126-
if (!hasBound && ngDevMode) {
127-
throw new RuntimeError(
128-
RuntimeErrorCode.INVALID_BINDING_TARGET,
129-
`${this.target.type.name} does not have an output with a public name of "${this.eventName}".`,
130-
);
131-
}
132-
}
133-
}
134-
13557
/**
13658
* Creates an input binding.
13759
* @param publicName Public name of the input to bind to.
@@ -150,8 +72,44 @@ class OutputBinding<T> implements Binding {
15072
* });
15173
* ```
15274
*/
153-
export function inputBinding<T>(publicName: string, value: () => unknown): Binding {
154-
return new InputBinding<T>(publicName, value);
75+
export function inputBinding(publicName: string, value: () => unknown): Binding {
76+
// Note: ideally we would use a class here, but it seems like they
77+
// don't get tree shaken when constructed by a function like this.
78+
const binding: Binding = {
79+
[BINDING]: INPUT_BINDING_METADATA,
80+
target: null,
81+
update: () => {
82+
const target = binding.target as DirectiveDef<unknown>;
83+
const lView = getLView();
84+
const bindingIndex = nextBindingIndex();
85+
const resolvedValue = value();
86+
if (bindingUpdated(lView, bindingIndex, resolvedValue)) {
87+
const tView = getTView();
88+
const tNode = getSelectedTNode();
89+
90+
if (!target && ngDevMode) {
91+
throw new RuntimeError(
92+
RuntimeErrorCode.NO_BINDING_TARGET,
93+
`Input binding to property "${publicName}" does not have a target.`,
94+
);
95+
}
96+
97+
const hasSet = setDirectiveInput(tNode, tView, lView, target, publicName, resolvedValue);
98+
99+
if (ngDevMode) {
100+
if (!hasSet) {
101+
throw new RuntimeError(
102+
RuntimeErrorCode.NO_BINDING_TARGET,
103+
`${stringifyForError(target.type)} does not have an input with a public name of "${publicName}".`,
104+
);
105+
}
106+
storePropertyBindingMetadata(tView.data, tNode, publicName, bindingIndex);
107+
}
108+
}
109+
},
110+
};
111+
112+
return binding;
155113
}
156114

157115
/**
@@ -176,5 +134,43 @@ export function inputBinding<T>(publicName: string, value: () => unknown): Bindi
176134
* ```
177135
*/
178136
export function outputBinding<T>(eventName: string, listener: (event: T) => unknown): Binding {
179-
return new OutputBinding<T>(eventName, listener);
137+
// Note: ideally we would use a class here, but it seems like they
138+
// don't get tree shaken when constructed by a function like this.
139+
const binding: Binding = {
140+
[BINDING]: OUTPUT_BINDING_METADATA,
141+
target: null,
142+
create: () => {
143+
const target = binding.target as DirectiveDef<unknown>;
144+
145+
if (!target && ngDevMode) {
146+
throw new RuntimeError(
147+
RuntimeErrorCode.NO_BINDING_TARGET,
148+
`Output binding to "${eventName}" does not have a target.`,
149+
);
150+
}
151+
152+
const lView = getLView<{} | null>();
153+
const tView = getTView();
154+
const tNode = getCurrentTNode()!;
155+
const context = lView[CONTEXT];
156+
const wrappedListener = wrapListener(tNode, lView, context, listener);
157+
const hasBound = listenToDirectiveOutput(
158+
tNode,
159+
tView,
160+
lView,
161+
target,
162+
eventName,
163+
wrappedListener,
164+
);
165+
166+
if (!hasBound && ngDevMode) {
167+
throw new RuntimeError(
168+
RuntimeErrorCode.INVALID_BINDING_TARGET,
169+
`${stringifyForError(target.type)} does not have an output with a public name of "${eventName}".`,
170+
);
171+
}
172+
},
173+
};
174+
175+
return binding;
180176
}

‎packages/core/test/bundling/defer/bundle.golden_symbols.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,6 @@
896896
"wasLastNodeCreated",
897897
"writeToDirectiveInput",
898898
"ɵɵdefer",
899-
"ɵɵdeferWhen",
900899
"ɵɵdefineComponent",
901900
"ɵɵdefineInjectable",
902901
"ɵɵdirectiveInject",

‎packages/core/test/bundling/hydration/bundle.golden_symbols.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,5 @@
458458
"ɵɵdefineComponent",
459459
"ɵɵdefineInjectable",
460460
"ɵɵdirectiveInject",
461-
"ɵɵinject",
462-
"ɵɵtext"
461+
"ɵɵinject"
463462
]

‎packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,5 @@
381381
"ɵɵdefineComponent",
382382
"ɵɵdefineInjectable",
383383
"ɵɵdirectiveInject",
384-
"ɵɵinject",
385-
"ɵɵtext"
384+
"ɵɵinject"
386385
]

0 commit comments

Comments
 (0)