Skip to content

Commit 3196b6a

Browse files
committed
perf: tree-shake console.* calls in production
This commit wraps calls to `console.*` with `ngDevMode`, an Angular CLI-provided compile-time variable. `isDevMode()` is a runtime function that still gets bundled into the JS file.
1 parent 8dd4e11 commit 3196b6a

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { ModuleWithProviders, NgModule } from '@angular/core';
22

3-
import { PageScrollService } from './providers/ngx-page-scroll.service';
43
import { NGXPS_CONFIG } from './providers/config.provider';
54
import { PageScrollConfig } from './types/page-scroll.config';
65

76
@NgModule({
8-
providers: [
9-
PageScrollService,
10-
{provide: NGXPS_CONFIG, useValue: {}},
11-
],
7+
providers: [{ provide: NGXPS_CONFIG, useValue: {} }],
128
})
139
export class NgxPageScrollCoreModule {
14-
static forRoot(config?: PageScrollConfig): ModuleWithProviders<NgxPageScrollCoreModule> {
10+
static forRoot(
11+
config?: PageScrollConfig
12+
): ModuleWithProviders<NgxPageScrollCoreModule> {
1513
return {
1614
ngModule: NgxPageScrollCoreModule,
17-
providers: [PageScrollService, {provide: NGXPS_CONFIG, useValue: config}],
15+
providers: [{ provide: NGXPS_CONFIG, useValue: config }],
1816
};
1917
}
2018
}

projects/ngx-page-scroll-core/src/lib/providers/config.provider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { InjectionToken } from '@angular/core';
22
import { PageScrollConfig } from '../types/page-scroll.config';
33

4-
export const NGXPS_CONFIG = new InjectionToken<PageScrollConfig>('ngxps_config');
4+
export const NGXPS_CONFIG = new InjectionToken<PageScrollConfig>(
5+
typeof ngDevMode !== 'undefined' && ngDevMode ? 'ngxps_config' : ''
6+
);
57

68
export const defaultPageScrollConfig: PageScrollConfig = {
79
_interval: 10,

projects/ngx-page-scroll-core/src/lib/providers/ngx-page-scroll.service.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, isDevMode } from '@angular/core';
1+
import { Inject, Injectable } from '@angular/core';
22

33
import { PageScrollConfig } from '../types/page-scroll.config';
44
import { InterruptReporter, PageScrollInstance, PageScrollOptions } from '../page-scroll-instance';
@@ -84,8 +84,10 @@ export class PageScrollService {
8484

8585
if (pageScrollInstance.pageScrollOptions.scrollViews === null || pageScrollInstance.pageScrollOptions.scrollViews.length === 0) {
8686
// No scrollViews specified, thus we can't animate anything
87-
if (this.config._logLevel >= 2 || (this.config._logLevel >= 1 && isDevMode())) {
88-
console.warn('No scrollViews specified, thus ngx-page-scroll does not know which DOM elements to scroll');
87+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
88+
if (this.config._logLevel >= 1) {
89+
console.warn('No scrollViews specified, thus ngx-page-scroll does not know which DOM elements to scroll');
90+
}
8991
}
9092

9193
return;
@@ -131,8 +133,10 @@ export class PageScrollService {
131133
if (isNaN(pageScrollInstance.distanceToScroll)) {
132134
// We weren't able to find the target position, maybe the element does not exist?
133135

134-
if (this.config._logLevel >= 2 || (this.config._logLevel >= 1 && isDevMode())) {
135-
console.log('Scrolling not possible, as we can\'t find the specified target');
136+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
137+
if (this.config._logLevel >= 1) {
138+
console.log('Scrolling not possible, as we can\'t find the specified target');
139+
}
136140
}
137141
pageScrollInstance.fireEvent(false);
138142

@@ -160,11 +164,13 @@ export class PageScrollService {
160164
const tooShortInterval = pageScrollInstance.executionDuration <= pageScrollInstance.pageScrollOptions._interval;
161165

162166
if (allReadyAtDestination || tooShortInterval) {
163-
if (this.config._logLevel >= 2 || (this.config._logLevel >= 1 && isDevMode())) {
164-
if (allReadyAtDestination) {
165-
console.log('Scrolling not possible, as we can\'t get any closer to the destination');
166-
} else {
167-
console.log('Scroll duration shorter that interval length, jumping to target');
167+
if (this.config._logLevel >= 2 || this.config._logLevel >= 1) {
168+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
169+
if (allReadyAtDestination) {
170+
console.log('Scrolling not possible, as we can\'t get any closer to the destination');
171+
} else {
172+
console.log('Scroll duration shorter that interval length, jumping to target');
173+
}
168174
}
169175
}
170176
pageScrollInstance.setScrollPosition(pageScrollInstance.targetScrollPosition);
@@ -177,8 +183,10 @@ export class PageScrollService {
177183
const alreadyInView = pageScrollInstance.targetScrollPosition > pageScrollInstance.startScrollPosition &&
178184
pageScrollInstance.targetScrollPosition <= pageScrollInstance.startScrollPosition + scrollRange;
179185
if (alreadyInView) {
180-
if (this.config._logLevel >= 2 || (this.config._logLevel >= 1 && isDevMode())) {
181-
console.log('Not scrolling, as target already in view');
186+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
187+
if (this.config._logLevel >= 1) {
188+
console.log('Not scrolling, as target already in view');
189+
}
182190
}
183191
pageScrollInstance.fireEvent(true);
184192

@@ -215,8 +223,10 @@ export class PageScrollService {
215223
instance.distanceToScroll,
216224
instance.executionDuration));
217225
}
218-
if (this.config._logLevel >= 5 && isDevMode()) {
219-
console.warn('Scroll Position: ' + newScrollPosition);
226+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
227+
if (this.config._logLevel >= 5) {
228+
console.warn('Scroll Position: ' + newScrollPosition);
229+
}
220230
}
221231
// Set the new scrollPosition to all scrollViews elements
222232
if (!instance.setScrollPosition(newScrollPosition)) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @internal */
2+
declare global {
3+
// Indicates whether the application is operating in development mode.
4+
// `ngDevMode` is a global flag set by Angular CLI.
5+
// https://github.com/angular/angular-cli/blob/9ceca0c4de1f351133c7c7df9e44c4b7a220ae8b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts#L600
6+
const ngDevMode: boolean;
7+
}
8+
9+
// This will be tree-shaken when built for production, as it is an empty object.
10+
// Thus, the `ng-dev-mode.ts` file could be treated as a module for type
11+
// augmentation (`declare global`) by the bundler.
12+
export default {};

projects/ngx-page-scroll-core/src/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Public API Surface of ngx-page-scroll-core
33
*/
44

5+
import './ng-dev-mode';
6+
57
export { NgxPageScrollCoreModule } from './lib/ngx-page-scroll-core.module';
68

79
export { defaultPageScrollConfig, NGXPS_CONFIG } from './lib/providers/config.provider';

0 commit comments

Comments
 (0)