diff --git a/src/server/middleware.js b/src/server/middleware.js index 97e50604b12d..5a594cf48b1b 100644 --- a/src/server/middleware.js +++ b/src/server/middleware.js @@ -1,3 +1,5 @@ +import fs from 'fs'; +import path from 'path'; import { Router } from 'express'; import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; @@ -6,6 +8,18 @@ import baseConfig from './config/webpack.config'; import loadConfig from './config'; import getIndexHtml from './index.html'; +function getMiddleware(configDir) { + const middlewarePath = path.resolve(configDir, 'middleware.js'); + if (fs.existsSync(middlewarePath)) { + let middlewareModule = require(middlewarePath); + if (middlewareModule.__esModule) { + middlewareModule = middlewareModule.default; + } + return middlewareModule; + } + return function () {}; +} + export default function (configDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files @@ -25,6 +39,9 @@ export default function (configDir) { }; const router = new Router(); + const middlewareFn = getMiddleware(configDir); + middlewareFn(router); + router.use(webpackDevMiddleware(compiler, devMiddlewareOptions)); router.use(webpackHotMiddleware(compiler));