Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-router使用webpack的require.ensure动态加载问题 #1526

Closed
flicker85 opened this issue Apr 27, 2016 · 5 comments
Closed

react-router使用webpack的require.ensure动态加载问题 #1526

flicker85 opened this issue Apr 27, 2016 · 5 comments

Comments

@flicker85
Copy link

flicker85 commented Apr 27, 2016

react-router使用webpack的require.ensure动态加载子页面模块,
比如首页里有A,B,C 3个链接, 点链接会通过require.ensure动态加载相应的子页面模块,
假如A,B,C三个页面中都引用了antd里的Button组件, 通过webpack打包时候,A,B,C会生成3个js文件,
1.chunk.js, 2.chunk.js, 3.chunk.js,
在这3个打包文件里,都会有重复的antd的Button组件的代码,
请问有什么办法能把重复的代码提取出来么?
CommonsChunkPlugin可以提取entry中指定文件的公共代码,
可是像这种通过require.ensure动态加载的应该如何提取呢?

@afc163
Copy link
Member

afc163 commented Apr 27, 2016

好像和 antd 没关系...建议搜索或到更对口的地方提问,以获得更专业的回答。

@afc163 afc163 closed this as completed Apr 27, 2016
@sethzhao
Copy link

遇到相同问题,请问找到解决方案了吗?

@flicker85
Copy link
Author

flicker85 commented May 20, 2016

module.exports = {
entry: {
    app: path.resolve(APP_PATH, 'entry/index.jsx'),
    vendor: [
      'react',
      'react-dom',
      'react-router'
    ],
    antd: [
      'antd/lib/button',
      'antd/lib/icon'
    ]
  },
  output: {
    path: BUILD_PATH,
    filename: 'js/[name].js',
    chunkFilename: 'js/[name].chunk.js'
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['antd', 'vendor'],
      minChunks: Infinity
    })
  ]
}

@blade254353074
Copy link
Contributor

@flicker85 很赞,用这种方法可以解决按需加载模块过大问题,主要是 antd/lib/table 占得多

@lock
Copy link

lock bot commented May 4, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants