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

Export storybook middleware #211

Merged
merged 1 commit into from
May 25, 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
30 changes: 3 additions & 27 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#!/usr/bin/env node

import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import getIndexHtml from './index.html';
import getIframeHtml from './iframe.html';
import express from 'express';
import program from 'commander';
import packageJson from '../../package.json';
import baseConfig from './webpack.config';
import loadConfig from './config';
import path from 'path';
import fs from 'fs';
import { getHeadHtml } from './utils';
import storybook from './middleware';
import packageJson from '../../package.json';

const logger = console;

Expand Down Expand Up @@ -53,24 +46,7 @@ if (program.staticDir) {
// Build the webpack configuration using the `baseConfig`
// custom `.babelrc` file and `webpack.config.js` files
const configDir = program.configDir || './.storybook';
const config = loadConfig('DEVELOPMENT', baseConfig, configDir);

const compiler = webpack(config);
const devMiddlewareOptions = {
noInfo: true,
publicPath: config.output.publicPath,
};
app.use(webpackDevMiddleware(compiler, devMiddlewareOptions));
app.use(webpackHotMiddleware(compiler));

app.get('/', function (req, res) {
res.send(getIndexHtml());
});

const headHtml = getHeadHtml(configDir);
app.get('/iframe.html', function (req, res) {
res.send(getIframeHtml(headHtml));
});
app.use(storybook(configDir));

app.listen(...listenAddr, function (error) {
if (error) {
Expand Down
35 changes: 35 additions & 0 deletions src/server/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Router } from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import baseConfig from './webpack.config';
import loadConfig from './config';
import getIndexHtml from './index.html';
import getIframeHtml from './iframe.html';
import { getHeadHtml } 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 compiler = webpack(config);
const devMiddlewareOptions = {
noInfo: true,
publicPath: config.output.publicPath,
};

const router = new Router();
router.use(webpackDevMiddleware(compiler, devMiddlewareOptions));
router.use(webpackHotMiddleware(compiler));

router.get('/', function (req, res) {
res.send(getIndexHtml());
});

const headHtml = getHeadHtml(configDir);
router.get('/iframe.html', function (req, res) {
res.send(getIframeHtml(headHtml));
});

return router;
}