Angular-cli:发射时间太慢

时间:2017-03-17 14:34:07

标签: javascript angular angular-cli

需要改进我的Angular2应用程序启动时间。预设它需要10-12秒才能启动。

应用程序中有多个模块,每个模块都使用延迟加载加载。

这是我的package.json文件

{
  "name": "AppStore-cli",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.3.1",
    "@angular/compiler": "2.3.1",
    "@angular/core": "2.3.1",
    "@angular/forms": "2.3.1",
    "@angular/http": "2.3.1",
    "@angular/material": "2.0.0-beta.1",
    "@angular/platform-browser": "2.3.1",
    "@angular/platform-browser-dynamic": "2.3.1",
    "@angular/router": "3.3.1",
    "@types/file-saver": "0.0.0",
    "angular2-moment": "1.2.0",
    "angular2-select": "1.0.0-beta.3",
    "autotable": "1.0.0",
    "core-js": "2.4.1",
    "file-saver": "^1.3.3",
    "jspdf": "1.3.2",
    "jspdf-autotable": "2.3.1",
    "md2": "0.0.16",
    "ng2-auto-complete": "0.10.9",
    "ng2-pagination": "2.0.0",
    "ng2-slimscroll": "1.2.1",
    "ng2-toastr": "1.4.1",
    "rxjs": "5.0.1",
    "ts-helpers": "1.1.1",
    "zone.js": "0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "2.3.1",
    "@types/file-saver": "0.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "6.0.42",
    "angular-cli": "1.0.0-beta.28.3",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "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.13",
    "ts-node": "1.2.1",
    "tslint": "4.3.0",
    "typescript": "~2.0.3"
  }
}

角cli.json

{
  "project": {
    "version": "1.0.0-beta.28.3",
    "name": "AppStore-cli"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico",
        "./images/"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "css/indigo-pink.css",
        "css/admin.css",
        "../node_modules/ng2-toastr/bundles/ng2-toastr.min.css"
      ],
      "scripts": [
        "../node_modules/core-js/client/shim.min.js",
        "../node_modules/reflect-metadata/Reflect.js",
        "../node_modules/jspdf/dist/jspdf.min.js",
        "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

我使用以下命令进行构建

ng build --prod

这是构建完成后文件生成的屏幕截图。

enter image description here 这是加载时间的屏障。

enter image description here

任何人都可以帮助我如何将发射时间减少到2-3秒或更短?

由于

1 个答案:

答案 0 :(得分:2)

你可以做的事情很少:

  • 运行ng build -prod -aot --stats-json
  • 修复您可能遇到的任何错误(并重建)
  • 安装webpack-bundle-analyze npm install --save-dev webpack-bundle-analyzer
  • 运行webpack-bundle-analyzer dist/stats.json
  • 查看您的捆绑包并删除任何不必要的代码
  • 在您的服务器上安装压缩(gzip)(仅此一项就可以将供应商捆绑包减少到~400kb)

如果事情仍然很慢,请查看使用通用,服务和网络工作者的服务器端呈现...... Google可以提供帮助(;

另请考虑升级到角度4.0.0。它产生的代码少2.x.x