Webpack learning notes series 05 devserver

CS Xiaoyao Sword Fairy 2021-02-23 03:37:42
webpack learning notes series devserver


webpack Learning notes series 05-devserver

Write By CS Xiaoyao sword immortal My home page : csxiaoyao.com GitHub: github.com/csxiaoyaojianxian Email: sunjianfeng@csxiaoyao.com QQ: 1724338257

1. Webpack Dev Server

1.1 Command line

webpack-dev-server It's a small express The server , It passes through express Middleware webpack-dev-middleware and webpack Interact .

# Install... In the project webpack-dev-server
$ npm i webpack-dev-server
# Use npx start-up
$ npx webpack-dev-server

Read after startup webpack Configure the file and package it into memory . It is recommended that the command be written to package.json in , It also supports the use of option parameters :

# modify port and host
$ webpack-dev-server --port 3000 --host 127.0.0.1
# start-up inline Automatic refresh of mode
$ webpack-dev-server --hot --inline
# Use monitor
$ webpack-dev-server --watch
# Appoint webpack config file
$ webpack-dev-server --config webpack.xxx.js
# Appoint webpack mode
$ webpack-dev-server --mode development
# Manually change the working directory to a non current directory
$ webpack-dev-server --content-base ./dist

Tips: webpack-dev-server Support two modes of automatic refresh page :iframe and inline iframe: Put the page in a iframe Inside , Content changes page reloads inline: take webpack-dev-server Overload code added to the output bundle in , comparison iframe It doesn't need to refresh the whole page

1.2 HMR(Hot Module Replacement)

Webpack Can be configured by webpack.HotModuleReplacementPlugin Plug in to open the global HMR, Without refreshing the page , Direct replacement 、 Add or delete modules .

const path = require('path');
module.exports = {
entry: './src/index.js',
devServer: {
contentBase: path.join(__dirname, 'dist'),
port: 8080,
// Turn on hmr Support , It's the command line --hot
hot: true
},
plugins: [
// add to hmr plug-in unit , If the command line adds --inline Can be configured automatically
new webpack.HotModuleReplacementPlugin()
]
};

perform webpack-dev-server, open http://localhost:8080 , When index.js You can see the result of modification in real time .

1.3 proxy agent

devServer.proxy It can solve the cross domain problem of local development .

module.exports = {
devServer: {
// case1: /api/users => http://csxiaoyao.com/api/users
proxy: {
'/api': 'http://csxiaoyao.com'
},
// case2: /api/users => https://csxiaoyao.com/users
proxy: {
'/api': {
target: 'http://csxiaoyao.com',
pathRewrite: { '^/api': '' },
secure: false
}
},
// case3: Agent bypasses html Resource file request
proxy: {
'/api': {
target: 'http://csxiaoyao.com',
bypass(req, res, proxyOptions) {
// Determine... In the request header accept value
if (req.headers.accept.indexOf('html') !== -1) {
// return contentBase The path of
// return xxx
}
}
}
},
// case4: Only agent /api and /auth Two addresses , The rest will be released
proxy: [{
context: ['/auth', '/api'],
target: 'http://csxiaoyao.com'
}]
}
};

1.4 Custom middleware & mock server

stay webpack-dev-server Before and after loading all internal middleware, you can use devServer.before and devServer.after Implement custom middleware .

module.exports = {
devServer: {
before(app, server) {
// Set up mock data ,http://localhost:8080/api/mock.json
app.get('/api/mock.json', (req, res) = > {
res.json({ text: 'hello world' });
});
},
after(app, server) {
// ...
}
}
};

1.5 webpack dev server Common configuration

devServer.historyApiFallback: Failed default page

devServer.compress: Enable gzip Compress

devServer.hotOnly: Is it not allowed to go back to refresh the web page when the build fails

devServer.inline: Mode switch , Default to inline mode , Use false Switch to iframe Pattern

devServer.open: After starting , Whether to automatically open the home page with the browser

devServer.openPage: After starting , Automatically use the browser to open the set page

devServer.overlay: Whether full screen overlay is allowed to display compilation errors , Not allowed by default

devServer.port: Monitor port number , Default 8080

devServer.host: Appoint host, Use 0.0.0.0 It can be accessed in LAN

devServer.contentBase: Static file root path

devServer.publicPath: Set the virtual path mapping of the package file in memory , The difference in output.publicPath

devServer.staticOptions: To configure express.static Parameters

devServer.clientLogLevel: stay inline Mode to control the printing in the browser log Level

devServer.quiet: silent mode , Set to true The output is not on the console log

devServer.noInfo: No output start log

devServer.lazy: Don't listen for file changes , But recompile when the request comes

devServer.watchOptions:watch Related configuration , Such as modifying the monitoring interval

devServer.headers: Custom request header , Such as custom userAgent etc.

devServer.https:https Certificate signature, etc

sign

Original statement , This article is authorized by the author + Community publication , Unauthorized , Shall not be reproduced .

If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

版权声明
本文为[CS Xiaoyao Sword Fairy]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210221215035476f.html

  1. An inexperienced front-end engineer, what are the common problems when writing CSS?
  2. HttpServletRequest get URL (parameter, path, port number, protocol, etc.) details
  3. Springboot starts http2
  4. Enabling http2.0 in spring boot
  5. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  6. Using JavaScript in Safari browser history.back () the page will not refresh after returning to the previous page
  7. vue.js Error in win10 NPM install
  8. In less than two months, musk made more than $1 billion, more than Tesla's annual profit
  9. Springboot starts http2
  10. Vue event bus
  11. JQuery easy UI tutorial: custom data grid Pagination
  12. Using okhttp and okhttpgo to obtain onenet cloud platform data
  13. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  14. HTTP 1. X learning notes: an authoritative guide to Web Performance
  15. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control
  16. HTTP 1. X learning notes: an authoritative guide to Web Performance
  17. JQuery:JQuery基本语法,JQuery选择器,JQuery DOM,综合案例 复选框,综合案例 随机图片
  18. Event bubble and capture in JavaScript
  19. The root element is missing solution
  20. Event bubble and capture in JavaScript
  21. Configure the certificate to enable ngnix to publish the trusted website of HTTPS
  22. Javascript数据类型
  23. HTTP interface debugging tool! 48000 star HTTP command line client!
  24. Parameter encryption of front end URL link band
  25. HTTP interface debugging tool! 48000 star HTTP command line client!
  26. Three front end frameworks: data binding and data flow
  27. Reading Axios source code (1) -- exploring the realization of basic ability
  28. Event bubble and capture in JavaScript
  29. 【微前端】微前端最終章-qiankun指南以及微前端整體探索
  30. R & D solution e-Car front end monitoring system
  31. [JS] 877 - 35 wonderful knowledge of JavaScript, long experience!
  32. R & D solution e-Car front end monitoring system
  33. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%
  34. 解决ajax跨域问题【5种解决方案】
  35. Top ten classic sorting of JavaScript
  36. HTTP 1. X learning notes: an authoritative guide to Web Performance
  37. Vue3 component (IX) Vue + element plus + JSON = dynamic rendering form control component
  38. My http / 1.1 is so slow!
  39. Why Vue uses asynchronous rendering
  40. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for
  41. The tapable instance object hook of webpack4. X core tool library
  42. The tapable instance object hook of webpack4. X core tool library
  43. Using libcurl for HTTP communication in C + +
  44. Using libcurl for HTTP communication in C + +
  45. Using CSS variable in Vue
  46. Deeply understand the update of state and props in react
  47. No matter how fast the Internet is, it's useless! In addition to Baidu disk, there is this website slow to let you eat shriveled
  48. Baidu share does not support the solution of HTTPS
  49. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  50. [micro front end] the final chapter of micro front end - Qiankun guide and overall exploration of micro front end
  51. Vue cli creates vue3 project
  52. Nginx reverse proxy for windows authentication using NTLM
  53. Rust tutorial: introduction to rust for JavaScript developers
  54. Deploying personal blog to Tencent cloud with serverless framework
  55. R & D solution e-Car front end monitoring system
  56. JavaScript advanced learning
  57. Spend 1 minute to master these 5 ppt tips, courseware making less detours
  58. Vue: vuex persistent state
  59. React native gets the current network state of the device Netinfo
  60. High performance nginx HTTPS tuning - how to speed up HTTPS by 30%