Skip to content

Instantly share code, notes, and snippets.

@RyosukeCla
Last active December 4, 2017 18:15
Show Gist options
  • Save RyosukeCla/7a6c2edf149efd71c5215e70358d5d0f to your computer and use it in GitHub Desktop.
Save RyosukeCla/7a6c2edf149efd71c5215e70358d5d0f to your computer and use it in GitHub Desktop.
hot reloadを自前のexpressサーバーに組み込む ref: https://qiita.com/RyosukeCla/items/d0e02777d0b5cf9358e1
// any js code. eg vue/react/...
npm i --save-dev express webpack webpack-dev-middleware webpack-hot-middleware
npm i --save-dev pug
const webpack = require('webpack')
const webpackConfig = require('./webpack.config.js')
const compiler = webpack(webpackConfig)
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
}))
app.use(require("webpack-hot-middleware")(compiler))
html
header
title hot reload
body
#app
script(src="/dist/app.js")
const express = require('express')
const app = express()
const isProd = process.env.NODE_ENV === 'production'
if (!isProd) {
const webpack = require('webpack')
const webpackConfig = require('./webpack.config.js')
const compiler = webpack(webpackConfig)
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
}))
app.use(require("webpack-hot-middleware")(compiler))
}
}
app.set('view engine', 'pug')
app.use((req, res, next) => {
res.render('index')
})
const server = setup(app)
server.listen(8080)
const path = require('path')
const webpack = require('webpack')
const isProd = process.env.NODE_ENV === 'production'
const webpackConfig = {
// webpack config
entry: {
app: [path.resolve(__dirname, 'client.js')]
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/'
},
plugins: []
// ...
}
if (isProd) {
// ...
} else {
// dev
webpackConfig.entry.app.unshift('webpack-hot-middleware/client')
webpackConfig.plugins.push(
new webpack.HotModuleReplacementPlugin()
)
}
module.exports = webpackConfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment