Skip to content

Commit

Permalink
Merge pull request #115 from storybooks/middleware
Browse files Browse the repository at this point in the history
Support custom middleware
  • Loading branch information
Muhammed Thanish authored Dec 7, 2016
2 parents d53e899 + 2ad454f commit f294122
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/server/middleware.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -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));

Expand Down

0 comments on commit f294122

Please sign in to comment.