Skip to content

Commit e75b597

Browse files
committed
docs: devServer.static
1 parent a5a052f commit e75b597

File tree

1 file changed

+194
-2
lines changed

1 file changed

+194
-2
lines changed

src/content/configuration/dev-server.md

Lines changed: 194 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ npx webpack serve --https-key ./path/to/server.key --https--cert ./path/to/serve
585585

586586
## `devServer.headers`
587587

588-
`object`
588+
`function` `object`
589589

590590
Adds headers to all responses:
591591

@@ -663,7 +663,7 @@ For more options and information, see the [connect-history-api-fallback](https:/
663663

664664
## devServer.host
665665

666-
`string`
666+
`'local-ip' | 'local-ipv4' | 'local-ipv6'` `string`
667667

668668
Specify a host to use. If you want your server to be accessible externally, specify it like this:
669669

@@ -1083,16 +1083,208 @@ module.exports = {
10831083

10841084
`boolean` `string` `[string]` `object` `[object]`
10851085

1086+
This options allows to configure options for serving static files from directory (by default 'public' directory). To disable set it to `false`:
1087+
1088+
**webpack.config.js**
1089+
1090+
```javascript
1091+
module.exports = {
1092+
//...
1093+
devServer: {
1094+
static: false,
1095+
},
1096+
};
1097+
```
1098+
1099+
Usage via CLI:
1100+
1101+
```bash
1102+
npx webpack serve --static
1103+
```
1104+
1105+
To disable:
1106+
1107+
```bash
1108+
npx webpack serve --no-static
1109+
```
1110+
10861111
### directory
10871112

1113+
`string = path.join(process.cwd(), 'public')`
1114+
1115+
Tell the server where to serve the content from. This is only necessary if you want to serve static files. [static.publicPath](#publicpath) will be used to determine where the bundles should be served from, and takes precedence.
1116+
1117+
**webpack.config.js**
1118+
1119+
```javascript
1120+
const path = require('path');
1121+
1122+
module.exports = {
1123+
//...
1124+
devServer: {
1125+
static: {
1126+
directory: path.join(__dirname, 'public'),
1127+
},
1128+
},
1129+
};
1130+
```
1131+
1132+
T> It is recommended to use an absolute path.
1133+
10881134
### staticOptions
10891135

1136+
It is possible to configure advanced options for serving static files from [static.directory](#directory). See the [Express documentation](http://expressjs.com/en/4x/api.html#express.static) for the possible options.
1137+
1138+
**webpack.config.js**
1139+
1140+
```javascript
1141+
module.exports = {
1142+
//...
1143+
devServer: {
1144+
static: {
1145+
staticOptions: {
1146+
redirect: true,
1147+
},
1148+
},
1149+
},
1150+
};
1151+
```
1152+
10901153
### publicPath
10911154

1155+
`string = '/'` `string`
1156+
1157+
Tell the server at which URL to serve [`static.directory`](#directory) content. For example to serve a file `assets/manifest.json` at `/serve-public-path-url/manifest.json`, your configurations should be as following:
1158+
1159+
**webpack.config.js**
1160+
1161+
```javascript
1162+
const path = require('path');
1163+
1164+
module.exports = {
1165+
//...
1166+
devServer: {
1167+
static: {
1168+
directory: path.join(__dirname, 'assets'),
1169+
publicPath: '/serve-public-path-url',
1170+
},
1171+
},
1172+
};
1173+
```
1174+
1175+
Provide an array of objects in case you have multiple static folders:
1176+
1177+
**webpack.config.js**
1178+
1179+
```javascript
1180+
const path = require('path');
1181+
1182+
module.exports = {
1183+
//...
1184+
devServer: {
1185+
static: [
1186+
{
1187+
directory: path.join(__dirname, 'assets'),
1188+
publicPath: '/serve-public-path-url',
1189+
},
1190+
{
1191+
directory: path.join(__dirname, 'css'),
1192+
publicPath: '/other-serve-public-path-url',
1193+
},
1194+
],
1195+
},
1196+
};
1197+
```
1198+
10921199
### serveIndex
10931200

1201+
`boolean` `object = { icons: true }`
1202+
1203+
Tell dev-server to use [`serveIndex`](https://github.com/expressjs/serve-index) middleware when enabled.
1204+
1205+
[`serveIndex`](https://github.com/expressjs/serve-index) middleware generates directory listings on viewing directories that don't have an `index.html` file.
1206+
1207+
**webpack.config.js**
1208+
1209+
```javascript
1210+
const path = require('path');
1211+
1212+
module.exports = {
1213+
//...
1214+
devServer: {
1215+
static: {
1216+
directory: path.join(__dirname, 'public'),
1217+
serveIndex: true,
1218+
},
1219+
},
1220+
};
1221+
```
1222+
1223+
Usage via CLI:
1224+
1225+
```bash
1226+
npx webpack serve --static-serve-index
1227+
```
1228+
1229+
To disable:
1230+
1231+
```bash
1232+
npx webpack serve --no-static-serve-index
1233+
```
1234+
10941235
### watch
10951236

1237+
`boolean` `object`
1238+
1239+
Tell dev-server to watch the files served by the [static.directory]($directory) option. It is disabled by default. When enabled, file changes will trigger a full page reload.
1240+
1241+
**webpack.config.js**
1242+
1243+
```javascript
1244+
const path = require('path');
1245+
1246+
module.exports = {
1247+
//...
1248+
devServer: {
1249+
static: {
1250+
directory: path.join(__dirname, 'public'),
1251+
watch: true,
1252+
},
1253+
},
1254+
};
1255+
```
1256+
1257+
Usage via CLI:
1258+
1259+
```bash
1260+
npx webpack serve --static-watch
1261+
```
1262+
1263+
To disable:
1264+
1265+
```bash
1266+
npx webpack serve --no-static-watch
1267+
```
1268+
1269+
It is possible to configure advanced options for watching static files from [static.directory](#directory). See the [chokidar](https://github.com/paulmillr/chokidar) documentation for the possible options.
1270+
1271+
```javascript
1272+
const path = require('path');
1273+
1274+
module.exports = {
1275+
//...
1276+
devServer: {
1277+
static: {
1278+
directory: path.join(__dirname, 'public'),
1279+
watch: {
1280+
ignored: '*.txt',
1281+
usePolling: false,
1282+
},
1283+
},
1284+
},
1285+
};
1286+
```
1287+
10961288
## devServer.watchFiles
10971289

10981290
## devServer.webSocketServer

0 commit comments

Comments
 (0)