Skip to content

Commit

Permalink
Merge branch 'bshy522-feature/open-middleware'
Browse files Browse the repository at this point in the history
  • Loading branch information
thani-sh committed Dec 5, 2016
2 parents eb86b3a + d641ddb commit 89f2ab2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/server/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import getBaseConfig 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 `getBaseConfig`
// custom `.babelrc` file and `webpack.config.js` files
const config = loadConfig('DEVELOPMENT', getBaseConfig(), configDir);
const middlewareFn = getMiddleware(configDir);

// remove the leading '/'
let publicPath = config.output.publicPath;
Expand All @@ -30,6 +31,9 @@ export default function (configDir) {
router.use(webpackDevMiddleware(compiler, devMiddlewareOptions));
router.use(webpackHotMiddleware(compiler));

// custom middleware
middlewareFn(router);

router.get('/', function (req, res) {
res.send(getIndexHtml({ publicPath }));
});
Expand Down
12 changes: 12 additions & 0 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {};
}

0 comments on commit 89f2ab2

Please sign in to comment.