简体   繁体   English

使用Webpack为移动和桌面分离捆绑包

[英]Separate bundles for mobile and desktop with Webpack

I'm trying to build two separate bundles from a single code base by having two entry points that point to the same file. 我试图通过指向同一文件的两个入口点从单个代码库构建两个单独的包。 The difference is that required files with .mobile in their name should not be included (noops) in the desktop bundle and vice-versa. 不同的是,与所需的文件.mobile在他们的名字不应该被包含在台式机套装(noops),反之亦然。
This is pretty easy to do do by running webpack two times and using the null-loader based on two separate configs but I was wondering if it would somehow be possible in a single run for performance reasons. 这很容易通过运行webpack两次并使用基于两个单独配置的null-loader来完成,但我想知道是否因为性能原因在一次运行中是否可能。 Any ideas? 有任何想法吗?

Well, I'm more than a year late, but if you are still interested in know how this is done, here is the way I achieved this: 好吧,我迟到了一年多,但是如果你仍然有兴趣知道这是怎么做的,这就是我实现这个目标的方式:

{

  entry: {

    'desktop/index': './js/desktop/index.js',
    'desktop/post': './js/desktop/post.js',
    'desktop/vendor': [
      'jquery'
    ],

    'mobile/index': './js/mobile/index.js',
    'mobile/vendor': [
      'jquery'
    ]

  },

  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: 'chunk.[chunkhash].js',
    path: path.resolve(__dirname, 'dist')
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['desktop/common', 'desktop/vendor'],
      chunks: ['desktop/common', 'desktop/index', 'desktop/post'],
      minChunks: 2
    }),
    new webpack.optimize.CommonsChunkPlugin({
      names: ['mobile/common', 'mobile/vendor'],
      chunks: ['mobile/common', 'mobile/index'],
      minChunks: 2
    })
  ]

};

I made a repo you can clone to see this working. 做了一个回购你可以克隆看到这个工作。 Just be aware that, for now, you'll have to manually change the src of the script tags in the .html files. 请注意,目前,您必须手动更改.html文件中脚本标记的src

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

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