简体   繁体   English

如何在angular 2应用程序上使用webpack进行代码拆分?

[英]How do I do code splitting with webpack on angular 2 app?

I have an app structure like this: 我有这样的应用程序结构:

├── /dashboard
│   ├── dashboard.css
│   ├── dashboard.html
│   ├── dashboard.component.ts
│   ├── dashboard.service.ts
│   ├── index.ts
├── /users
│   ├── users.css
│   ├── users.html
│   ├── users.component.ts
│   ├── users.service.ts
│   ├── index.ts
├── /login
│   ├── login.css
│   ├── login.html
│   ├── login.component.ts
│   ├── login.service.ts
│   ├── index.ts
├── app.component.ts
├── app.module.ts
├── app.routes.ts
├── app.styles.css

And I want to code split my app into something like this: 我想将我的应用程序代码拆分成这样的代码:

├── dashboard.js
├── users.js
├── login.js
├── app.js

I cannot seem to find an example of how I can do this with webpack. 我似乎无法找到一个如何用webpack做这个的例子。 So 2 questions. 所以2个问题。 Can this be done? 可以这样做吗? And, How can this be done? 而且,如何做到这一点?

Any leads or help would be appreciated. 任何线索或帮助将不胜感激。 I have been researching this all morning. 我整个上午一直在研究这个问题。

Angular documentation suggests it here , but no examples or tutorials that I can find. Angular文档在这里提出了建议,但没有我能找到的示例或教程。 So it's possible, yet no one know how to do it?. 所以这是可能的,但没有人知道该怎么做?

you can find the webpack configuration here 你可以在这里找到webpack配置

You will have to put each one of them as an entry point 您必须将每一个作为切入点

entry: {
  'dashboard': './src/dashboard/index.ts',
  'users': './src/users/index.ts',
  'login': './src/login/index.ts',
  'app': './src/app.module.ts'
}

and then to make sure no code is duplicate across the different entry points set them in the commons chunk plugin. 然后确保没有代码在不同的入口点重复设置它们在commons chunk插件中。 The order is important code encountered in app and subsequently also important in dashboard or users will only show up in the last one it is present/required in. 订单是应用程序中遇到的重要代码,随后在仪表板中也很重要,或者用户只会出现在最后一个存在/需要的位置。

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
       name: ['app', 'dashboard', 'login', 'users'] 
    })
]

you can also get some inspiration from here: https://angular.io/docs/ts/latest/guide/webpack.html#!#common-configuration 你也可以从这里获得一些灵感: https//angular.io/docs/ts/latest/guide/webpack.html#!#common-configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM