perf(core): use `ngDevMode` to tree-shake error messages (#38612)

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
This commit is contained in:
Sonu Kapoor 2020-08-27 08:40:19 -04:00 committed by atscott
parent 7869de6136
commit 1150649139
7 changed files with 14 additions and 27 deletions

View File

@ -21,7 +21,7 @@
"master": { "master": {
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 147573, "main-es2015": 146989,
"polyfills-es2015": 36571 "polyfills-es2015": 36571
} }
} }

View File

@ -384,7 +384,7 @@ export class R3Injector {
let multiRecord = this.records.get(token); let multiRecord = this.records.get(token);
if (multiRecord) { if (multiRecord) {
// It has. Throw a nice error if // It has. Throw a nice error if
if (multiRecord.multi === undefined) { if (ngDevMode && multiRecord.multi === undefined) {
throwMixedMultiProviderError(); throwMixedMultiProviderError();
} }
} else { } else {
@ -396,7 +396,7 @@ export class R3Injector {
multiRecord.multi!.push(provider); multiRecord.multi!.push(provider);
} else { } else {
const existing = this.records.get(token); const existing = this.records.get(token);
if (existing && existing.multi !== undefined) { if (ngDevMode && existing && existing.multi !== undefined) {
throwMixedMultiProviderError(); throwMixedMultiProviderError();
} }
} }
@ -404,7 +404,7 @@ export class R3Injector {
} }
private hydrate<T>(token: Type<T>|InjectionToken<T>, record: Record<T>): T { private hydrate<T>(token: Type<T>|InjectionToken<T>, record: Record<T>): T {
if (record.value === CIRCULAR) { if (ngDevMode && record.value === CIRCULAR) {
throwCyclicDependencyError(stringify(token)); throwCyclicDependencyError(stringify(token));
} else if (record.value === NOT_YET) { } else if (record.value === NOT_YET) {
record.value = CIRCULAR; record.value = CIRCULAR;
@ -511,7 +511,7 @@ export function providerToFactory(
const classRef = resolveForwardRef( const classRef = resolveForwardRef(
provider && provider &&
((provider as StaticClassProvider | ClassProvider).useClass || provider.provide)); ((provider as StaticClassProvider | ClassProvider).useClass || provider.provide));
if (!classRef) { if (ngDevMode && !classRef) {
throwInvalidProviderError(ngModuleType, providers, provider); throwInvalidProviderError(ngModuleType, providers, provider);
} }
if (hasDeps(provider)) { if (hasDeps(provider)) {

View File

@ -1375,6 +1375,7 @@ function findDirectiveDefMatches(
ngDevMode && ngDevMode &&
assertNodeOfPossibleTypes( assertNodeOfPossibleTypes(
tNode, [TNodeType.Element, TNodeType.ElementContainer, TNodeType.Container]); tNode, [TNodeType.Element, TNodeType.ElementContainer, TNodeType.Container]);
const registry = tView.directiveRegistry; const registry = tView.directiveRegistry;
let matches: any[]|null = null; let matches: any[]|null = null;
if (registry) { if (registry) {
@ -1385,13 +1386,14 @@ function findDirectiveDefMatches(
diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, viewData), tView, def.type); diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, viewData), tView, def.type);
if (isComponentDef(def)) { if (isComponentDef(def)) {
ngDevMode && if (ngDevMode) {
assertNodeOfPossibleTypes( assertNodeOfPossibleTypes(
tNode, [TNodeType.Element], tNode, [TNodeType.Element],
`"${tNode.tagName}" tags cannot be used as component hosts. ` + `"${tNode.tagName}" tags cannot be used as component hosts. ` +
`Please use a different tag to activate the ${ `Please use a different tag to activate the ${stringify(def.type)} component.`);
stringify(def.type)} component.`);
if (tNode.flags & TNodeFlags.isComponentHost) throwMultipleComponentError(tNode); if (tNode.flags & TNodeFlags.isComponentHost) throwMultipleComponentError(tNode);
}
markAsComponentHost(tView, tNode); markAsComponentHost(tView, tNode);
// The component is always stored first with directives after. // The component is always stored first with directives after.
matches.unshift(def); matches.unshift(def);

View File

@ -350,9 +350,6 @@
{ {
"name": "setUpAttributes" "name": "setUpAttributes"
}, },
{
"name": "throwMultipleComponentError"
},
{ {
"name": "unwrapRNode" "name": "unwrapRNode"
}, },

View File

@ -1586,12 +1586,6 @@
{ {
"name": "syncPendingControls" "name": "syncPendingControls"
}, },
{
"name": "throwMixedMultiProviderError"
},
{
"name": "throwMultipleComponentError"
},
{ {
"name": "toObservable" "name": "toObservable"
}, },

View File

@ -128,9 +128,6 @@
{ {
"name": "stringify" "name": "stringify"
}, },
{
"name": "throwMixedMultiProviderError"
},
{ {
"name": "ɵɵdefineInjectable" "name": "ɵɵdefineInjectable"
}, },

View File

@ -713,9 +713,6 @@
{ {
"name": "stringifyForError" "name": "stringifyForError"
}, },
{
"name": "throwMultipleComponentError"
},
{ {
"name": "toTStylingRange" "name": "toTStylingRange"
}, },