webpack-dev-server å¯ç¨äºå¿«éå¼ååºç¨ç¨åºã请æ¥é å¼åæå å¼å§ä½¿ç¨ã
å½å页é¢è®°å½äºå½±å webpack-dev-server
(ç®å: dev-server
) version >= 4.0.0 é
ç½®çé项ãå¯ä»¥å¨ è¿é æ¾å° v3
å° v4
çè¿ç§»æå
object
éè¿ webpack-dev-server çè¿äºé
ç½®ï¼è½å¤ä»¥å¤ç§æ¹å¼æ¹åå
¶è¡ä¸ºãè¿æ¯ä¸ä¸ªåºæ¬ç示ä¾ï¼å©ç¨ gzips
å缩 public/
ç®å½å½ä¸çææå
容并æä¾ä¸ä¸ªæ¬å°æå¡(serve)ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 9000,
},
};
å½æå¡(server
)å¯å¨åï¼å¨è§£æ模åå表ä¹åè¾åºä¸æ¡æ¶æ¯ï¼
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:9000/
<i> [webpack-dev-server] On Your Network (IPv4): http://197.158.164.104:9000/
<i> [webpack-dev-server] On Your Network (IPv6): http://[fe80::1]:9000/
<i> [webpack-dev-server] Content not from webpack is served from '/path/to/public' directory
è¿éå°ä¼ç»åºæå¡å¯å¨ä½ç½®ä»¥åå 容çä¸äºåºæ¬ä¿¡æ¯ã
å¦æä½ éè¿ Node.js API ä½¿ç¨ dev-serverï¼å devServer
ä¸çé
ç½®é项å°è¢«å¿½ç¥ãä½å¯ä»¥å°é
ç½®é项ä½ä¸ºç¬¬ä¸ä¸ªåæ°ä¼ å
¥ï¼new WebpackDevServer({...}, compiler)
ãæ¤ç¤ºä¾å±ç¤ºäºå¦ä½éè¿ Node.js API ä½¿ç¨ webpack-dev-serverã
ä½ å¯ä»¥éè¿ CLI è°ç¨ webpack-dev-serverï¼æ¹å¼æ¯ï¼
npx webpack serve
CLI é 置项å表å¯ä»¥å¨ è¿é æ¥è¯¢ã
è½ç¶æ¨èéè¿ CLI è¿è¡ webpack-dev-server
ï¼ä½æ¯ä½ ä¹å¯ä»¥éè¿ API æ¥å¯ç¨æå¡å¨ã
æ¥çç¸å
³ç webpack-dev-server
API ææ¡£ã
'auto' | 'all'
[string]
该é项å 许å°å 许访é®å¼åæå¡å¨çæå¡åå ¥ç½ååã
webpack.config.js
module.exports = {
//...
devServer: {
allowedHosts: [
'host.com',
'subdomain.host.com',
'subdomain2.host.com',
'host2.com',
],
},
};
模仿 django çALLOWED_HOSTS
ï¼ç¨ .
ä½ä¸ºååéé
符ã.host.com
ä¼ä¸ host.com
ï¼www.host.com
以å host.com
çå
¶ä»ä»»ä½å
¶ä»ååå¹é
ã
webpack.config.js
module.exports = {
//...
devServer: {
// this achieves the same effect as the first example
// with the bonus of not having to update your config
// if new subdomains need to access the dev server
allowedHosts: ['.host.com', 'host2.com'],
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --allowed-hosts .host.com --allowed-hosts host2.com
å½è®¾ç½®ä¸º 'all'
æ¶ä¼è·³è¿ host æ£æ¥ã并ä¸æ¨èè¿æ ·åï¼å 为ä¸æ£æ¥ host çåºç¨ç¨åºå®¹æåå° DNS éç»å®æ»å»ã
webpack.config.js
module.exports = {
//...
devServer: {
allowedHosts: 'all',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --allowed-hosts all
å½è®¾ç½®ä¸º 'auto'
æ¶ï¼æ¤é
置项æ»æ¯å
许 localhost
ã host
å client.webSocketURL.hostname
ï¼
webpack.config.js
module.exports = {
//...
devServer: {
allowedHosts: 'auto',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --allowed-hosts auto
boolean = false
object
è¿ä¸ªé ç½®ç¨äºå¨å¯å¨æ¶éè¿ ZeroConf ç½ç»å¹¿æä½ çå¼åæå¡å¨ã
webpack.config.js
module.exports = {
//...
devServer: {
bonjour: true,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --bonjour
å¦éç¦ç¨ï¼
npx webpack serve --no-bonjour
ä½ ä¹å¯ä»¥ä¸º bonjour 设置 èªå®ä¹é 置项ï¼ä¾å¦ï¼
webpack.config.js
module.exports = {
//...
devServer: {
bonjour: {
type: 'http',
protocol: 'udp',
},
},
};
'log' | 'info' | 'warn' | 'error' | 'none' | 'verbose'
å 许å¨æµè§å¨ä¸è®¾ç½®æ¥å¿çº§å«ï¼ä¾å¦å¨éè½½ä¹åï¼å¨ä¸ä¸ªé误ä¹åæè 模åçæ¿æ¢ å¯ç¨æ¶ã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
logging: 'info',
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-logging info
boolean = true
object: { errors boolean = true, warnings boolean = true }
å½åºç°ç¼è¯é误æè¦åæ¶ï¼å¨æµè§å¨ä¸æ¾ç¤ºå ¨å±è¦çã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
overlay: true,
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-overlay
å¦éç¦ç¨ï¼
npx webpack serve --no-client-overlay
å¦æä½ åªæ³æ¾ç¤ºé误信æ¯ï¼
webpack.config.js
module.exports = {
//...
devServer: {
client: {
overlay: {
errors: true,
warnings: false,
},
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-overlay-errors --no-client-overlay-warnings
boolean
å¨æµè§å¨ä¸ä»¥ç¾åæ¯æ¾ç¤ºç¼è¯è¿åº¦ã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
progress: true,
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-progress
å¦éç¦ç¨ï¼
npx webpack serve --no-client-progress
boolean = true
number
åè¯ dev-server å®åºè¯¥å°è¯éæ°è¿æ¥å®¢æ·ç«¯ç次æ°ãå½ä¸º true
æ¶ï¼å®å°æ é次å°è¯éæ°è¿æ¥ã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
reconnect: true,
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --client-reconnect
å½è®¾ç½®ä¸º false
æ¶ï¼å®å°ä¸ä¼å°è¯è¿æ¥ã
module.exports = {
//...
devServer: {
client: {
reconnect: false,
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --no-client-reconnect
æ¨è¿å¯ä»¥æå®å®¢æ·ç«¯åºè¯¥å°è¯éæ°è¿æ¥çç¡®å次æ°ã
module.exports = {
//...
devServer: {
client: {
reconnect: 5,
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --client-reconnect 5
'ws' | 'sockjs'
string
该é
置项å
许æ们为客æ·ç«¯åç¬éæ©å½åç devServer
ä¼ è¾æ¨¡å¼ï¼æè
æä¾èªå®ä¹ç客æ·ç«¯å®ç°ãè¿å
许æå®æµè§å¨æå
¶ä»å®¢æ·ç«¯ä¸ devServer
çéä¿¡æ¹å¼ã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketTransport: 'ws',
},
webSocketServer: 'ws',
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-web-socket-transport ws --web-socket-server ws
å建ä¸ä¸ªèªå®ä¹å®¢æ·ç«¯å®ç°ï¼å建ä¸ä¸ªç±»æå± BaseClient
ã
ä½¿ç¨ CustomClient.js
çè·¯å¾ï¼ä¸ä¸ªèªå®ä¹ WebSocket 客æ·ç«¯å®ç°ï¼è¿åå
¼å®¹ç 'ws'
æå¡å¨ï¼
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketTransport: require.resolve('./CustomClient'),
},
webSocketServer: 'ws',
},
};
使ç¨èªå®ä¹ä¸å ¼å®¹ç WebSocket 客æ·ç«¯åæå¡ç«¯å®ç°ï¼
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketTransport: require.resolve('./CustomClient'),
},
webSocketServer: require.resolve('./CustomServer'),
},
};
string
object
è¿ä¸ªé项å 许æå® URL å° web socket æå¡å¨ï¼å½ä½ 代çå¼åæå¡å¨å客æ·ç«¯èæ¬ä¸æ»æ¯ç¥éè¿æ¥å°åªéæ¶å¾æç¨ï¼ã
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketURL: 'ws://0.0.0.0:8080/ws',
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --client-web-socket-url ws://0.0.0.0:8080/ws
æ¨è¿å¯ä»¥æå®å ·æ以ä¸å±æ§ç对象ï¼
hostname
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾ç hostname è¿è¡è¿æ¥ãpathname
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾çè·¯å¾è¿è¡è¿æ¥ãpassword
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾çå¯ç è¿è¡è®¤è¯ãport
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾ç端å£è¿è¡è¿æ¥ãprotocol
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾çåè®®è¿è¡è¿æ¥ãusername
: åè¯è¿æ¥å° devServer ç客æ·ç«¯ä½¿ç¨æä¾çç¨æ·åè¿è¡è®¤è¯ãwebpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketURL: {
hostname: '0.0.0.0',
pathname: '/ws',
password: 'dev-server',
port: 8080,
protocol: 'ws',
username: 'webpack',
},
},
},
};
boolean = true
å¯ç¨ gzip compressionï¼
webpack.config.js
module.exports = {
//...
devServer: {
compress: true,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --compress
å¦éç¦ç¨
npx webpack serve --no-compress
object
为 webpack-dev-middleware æä¾å¤ç webpack èµæºçé 置项ã
webpack.config.js
module.exports = {
devServer: {
devMiddleware: {
index: true,
mimeTypes: { phtml: 'text/html' },
publicPath: '/publicPathForDevServe',
serverSideRender: true,
writeToDisk: true,
},
},
};
boolean
ä½¿ç¨ spdy æä¾ HTTP/2 æå¡ãå¯¹äº Node 15.0.0 åæ´é«çæ¬ï¼æ¤é项å°è¢«å¿½ç¥ï¼å 为 spdy å¨è¿äºçæ¬ä¸å·²è¢«ç ´åãä¸æ¦ Express æ¯æï¼å¼åæå¡å¨å°è¿ç§»å° Node å ç½®ç HTTP/2ã
HTTP/2 带æèªç¾åè¯ä¹¦ï¼
webpack.config.js
module.exports = {
//...
devServer: {
http2: true,
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --http2
ç¦ç¨ï¼
npx webpack serve --no-http2
éè¿ https é ç½®ä½ èªå·±çè¯ä¹¦æ件ï¼
webpack.config.js
const fs = require('fs');
module.exports = {
//...
devServer: {
http2: true,
https: {
key: fs.readFileSync('/path/to/server.key'),
cert: fs.readFileSync('/path/to/server.crt'),
ca: fs.readFileSync('/path/to/ca.pem'),
},
},
};
è¦éè¿ CLI 使ç¨ä½ çè¯ä¹¦ï¼è¯·ä½¿ç¨ä»¥ä¸é 置项ï¼
npx webpack serve --http2 --https-key ./path/to/server.key --https-cert ./path/to/server.crt --https-ca ./path/to/ca.pem
boolean
object
é»è®¤æ åµä¸ï¼å¼åæå¡å¨å°éè¿ HTTP æä¾æå¡ãå¯ä»¥éæ©ä½¿ç¨ HTTPS æä¾æå¡ï¼
webpack.config.js
module.exports = {
//...
devServer: {
https: true,
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --https
ç¦ç¨ï¼
npx webpack serve --no-https
æ ¹æ®ä¸è¿°é ç½®ï¼å°ä½¿ç¨èªç¾åè¯ä¹¦ï¼ä½æ¯ä½ ä¹å¯ä»¥æä¾èªå·±çè¯ä¹¦ï¼
webpack.config.js
module.exports = {
devServer: {
https: {
ca: './path/to/server.pem',
pfx: './path/to/server.pfx',
key: './path/to/server.key',
cert: './path/to/server.crt',
passphrase: 'webpack-dev-server',
requestCert: true,
},
},
};
该对象ç´æ¥ä¼ éå° Node.js HTTPS 模åï¼å æ¤è¯·åé HTTPS documentation 以è·åæ´å¤ä¿¡æ¯ã
è¦éè¿ CLI 使ç¨èªå·±çè¯ä¹¦ï¼è¯·ä½¿ç¨ä»¥ä¸é项ï¼
npx webpack serve --https-request-cert --https-key ./path/to/server.key --https-cert ./path/to/server.crt --https-ca ./path/to/ca.pem --https-pfx ./path/to/server.pfx --https-passphrase webpack-dev-server
webpack-dev-server >= v4.2.0
å
è®¸ä½ è®¾ç½®é¢å¤ç TLS é
置项ï¼æ¯å¦ minVersion
ãåæ ·ä½ å¯ä»¥ç´æ¥ä¼ éåèªæ件çå
容ï¼
webpack.config.js
const fs = require('fs');
const path = require('path');
module.exports = {
devServer: {
https: {
minVersion: 'TLSv1.1',
key: fs.readFileSync(path.join(__dirname, './server.key')),
pfx: fs.readFileSync(path.join(__dirname, './server.pfx')),
cert: fs.readFileSync(path.join(__dirname, './server.crt')),
ca: fs.readFileSync(path.join(__dirname, './ca.pem')),
passphrase: 'webpack-dev-server',
requestCert: true,
},
},
};
array
function
object
为ææååºæ·»å headersï¼
webpack.config.js
module.exports = {
//...
devServer: {
headers: {
'X-Custom-Foo': 'bar',
},
},
};
ä½ ä¹å¯ä»¥ä¼ éä¸ä¸ªæ°ç»ï¼
webpack.config.js
module.exports = {
//...
devServer: {
headers: [
{
key: 'X-Custom',
value: 'foo',
},
{
key: 'Y-Custom',
value: 'bar',
},
],
},
};
ä½ ä¹å¯ä»¥ä¼ éä¸ä¸ªå½æ°ï¼
module.exports = {
//...
devServer: {
headers: () => {
return { 'X-Bar': ['key1=value1', 'key2=value2'] };
},
},
};
boolean = false
object
When using the HTML5 History API, the index.html
page will likely have to be served in place of any 404
responses. Enable devServer.historyApiFallback
by setting it to true
:
webpack.config.js
module.exports = {
//...
devServer: {
historyApiFallback: true,
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --history-api-fallback
ç¦ç¨ï¼
npx webpack serve --no-history-api-fallback
éè¿æä¾ä¸ä¸ªå¯¹è±¡ï¼è¿ç§è¡ä¸ºå¯ä»¥éè¿å rewrites
è¿æ ·çé
置项è¿ä¸æ¥æ§å¶ï¼
webpack.config.js
module.exports = {
//...
devServer: {
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/views/landing.html' },
{ from: /^\/subpage/, to: '/views/subpage.html' },
{ from: /./, to: '/views/404.html' },
],
},
},
};
å¨ä½ çè·¯å¾ä¸ä½¿ç¨ç¹ï¼ä¸ Angular ç¸åï¼ï¼ä½ å¯è½éè¦ä½¿ç¨ disableDotRule
ï¼
webpack.config.js
module.exports = {
//...
devServer: {
historyApiFallback: {
disableDotRule: true,
},
},
};
For more options and information, see the connect-history-api-fallback documentation.
'local-ip' | 'local-ipv4' | 'local-ipv6'
string
æå®è¦ä½¿ç¨ç hostãå¦æä½ æ³è®©ä½ çæå¡å¨å¯ä»¥è¢«å¤é¨è®¿é®ï¼åè¿æ ·æå®ï¼
webpack.config.js
module.exports = {
//...
devServer: {
host: '0.0.0.0',
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --host 0.0.0.0
è¿ä¹éç¨äº IPv6ï¼
npx webpack serve --host ::
Specifying local-ip
as host will try to resolve the host option as your local IPv4
address if available, if IPv4
is not available it will try to resolve your local IPv6
address.
npx webpack serve --host local-ip
Specifying local-ipv4
as host will try to resolve the host option as your local IPv4
address.
npx webpack serve --host local-ipv4
æå® local-ipv6 ä½ä¸ºä¸»æºå°å°è¯å°ä¸»æºé项解æ为æ¨çæ¬å° IPv6 å°åã
npx webpack serve --host local-ipv6
'only'
boolean = true
å¯ç¨ webpack ç 模åçæ¿æ¢ ç¹æ§ï¼
webpack.config.js
module.exports = {
//...
devServer: {
hot: true,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --hot
å¦éç¦ç¨ï¼
npx webpack serve --no-hot
å¯ç¨æ¨¡åçæ¿æ¢åè½ï¼å¨æ建失败æ¶ä¸å·æ°é¡µé¢ä½ä¸ºåéï¼ä½¿ç¨ hot: 'only'
ï¼
webpack.config.js
module.exports = {
//...
devServer: {
hot: 'only',
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --hot only
true
string
The Unix socket to listen to (instead of a host
).
å°å
¶è®¾ç½®ä¸º true
å°ä¼çå¬ /your-os-temp-dir/webpack-dev-server.sock
ç socketï¼
webpack.config.js
module.exports = {
//...
devServer: {
ipc: true,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --ipc
ä½ ä¹å¯ä»¥è¿æ ·çå¬ä¸ä¸ªä¸åç socketï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
ipc: path.join(__dirname, 'my-socket.sock'),
},
};
boolean = true
é»è®¤æ
åµä¸ï¼å½çå¬å°æ件ååæ¶ dev-server å°ä¼éæ°å è½½æå·æ°é¡µé¢ãä¸ºäº liveReload
è½å¤çæï¼devServer.hot
é
置项å¿
é¡»ç¦ç¨æè
devServer.watchFiles
é
置项å¿
é¡»å¯ç¨ãå°å
¶è®¾ç½®ä¸º false
以ç¦ç¨ devServer.liveReload
ï¼
webpack.config.js
module.exports = {
//...
devServer: {
liveReload: false,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --live-reload
ç¦ç¨è¯¥åè½ï¼
npx webpack serve --no-live-reload
boolean = true
Tell dev-server to enable/disable magic HTML routes (routes corresponding to your webpack output, for example /main
for main.js
).
webpack.config.js
module.exports = {
//...
devServer: {
magicHtml: true,
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --magic-html
ç¦ç¨ï¼
npx webpack serve --no-magic-html
function (devServer)
æä¾æå¡å¨å é¨å¨ææå ¶ä»ä¸é´ä»¶ä¹åæ§è¡ èªå®ä¹ä¸é´ä»¶çè½å
webpack.config.js
module.exports = {
//...
devServer: {
onAfterSetupMiddleware: function (devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.get('/some/path', function (req, res) {
res.json({ custom: 'response' });
});
},
},
};
function (devServer)
æä¾å¨æå¡å¨å é¨æ§è¡ææå ¶ä»ä¸é´ä»¶ä¹åæ§è¡èªå®ä¹ä¸é´ä»¶çè½åã è¿å¯ä»¥ç¨æ¥å®ä¹èªå®ä¹å¤çç¨åºï¼ ä¾å¦:
webpack.config.js
module.exports = {
//...
devServer: {
onBeforeSetupMiddleware: function (devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.get('/some/path', function (req, res) {
res.json({ custom: 'response' });
});
},
},
};
function (devServer)
æä¾å¨ webpack-dev-server å¼å§çå¬ç«¯å£è¿æ¥æ¶æ§è¡èªå®ä¹å½æ°çè½åã
webpack.config.js
module.exports = {
//...
devServer: {
onListening: function (devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
const port = devServer.server.address().port;
console.log('Listening on port:', port);
},
},
};
boolean
string
object
[string, object]
åè¯ dev-server å¨æå¡å¨å·²ç»å¯å¨åæå¼æµè§å¨ã设置å
¶ä¸º true
以æå¼ä½ çé»è®¤æµè§å¨ã
webpack.config.js
module.exports = {
//...
devServer: {
open: true,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --open
å¦éç¦ç¨ï¼
npx webpack serve --no-open
å¨æµè§å¨ä¸æå¼æå®é¡µé¢ï¼
webpack.config.js
module.exports = {
//...
devServer: {
open: ['/my-page'],
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --open /my-page
å¨æµè§å¨ä¸æå¼å¤ä¸ªæå®é¡µé¢ï¼
webpack.config.js
module.exports = {
//...
devServer: {
open: ['/my-page', '/another-page'],
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --open /my-page --open /another-page
æä¾è¦ä½¿ç¨çæµè§å¨å称ï¼èä¸æ¯é»è®¤å称ï¼
webpack.config.js
module.exports = {
//...
devServer: {
open: {
app: {
name: 'google-chrome',
},
},
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --open-app-name 'google-chrome'
该对象æ¥æ¶ææ open é 置项ï¼
webpack.config.js
module.exports = {
//...
devServer: {
open: {
target: ['first.html', 'http://localhost:8080/second.html'],
app: {
name: 'google-chrome',
arguments: ['--incognito', '--new-window'],
},
},
},
};
'auto'
string
number
æå®çå¬è¯·æ±ç端å£å·ï¼
webpack.config.js
module.exports = {
//...
devServer: {
port: 8080,
},
};
éè¿å½ä»¤è¡ä½¿ç¨ï¼
npx webpack serve --port 8080
port
é
置项ä¸è½è®¾ç½®ä¸º null
æè
空å符串ï¼è¦æ³èªå¨ä½¿ç¨ä¸ä¸ªå¯ç¨ç«¯å£è¯·ä½¿ç¨ port: 'auto'
ï¼
webpack.config.js
module.exports = {
//...
devServer: {
port: 'auto',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --port auto
object
[object, function]
å½æ¥æåç¬ç API å端å¼åæå¡å¨å¹¶ä¸å¸æå¨åä¸åä¸åé API 请æ±æ¶ï¼ä»£çæäº URL å¯è½ä¼å¾æç¨ã
å¼åæå¡å¨ä½¿ç¨åè½å¼ºå¤§ç http-proxy-middleware 软件å
ã æ¥çå
¶ documentation äºè§£æ´å¤é«çº§ç¨æ³ã 请注æï¼http-proxy-middleware
çæäºåè½ä¸éè¦target
é®ï¼ä¾å¦ å®ç router
åè½ï¼ä½æ¯ä»ç¶éè¦å¨æ¤å¤çé
ç½®ä¸å
å«target
ï¼å¦åwebpack-dev-server
ä¸ä¼å°å
¶ä¼ éç» http-proxy-middleware
ã
使ç¨åç«¯å¨ localhost:3000
ä¸ï¼å¯ä»¥ä½¿ç¨å®æ¥å¯ç¨ä»£çï¼
webpack.config.js
module.exports = {
//...
devServer: {
proxy: {
'/api': 'http://localhost:3000',
},
},
};
ç°å¨ï¼å¯¹ /api/users
ç请æ±ä¼å°è¯·æ±ä»£çå° http://localhost:3000/api/users
ã
å¦æä¸å¸æä¼ é/api
ï¼åéè¦éåè·¯å¾ï¼
webpack.config.js
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: { '^/api': '' },
},
},
},
};
é»è®¤æ åµä¸ï¼å°ä¸æ¥åå¨ HTTPS ä¸è¿è¡ä¸è¯ä¹¦æ æçå端æå¡å¨ã å¦æéè¦ï¼å¯ä»¥è¿æ ·ä¿®æ¹é ç½®ï¼
webpack.config.js
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
secure: false,
},
},
},
};
ææ¶ä¸æ³ä»£çææå 容ã å¯ä»¥åºäºå½æ°çè¿åå¼ç»è¿ä»£çã
å¨è¯¥åè½ä¸ï¼å¯ä»¥è®¿é®è¯·æ±ï¼ååºå代çé项ã
null
æ undefined
以继ç»ä½¿ç¨ä»£çå¤ç请æ±ãfalse
ä¼ä¸ºè¯·æ±äº§ç 404 é误ãä¾å¦ã 对äºæµè§å¨è¯·æ±ï¼æ³è¦æä¾ HTML 页é¢ï¼ä½æ¯å¯¹äº API 请æ±ï¼æ³è¦ä»£çå®ã å¯ä»¥æ§è¡ä»¥ä¸æä½ï¼
webpack.config.js
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
bypass: function (req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
console.log('Skipping proxy for browser request.');
return '/index.html';
}
},
},
},
},
};
å¦ææ³å°å¤ä¸ªç¹å®è·¯å¾ä»£çå°åä¸ç®æ ï¼åå¯ä»¥ä½¿ç¨ä¸ä¸ªæå¤ä¸ªå¸¦æ context
å±æ§ç对象çæ°ç»ï¼
webpack.config.js
module.exports = {
//...
devServer: {
proxy: [
{
context: ['/auth', '/api'],
target: 'http://localhost:3000',
},
],
},
};
请注æï¼é»è®¤æ
åµä¸ä¸ä¼ä»£ç对 root ç请æ±ã è¦å¯ç¨æ ¹ä»£çï¼åºå° devMiddleware.index
é项æå®ä¸ºèåå¼ï¼
webpack.config.js
module.exports = {
//...
devServer: {
devMiddleware: {
index: false, // specify to enable root proxying
},
proxy: {
context: () => true,
target: 'http://localhost:1234',
},
},
};
é»è®¤æ
åµä¸ï¼ä»£çæ¶ä¼ä¿ç主æºå¤´çæ¥æºï¼å¯ä»¥å° changeOrigin
设置为 true
以è¦çæ¤è¡ä¸ºã å¨æäºæ
åµä¸ï¼ä¾å¦ä½¿ç¨ name-based virtual hosted sitesï¼å®å¾æç¨ã
webpack.config.js
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
},
},
},
};
'http' | 'https' | 'spdy'
string
object
å 许设置æå¡å¨åé 置项ï¼é»è®¤ä¸º 'http'ï¼ã
webpack.config.js
module.exports = {
//...
devServer: {
server: 'http',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --server-type http
使ç¨èªç¾åè¯ä¹¦éè¿ HTTPS
æä¾æå¡ï¼
webpack.config.js
module.exports = {
//...
devServer: {
server: 'https',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --server-type https
ä½¿ç¨ spdy 使ç¨èªç¾åè¯ä¹¦éè¿ HTTP/2
æä¾æå¡ï¼
webpack.config.js
module.exports = {
//...
devServer: {
server: 'spdy',
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --server-type spdy
使ç¨å¯¹è±¡è¯æ³æä¾èªå·±çè¯ä¹¦ï¼
webpack.config.js
module.exports = {
//...
devServer: {
server: {
type: 'https',
options: {
ca: './path/to/server.pem',
pfx: './path/to/server.pfx',
key: './path/to/server.key',
cert: './path/to/server.crt',
passphrase: 'webpack-dev-server',
requestCert: true,
},
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --server-type https --server-options-key ./path/to/server.key --server-options-cert ./path/to/server.crt --server-options-ca ./path/to/ca.pem --server-options-passphrase webpack-dev-server
It also allows you to set additional TLS options like minVersion
and you can directly pass the contents of respective files:
webpack.config.js
const fs = require('fs');
const path = require('path');
module.exports = {
//...
devServer: {
server: {
type: 'https',
options: {
minVersion: 'TLSv1.1',
key: fs.readFileSync(path.join(__dirname, './server.key')),
pfx: fs.readFileSync(path.join(__dirname, './server.pfx')),
cert: fs.readFileSync(path.join(__dirname, './server.crt')),
ca: fs.readFileSync(path.join(__dirname, './ca.pem')),
passphrase: 'webpack-dev-server',
requestCert: true,
},
},
},
};
boolean = true
å
è®¸å¨ SIGINT
å SIGTERM
ä¿¡å·æ¶å
³éå¼åæå¡å¨åéåºè¿ç¨ã
webpack.config.js
module.exports = {
//...
devServer: {
setupExitSignals: true,
},
};
function (middlewares, devServer)
æä¾æ§è¡èªå®ä¹å½æ°ååºç¨èªå®ä¹ä¸é´ä»¶çè½åã
webpack.config.js
module.exports = {
// ...
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.get('/setup-middleware/some/path', (_, response) => {
response.send('setup-middlewares option GET');
});
// å¦æä½ æ³å¨ææå
¶ä»ä¸é´ä»¶ä¹åè¿è¡ä¸ä¸ªä¸é´ä»¶æè
å½ä½ ä» `onBeforeSetupMiddleware` é
置项è¿ç§»æ¶ï¼
// å¯ä»¥ä½¿ç¨ `unshift` æ¹æ³
middlewares.unshift({
name: 'first-in-array',
// `path` æ¯å¯éç
path: '/foo/path',
middleware: (req, res) => {
res.send('Foo!');
},
});
// å¦æä½ æ³å¨ææå
¶ä»ä¸é´ä»¶ä¹åè¿è¡ä¸ä¸ªä¸é´ä»¶æè
å½ä½ ä» `onAfterSetupMiddleware` é
置项è¿ç§»æ¶ï¼
// å¯ä»¥ä½¿ç¨ `push` æ¹æ³
middlewares.push({
name: 'hello-world-test-one',
// `path` æ¯å¯éç
path: '/foo/bar',
middleware: (req, res) => {
res.send('Foo Bar!');
},
});
middlewares.push((req, res) => {
res.send('Hello World!');
});
return middlewares;
},
},
};
boolean
string
object
[string, object]
该é
置项å
许é
ç½®ä»ç®å½æä¾éææ件çé项ï¼é»è®¤æ¯ 'public' æ件夹ï¼ãå°å
¶è®¾ç½®ä¸º false
以ç¦ç¨ï¼
webpack.config.js
module.exports = {
//...
devServer: {
static: false,
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --static
ç¦ç¨è¯¥åè½ï¼
npx webpack serve --no-static
çå¬å个ç®å½ï¼
webpack.config.js
module.exports = {
// ...
devServer: {
static: ['assets'],
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --static assets
çå¬å¤ä¸ªéæèµæºç®å½ï¼
webpack.config.js
module.exports = {
// ...
devServer: {
static: ['assets', 'css'],
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --static assets --static css
string = path.join(process.cwd(), 'public')
åè¯æå¡å¨ä»åªéæä¾å
容ãåªæå¨ä½ å¸ææä¾éææ件æ¶æéè¦è¿æ ·åãstatic.publicPath
å°ä¼è¢«ç¨æ¥å³å®åºè¯¥ä»åªéæä¾ bundleï¼å¹¶å
·æä¼å
级ã
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
},
};
æä¾ä¸ä¸ªå¯¹è±¡æ°ç»ï¼ä»¥é²ä½ æå¤ä¸ªéæèµæºæ件夹ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: [
{
directory: path.join(__dirname, 'assets'),
},
{
directory: path.join(__dirname, 'css'),
},
],
},
};
object
å¯ä»¥é
ç½®ä» static.directory
æä¾éææ件çé«çº§é项ãå
³äºå¯ç¨é
置项å¯ä»¥æ件 Express ææ¡£ã
webpack.config.js
module.exports = {
//...
devServer: {
static: {
staticOptions: {
redirect: true,
},
},
},
};
string = '/'
[string]
åè¯æå¡å¨å¨åªä¸ª URL ä¸æä¾ static.directory
çå
容ãä¾å¦ä¸ºå¨ /serve-public-path-url/manifest.json
ä¸ç assets/manifest.json
æä¾æå¡ï¼ä½ çé
置项åºè¯¥åä¸é¢è¿æ ·ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'assets'),
publicPath: '/serve-public-path-url',
},
},
};
æä¾ä¸ä¸ªå¯¹è±¡æ°ç»ï¼ä»¥é²ä½ æå¤ä¸ªéææ件夹ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: [
{
directory: path.join(__dirname, 'assets'),
publicPath: '/serve-public-path-url',
},
{
directory: path.join(__dirname, 'css'),
publicPath: '/other-serve-public-path-url',
},
],
},
};
boolean
object = { icons: true }
åè¯å¼åæå¡å¨å¯ç¨åä½¿ç¨ serveIndex
ä¸é´ä»¶ã
serveIndex
ä¸é´ä»¶ä¼å¨æ¥ç没æ index.html æ件çç®å½æ¶çæç®å½å表ã
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'public'),
serveIndex: true,
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --static-serve-index
ç¦ç¨è¯¥åè½ï¼
npx webpack serve --no-static-serve-index
boolean
object
éè¿ static.directory
é
置项åè¯ dev-server çå¬æ件ãé»è®¤å¯ç¨ï¼æ件æ´æ¹å°è§¦åæ´ä¸ªé¡µé¢éæ°å è½½ãå¯ä»¥éè¿å° watch
设置为 false
ç¦ç¨ã
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'public'),
watch: false,
},
},
};
éè¿ CLI 使ç¨ï¼
npx webpack serve --static-watch
ç¦ç¨è¯¥åè½ï¼
npx webpack serve --no-static-watch
å¯ä»¥é
ç½®é«çº§é项以çå¬ static.directory
ä¸çéææ件ãå
³äºå¯ç¨é项å¯ä»¥æ¥ç chokidar
ææ¡£ã
webpack.config.js
const path = require('path');
module.exports = {
//...
devServer: {
static: {
directory: path.join(__dirname, 'public'),
watch: {
ignored: '*.txt',
usePolling: false,
},
},
},
};
string
object
[string, object]
该é 置项å è®¸ä½ é ç½® globs/directories/files æ¥çå¬æ件ååãä¾å¦ï¼
webpack.config.js
module.exports = {
//...
devServer: {
watchFiles: ['src/**/*.php', 'public/**/*'],
},
};
å¯ä»¥é
ç½®é«çº§é项æ¥çå¬æ件ãå
³äºå¯ç¨é项å¯ä»¥æ¥ç chokidar
ææ¡£ã
webpack.config.js
module.exports = {
//...
devServer: {
watchFiles: {
paths: ['src/**/*.php', 'public/**/*'],
options: {
usePolling: false,
},
},
},
};
false | 'sockjs' | 'ws'
string
function
object
该é 置项å 许æ们éæ©å½åç web-socket æå¡å¨æè æä¾èªå®ä¹ç web-socket æå¡å¨å®ç°ã
å½åé»è®¤æ¨¡å¼ä¸º 'ws'
ã该模å¼ä½¿ç¨ ws
ä½ä¸ºæå¡å¨ï¼å®¢æ·ç«¯ä¸ç WebSocketsã
webpack.config.js
module.exports = {
//...
devServer: {
webSocketServer: 'ws',
},
};
为äºå建ä¸ä¸ªèªå®ä¹æå¡ç«¯å®ç°ï¼å¯ä»¥å建ä¸ä¸ªæå± BaseServer
çç±»ã
ä½¿ç¨ CustomServer.js
导åºçç±»å®ç°èªå®ä¹ WebSocket 客æ·ç«¯å¹¶å
¼å®¹ws
æå¡ç«¯ï¼
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketTransport: 'ws',
},
webSocketServer: require.resolve('./CustomServer'),
},
};
使ç¨èªå®ä¹ä¸å ¼å®¹ç WebSocket 客æ·ç«¯ä»¥åæå¡ç«¯å®ç°ï¼
webpack.config.js
module.exports = {
//...
devServer: {
client: {
webSocketTransport: require.resolve('./CustomClient'),
},
webSocketServer: require.resolve('./CustomServer'),
},
};