Created
May 1, 2016 16:59
-
-
Save bkonkle/c0f4b1ae728a96732c019a2d1b4d646e to your computer and use it in GitHub Desktop.
dev-server.js
This file contains hidden or 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
| /* eslint-disable no-console,prefer-template */ | |
| const chalk = require('chalk') | |
| const devMiddleware = require('webpack-dev-middleware') | |
| const express = require('express') | |
| const hotMiddleware = require('webpack-hot-middleware') | |
| const path = require('path') | |
| const webpack = require('webpack') | |
| function run() { | |
| const PORT = process.env.PORT || 3000 | |
| const DOMAIN = 'localhost' | |
| const URL = `http://${DOMAIN}:${PORT}` | |
| const app = express() | |
| const config = require('../webpack.' + process.env.NODE_ENV) | |
| const compiler = webpack(config) | |
| app.use(devMiddleware(compiler, { | |
| publicPath: config.output.publicPath, | |
| stats: {colors: true, chunks: false}, | |
| watchOptions: {aggregateTimeout: 1000}, | |
| })) | |
| app.use(hotMiddleware(compiler)) | |
| app.get('*', (req, res) => { | |
| const memoryFs = compiler.outputFileSystem | |
| const index = path.join(config.output.path, 'index.html') | |
| const html = memoryFs.readFileSync(index) | |
| res.end(html) | |
| }) | |
| app.listen(PORT, 'localhost', err => { | |
| if (err) throw new Error(err) | |
| console.log(`[dev-server] Webpack dev server listening at: ${chalk.green(URL)}`) | |
| }) | |
| } | |
| module.exports = run | |
| if (require.main === module) { | |
| run() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment