1、先安装:
    package.json:

    1. "devDependencies": {
    2. "clean-webpack-plugin": "^3.0.0",
    3. "html-webpack-plugin": "^3.2.0",
    4. "webpack": "^4.41.5",
    5. "webpack-cli": "^3.3.10"
    6. }

    webpack.config:

    1. const { CleanWebpackPlugin } = require("clean-webpack-plugin")
    2. const HtmlWebpackPlugin = require('html-webpack-plugin')
    3. module.exports = {
    4. mode: "development",
    5. devtool: "source-map",
    6. entry: {
    7. home: "./src/index.js", //可以让页面两个都引用
    8. a: "./src/a.js"
    9. },
    10. output: {
    11. filename: "scripts/[name].[chunkhash:5].js" //放在script的文件夹 原理:他能读取asset数组
    12. },
    13. plugins: [
    14. new CleanWebpackPlugin(),
    15. new HtmlWebpackPlugin({
    16. template: "./public/index.html",
    17. filename: "home.html",
    18. chunks: ["home"]
    19. }),
    20. new HtmlWebpackPlugin({ //原理:emit利用fs模块生成一个页面文件,给文件内容的合适的位置添加一个script元素,元素的src路径引用打包后的js
    21. template: "./public/index.html", //以这个页面模板来生成新的页面
    22. filename: "a.html", //生成的文件名
    23. chunks: ["a"] //只引用一个js[a的js]
    24. })
    25. ]
    26. }

    指令:

    1. html-webpack-plugin

    效果图:
    image.png