【问题标题】:Creating multiple bundles using angular-cli webpack使用 angular-cli webpack 创建多个包
【发布时间】:2016-09-21 14:09:38
【问题描述】:

当我使用 angular-cli 构建项目时,它会将所有项目文件捆绑到一个大的主包中。

我在应用程序中使用了惰性路由,一旦应用程序加载,我就可以正常导航。

有没有一种方法可以根据延迟加载的路由模块将主包分成多个文件?

下面是angular-cli.json中的配置

    {
  "project": {
    "version": "1.0.0-beta.15",
    "name": "maddy-test-project"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "styles/content",
      "index": "default.htm",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "",
      "mobile": false,
      "styles": [
        "styles.less"
      ],
      "scripts": [
        "styles/wfa-myriad-pro-typekit.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "less",
    "prefixInterfaces": false
  }
}

下面是package.json

{
  "name": "maddy-test-project",
  "version": "0.0.1",
  "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.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "d3": "^4.2.3",
    "jquery": "^3.1.0",
    "lodash": "^4.15.0",
    "moment": "^2.15.0",
    "core-js": "^2.4.1",
    "rxjs": "5.0.0-beta.12",
    "toastr": "^2.1.2",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23",  
    "bootstrap-daterangepicker": "^2.1.24"
  },
  "devDependencies": {
    "@types/d3": "^3.5.35",
    "@types/google-maps": "^3.1.27",
    "@types/jasmine": "^2.2.30",
    "@types/jquery": "^1.10.31",
    "@types/lodash": "^4.14.34",
    "@types/toastr": "^2.1.29",
    "angular-cli": "1.0.0-beta.15",
    "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"
  }
}

提前致谢!!

【问题讨论】:

    标签: angular angular-cli angular2-cli


    【解决方案1】:

    就是NgModuleRouterModule.forChild()的作用。这是一篇非常好的文章,用于启动大型 Angular 2 模块化应用程序开发:http://blog.angular-university.io/angular2-ngmodule/

    我们需要做的第一件事是从 App 组件和主路由配置中删除对 Home 组件或 HomeModule 的所有提及:

    我们可以在这里看到 App 组件不再导入 HomeModule,而是路由配置使用 loadChildren 来表示如果 /home 或任何其他以它开头的 url 被命中,那么文件 home.module 应该通过 Ajax 加载打电话。

    简而言之,要在惰性模块中移动一些逻辑和组件,您可以运行以下命令:

    ng g module child --routing
    

    然后 angular-cli 会生成一个 NgModule (app/child/child.module.ts) 和一个子路由器配置 (app/child/child-routing.module.ts)。

    延迟加载这个子路由器的路径是:

     { path: 'child', loadChildren: 'app/child/child.module#ChildModule' }
    

    最后,通过一个约束在 ChildModule 中移动您想要的内容:其他模块(作为 AppModule)将无法使用任何 ChildModule 依赖项(例如服务)。如果需要,一个好的做法是创建一个共享模块。

    【讨论】:

    • ng g module child 将不再生成该组件,因为 beta.24(beta.23 未发布)。我们需要使用ng g module child --routing
    • 感谢您的回答。我还需要知道 1 件事,你怎么知道“ng g module child 自 beta.24 以来将不再生成组件”?是否有任何文档可以从我那里获得这些重要的 angular-cli 发布信息?
    • 组件生成对于模块生成是可选的。这是一个带有 beta 23 的功能(github.com/angular/angular-cli/pull/3389)(没有像@Timathon 所说的那样发布):github.com/angular/angular-cli/blob/master/…。在 beta 24 中,默认情况下会生成组件。
    【解决方案2】:

    如果您将要延迟加载的区域拆分为单独的模块,您应该能够生成多个捆绑包。看看这个plnkr 直接取自 angular.io 网站的路由和导航。 如果您以这种方式配置路由,您应该能够在运行 ng serve 时生成类似于:
    注意块文件。在我的项目中,当导航到“危机”和“英雄”路线时,这些块文件会异步加载。

    export const routes: Routes = [
      { path: '', redirectTo: 'contact', pathMatch: 'full'},
      { path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' },
      { path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' }
    ];
    

    【讨论】:

    • 如何使用 webpack 进行分块
    猜你喜欢
    • 2017-06-14
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2016-05-09
    相关资源
    最近更新 更多