html文件如何打包 ?html文件打包的方法介绍


当前第2页 返回上一页

在实际开发中,webpack配置文件是分开的,开发环境一个文件,生产环境一个文件。

1、html文件的打包

  • 把dist目录下面的index.html剪切复制到src目录下,index.html里面引入的js代码可以删除掉(会自动引入js代码),这就是模板文件

  • 在webpack-config.js里面引入插件

    1

    2

    3

    4

    5

    6

    7

    8

    9

    plugins:[

          new htmlPlugin({

             minify:{

                 removeAttributeQuotes:true       //对html进行压缩,去掉属性的双引号

             },

             hash:true,      //为了开发中js有缓存效果,加入hash可以有效避免缓存JS

             template:'./src/index.html'      //是要打包的html模板路径和文件名称

         })

       ]

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    i ?wds?: Project is running at http://10.212.109.18:8087/

     i ?wds?: webpack output is served from /

     i ?wds?: Content not from webpack is served from F:\webLearn\webpackLearn\dist

     ? ?wdm?: Hash: 027dd749b565ba3b200d

     Version: webpack 4.15.1

     Time: 3008ms

     Built at: 2018-07-11 08:49:13

      Asset       Size  Chunks             Chunk Names

     entry2.js    139 KiB       0  [emitted]  entry2

     entry.js    145 KiB       1  [emitted]  entry index.html  427 bytes          [emitted]

     Entrypoint entry = entry.js

     Entrypoint entry2 = entry2.js

    出现上述代码即成功

    • 如下所示即打包成功

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      Hash: 40112e2b8d4dc81b512b

      Version: webpack 4.15.1

      Time: 5299ms

      Built at: 2018-07-11 08:47:26

      Asset       Size  Chunks             Chunk Names

      entry2.js  962 bytes       0  [emitted]  entry2

      entry.js   6.75 KiB       1  [emitted]  entry

      index.html  427 bytes          [emitted]

      [0] ./src/entry2.js 33 bytes {0} [built]

      [4] ./node_modules/css-loader!./src/css/index.css 227 bytes {1} [built]

      [5] ./src/css/index.css 1.06 KiB {1} [built]

      [6] ./src/entry.js 94 bytes {1} [built]

      + 3 hidden modules

       

      WARNING in configuration

      The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.

      You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

      Child html-webpack-plugin for "index.html": 1 asset

      [0] (webpack)/buildin/module.js 497 bytes {0} [built]

      [1] (webpack)/buildin/global.js 489 bytes {0} [built]

      [3] ./node_modules/html-webpack-plugin/lib/loader.js!./src/index.html 511 bytes {0} [built]

      + 1 hidden module

    • 把dist目录删掉 ,在终端输入webpack进行打包

    • 在终端输入 npm run dev 在浏览器中运行

    • plugins里面配置如下:

    • const htmlPlugin=require(‘html-webpack-plugin’);

    • 在终端里面进行安装:npm install ?Csave-dev html-webpack-plugin

    • 配置html

相关推荐:

自己写HTML用Cordova打包与用AppCan、Dcloud、WeX5、ApiCloud有何区别?

css文件如何进行打包?css文件打包的方法

以上就是html文件如何打包 ?html文件打包的方法介绍的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

html文件如何打包 ?html文件打包的方法介绍

更多相关阅读请进入《html文件打包》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...