Home > Web Front-end > JS Tutorial > Detailed explanation of webpack-dev-server configuration and usage steps

Detailed explanation of webpack-dev-server configuration and usage steps

php中世界最好的语言
Release: 2018-05-28 15:52:36
Original
2118 people have browsed it

This time I will bring you a detailed explanation of the steps for configuring and using webpack-dev-server. What are the precautions for configuring and using webpack-dev-server? The following is a practical case. Let’s take a look. .

1Install 's WebPack-dev-server

Enter

npm i webpack-dev-server
Copy after login
Install webpack-dev-server package# in the terminal

##2. Configure dev-server

Add code in the script in the package.json file

"dev":"WebPack-dev-server --config webpack.config.js”
Copy after login

Globally in the webpack.config.js file Add

target:"web"
Copy after login

Terminal input

npm i cross-env
Copy after login

Install env

Configure environment

Variables

Add in webpack.config .JS file Statement

const isDev = process.env.NODE_ENV ==='development'
Copy after login

Determine whether the value of isDev is equal to development

Change

module

.exports to a constant configuration and add the statement

to facilitate changes Configuration

Add statement in webpack.config.js

if(isDev){
 config.devtool =“#廉价模块-EVAL-源映射”//代码映射
 config.devServer = {
  port:8000,//启动服务监听端口
  host:'0.0.0.0',//可以通过localhost访问
  overlay:{//在页面上显示错误信息
   errors:true,
   },
   open:true,//启动webpack-dev-server时自动打开浏览器
   hot:true //启用热更
 } 
 config.plugins.push(
  new webpack.HotModuleReplacementPlugin(),
  new webpack.NoEmitOnErrorsPlugin()//热更相关插件
 )
}
Copy after login

3. Create HTML page

Terminal input

npm i html-webpack-plugin
Copy after login

Installation html-webpack-plugin plug-in

Add statement in webpack.config.js

const HTMLPlugin = require('html-webpack-plugin')
Copy after login

Configuration

 plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: isDev ? '"development"' : '"production"'
      }
    }),
    new HTMLPlugin()
  ]
Copy after login

After completing the above steps, enter

npm run dev
Copy after login

in the terminal After packaging is completed, open the browser and enter the address localhost: 8000 to enter the page

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 build a webpack react development environment


How to build a React family bucket environment

The above is the detailed content of Detailed explanation of webpack-dev-server configuration and usage steps. 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