webpack--生成html文件的插件:html-webpack-plugin

赵炯
2023-12-01

1 安装指定版本2的插件:npm i -D html-webpack-plugin@2

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^4.0.3",
    "webpack": "^3.12.0"
  }
}

2 在webpack.config.js中配置:

const HtmpWebpackPlugin=require('html-webpack-plugin');
const path=require('path');

module.exports={
  entry: './src/main.js',
  output:{
    path: path.resolve(__dirname,'dist'),
    filename: 'app.js'
  },
  plugins:[
    new HtmpWebpackPlugin()
  ]
}

3 编译:npm run dev:会在dist目录中生成index.html和app.js文件

同样可以指定html模板和文件名进行编译,在配置中修改如下:

new HtmpWebpackPlugin({
      filename: 'app.html',
      template: './src/index.html'
    })

 

 类似资料: