angular 项目的包大小?

bundle size for an angular project?

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

低于使用以下命令为生产生成的包大小:

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

prod模式下所有请求的大小约为2.5MB。

对于一个几乎没有依赖项、模块和组件的项目来说,这是一个可以接受的大小吗?

{
  "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 上激活压缩减少发送到客户端的数据:

是的,看起来不错。

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

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

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