Skip to content

Commit

Permalink
perf: use ngDevMode to tree-shake error messages
Browse files Browse the repository at this point in the history
This commit adds a guard before throwing any errors from the `r3_injector`.
This will tree-shake error messages which cannot be minified.
  • Loading branch information
sonukapoor committed Aug 28, 2020
1 parent 618cb32 commit a39e786
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
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
22 changes: 12 additions & 10 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,16 +1386,17 @@ 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);
markAsComponentHost(tView, tNode);
// The component is always stored first with directives after.
matches.unshift(def);
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);
}
} else {
matches.push(def);
}
Expand Down

0 comments on commit a39e786

Please sign in to comment.