forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request react-bootstrap#628 from react-bootstrap/docs-dev-…
…workflow Use webpack-dev-server instead of webpack-dev-middleware
- Loading branch information
Showing
4 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env babel-node | ||
/* eslint no-process-exit: 0 */ | ||
import 'colors'; | ||
import portfinder from 'portfinder'; | ||
import { exec } from 'child-process-promise'; | ||
|
||
portfinder.basePort = 4000; | ||
|
||
const SIGINT = 'SIGINT'; | ||
let webpackDevServer; | ||
let docsServer; | ||
|
||
function output(prefix, message) { | ||
let formattedMessage = message.trim().split('\n') | ||
.reduce((acc, line) => `${acc}${ acc !== '' ? '\n' : '' }${prefix} ${line}`, ''); | ||
|
||
console.log(formattedMessage); | ||
} | ||
|
||
function listen({stdout, stderr}, name) { | ||
stdout.on('data', data => output(`[${name}] `.grey, data)); | ||
stderr.on('data', data => output(`[${name}] `.grey, data)); | ||
} | ||
|
||
function shutdown() { | ||
if (webpackDevServer) { | ||
webpackDevServer.kill(SIGINT); | ||
} | ||
if (docsServer) { | ||
docsServer.kill(SIGINT); | ||
} | ||
} | ||
|
||
function catchExec(name, err) { | ||
if (err.killed) { | ||
console.log('Shutdown: '.cyan + name.green); | ||
} else { | ||
console.log(`${name} -- Failed`.red); | ||
console.log(err.toString().red); | ||
} | ||
shutdown(); | ||
} | ||
|
||
console.log('Starting docs in Development mode'.cyan); | ||
|
||
process.on(SIGINT, shutdown); | ||
|
||
portfinder.getPorts(2, {}, (portFinderErr, [docsPort, webpackPort]) => { | ||
if (portFinderErr) { | ||
console.log('Failed to acquire ports'.red); | ||
process.exit(1); | ||
} | ||
|
||
exec(`webpack-dev-server --quiet --config webpack.docs.js --color --port ${webpackPort}`) | ||
.progress(childProcess => { | ||
listen(childProcess, 'webpack-dev-server'); | ||
webpackDevServer = childProcess; | ||
return; | ||
}) | ||
.then(() => console.log('Shutdown: '.cyan + 'webpack-dev-server'.green)) | ||
.catch(err => catchExec('webpack-dev-server', err)); | ||
|
||
exec('nodemon --exec babel-node docs/server.js', { | ||
env: { | ||
PORT: docsPort, | ||
WEBPACK_DEV_PORT: webpackPort, | ||
...process.env | ||
} | ||
}) | ||
.progress(childProcess => { | ||
listen(childProcess, 'docs-server'); | ||
docsServer = childProcess; | ||
return; | ||
}) | ||
.then(() => console.log('Shutdown: '.cyan + 'docs-server'.green)) | ||
.catch(err => catchExec('docs-server', err)); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,40 @@ | ||
import 'colors'; | ||
import React from 'react'; | ||
import express from 'express'; | ||
import path from 'path'; | ||
import webpack from 'webpack'; | ||
import webpackMiddleware from 'webpack-dev-middleware'; | ||
import webpackConfigBuilder from '../webpack/webpack.config'; | ||
import Router from 'react-router'; | ||
import routes from './src/Routes'; | ||
import httpProxy from 'http-proxy'; | ||
|
||
const development = process.env.NODE_ENV !== 'production'; | ||
const port = process.env.PORT || 4000; | ||
|
||
let app = express(); | ||
|
||
if (development) { | ||
let webpackConfig = webpackConfigBuilder({ | ||
development: development, | ||
docs: true | ||
}); | ||
let publicPath = webpackConfig.output.publicPath; | ||
let proxy = httpProxy.createProxyServer(); | ||
let webpackPort = process.env.WEBPACK_DEV_PORT; | ||
let target = `http://localhost:${webpackPort}`; | ||
|
||
webpackConfig.output.path = '/'; | ||
webpackConfig.output.publicPath = undefined; | ||
webpackConfig.module.noParse = /babel-core\/browser/; | ||
app.get('/assets/*', function (req, res) { | ||
proxy.web(req, res, { target }); | ||
}); | ||
|
||
app = app | ||
.use(webpackMiddleware(webpack(webpackConfig), { | ||
noInfo: false, | ||
publicPath: publicPath, | ||
stats: { | ||
colors: true | ||
} | ||
})) | ||
.use(function renderApp(req, res) { | ||
Router.run(routes, req.url, Handler => { | ||
let html = React.renderToString(<Handler />); | ||
res.send(html); | ||
}); | ||
app.use(function renderApp(req, res) { | ||
Router.run(routes, req.url, Handler => { | ||
let html = React.renderToString(<Handler />); | ||
res.send(html); | ||
}); | ||
}); | ||
|
||
proxy.on('error', function(e) { | ||
console.log('Could not connect to webpack proxy'.red); | ||
console.log(e.toString().red); | ||
}); | ||
} else { | ||
app = app | ||
.use(express.static(path.join(__dirname, '../docs-built'))); | ||
app.use(express.static(path.join(__dirname, '../docs-built'))); | ||
} | ||
|
||
app | ||
.listen(4000, function () { | ||
console.log('Server started at http://localhost:4000'); | ||
}); | ||
app.listen(port, function () { | ||
console.log(`Server started at http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters