Skip to content

Commit d73213a

Browse files
authored
feat: added the watchFiles option (#3136)
1 parent 03a344b commit d73213a

File tree

9 files changed

+415
-3
lines changed

9 files changed

+415
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ yarn.lock
1313

1414
.eslintcache
1515

16+
test/fixtures/contentbase-config/public/assets/non-exist.txt
1617
test/fixtures/reload-config/main.css
1718
test/fixtures/reload-config-2/main.css
1819
!/test/fixtures/contentbase-config/public/node_modules

bin/cli-flags.js

+11
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,16 @@ module.exports = {
228228
multiple: true,
229229
negative: true,
230230
},
231+
{
232+
name: 'watch-files',
233+
type: String,
234+
configs: [
235+
{
236+
type: 'string',
237+
},
238+
],
239+
description: 'Watch static files for file changes',
240+
multiple: true,
241+
},
231242
],
232243
};

lib/Server.js

+22
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Server {
7171
// Should be after `webpack-dev-middleware`, otherwise other middlewares might rewrite response
7272
routes(this);
7373

74+
this.setupWatchFiles();
7475
this.setupFeatures();
7576
this.setupHttps();
7677
this.createServer();
@@ -349,6 +350,27 @@ class Server {
349350
this.options.onBeforeSetupMiddleware(this);
350351
}
351352

353+
setupWatchFiles() {
354+
if (this.options.watchFiles) {
355+
const { watchFiles } = this.options;
356+
357+
if (typeof watchFiles === 'string') {
358+
this.watchFiles(watchFiles, {});
359+
} else if (Array.isArray(watchFiles)) {
360+
watchFiles.forEach((file) => {
361+
if (typeof file === 'string') {
362+
this.watchFiles(file, {});
363+
} else {
364+
this.watchFiles(file.paths, file.options || {});
365+
}
366+
});
367+
} else {
368+
// { paths: [...], options: {} }
369+
this.watchFiles(watchFiles.paths, watchFiles.options || {});
370+
}
371+
}
372+
}
373+
352374
setupMiddleware() {
353375
this.app.use(this.middleware);
354376
}

lib/options.json

+55-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@
103103
]
104104
}
105105
}
106+
},
107+
"WatchFilesString": {
108+
"type": "string",
109+
"minLength": 1
110+
},
111+
"WatchFilesObject": {
112+
"type": "object",
113+
"properties": {
114+
"paths": {
115+
"anyOf": [
116+
{
117+
"type": "string",
118+
"minLength": 1
119+
},
120+
{
121+
"type": "array",
122+
"items": {
123+
"type": "string",
124+
"minLength": 1
125+
}
126+
}
127+
]
128+
},
129+
"options": {
130+
"type": "object",
131+
"additionalProperties": true
132+
}
133+
},
134+
"additionalProperties": false
106135
}
107136
},
108137
"properties": {
@@ -424,6 +453,29 @@
424453
"enum": ["sockjs", "ws"]
425454
}
426455
]
456+
},
457+
"watchFiles": {
458+
"anyOf": [
459+
{
460+
"$ref": "#/definitions/WatchFilesString"
461+
},
462+
{
463+
"$ref": "#/definitions/WatchFilesObject"
464+
},
465+
{
466+
"type": "array",
467+
"items": {
468+
"anyOf": [
469+
{
470+
"$ref": "#/definitions/WatchFilesString"
471+
},
472+
{
473+
"$ref": "#/definitions/WatchFilesObject"
474+
}
475+
]
476+
}
477+
}
478+
]
427479
}
428480
},
429481
"errorMessage": {
@@ -443,13 +495,14 @@
443495
"onAfterSetupMiddleware": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserverafter)",
444496
"onBeforeSetupMiddleware": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserverbefore)",
445497
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
446-
"open": "should be {Boolean|String|(STRING | Object)[]} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
498+
"open": "should be {Boolean|String|(String | Object)[]} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
447499
"port": "should be {Number|String|Null} (https://webpack.js.org/configuration/dev-server/#devserverport)",
448500
"proxy": "should be {Object|Array} (https://webpack.js.org/configuration/dev-server/#devserverproxy)",
449501
"public": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserverpublic)",
450502
"setupExitSignals": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserversetupexitsignals)",
451503
"static": "should be {Boolean|String|Object|Array} (https://webpack.js.org/configuration/dev-server/#devserverstatic)",
452-
"transportMode": "should be {String|Object} (https://webpack.js.org/configuration/dev-server/#devservertransportmode)"
504+
"transportMode": "should be {String|Object} (https://webpack.js.org/configuration/dev-server/#devservertransportmode)",
505+
"watchFiles": "should be {String|Array|Object} (https://webpack.js.org/configuration/dev-server/#devserverwatchfiles)"
453506
}
454507
},
455508
"additionalProperties": false

test/__snapshots__/Validation.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ exports[`Validation validation should fail validation for invalid \`static\` con
4343
exports[`Validation validation should fail validation for no additional properties 1`] = `
4444
"Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
4545
- configuration has an unknown property 'additional'. These properties are valid:
46-
object { bonjour?, client?, compress?, dev?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, public?, setupExitSignals?, static?, transportMode? }"
46+
object { bonjour?, client?, compress?, dev?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, public?, setupExitSignals?, static?, transportMode?, watchFiles? }"
4747
`;

test/fixtures/contentbase-config/public/assets/other.txt

Whitespace-only changes.

test/options.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ describe('options', () => {
401401
},
402402
],
403403
},
404+
watchFiles: {
405+
success: [
406+
'dir',
407+
['one-dir', 'two-dir'],
408+
{ paths: ['dir'] },
409+
{ paths: ['dir'], options: { usePolling: true } },
410+
[{ paths: ['one-dir'] }, 'two-dir'],
411+
],
412+
failure: [false, 123],
413+
},
404414
};
405415

406416
Object.keys(cases).forEach((key) => {

test/ports-map.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const portsList = {
4444
bundle: 1,
4545
ModuleFederation: 1,
4646
'setupExitSignals-option': 1,
47+
'watchFiles-option': 1,
4748
};
4849

4950
let startPort = 8089;

0 commit comments

Comments
 (0)