Skip to content

Commit 4914fc3

Browse files
committed
refactor(transloco): tree-shake errors
1 parent 21dd562 commit 4914fc3

File tree

7 files changed

+77
-16
lines changed

7 files changed

+77
-16
lines changed

libs/transloco/src/lib/resolve-loader.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { TranslocoLoader, TranslocoLoaderData } from './transloco.loader';
22
import { InlineLoader } from './types';
33
import { isFunction } from './helpers';
4+
import {
5+
formatTranslocoError,
6+
TranslocoErrorCode,
7+
} from './transloco-error-code';
48

59
interface Options {
610
inlineLoader?: InlineLoader;
@@ -15,7 +19,15 @@ export function resolveLoader(options: Options) {
1519
if (inlineLoader) {
1620
const pathLoader = inlineLoader[path];
1721
if (isFunction(pathLoader) === false) {
18-
throw `You're using an inline loader but didn't provide a loader for ${path}`;
22+
let message: string;
23+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
24+
message = `You're using an inline loader but didn't provide a loader for ${path}`;
25+
} else {
26+
message = formatTranslocoError(
27+
TranslocoErrorCode.NoLoaderProvidedForPath,
28+
);
29+
}
30+
throw new Error(message);
1931
}
2032

2133
return inlineLoader[path]().then((res) =>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const enum TranslocoErrorCode {
2+
NoLoaderProvidedForPath = 1,
3+
FunctionalTranspilerInvalidSyntax,
4+
UnableToLoadTranslation,
5+
NoFallbackLanguageProvided,
6+
}
7+
8+
export function formatTranslocoError(code: TranslocoErrorCode) {
9+
return `TRNSL:${code}`;
10+
}

libs/transloco/src/lib/transloco-fallback-strategy.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Inject, Injectable, InjectionToken } from '@angular/core';
22

33
import { TRANSLOCO_CONFIG, TranslocoConfig } from './transloco.config';
4+
import {
5+
formatTranslocoError,
6+
TranslocoErrorCode,
7+
} from './transloco-error-code';
48

59
export const TRANSLOCO_FALLBACK_STRATEGY =
610
new InjectionToken<TranslocoFallbackStrategy>(
@@ -20,9 +24,16 @@ export class DefaultFallbackStrategy implements TranslocoFallbackStrategy {
2024
getNextLangs() {
2125
const fallbackLang = this.userConfig.fallbackLang;
2226
if (!fallbackLang) {
23-
throw new Error(
24-
'When using the default fallback, a fallback language must be provided in the config!',
25-
);
27+
let message: string;
28+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
29+
message =
30+
'When using the default fallback, a fallback language must be provided in the config!';
31+
} else {
32+
message = formatTranslocoError(
33+
TranslocoErrorCode.NoFallbackLanguageProvided,
34+
);
35+
}
36+
throw new Error(message);
2637
}
2738

2839
return Array.isArray(fallbackLang) ? fallbackLang : [fallbackLang];

libs/transloco/src/lib/transloco-missing-handler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ export interface TranslocoMissingHandler {
2121
@Injectable()
2222
export class DefaultMissingHandler implements TranslocoMissingHandler {
2323
handle(key: string, config: TranslocoConfig) {
24-
if (config.missingHandler.logMissingKey && !config.prodMode) {
25-
const msg = `Missing translation for '${key}'`;
26-
console.warn(`%c ${msg}`, 'font-size: 12px; color: red');
24+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
25+
if (config.missingHandler.logMissingKey) {
26+
const msg = `Missing translation for '${key}'`;
27+
console.warn(`%c ${msg}`, 'font-size: 12px; color: red');
28+
}
2729
}
2830

2931
return key;

libs/transloco/src/lib/transloco.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AvailableLangs } from './types';
55
export interface TranslocoConfig {
66
defaultLang: string;
77
reRenderOnLangChange: boolean;
8+
/** @deprecated */
89
prodMode: boolean;
910
fallbackLang?: string | string[];
1011
failedRetries: number;

libs/transloco/src/lib/transloco.service.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ import {
7171
} from './shared';
7272
import { getFallbacksLoaders } from './get-fallbacks-loaders';
7373
import { resolveLoader } from './resolve-loader';
74+
import {
75+
formatTranslocoError,
76+
TranslocoErrorCode,
77+
} from './transloco-error-code';
7478

7579
let service: TranslocoService;
7680

@@ -234,7 +238,7 @@ export class TranslocoService implements OnDestroy {
234238
this.handleSuccess(path, translation);
235239
}),
236240
catchError((error) => {
237-
if (!this.config.prodMode) {
241+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
238242
console.error(`Error while trying to load "${path}"`, error);
239243
}
240244

@@ -762,12 +766,19 @@ export class TranslocoService implements OnDestroy {
762766
const isFallbackLang = nextLang === splitted[splitted.length - 1];
763767

764768
if (!nextLang || isFallbackLang) {
765-
let msg = `Unable to load translation and all the fallback languages`;
766-
if (splitted.length > 1) {
767-
msg += `, did you misspelled the scope name?`;
769+
let message: string;
770+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
771+
message = `Unable to load translation and all the fallback languages`;
772+
if (splitted.length > 1) {
773+
message += `, did you misspelled the scope name?`;
774+
}
775+
} else {
776+
message = formatTranslocoError(
777+
TranslocoErrorCode.UnableToLoadTranslation,
778+
);
768779
}
769780

770-
throw new Error(msg);
781+
throw new Error(message);
771782
}
772783

773784
let resolveLang = nextLang;

libs/transloco/src/lib/transloco.transpiler.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
TRANSLOCO_CONFIG,
88
TranslocoConfig,
99
} from './transloco.config';
10+
import {
11+
formatTranslocoError,
12+
TranslocoErrorCode,
13+
} from './transloco-error-code';
1014

1115
export const TRANSLOCO_TRANSPILER = new InjectionToken<TranslocoTranspiler>(
1216
typeof ngDevMode !== 'undefined' && ngDevMode ? 'TRANSLOCO_TRANSPILER' : '',
@@ -164,6 +168,8 @@ export function getFunctionArgs(argsString: string): string[] {
164168
return args;
165169
}
166170

171+
const functionalCallRegExp = /\[\[\s*(\w+)\((.*?)\)\s*]]/g;
172+
167173
@Injectable()
168174
export class FunctionalTranspiler
169175
extends DefaultTranspiler
@@ -175,19 +181,27 @@ export class FunctionalTranspiler
175181
let transpiled = value;
176182
if (isString(value)) {
177183
transpiled = value.replace(
178-
/\[\[\s*(\w+)\((.*?)\)\s*]]/g,
184+
functionalCallRegExp,
179185
(match: string, functionName: string, args: string) => {
180186
try {
181187
const func: TranslocoTranspilerFunction =
182188
this.injector.get(functionName);
183189

184190
return func.transpile(...getFunctionArgs(args));
185191
} catch (e: unknown) {
186-
let message = `There is an error in: '${value}'.
192+
let message: string;
193+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
194+
message = `There is an error in: '${value}'.
187195
Check that the you used the right syntax in your translation and that the implementation of ${functionName} is correct.`;
188-
if ((e as Error).message.includes('NullInjectorError')) {
189-
message = `You are using the '${functionName}' function in your translation but no provider was found!`;
196+
if ((e as Error).message.includes('NullInjectorError')) {
197+
message = `You are using the '${functionName}' function in your translation but no provider was found!`;
198+
}
199+
} else {
200+
message = formatTranslocoError(
201+
TranslocoErrorCode.FunctionalTranspilerInvalidSyntax,
202+
);
190203
}
204+
191205
throw new Error(message);
192206
}
193207
},

0 commit comments

Comments
 (0)