首頁  >  文章  >  web前端  >  html檔案如何打包 ? html文件打包的方法介紹

html檔案如何打包 ? html文件打包的方法介紹

不言
不言原創
2018-08-18 15:38:1913443瀏覽

這篇文章帶給大家的內容是關於html檔案如何打包 ? html文件打包的方法介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

根據上篇文章講的js壓縮,npm run server運行後會報錯

要弄清楚這個問題,我們先要弄清楚清楚什麼是開發環境,什麼是生產環境。開發環境中是基本上不會對js進行壓縮的,在開發預覽時我們需要明確的報錯行數和錯誤訊息,所以完全沒有必要壓縮JavasScript程式碼。而生產環境才會壓縮JS程式碼,用來加快程式的工作效率。 devServer用於開發環境,而壓縮JS用於生產環境,在開發環境中作生產環境的事情所以Webpack設定了衝突報錯。

生產環境中壓縮js會無法調試(找不到第幾行)

在實際開發中,webpack設定檔是分開的,開發環境一個文件,生產環境一個文件。

1、html檔案的打包

  • 把dist目錄下面的index.html剪切複製到src目錄下,index.html裡面引入的js程式碼可以刪除掉(會自動引入js程式碼),這就是模板檔案

  • 在webpack-config.js裡面引入外掛程式

     plugins:[
           new htmlPlugin({
              minify:{
                  removeAttributeQuotes:true       //对html进行压缩,去掉属性的双引号
              },
              hash:true,      //为了开发中js有缓存效果,加入hash可以有效避免缓存JS
              template:'./src/index.html'      //是要打包的html模板路径和文件名称
          })
        ]
    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

    出現上述程式碼即成功

    • 如下所示即打包成功

      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 –save-dev html-webpack-plugin

    • 設定html

相關推薦:

自己寫HTML用Cordova打包與用AppCan、Dcloud、WeX5、ApiCloud有何不同?

css檔案如何進行打包? css檔案打包的方法

以上是html檔案如何打包 ? html文件打包的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn