Ind*_*ika 4 visual-studio asp.net-core angular-cli asp.net-core-webapi angular
我是 Angular 的新手,目前正在开发一个 Angular 应用程序,该应用程序通过 Visual Studio 创建,最近更新到最新的 Angular 版本(12)。
我的应用程序没有任何延迟加载的实现。但是当我发布应用程序时,它会生成大量名为Lazy Chunk Files的 js 文件。
我的 tsconfig.json 文件如下,
而且,我的 angular.json 文件如下,
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"Website": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"progress": true,
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/font-awesome/css/font-awesome.min.css",
"src/styles.css"
],
"scripts": [],
"allowedCommonJsDependencies": ["@ctrl/ngx-codemirror", "xlsx"],
"vendorChunk": true,
"extractLicenses": false,
"buildOptimizer": false,
"sourceMap": true,
"optimization": false,
"namedChunks": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "anyComponentStyle",
"maximumWarning": "6kb"
}
]
}
},
"defaultConfiguration": ""
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "Website:build"
},
"configurations": {
"production": {
"browserTarget": "Website:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "Website:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
],
"scripts": [],
"assets": [
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist-server",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.server.json",
"sourceMap": true,
"optimization": false
},
"configurations": {
"dev": {
"optimization": true,
"outputHashing": "all",
"namedChunks": false,
"extractLicenses": true
},
"production": {
"optimization": true,
"outputHashing": "all",
"namedChunks": false,
"extractLicenses": true
}
},
"defaultConfiguration": ""
}
}
},
"Website-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "Website:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "Website"
}Run Code Online (Sandbox Code Playgroud)
我的 app-routing.module.ts 文件如下,(减少了一些重复的代码行和导入)
// Imports for the necessary components
const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'default' },
{ path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
{ path: 'logout', component: LogoutComponent, canActivate: [AuthGuard] },
{ path: 'authentication', component: AuthenticationComponent },
{ path: 'dialog-status', component: DialogStatusComponent },
{ path: 'dialog-confirm', component: DialogConfirmComponent },
// There are some more paths defined for the dialog components in the real file
{ path: 'password', component: ChangePasswordComponent, canActivate: [AuthGuard] },
{ path: 'reports', component: ReportsComponent, canActivate: [AuthGuard], canDeactivate: [PendingChangesGuard] },
// There are some more paths defined for the components in the real file
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }Run Code Online (Sandbox Code Playgroud)
我的app-module.ts文件如下,(减少了一些重复的代码行和导入)
// Necessary imports
registerLocaleData(localeEs);
@NgModule({
declarations: [
AppComponent,
LoginComponent,
HomeComponent,
// And some more components
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
AppRoutingModule,
FormsModule,
BrowserAnimationsModule,
MaterialModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: CustomLoader,
deps: [HttpClient]
}
})
],
providers: [
AppService,
TranslateService,
PendingChangesGuard,
{
provide: LOCALE_ID,
useFactory: (appService: AppService) => appService.getLocalLanguage(),
deps: [AppService]
}
],
bootstrap: [AppComponent],
exports: [TranslateModule]
})
export class AppModule { }Run Code Online (Sandbox Code Playgroud)
当我执行构建时,我需要停止生成这些惰性块文件。你们中有人对此有任何想法吗?
(如果您需要更多信息,请告诉我)
我正在回答我自己的问题,因为我刚刚找到了生成大量 js 文件的原因。
首先,我使用以下设置更改了生产构建配置设置,以生成带有名称的块文件(当前它只是一个 Guid),
"namedChunks": true
Run Code Online (Sandbox Code Playgroud)
看到这些名称后,我意识到这些块文件来自Angular/common/locals,我在应用程序中使用它们来启用西班牙语和英语。
@angular/common/locals 拥有大量适用于世界上所有文化的 js 语言文件。但就我而言,我只需要 es 和 en 语言。因此,我干脆删除了那些对于其他文化来说不必要的js文件。
(更新)
如果您仍然收到大量 js 惰性块文件,请尝试删除 Angular 缓存文件夹。(客户端应用程序/.角度)
| 归档时间: |
|
| 查看次数: |
4517 次 |
| 最近记录: |