【问题标题】:Warning: budgets: initial exceeded maximum budget警告:预算:初始超出最大预算
【发布时间】:2021-03-29 11:22:53
【问题描述】:

我在运行npm build --prod 时收到以下错误:

Error: budgets: initial exceeded maximum budget. Budget 1.00 MB was not met by 500.42 kB with a total of 1.49 MB.

我也收到此警告:

Warning: C:\Users\PATH_TO_FILE\socket.service.ts depends on 'socket.io-client'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

我还导入了很多 Angular Material 模块。这是我的app.module.ts(这是我整个项目中唯一的模块):

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';

import { AppComponent } from './app.component';
import { SocketService } from './services/socket.service';
import { AppRoutingModule } from './app-routing.module';

import { ClipboardModule } from '@angular/cdk/clipboard';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatIconModule } from '@angular/material/icon';
import { MatChipsModule } from '@angular/material/chips';
import { MatSliderModule } from '@angular/material/slider';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatDialogModule } from '@angular/material/dialog';

import { AdminPanelComponent } from './components/admin-panel/admin-panel.component';
import { ChatMessageComponent } from './components/chat-message/chat-message.component';
import { ChatPanelComponent } from './components/chat-panel/chat-panel.component';
import { DrawingPanelComponent } from './components/drawing-panel/drawing-panel.component';
import { PlayerComponent } from './components/player/player.component';
import { PlayersPanelComponent } from './components/players-panel/players-panel.component';
import { ToolsPanelComponent } from './components/tools-panel/tools-panel.component';
import { ChatMessageDirective } from './directives/chat-message.directive';
import { PlayerDirective } from './directives/player.directive';
import { GameManagerComponent } from './components/game-manager/game-manager.component';
import { ArtistOptionsComponent } from './components/artist-options/artist-options.component';
import { InfoPanelComponent } from './components/info-panel/info-panel.component';
import { DialogComponent } from './components/dialog/dialog.component';

@NgModule({
  declarations: [
    AppComponent,
    AdminPanelComponent,
    PlayersPanelComponent,
    DrawingPanelComponent,
    ChatPanelComponent,
    PlayersPanelComponent,
    ToolsPanelComponent,
    ChatMessageComponent,
    ChatMessageDirective,
    PlayerDirective,
    PlayerComponent,
    GameManagerComponent,
    ArtistOptionsComponent,
    InfoPanelComponent,
    DialogComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    FontAwesomeModule,
    ClipboardModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatTooltipModule,
    MatIconModule,
    MatChipsModule,
    MatSliderModule,
    MatButtonToggleModule,
    MatDialogModule
  ],
  providers: [SocketService],
  bootstrap: [AppComponent]
})
export class AppModule { }

如何解决超出最大预算的问题(我认为是socket.io-client 模块)?作为一个附带问题:我可以优化app.module.ts 文件吗?

【问题讨论】:

  • @ChrisG 感谢您的快速回复!我之前遇到过那个帖子,对增加预算有点不确定(我不能压缩文件)。增加预算是个好主意还是可以通过其他方式减小文件大小?当我运行webpack-bundle-analyzer ./dist/client/stats.json 时,总文件大小超过6mb,这非常令人震惊,我猜这是因为socket.io-client 模块。有什么办法可以找出占用这么多空间的原因吗? webpack-bundle-analyzervendor.js 只占用了超过 6mb)
  • 不确定 gzip 是如何影响这一点的,您能否将budgetangular.json 部分添加到问题中?
  • @ChrisG 是的,我可以,但不确定这是否是个好主意。不过,我现在会同意它。感谢您的帮助!

标签: optimization socket.io angular-cli


【解决方案1】:

您始终可以通过提高预算限制来提高阈值,但您应该寻找降低初始预算的方法。您可以使用 source-map-explorer 分析应用程序中的每个模块,并确定启动应用程序真正需要和不重要的内容。

例如,您可以从应用程序模块中删除一些不需要启动应用程序的依赖项和功能,并将它们带到延迟加载的模块中。这将通过减小初始包大小来减少您的初始应用加载时间。

下图是 Angular 应用程序启动时所需的初始捆绑包示例。

如果您需要为您的项目设置 source-map-explorer,请参阅本文。 https://dev.to/salimchemes/analyzing-angular-bundle-with-source-map-explorer-341

【讨论】:

    【解决方案2】:

    这些是您的angular.json 中的配置。 找到它,然后寻找预算。

    现在您可以根据需要更改那里的值。

    例如:

    "budgets": [
                    {
                      "type": "initial",
                      "maximumWarning": "500kb",
                      "maximumError": "1mb"
                    },
                    {
                      "type": "anyComponentStyle",
                      "maximumWarning": "2kb",
                      "maximumError": "4kb"
                    }
                  ],
    

    "budgets": [
                    {
                      "type": "initial",
                      "maximumWarning": "2mb",
                      "maximumError": "5mb"
                    },
                    {
                      "type": "anyComponentStyle",
                      "maximumWarning": "2kb",
                      "maximumError": "4kb"
                    }
                  ],
    

    您可以给出适合您限制的值。

    【讨论】:

      猜你喜欢
      • 2019-05-28
      • 2020-07-27
      • 2021-10-02
      • 2012-08-19
      • 2012-06-25
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多