Detailed explanation of using webpack hot module replacement

php中世界最好的语言
Release: 2018-04-20 09:43:16
Original
1534 people have browsed it

This time I will bring you a detailed explanation of the use of webpack hot module replacement. What are theprecautionswhen using webpack hot module replacement. The following is a practical case, let's take a look.

The function of hot module replacement (HMR) is that when the application is running, necessary modules can be replaced, added, and deleted without refreshing the page. This article mainly introduces webpack hot module replacement (HMR)/ The editor thinks the hot update method is quite good, so I would like to share it with you now and give it as a reference. Let’s follow the editor and take a look.

This is an article about the simplest configuration of webpack hot module replacement (react is not required), also called hot update.

The function of hot module replacement (HMR) is to replace, add, and delete necessary modules without refreshing the page when the application is running. HMR is useful for applications that consist of a single state tree. Because the components of these applications are "dumb" (as opposed to "smart"), after the component's code changes, the state of the component can still correctly reflect the latest state of the application.

webpack-dev-server has built-in "live reload", which will automatically refresh the page.

The file directory is as follows:

  1. app

    1. ##a.js

    2. component.js

    3. index.js

  2. node_modules

  3. package.json

  4. webpack.config.js

  5. ##a.js is imported into component.js. index.js imports component.js. Modifying any file can achieve the hot update function.

The most important thing is that in index.js, there is such a piece of code: (Required to complete hot update)

if(module.hot){ module.hot.accept(moduleId, callback); }
Copy after login

The following is the package.json configuration:

{ "name": "webpack-hmr", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "nodemon --watch webpack.config.js --exec \"webpack-dev-server --env development\"", "build": "webpack --env production" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "html-webpack-plugin": "^2.28.0", "nodemon": "^1.11.0", "webpack": "^2.2.1", "webpack-dev-server": "^2.4.1" } }
Copy after login

As you can see from the dependencies, this is really the simplest configuration. Among them, nodemon is used to monitor the status of the webpack.config.js file. As long as there is a change, the command will be re-executed.

Regarding "html-webpack-plugin", it can be omitted here. For specific configuration, please see:

https://

www.npmjs.com/package/html-webpack-plugin.Here, two commands are defined, one is start, used in the development environment; the other is build, used in the production environment. --watch is used to monitor one or more files, and --exec is used by nodemon to execute other commands. For specific configuration, please see: https://c9.io/remy/nodemon.

The next step is webpack.config.js. Since two commands are defined in the scripts of package.json, we still have to implement two situations (development and production) in the

config file

, you can also modify the configuration of one of them).

Regarding Object.assign, the first parameter is the target object, if the properties in the target object have the same key, the properties will be overwritten by the properties in the source. Properties of later sources will similarly override earlier properties. Shallow assignment, use = for object copying, that is, reference copying.

The env parameter is passed in through cli.

Then open the command line to the current directory and run npm start or npm run build. Note that the former is in the development environment and has no output file (in memory). Only when the latter is run will there be an output file.

The code of the demo is at: https://github.com/yuwanlin/webpackHMR

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

How to choose the jQuery version


##Detailed explanation of using mint-ui in vue


JS capture page scroll bar

The above is the detailed content of Detailed explanation of using webpack hot module replacement. 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
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!