Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/middleware: open router handler for developers. #435

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/server/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
});
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 () {};
}