Skip to content

Commit

Permalink
perf(core): use ngDevMode to tree-shake error messages (#38612)
Browse files Browse the repository at this point in the history
This commit adds `ngDevMode` guard to throw some errors only in dev mode
(similar to how things work in other parts of Ivy runtime code). The
`ngDevMode` flag helps to tree-shake these error messages from production
builds (in dev mode everything will work as it works right now) to decrease
production bundle size.

PR Close #38612
  • Loading branch information
sonukapoor authored and atscott committed Sep 8, 2020
1 parent 6a28675 commit b084bff
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 27 deletions.
2 changes: 1 addition & 1 deletion goldens/size-tracking/integration-payloads.json
Expand Up @@ -21,7 +21,7 @@
"master": {
"uncompressed": {
"runtime-es2015": 1485,
"main-es2015": 147573,
"main-es2015": 146989,
"polyfills-es2015": 36571
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/di/r3_injector.ts
Expand Up @@ -384,7 +384,7 @@ export class R3Injector {
let multiRecord = this.records.get(token);
if (multiRecord) {
// It has. Throw a nice error if
if (multiRecord.multi === undefined) {
if (ngDevMode && multiRecord.multi === undefined) {
throwMixedMultiProviderError();
}
} else {
Expand All @@ -396,15 +396,15 @@ export class R3Injector {
multiRecord.multi!.push(provider);
} else {
const existing = this.records.get(token);
if (existing && existing.multi !== undefined) {
if (ngDevMode && existing && existing.multi !== undefined) {
throwMixedMultiProviderError();
}
}
this.records.set(token, record);
}

private hydrate<T>(token: Type<T>|InjectionToken<T>, record: Record<T>): T {
if (record.value === CIRCULAR) {
if (ngDevMode && record.value === CIRCULAR) {
throwCyclicDependencyError(stringify(token));
} else if (record.value === NOT_YET) {
record.value = CIRCULAR;
Expand Down Expand Up @@ -511,7 +511,7 @@ export function providerToFactory(
const classRef = resolveForwardRef(
provider &&
((provider as StaticClassProvider | ClassProvider).useClass || provider.provide));
if (!classRef) {
if (ngDevMode && !classRef) {
throwInvalidProviderError(ngModuleType, providers, provider);
}
if (hasDeps(provider)) {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/render3/instructions/shared.ts
Expand Up @@ -1375,6 +1375,7 @@ function findDirectiveDefMatches(
ngDevMode &&
assertNodeOfPossibleTypes(
tNode, [TNodeType.Element, TNodeType.ElementContainer, TNodeType.Container]);

const registry = tView.directiveRegistry;
let matches: any[]|null = null;
if (registry) {
Expand All @@ -1385,13 +1386,14 @@ function findDirectiveDefMatches(
diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, viewData), tView, def.type);

if (isComponentDef(def)) {
ngDevMode &&
assertNodeOfPossibleTypes(
tNode, [TNodeType.Element],
`"${tNode.tagName}" tags cannot be used as component hosts. ` +
`Please use a different tag to activate the ${
stringify(def.type)} component.`);
if (tNode.flags & TNodeFlags.isComponentHost) throwMultipleComponentError(tNode);
if (ngDevMode) {
assertNodeOfPossibleTypes(
tNode, [TNodeType.Element],
`"${tNode.tagName}" tags cannot be used as component hosts. ` +
`Please use a different tag to activate the ${stringify(def.type)} component.`);

if (tNode.flags & TNodeFlags.isComponentHost) throwMultipleComponentError(tNode);
}
markAsComponentHost(tView, tNode);
// The component is always stored first with directives after.
matches.unshift(def);
Expand Down
Expand Up @@ -350,9 +350,6 @@
{
"name": "setUpAttributes"
},
{
"name": "throwMultipleComponentError"
},
{
"name": "unwrapRNode"
},
Expand Down
6 changes: 0 additions & 6 deletions packages/core/test/bundling/forms/bundle.golden_symbols.json
Expand Up @@ -1586,12 +1586,6 @@
{
"name": "syncPendingControls"
},
{
"name": "throwMixedMultiProviderError"
},
{
"name": "throwMultipleComponentError"
},
{
"name": "toObservable"
},
Expand Down
Expand Up @@ -128,9 +128,6 @@
{
"name": "stringify"
},
{
"name": "throwMixedMultiProviderError"
},
{
"name": "ɵɵdefineInjectable"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Expand Up @@ -713,9 +713,6 @@
{
"name": "stringifyForError"
},
{
"name": "throwMultipleComponentError"
},
{
"name": "toTStylingRange"
},
Expand Down

0 comments on commit b084bff

Please sign in to comment.