【问题标题】:Create vendor.bundle for each entry with webapack 4使用 webapack 4 为每个条目创建 vendor.bundle
【发布时间】:2018-12-14 11:00:41
【问题描述】:

我想使用 webapack4 为每个条目制作 vendor.bundle。
例子。

src/
  pages/
    home/
     index.js
    list/
     index.js

构建之后。

dist/
  pages/
    home/
      index.bundle.js
      home.vendor.bundle.js
    list/
      index.bundle.js
      list.vendor.bundle.js

如何配置拆分块?
当前配置。

const path = require('path')

module.exports = {
  mode: 'development',

  entry: {
    '/pages/home/index': path.resolve(__direname, 'src', 'pages', 'home', 'index.js'),
    '/pages/list/index': path.resolve(__direname, 'src', 'pages', 'list', 'index.js'),
  },

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

  // I am troubled with the setting here.
  optimization: {
    runtimeChunk: {
      name: 'runtime'
    },
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          chunks: 'all',
          enforce: true,
          name: 'vendor'
        },
      }
    }
  },
}

我尝试使用函数而不是字符串来处理名称,但它不起作用。

谢谢。

【问题讨论】:

  • 你当前的配置是什么?请在此处粘贴。
  • 我粘贴了当前配置。

标签: javascript webpack webpack-splitchunks


【解决方案1】:

解决了这个问题。

const path = require('path')
module.exports = {
  mode: 'development',

  entry: {
    '/pages/home/index': path.resolve(__direname, 'src', 'pages', 'home', 'index.js'),
    '/pages/list/index': path.resolve(__direname, 'src', 'pages', 'list', 'index.js'),
  },

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

  optimization: {
    runtimeChunk: {
      name: 'runtime'
    },
    splitChunks: {
      cacheGroups: {
        'top-vendor': {
          test: /[\\/]node_modules[\\/]/,
          chunks: chunk => chunk.name === '/pages/home/index',
          enforce: true,
          name: `${path.dirname('/pages/home/index')}/vendor`
        },
        'list-vendor': {
          test: /[\\/]node_modules[\\/]/,
          chunks: chunk => chunk.name === '/pages/list/index',
          enforce: true,
          name: `${path.dirname('/pages/list/index')}/vendor`
        },
      }
    }
  },
}

【讨论】:

  • 关键部分是optimization.splitChunks.cacheGroups[name].chunks。非常感谢,花了很长时间试图让它工作!
猜你喜欢
  • 2014-09-18
  • 1970-01-01
  • 2013-01-15
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多