gpt4 book ai didi

angular - Angular 项目的 bundle 大小?

转载 作者:行者123 更新时间:2023-12-03 09:12:48 28 4
gpt4 key购买 nike

我使用 angular-cli 从事 anuglar 项目。开发模式下生成的 main.bundle 大小约为 4.32 MB。

bundle dev

使用以下 cmd 生成的生产包大小低于:

ng build --target=production --environment=prod

bundle prod

生产模式下所有请求的大小约为 2.5MB。 firebug network

对于依赖项、模块和组件很少的项目来说,这是可接受的大小吗?

{
"name": "frontend",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "2.2.0",
"@angular/compiler": "2.2.0",
"@angular/core": "2.2.0",
"@angular/forms": "2.2.0",
"@angular/upgrade": "2.2.0",
"@angular/http": "2.2.0",
"@angular/platform-browser": "2.2.0",
"@angular/platform-browser-dynamic": "2.2.0",
"@angular/router": "3.2.0",
"core-js": "^2.4.1",
"ng2-translate": "^4.0.0",
"primeng": "^1.0.0-rc.4",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},
"devDependencies": {
"@types/jasmine": "^2.2.30",
"angular-cli": "1.0.0-beta.19-3",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.5",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
}

**aot 编译失败,堆栈跟踪如下:

>ng build --prod --aot
0% compilingthis._directiveNormalizer.normalizeTemplate is not a function
TypeError: this._directiveNormalizer.normalizeTemplate is not a function
at CompileMetadataResolver._loadDirectiveMetadata (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:14493:60)
at C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:14644:50
at Array.forEach (native)
at CompileMetadataResolver._loadNgModuleMetadata (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:14634:56)
at CompileMetadataResolver.loadNgModuleMetadata (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:14566:31)
at addNgModule (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:12915:37)
at C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:12929:16
at Array.forEach (native)
at _loadNgModules (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:12928:28)
at analyzeNgModules (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:12685:14)
at OfflineCompiler.compileModules (C:\dev\clients\cf\projects\op\frontend\node_modules\@angular\compiler\bundles\compiler.umd.js:12744:18)
at CodeGenerator.codegen (C:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\codegen.ts:71:26)
at AotPlugin._make (C:\dev\clients\cf\projects\op\frontend\node_modules\@ngtools\webpack\src\plugin.js:186:43)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\@ngtools\webpack\src\plugin.js:151:75)
at Compiler.applyPluginsParallel (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:156:14)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:463:8)
at Compiler.applyPluginsAsync (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:73:70)
at Compiler.compile (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:456:7)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:219:10)
at Compiler.readRecords (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:367:10)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:216:9)
at next (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:81:11)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\CachePlugin.js:34:58)
at Compiler.applyPluginsAsync (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:85:13)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:213:8)
at next (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:81:11)
at Compiler.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\node\NodeEnvironmentPlugin.js:23:3)
at Compiler.applyPluginsAsync (C:\dev\clients\cf\projects\op\frontend\node_modules\tapable\lib\Tapable.js:85:13)
at Compiler.run (C:\dev\clients\cf\projects\op\frontend\node_modules\webpack\lib\Compiler.js:210:7)
at C:\dev\clients\cf\projects\op\frontend\node_modules\angular-cli\tasks\build-webpack.js:26:29
at initializePromise (C:\dev\clients\cf\projects\op\frontend\node_modules\es6-promise\dist\lib\es6-promise\-internal.js:244:5)
at new Promise (C:\dev\clients\cf\projects\op\frontend\node_modules\es6-promise\dist\lib\es6-promise\promise.js:135:31)
at Class.exports.default.Task.extend.run (C:\dev\clients\cf\projects\op\frontend\node_modules\angular-cli\tasks\build-webpack.js:25:16)
at Class.Command.extend.run (C:\dev\clients\cf\projects\op\frontend\node_modules\angular-cli\commands\build.js:50:26)
at Class.<anonymous> (C:\dev\clients\cf\projects\op\frontend\node_modules\angular-cli\lib\models\command.js:152:17)
at tryCatch (C:\dev\clients\cf\projects\op\frontend\node_modules\rsvp\dist\lib\rsvp\-internal.js:215:12)
at invokeCallback (C:\dev\clients\cf\projects\op\frontend\node_modules\rsvp\dist\lib\rsvp\-internal.js:230:13)
at C:\dev\clients\cf\projects\op\frontend\node_modules\rsvp\dist\lib\rsvp\then.js:29:16
at flush (C:\dev\clients\cf\projects\op\frontend\node_modules\rsvp\dist\lib\rsvp\asap.js:85:5)
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)

**在 tomcat 上激活压缩会减少发送到客户端的数据: network gzip enabled

最佳答案

是的,看起来不错。

如果您想减小应用大小,可以执行以下操作:

  1. 将您的应用划分为子模块并使用延迟加载。
  2. 使用 AoT 编译机制,这将编译您的代码,而不是部署可运行的代码,并在 JIT(及时)编译机制中将代码编译到用户的浏览器中。
  3. 您可以应用 gzip 并缩小文件。

这些步骤将极大地帮助您减小应用程序的大小。

关于angular - Angular 项目的 bundle 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40629362/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com