Last active
April 23, 2017 13:31
-
-
Save jprivillaso/fd9c670fdbb72d20ddaab0aca46c0fa6 to your computer and use it in GitHub Desktop.
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
const path = require('path'); | |
const express = require('express'); | |
const webpack = require('webpack'); | |
const webpackDevMiddleware = require('webpack-dev-middleware'); | |
const webpackHotMiddleware = require('webpack-hot-middleware'); | |
const port = 3000; | |
const config = require(`./webpack.${process.env.NODE_ENV}.config.js`); | |
const isDevMode = process.env.NODE_ENV !== 'production'; | |
let app = express(); | |
if (isDevMode) { | |
const compiler = webpack(config); | |
const middleware = webpackDevMiddleware(compiler, { | |
publicPath: config.output.publicPath, | |
contentBase: 'src', | |
stats: { | |
colors: true, | |
hash: false, | |
timings: true, | |
chunks: false, | |
chunkModules: false, | |
modules: false | |
} | |
}); | |
app.use(middleware); | |
app.use(webpackHotMiddleware(compiler)); | |
// Configuramos esse route para ele retornar o arquivo index.html, | |
// que foi gerado pelo webpack | |
app.get('*', function response(req, res) { | |
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html'))); | |
res.end(); | |
}); | |
} else { | |
// Configuramos esse route para produção, veja que estamos carregando | |
// o arquivo da pasta dist dessa vez. | |
// Esse arquivo index.html será gerado pelo webpack como o plugin HtmlWebpackPlugin | |
app.use(express.static(__dirname + '/dist')); | |
app.get('*', function response(req, res) { | |
res.sendFile(path.join(__dirname, 'dist/index.html')); | |
}); | |
} | |
// Server escutando na porta 3000 | |
app.listen(port, function(error) { | |
if (error) { | |
console.error(error); | |
} else { | |
console.info('==> 🌎 Listening on port %s. Open up ' + | |
'http://localhost:%s/ in your browser.', port, port); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment