角项目的包大小?

时间:2016-11-16 10:12:11

标签: angular optimization angular-cli

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

bundle dev

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

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

bundle prod

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 个答案:

答案 0 :(得分:3)

是的,看起来不错。

如果您想减少应用尺寸,可以执行以下操作:

  1. 在子模块中划分您的应用并使用延迟加载。
  2. 使用AoT编译机制,这将编译代码而不是部署可运行代码,并且在JIT中(及时)编译机制代码在用户的浏览器中编译。
  3. 您可以应用gzip并缩小文件。
  4. 这些步骤可以帮助您减少应用程序的大小。