הסרת `@ngrx/store-devtools` בסביבת production
This video explains the best practice of removing @ngrx/store-devtools in production environments to optimize application performance, along with practical steps to implement this effectively.
בסביבת production עליך להסיר את @ngrx/store-devtools מה - bundle.
נראה דוגמא:
יצירת אפליקציה Angular חדשה
Section titled “יצירת אפליקציה Angular חדשה”npx @angular/cli@latest new ngrx-remove-devtools --style css --routing false --minimalהתקנת @ngrx/store ו @ngrx/store-devtools
Section titled “התקנת @ngrx/store ו @ngrx/store-devtools”cd ngrx-remove-devtoolsnpx ng add @ngrx/store --skip-confirmationnpx ng add @ngrx/store-devtools@latest --skip-confirmationאכלוס ה state
Section titled “אכלוס ה state”@NgModule({ declarations: [AppComponent], imports: [ BrowserModule, StoreModule.forRoot( { counter: () => 0, }, {} ), StoreDevtoolsModule.instrument({maxAge: 25, logOnly: !isDevMode()}), ], providers: [], bootstrap: [AppComponent],})export class AppModule {}בחינת ה state
Section titled “בחינת ה state”במידה והתקנתם את Redux dev tools תוכלו לבחון את ה state שלכם.
ng serve
בעיה בבדיקת ה state בסביבת production
Section titled “בעיה בבדיקת ה state בסביבת production”מומלץ להשבית את redux dev tools בסביבת production.
npx ng buildcd dist/ngrx-remove-devtoolsnpx http-serverredux dev tools עדיין עובד ב production
פתרון 1
Section titled “פתרון 1”הסר את StoreDevtoolsModule.instrument מה imports בסביבת production.
npx ng generate environmentsהוסיפו מפתח production לקובץ הסביבות:
export const environment = { production: false,};export const environment = { production: true,};import {environment} from '../environments/environment';
@NgModule({ declarations: [AppComponent], imports: [ BrowserModule, StoreModule.forRoot( { counter: () => 0, }, {} ), !environment.production ? StoreDevtoolsModule.instrument({maxAge: 25, logOnly: !isDevMode()}) : [], ], providers: [], bootstrap: [AppComponent],})export class AppModule {}@ngrx/store-devtools עדיין ב bundle.
npx ng build --source-mapnpx source-map-explorer dist/ngrx-remove-devtools/main.4a92d461e97ce051.js
@ngrx/store-devtools עדיין מוסיף כ - 16kb של קוד ל bundle שלך.
החלפה ב app.module.ts
Section titled “החלפה ב app.module.ts”ניתן להשיג את אותו תוצאה עם fileReplacement.
import {BrowserModule} from '@angular/platform-browser';import {StoreModule} from '@ngrx/store';
export const commonImports = [ BrowserModule, StoreModule.forRoot( { counter: () => 0, }, {} ),];import {StoreDevtoolsModule} from '@ngrx/store-devtools';import {commonImports} from './imports.common';import {isDevMode} from '@angular/core';
export const imports = [ ...commonImports, StoreDevtoolsModule.instrument({maxAge: 25, logOnly: !isDevMode()})];import {commonImports} from './imports.common';
export const imports = [...commonImports];import {NgModule} from '@angular/core';import {AppComponent} from './app.component';import {imports} from './imports';
@NgModule({ declarations: [AppComponent], imports: [...imports], providers: [], bootstrap: [AppComponent],})export class AppModule {}התוצאה היא הסרה של @ngrx/store-devtools מה - bundle.