Skip to content

Commit

Permalink
feat: deprecate setup in favor of before and after hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnbroder committed Sep 21, 2017
1 parent 3e24ac4 commit fd842ed
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/node-api-middleware/server.js
Expand Up @@ -9,7 +9,7 @@ const server = new WebpackDevServer(compiler, {
stats: {
colors: true
},
setup(app) {
before(app) {
app.use((req, res, next) => {
console.log(`Using middleware for ${req.url}`);
next();
Expand Down
16 changes: 13 additions & 3 deletions lib/Server.js
Expand Up @@ -321,11 +321,19 @@ function Server(compiler, options) {
}
},

before: () => {
if (typeof options.before === 'function') { options.before(app, this); }
},

middleware: () => {
// include our middleware to ensure it is able to handle '/index.html' request after redirect
app.use(this.middleware);
},

after: () => {
if (typeof options.after === 'function') { options.after(app, this); }
},

headers: () => {
app.all('*', this.setContentHeaders.bind(this));
},
Expand All @@ -335,16 +343,18 @@ function Server(compiler, options) {
},

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

const defaultFeatures = ['setup', 'headers', 'middleware'];
if (options.proxy) { defaultFeatures.push('proxy', 'middleware'); }
const defaultFeatures = ['before', 'setup', 'headers', 'middleware', 'after'];
if (options.proxy) { defaultFeatures.push('proxy', 'middleware', 'after'); }
if (contentBase !== false) { defaultFeatures.push('contentBaseFiles'); }
if (options.watchContentBase) { defaultFeatures.push('watchContentBase'); }
if (options.historyApiFallback) {
defaultFeatures.push('historyApiFallback', 'middleware');
defaultFeatures.push('historyApiFallback', 'middleware', 'after');
if (contentBase !== false) { defaultFeatures.push('contentBaseFiles'); }
}
defaultFeatures.push('magicHtml');
Expand Down
8 changes: 8 additions & 0 deletions lib/optionsSchema.json
Expand Up @@ -274,6 +274,14 @@
"description": "Exposes the Express server to add custom middleware or routes.",
"instanceof": "Function"
},
"before": {
"description": "Exposes the Express server to add custom middleware or routes before webpack-dev-middleware will be added.",
"instanceof": "Function"
},
"after": {
"description": "Exposes the Express server to add custom middleware or routes after webpack-dev-middleware got added.",
"instanceof": "Function"
},
"stats": {
"description": "Decides what bundle information is displayed.",
"anyOf": [
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Expand Up @@ -51,7 +51,7 @@ describe('Validation', () => {
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
'watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, ' +
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, before?, after? stats?, reporter?, ' +
'noInfo?, quiet?, serverSideRender?, index?, log?, warn? }'
]
}];
Expand Down

0 comments on commit fd842ed

Please sign in to comment.