How to package html files? Introduction to the method of packaging html files

不言
Release: 2018-08-18 15:38:19
Original
13501 people have browsed it

The content of this article is about how to package html files? The introduction to the method of packaging html files has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

According to the js compression mentioned in the previous article, npm run server will report an error after running

To understand this problem, we must first figure it out Know what is a development environment and what is a production environment. JS is basically not compressed in the development environment. During development preview, we need to clearly indicate the number of error lines and error information, so there is no need to compress JavaScript code. Only in the production environment will JS code be compressed to speed up the work efficiency of the program. devServer is used in the development environment, while compressed JS is used in the production environment. In the development environment, the production environment is done, so Webpack sets up conflict reporting.

Compressed js in the production environment will not be able to debug (the line cannot be found)

In actual development, the webpack configuration files are separated, one file for the development environment and one file for the production environment.

1. Packaging of html files

  • Cut and copy the index.html under the dist directory to the src directory, and the js introduced in index.html The code can be deleted (js code will be automatically introduced), this is the template file

  • Introduce the plug-in in webpack-config.js

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

    The above code appears That is, it is successful

    • The packaging is successful as shown below

      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
      Copy after login
    • Delete the dist directory and run it in the terminal Enter webpack for packaging

    • Enter npm run dev in the terminal and run it in the browser

    • The configuration in the plugins is as follows:

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

    • Install in the terminal: npm install –save-dev html-webpack-plugin

    • Configure html

    Related recommendations:

    Write your own HTML and package it with Cordova What are the differences between using AppCan, Dcloud, WeX5, and ApiCloud?

    How to package css files? How to package css files

    The above is the detailed content of How to package html files? Introduction to the method of packaging html files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!