Webpack 4.X from beginner to proficient-devserver and Mode (iii)

Source: Internet
Author: User

The previous article describes the use of plug-ins in detail, this article then enriched module.exportsProperties in the. Today's front-end development has been very rapid, accompanied by the transformation of the development model. Now it is no longer a static page and put it in the browser to open a preview. In real-world development, it is often necessary to use httpservers, such as the previous ajax, you need to build a server to see the effect. There are a lot of ways to build a server, and webpackThe native server support, you do not have to download the software. It also provides automatic refresh, hot update and other functions, making development very convenient.

Devserver Installation and use

NPM I webpack-dev-server-d

Open the terminal and enter into the corresponding project (my for webpack-demo ), execute the command webpack-dev-server , if the terminal shows the following indicates that the server has been successfully opened

Attention:
1, at this point may be prompted webpack-dev-server is not internal command, the solution is to install the webpack-dev-server module in the global, or package.json add in the inside scripts "dev": "webpack-dev-server" , and then execute the commandnpm run dev
2. At this point, I did not webpack generate a directory through the command dist (directory structure, such as), and then enter the address in the browser http://localhost:8080/ , the page will display normally. This is because the devServer webpack built-in files are saved in memory and can be previewed without a package build

Configuration parameters

webpack.config.jsThe contents are as follows:

const path=require(‘path‘);const HtmlWebpackPlugin=require(‘html-webpack-plugin‘);module.exports={    entry:{        index:‘./src/index.js‘,    },    output:{        path:path.resolve(__dirname,‘dist‘),        filename:‘[name].bundle.js‘    },    plugins:[        new HtmlWebpackPlugin({            title:‘陈学辉‘,            template:‘./src/template.html‘,            filename:‘index.html‘,        })    ],    devServer:{        host:‘localhost‘,   //服务器的ip地址        port:1573,  //端口        open:true,  //自动打开页面    }}

index.jsThe contents of the file are as follows:

console.log(‘这是入口文件‘);

template.htmlThe contents of the file are as follows:

<!DOCTYPE html>

After the command is executed webpack-dev-server , the browser automatically opens the page, and if index.js the file is modified, the browser automatically refreshes, such as:

Hot update

By default, when the server is turned on, the entire page is refreshed as soon as the portal file is updated. If the number of modules introduced in the page to make the entire page refresh will become somewhat slow, this problem can be referred to the hot update to solve. The hot update means updating only the changed modules (local refreshes like Ajax)

Use steps

1. Introduction webpack Module

const webpack=require(‘webpack‘);

2. Write Plugin

plugins:[    new HtmlWebpackPlugin({        title:‘陈学辉‘,        template:‘./src/template.html‘,        filename:‘index.html‘,    }),    new webpack.HotModuleReplacementPlugin()    //引入热更新插件]

3, the devServer increase in hot parameters

devServer:{    host:‘localhost‘,   //服务器的ip地址    port:1573,  //端口    open:true,  //自动打开页面,    hot:true,   //开启热更新}

At this time can not see the effect, to the back of the article in the loader when you can see the effect
devServerOther configurations: https://webpack.docschina.org/configuration/dev-server/

Mode

modeIs webpack4 a new property that is meant for the currently developing environment. has mode reduced the number of configurations, it has built up a lot of features. Improved a lot compared to the previous version and reduced the number of specialized configurations

  1. Improved build speed
  2. Default to development environment, no special configuration required
  3. Provides compression functionality without the need for plugins
  4. Provided SouceMap , no special configuration required

modeIt is divided into two environments, one is the development environment ( development ) and the other is the production environment ( production ). The development environment is the environment in which we write code, and the production environment is where the code is placed on the line. The most intuitive difference between the two environments is that the code for the development environment does not provide compression, and the code for the production environment provides compression.

How to use 1: Add after command

Webpack--mode Development
Webpack--mode Production

Mode of Use 2: inpackage.jsonAdded in
"scripts": {    "build": "webpack --mode production",    "dev": "webpack-dev-server --mode development"  },

At this point, the dist directory is a production environment, open the server is the development environment, and then through the command execution npm run build or npm run dev , they are the following differences
distThe contents of the catalogue index.bundle.js are as follows:

Http://localhost:1573/index.bundle.js content is as follows:

Data download

Filed under: Webpack 4.X from Beginner to proficient-module (iv)

Webpack 4.X from beginner to proficient-devserver and Mode (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.