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 26, 2017
1 parent 33bef0d commit febf76f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 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
13 changes: 12 additions & 1 deletion 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,11 +343,13 @@ 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'];
const defaultFeatures = ['before', 'setup', 'headers', 'middleware'];
if (options.proxy) { defaultFeatures.push('proxy', 'middleware'); }
if (contentBase !== false) { defaultFeatures.push('contentBaseFiles'); }
if (options.watchContentBase) { defaultFeatures.push('watchContentBase'); }
Expand All @@ -351,6 +361,7 @@ function Server(compiler, options) {
if (contentBase !== false) { defaultFeatures.push('contentBaseIndex'); }
// compress is placed last and uses unshift so that it will be the first middleware used
if (options.compress) { defaultFeatures.unshift('compress'); }
if (options.after) { defaultFeatures.push('after'); }

(options.features || defaultFeatures).forEach((feature) => {
features[feature]();
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 febf76f

Please sign in to comment.