Skip to content

7多页配置

js
module.exports = {
  entry: {
    home: './src/pages/home/index.js',
    about: './src/pages/about/index.js',
    login: './src/pages/login/index.js'
  },
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/pages/home/home.html',
      filename: 'index.html',
      chunks: ['runtime', 'vendors', 'home']
    }),
    new HtmlWebpackPlugin({
      template: './src/pages/about/about.html',
      filename: 'about.html',
      chunks: ['runtime', 'vendors', 'about']
    }),
    new HtmlWebpackPlugin({
      template: './src/pages/login/login.html',
      filename: 'login.html',
      chunks: ['runtime', 'vendors', 'login']
    })
  ]
}

上面可优化~

js
const configs = {} // 保存上面的所有配置
configs.plugins = generatePlugins(configs)

function generatePlugins(configs) {
  const plugins = [
    // 存放默认要用到的plugin
  ]

  Object.keys(configs.entry).forEach(item => {
    plugins.push(
      new HtmlWebpackPlugin({
        template: `./src/pages/${item}/${item}.html`,
        filename: `${item}.html`,
        chunks: ['runtime', 'vendors', item]
      })
    )
  })

  return plugins
}

module.exports = configs

共 20 个模块,1301 篇 Markdown 文档。