webpack多页面打包

683 阅读1分钟
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require("clean-webpack-plugin")

module.exports = {
  entry: {
    index: './src/index.js',
    other: './src/other.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: "[name].js"
  },
  plugins: [
    new CleanWebpackPlugin({
      paths: path.resolve(__dirname, 'dist'),    
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src/index.html'),//模板地址
      filename: 'index.html',//生成的文件名
      hash: true,//是否在引入的js文件后面添加hash随机数 src="other.js?bbe98d06b18a1dc381fd"
      title: 'index页面', //生成的index文件的title  注:模板页面title <title><%= htmlWebpackPlugin.options.title %></title>
      chunks: ['index'] //引入打包后的js文件
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src/other.html'),
      filename: 'other.html',
      hash: true,
      title: 'other页面',
      chunks: ['other']
    })
  ]}