Skip to content

Commit dcb4e3d

Browse files
mrtnbrodershellscape
authored andcommitted
feat: deprecate setup in favor of before and after hooks (#1108)
fixes #869
1 parent 8bc6daa commit dcb4e3d

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

examples/node-api-middleware/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const server = new WebpackDevServer(compiler, {
99
stats: {
1010
colors: true
1111
},
12-
setup(app) {
12+
before(app) {
1313
app.use((req, res, next) => {
1414
console.log(`Using middleware for ${req.url}`);
1515
next();

lib/Server.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,19 @@ function Server(compiler, options) {
321321
}
322322
},
323323

324+
before: () => {
325+
if (typeof options.before === 'function') { options.before(app, this); }
326+
},
327+
324328
middleware: () => {
325329
// include our middleware to ensure it is able to handle '/index.html' request after redirect
326330
app.use(this.middleware);
327331
},
328332

333+
after: () => {
334+
if (typeof options.after === 'function') { options.after(app, this); }
335+
},
336+
329337
headers: () => {
330338
app.all('*', this.setContentHeaders.bind(this));
331339
},
@@ -335,11 +343,13 @@ function Server(compiler, options) {
335343
},
336344

337345
setup: () => {
346+
log('Using "setup" is deprecated and will be removed in the next major version. Please use the "before" and "after" hooks instead.');
347+
log('If "setup" was working fine for you until now, simply replace it with "before"');
338348
if (typeof options.setup === 'function') { options.setup(app, this); }
339349
}
340350
};
341351

342-
const defaultFeatures = ['setup', 'headers', 'middleware'];
352+
const defaultFeatures = ['before', 'setup', 'headers', 'middleware'];
343353
if (options.proxy) { defaultFeatures.push('proxy', 'middleware'); }
344354
if (contentBase !== false) { defaultFeatures.push('contentBaseFiles'); }
345355
if (options.watchContentBase) { defaultFeatures.push('watchContentBase'); }
@@ -351,6 +361,7 @@ function Server(compiler, options) {
351361
if (contentBase !== false) { defaultFeatures.push('contentBaseIndex'); }
352362
// compress is placed last and uses unshift so that it will be the first middleware used
353363
if (options.compress) { defaultFeatures.unshift('compress'); }
364+
if (options.after) { defaultFeatures.push('after'); }
354365

355366
(options.features || defaultFeatures).forEach((feature) => {
356367
features[feature]();

lib/optionsSchema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@
274274
"description": "Exposes the Express server to add custom middleware or routes.",
275275
"instanceof": "Function"
276276
},
277+
"before": {
278+
"description": "Exposes the Express server to add custom middleware or routes before webpack-dev-middleware will be added.",
279+
"instanceof": "Function"
280+
},
281+
"after": {
282+
"description": "Exposes the Express server to add custom middleware or routes after webpack-dev-middleware got added.",
283+
"instanceof": "Function"
284+
},
277285
"stats": {
278286
"description": "Decides what bundle information is displayed.",
279287
"anyOf": [

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Validation', () => {
5151
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
5252
'watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
5353
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
54-
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, ' +
54+
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, before?, after?, stats?, reporter?, ' +
5555
'noInfo?, quiet?, serverSideRender?, index?, log?, warn? }'
5656
]
5757
}];

0 commit comments

Comments
 (0)