Skip to content

Commit 479d3a1

Browse files
authored
Merge pull request #92 from arturovt/perf/tree-shake-errors
perf: tree-shake errors and warnings in production mode
2 parents ca05ba5 + 3b70829 commit 479d3a1

File tree

7 files changed

+27
-14
lines changed

7 files changed

+27
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"lint": "ng lint",
99
"test": "ng lint && npm run format:test && ng test --watch false --code-coverage",
1010
"watch": "ng test",
11-
"build": "ng build && ng build --project elements",
11+
"build": "ng build && ng build elements",
1212
"postbuild": "replace \"onFetch.*\\{\" \"onFetch(event) { if (event.request.url.indexOf('unpkg.com') > -1) { return; }\" ./dist/elements-demo/ngsw-worker.js",
1313
"ci": "npm run test && npm run build && cpx README.md dist/elements",
1414
"format:write": "prettier projects/**/*.{ts,json,md} --write",

projects/elements/src/lib/lazy-elements/lazy-element-dynamic/lazy-element-dynamic.directive.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ export class LazyElementDynamicDirective implements OnInit, OnDestroy {
5050
) {}
5151

5252
ngOnInit() {
53-
if (!this.tag || this.tag.length === 0 || !this.tag.includes('-')) {
54-
throw new Error(
55-
`${LOG_PREFIX} - Valid tag has to be specified when using *axLazyElementDynamic directive (use *axLazyElementDynamic="'some-tag'"), got: "${this.tag}"`
56-
);
53+
if (ngDevMode) {
54+
if (!this.tag || this.tag.length === 0 || !this.tag.includes('-')) {
55+
throw new Error(
56+
`${LOG_PREFIX} - Valid tag has to be specified when using *axLazyElementDynamic directive (use *axLazyElementDynamic="'some-tag'"), got: "${this.tag}"`
57+
);
58+
}
5759
}
5860

5961
const elementConfig =
@@ -107,7 +109,7 @@ export class LazyElementDynamicDirective implements OnInit, OnDestroy {
107109
const factory = this.cfr.resolveComponentFactory(errorComponent);
108110
this.vcr.createComponent(factory);
109111
this.cdr.markForCheck();
110-
} else {
112+
} else if (ngDevMode) {
111113
console.error(
112114
`${LOG_PREFIX} - Loading of element <${this.tag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElementDynamic="errorTemplate: error" to display customized error message in place of element\n\n`,
113115
error

projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class LazyElementDirective implements OnInit, OnDestroy {
8989
const factory = this.cfr.resolveComponentFactory(errorComponent);
9090
this.vcr.createComponent(factory);
9191
this.cdr.markForCheck();
92-
} else {
92+
} else if (ngDevMode) {
9393
console.error(
9494
`${LOG_PREFIX} - Loading of element <${elementTag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElement="errorTemplate: error" to display customized error message in place of element`
9595
);

projects/elements/src/lib/lazy-elements/lazy-elements-loader.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ export class LazyElementsLoaderService {
4949
newConfigs.forEach((newConfig) => {
5050
const existingConfig = this.getElementConfig(newConfig.tag);
5151
if (existingConfig) {
52-
console.warn(
53-
`${LOG_PREFIX} - ElementConfig for tag '${newConfig.tag}' was previously added, it will not be added multiple times, continue...`
54-
);
52+
ngDevMode &&
53+
console.warn(
54+
`${LOG_PREFIX} - ElementConfig for tag '${newConfig.tag}' was previously added, it will not be added multiple times, continue...`
55+
);
5556
} else {
5657
newConfig.isAdded = true;
5758
this.configs.push(newConfig);
@@ -103,14 +104,14 @@ export class LazyElementsLoaderService {
103104
isModule ??= config?.isModule ?? this.options.isModule;
104105
importMap ??= config?.importMap ?? this.options.importMap;
105106

106-
if (!tag) {
107+
if (ngDevMode && !tag) {
107108
throw new Error(
108109
`${LOG_PREFIX} - tag for '${url}' not found, the *axLazyElement has to be used on HTML element`
109110
);
110111
}
111112

112113
if (!url) {
113-
if (!config?.url && !importMap) {
114+
if (ngDevMode && !config?.url && !importMap) {
114115
throw new Error(`${LOG_PREFIX} - url for <${tag}> not found`);
115116
} else if (importMap) {
116117
url = tag;
@@ -191,7 +192,7 @@ export class LazyElementsLoaderService {
191192
if (System) {
192193
await System.prepareImport();
193194
url = System.resolve(url);
194-
} else {
195+
} else if (ngDevMode) {
195196
throw new Error(
196197
`${LOG_PREFIX} - importMap feature depends on SystemJS library to be globally loaded but none was found, thus '${url}' can't be resolved. You should either load SystemJS or remove the importMap flag.`
197198
);

projects/elements/src/lib/lazy-elements/lazy-elements.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import {
2121
LAZY_ELEMENT_ROOT_OPTIONS,
2222
} from './lazy-elements.tokens';
2323

24+
import './ng-dev-mode';
25+
2426
export function createLazyElementRootGuard(
2527
rootOptions: LazyElementRootOptions
2628
) {
27-
if (rootOptions) {
29+
if (ngDevMode && rootOptions) {
2830
throw new TypeError(
2931
`LazyElementsModule.forRoot() called twice. Feature modules should use LazyElementsModule.forFeature() instead.`
3032
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @description Will be provided through Terser global definitions by Angular CLI during the
3+
* production build (ng build --configuration production). This is how Angular does tree-shaking internally.
4+
*
5+
* @internal
6+
*/
7+
declare const ngDevMode: boolean;

projects/elements/tsconfig.lib.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"declaration": true,
77
"declarationMap": true,
88
"inlineSources": true,
9+
"stripInternal": true,
910
"types": [],
1011
"lib": ["dom", "es2018"]
1112
},

0 commit comments

Comments
 (0)