【问题标题】:Setup webpack with multiple entry points, common and vendor and chunks使用多个入口点设置 webpack,common 和 vendor 和 chunks
【发布时间】:2017-01-15 22:38:03
【问题描述】:

我有一个 webpack 配置,其中有 2 个入口点(a.js 和 b.js),并且想将我的共享代码拆分为 2 个附加文件:common 和 vendor。

我正在使用Explicit Vendor Chunk 示例

a.js 和 b.js 应该很小 - 只有模块本身,而不是任何共享代码。我希望在应用程序运行时 (common.js) 和供应商运行时 (vendor.js) 之间拆分“运行时”

当我加载 a.html 时,我可以加载 vendor.js、common.js 和 a.js 脚本

module.exports = {
  context: __dirname,
  devtool: 'inline-source-map',
  entry: {
    a: './src/a.js',
    b: './src/b.js',
    vendor: [
      'react',
      'react-dom',
      'react-router'
    ]
  },
  output: {
    path: path.join(__dirname, './build'),
    filename: '[name].js'
  },
  plugins: [
    new CommonsChunkPlugin({
    name: "vendor",
    minChunks: Infinity,
    })
  ]
}

这会创建一个供应商块,但我还想在 a.js 和 b.js 之间创建一个通用应用块。

如何使用 webpack 创建一个通用的应用程序块?

【问题讨论】:

    标签: webpack


    【解决方案1】:

    几个月前我也遇到了这个问题,终于找到了解决方案。这是我的 webpack 配置,假设两个入口点 app.en.js 和 app.es.js 具有一些特定于语言环境的代码,然后导入我们的应用程序,其中包含我们的 React 组件、Redux 减速器等。

    entry: {
      'app.en': './frontend/src/app.en.js',
      'app.es': './frontend/src/app.es.js',
    },
    
    /**
     * The order matters here a lot!
     */
    plugins: [
      /**
       * The `common` chunk will catch code shared between `app.en.js` and `app.es.js`, 
       * e.g. the React components used in your app.
       */
      new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
      }),
    
      /**
       * Catches all your vendor files, assuming they're in `node_modules`. You can 
       * change the logic to include other vendor folders as well, as shown 
       * below (we have one simply called 'vendor').
       */
      new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        minChunks: function (module) {
          // this assumes your vendor imports exist in the node_modules directory
          return module.context && (module.context.indexOf('node_modules') !== -1)
        }
      }),
    ],
    

    另外请注意,我在入口点中不包括实际应用(在我们的例子中,application.js)。

    同样重要的是要注意,我没有像示例中经常显示的那样指定“供应商”入口点。这完全不适合我。

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2017-08-06
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多