diff --git a/src/server/middleware.js b/src/server/middleware.js index f8ef41c85672..e1975f21389a 100644 --- a/src/server/middleware.js +++ b/src/server/middleware.js @@ -6,12 +6,13 @@ import baseConfig from './config/webpack.config'; import loadConfig from './config'; import getIndexHtml from './index.html'; import getIframeHtml from './iframe.html'; -import { getHeadHtml } from './utils'; +import { getHeadHtml, getMiddleware } from './utils'; export default function (configDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files const config = loadConfig('DEVELOPMENT', baseConfig, configDir); + const middlewareFn = getMiddleware(configDir); // remove the leading '/' let publicPath = config.output.publicPath; @@ -30,6 +31,8 @@ export default function (configDir) { router.use(webpackDevMiddleware(compiler, devMiddlewareOptions)); router.use(webpackHotMiddleware(compiler)); + middlewareFn(router); + router.get('/', function (req, res) { res.send(getIndexHtml(publicPath)); }); diff --git a/src/server/utils.js b/src/server/utils.js index 07f67fc9ea91..dba71980c852 100644 --- a/src/server/utils.js +++ b/src/server/utils.js @@ -24,3 +24,15 @@ export function getEnvConfig(program, configEnv) { } }); } + +export function getMiddleware(configDir) { + const middlewarePath = path.resolve(configDir, 'middleware.js'); + if (fs.existsSync(middlewarePath)) { + let middlewareModule = require(middlewarePath); // eslint-disable-line global-require + if (middlewareModule.__esModule) { + middlewareModule = middlewareModule.default; + } + return middlewareModule; + } + return function () {}; +}