Skip to content

Commit 2016afd

Browse files
mheverybenlesh
authored andcommitted
fix(ivy): remove ivy dependency on ViewEngine's resolveRendererType2 (angular#25396)
PR Close angular#25396
1 parent c8c1aa7 commit 2016afd

File tree

10 files changed

+102
-69
lines changed

10 files changed

+102
-69
lines changed

packages/core/src/render3/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function renderComponent<T>(
106106
const rootContext = createRootContext(opts.scheduler || requestAnimationFrame.bind(window));
107107

108108
const rootView: LViewData = createLViewData(
109-
rendererFactory.createRenderer(hostNode, componentDef.rendererType),
109+
rendererFactory.createRenderer(hostNode, componentDef),
110110
createTView(-1, null, null, null, null), rootContext,
111111
componentDef.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways);
112112
rootView[INJECTOR] = opts.injector || null;

packages/core/src/render3/component_ref.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
8787
const rendererFactory =
8888
ngModule ? ngModule.injector.get(RendererFactory2) : domRendererFactory3;
8989
const hostNode = isInternalRootView ?
90-
elementCreate(
91-
this.selector, rendererFactory.createRenderer(null, this.componentDef.rendererType)) :
90+
elementCreate(this.selector, rendererFactory.createRenderer(null, this.componentDef)) :
9291
locateHostElement(rendererFactory, rootSelectorOrNode);
9392

9493
// The first index of the first selector is the tag name.
@@ -100,7 +99,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
10099

101100
// Create the root view. Uses empty TView and ContentTemplate.
102101
const rootView: LViewData = createLViewData(
103-
rendererFactory.createRenderer(hostNode, this.componentDef.rendererType),
102+
rendererFactory.createRenderer(hostNode, this.componentDef),
104103
createTView(-1, null, null, null, null), rootContext,
105104
this.componentDef.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways);
106105
rootView[INJECTOR] = ngModule && ngModule.injector || null;

packages/core/src/render3/definition.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import './ng_dev_mode';
10+
911
import {ChangeDetectionStrategy} from '../change_detection/constants';
10-
import {Provider} from '../core';
12+
import {Provider, ViewEncapsulation} from '../core';
1113
import {NgModuleDef, NgModuleDefInternal} from '../metadata/ng_module';
12-
import {RendererType2} from '../render/api';
1314
import {Type} from '../type';
14-
import {resolveRendererType2} from '../view/util';
1515

1616
import {BaseDef, ComponentDefFeature, ComponentDefInternal, ComponentQuery, ComponentTemplate, ComponentType, DirectiveDefFeature, DirectiveDefInternal, DirectiveType, DirectiveTypesOrFactory, PipeDefInternal, PipeType, PipeTypesOrFactory} from './interfaces/definition';
1717
import {CssSelectorList, SelectorFlags} from './interfaces/projection';
1818

1919

20+
const EMPTY: {} = {};
21+
const EMPTY_ARRAY: any[] = [];
22+
ngDevMode && Object.freeze(EMPTY);
23+
ngDevMode && Object.freeze(EMPTY_ARRAY);
24+
let _renderCompCount = 0;
2025

2126
/**
2227
* Create a component definition object.
@@ -180,8 +185,28 @@ export function defineComponent<T>(componentDefinition: {
180185
*/
181186
features?: ComponentDefFeature[];
182187

183-
rendererType?: RendererType2;
188+
/**
189+
* Defines template and style encapsulation options available for Component's {@link Component}.
190+
*/
191+
encapsulation?: ViewEncapsulation;
192+
193+
/**
194+
* Defines arbitrary developer-defined data to be stored on a renderer instance.
195+
* This is useful for renderers that delegate to other renderers.
196+
*
197+
* see: animation
198+
*/
199+
data?: {[kind: string]: any};
184200

201+
/**
202+
* A set of styles that the component needs to be present for component to render correctly.
203+
*/
204+
styles?: string[];
205+
206+
/**
207+
* The strategy that the default change detector uses to detect changes.
208+
* When set, takes effect the next time change detection is triggered.
209+
*/
185210
changeDetection?: ChangeDetectionStrategy;
186211

187212
/**
@@ -215,6 +240,7 @@ export function defineComponent<T>(componentDefinition: {
215240
const pipeTypes = componentDefinition.pipes !;
216241
const directiveTypes = componentDefinition.directives !;
217242
const declaredInputs: {[key: string]: string} = {} as any;
243+
const encapsulation = componentDefinition.encapsulation;
218244
const def: ComponentDefInternal<any> = {
219245
type: type,
220246
diPublic: null,
@@ -227,7 +253,6 @@ export function defineComponent<T>(componentDefinition: {
227253
inputs: invertObject(componentDefinition.inputs, declaredInputs),
228254
declaredInputs: declaredInputs,
229255
outputs: invertObject(componentDefinition.outputs),
230-
rendererType: resolveRendererType2(componentDefinition.rendererType) || null,
231256
exportAs: componentDefinition.exportAs || null,
232257
onInit: type.prototype.ngOnInit || null,
233258
doCheck: type.prototype.ngDoCheck || null,
@@ -247,6 +272,12 @@ export function defineComponent<T>(componentDefinition: {
247272
selectors: componentDefinition.selectors,
248273
viewQuery: componentDefinition.viewQuery || null,
249274
features: componentDefinition.features || null,
275+
data: componentDefinition.data || EMPTY,
276+
// TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in the
277+
// next line. Also `None` should be 0 not 2.
278+
encapsulation: encapsulation == null ? 2 /* ViewEncapsulation.None */ : encapsulation,
279+
id: `c${_renderCompCount++}`,
280+
styles: EMPTY_ARRAY,
250281
};
251282
const feature = componentDefinition.features;
252283
feature && feature.forEach((fn) => fn(def));
@@ -273,17 +304,15 @@ export function extractPipeDef(type: PipeType<any>): PipeDefInternal<any> {
273304
export function defineNgModule<T>(def: {type: T} & Partial<NgModuleDef<T, any, any, any>>): never {
274305
const res: NgModuleDefInternal<T> = {
275306
type: def.type,
276-
bootstrap: def.bootstrap || [],
277-
declarations: def.declarations || [],
278-
imports: def.imports || [],
279-
exports: def.exports || [],
307+
bootstrap: def.bootstrap || EMPTY_ARRAY,
308+
declarations: def.declarations || EMPTY_ARRAY,
309+
imports: def.imports || EMPTY_ARRAY,
310+
exports: def.exports || EMPTY_ARRAY,
280311
transitiveCompileScopes: null,
281312
};
282313
return res as never;
283314
}
284315

285-
const EMPTY = {};
286-
287316
/**
288317
* Inverts an inputs or outputs lookup such that the keys, which were the
289318
* minified keys, are part of the values, and the values are parsed so that

packages/core/src/render3/instructions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {AttributeMarker, InitialInputData, InitialInputs, LContainerNode, LEleme
2222
import {CssSelectorList, NG_PROJECT_AS_ATTR_NAME} from './interfaces/projection';
2323
import {LQueries} from './interfaces/query';
2424
import {ProceduralRenderer3, RComment, RElement, RText, Renderer3, RendererFactory3, RendererStyleFlags3, isProceduralRenderer} from './interfaces/renderer';
25-
import {BINDING_INDEX, CLEANUP, CONTAINER_INDEX, CONTENT_QUERIES, CONTEXT, CurrentMatchesList, DECLARATION_VIEW, DIRECTIVES, FLAGS, HEADER_OFFSET, HOST_NODE, INJECTOR, LViewData, LViewFlags, NEXT, OpaqueViewState, PARENT, QUERIES, RENDERER, RootContext, SANITIZER, TAIL, TData, TVIEW, TView} from './interfaces/view';
25+
import {BINDING_INDEX, CLEANUP, CONTENT_QUERIES, CONTEXT, CurrentMatchesList, DECLARATION_VIEW, DIRECTIVES, FLAGS, HEADER_OFFSET, HOST_NODE, INJECTOR, LViewData, LViewFlags, NEXT, OpaqueViewState, PARENT, QUERIES, RENDERER, RootContext, SANITIZER, TAIL, TData, TVIEW, TView} from './interfaces/view';
2626
import {assertNodeOfPossibleTypes, assertNodeType} from './node_assert';
2727
import {appendChild, appendProjectedNode, canInsertNativeNode, createTextNode, findComponentHost, getChildLNode, getLViewChild, getNextLNode, getParentLNode, insertView, removeView} from './node_manipulation';
2828
import {isNodeMatchingSelectorList, matchingSelectorIndex} from './node_selector_matcher';
@@ -1668,9 +1668,8 @@ function addComponentLogic<T>(
16681668
const componentView = addToViewTree(
16691669
viewData, previousOrParentNode.tNode.index as number,
16701670
createLViewData(
1671-
rendererFactory.createRenderer(previousOrParentNode.native as RElement, def.rendererType),
1672-
tView, instance, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways,
1673-
getCurrentSanitizer()));
1671+
rendererFactory.createRenderer(previousOrParentNode.native as RElement, def), tView,
1672+
instance, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways, getCurrentSanitizer()));
16741673

16751674
// We need to set the host node/data here because when the component LNode was created,
16761675
// we didn't yet know it was a component (just an element).

packages/core/src/render3/interfaces/definition.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Provider} from '../../core';
10-
import {RendererType2} from '../../render/api';
9+
import {Provider, ViewEncapsulation} from '../../core';
1110
import {Type} from '../../type';
1211
import {CssSelectorList} from './projection';
1312

@@ -187,20 +186,42 @@ export type ComponentDefInternal<T> = ComponentDef<T, string>;
187186
* See: {@link defineComponent}
188187
*/
189188
export interface ComponentDef<T, Selector extends string> extends DirectiveDef<T, Selector> {
189+
/**
190+
* Runtime unique component ID.
191+
*/
192+
id: string;
193+
190194
/**
191195
* The View template of the component.
192196
*/
193197
readonly template: ComponentTemplate<T>;
194198

199+
/**
200+
* A set of styles that the component needs to be present for component to render correctly.
201+
*/
202+
readonly styles: string[];
203+
195204
/**
196205
* Query-related instructions for a component.
197206
*/
198207
readonly viewQuery: ComponentQuery<T>|null;
199208

200209
/**
201-
* Renderer type data of the component.
210+
* The view encapsulation type, which determines how styles are applied to
211+
* DOM elements. One of
212+
* - `Emulated` (default): Emulate native scoping of styles.
213+
* - `Native`: Use the native encapsulation mechanism of the renderer.
214+
* - `ShadowDom`: Use modern [ShadowDOM](https://w3c.github.io/webcomponents/spec/shadow/) and
215+
* create a ShadowRoot for component's host element.
216+
* - `None`: Do not provide any template or style encapsulation.
217+
*/
218+
readonly encapsulation: ViewEncapsulation;
219+
220+
/**
221+
* Defines arbitrary developer-defined data to be stored on a renderer instance.
222+
* This is useful for renderers that delegate to other renderers.
202223
*/
203-
readonly rendererType: RendererType2|null;
224+
readonly data: {[kind: string]: any};
204225

205226
/** Whether or not this component's ChangeDetectionStrategy is OnPush */
206227
readonly onPush: boolean;

packages/core/src/render3/ng_dev_mode.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const __global: {ngDevMode: NgDevModePerfCounters | boolean} =
4242
typeof global != 'undefined' && global;
4343

4444
export function ngDevModeResetPerfCounters(): NgDevModePerfCounters {
45-
return __global.ngDevMode = {
45+
// Make sure to refer to ngDevMode as ['ngDevMode'] for clousre.
46+
return __global['ngDevMode'] = {
4647
firstTemplatePass: 0,
4748
tNode: 0,
4849
tView: 0,
@@ -75,5 +76,6 @@ export function ngDevModeResetPerfCounters(): NgDevModePerfCounters {
7576
* as much early warning and errors as possible.
7677
*/
7778
if (typeof ngDevMode === 'undefined' || ngDevMode) {
78-
__global.ngDevMode = ngDevModeResetPerfCounters();
79+
// Make sure to refer to ngDevMode as ['ngDevMode'] for clousre.
80+
__global['ngDevMode'] = ngDevModeResetPerfCounters();
7981
}

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"name": "EMPTY$1"
2828
},
2929
{
30-
"name": "EMPTY_RENDERER_TYPE_ID"
30+
"name": "EMPTY_ARRAY$2"
3131
},
3232
{
3333
"name": "FLAGS"
@@ -77,15 +77,9 @@
7777
{
7878
"name": "TVIEW"
7979
},
80-
{
81-
"name": "UNDEFINED_RENDERER_TYPE_ID"
82-
},
8380
{
8481
"name": "VIEWS"
8582
},
86-
{
87-
"name": "ViewEncapsulation$1"
88-
},
8983
{
9084
"name": "_CLEAN_PROMISE"
9185
},
@@ -269,9 +263,6 @@
269263
{
270264
"name": "resetApplicationState"
271265
},
272-
{
273-
"name": "resolveRendererType2"
274-
},
275266
{
276267
"name": "setHostBindings"
277268
},

packages/core/test/bundling/todo/bundle.golden_symbols.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"name": "EMPTY$1"
4646
},
4747
{
48-
"name": "EMPTY_RENDERER_TYPE_ID"
48+
"name": "EMPTY_ARRAY$2"
4949
},
5050
{
5151
"name": "ElementRef"
@@ -182,9 +182,6 @@
182182
{
183183
"name": "TodoStore"
184184
},
185-
{
186-
"name": "UNDEFINED_RENDERER_TYPE_ID"
187-
},
188185
{
189186
"name": "VIEWS"
190187
},
@@ -194,9 +191,6 @@
194191
{
195192
"name": "ViewContainerRef$1"
196193
},
197-
{
198-
"name": "ViewEncapsulation$1"
199-
},
200194
{
201195
"name": "ViewRef"
202196
},
@@ -836,9 +830,6 @@
836830
{
837831
"name": "resolveDirective"
838832
},
839-
{
840-
"name": "resolveRendererType2"
841-
},
842833
{
843834
"name": "restoreView"
844835
},

packages/core/test/render3/component_spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ describe('encapsulation', () => {
204204
}
205205
},
206206
factory: () => new EncapsulatedComponent,
207-
rendererType:
208-
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
207+
encapsulation: ViewEncapsulation.Emulated,
208+
styles: [],
209+
data: {},
209210
directives: () => [LeafComponent]
210211
});
211212
}
@@ -250,8 +251,9 @@ describe('encapsulation', () => {
250251
}
251252
},
252253
factory: () => new WrapperComponentWith,
253-
rendererType:
254-
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
254+
encapsulation: ViewEncapsulation.Emulated,
255+
styles: [],
256+
data: {},
255257
directives: () => [LeafComponentwith]
256258
});
257259
}
@@ -268,8 +270,9 @@ describe('encapsulation', () => {
268270
}
269271
},
270272
factory: () => new LeafComponentwith,
271-
rendererType:
272-
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
273+
encapsulation: ViewEncapsulation.Emulated,
274+
styles: [],
275+
data: {},
273276
});
274277
}
275278

packages/core/test/render3/renderer_factory_spec.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,22 @@ describe('animation renderer factory', () => {
158158
}
159159
},
160160
factory: () => new SomeComponentWithAnimation,
161-
rendererType: createRendererType2({
162-
encapsulation: ViewEncapsulation.None,
163-
styles: [],
164-
data: {
165-
animation: [{
166-
type: 7,
167-
name: 'myAnimation',
168-
definitions: [{
169-
type: 1,
170-
expr: '* => on',
171-
animation:
172-
[{type: 4, styles: {type: 6, styles: {opacity: 1}, offset: null}, timings: 10}],
173-
options: null
174-
}],
175-
options: {}
176-
}]
177-
}
178-
}),
161+
encapsulation: ViewEncapsulation.None,
162+
styles: [],
163+
data: {
164+
animation: [{
165+
type: 7,
166+
name: 'myAnimation',
167+
definitions: [{
168+
type: 1,
169+
expr: '* => on',
170+
animation:
171+
[{type: 4, styles: {type: 6, styles: {opacity: 1}, offset: null}, timings: 10}],
172+
options: null
173+
}],
174+
options: {}
175+
}]
176+
},
179177
});
180178
}
181179

0 commit comments

Comments
 (0)