Last active
December 4, 2017 18:15
-
-
Save RyosukeCla/7a6c2edf149efd71c5215e70358d5d0f to your computer and use it in GitHub Desktop.
hot reloadを自前のexpressサーバーに組み込む ref: https://qiita.com/RyosukeCla/items/d0e02777d0b5cf9358e1
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
// any js code. eg vue/react/... |
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
npm i --save-dev express webpack webpack-dev-middleware webpack-hot-middleware |
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
npm i --save-dev pug |
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
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)) |
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
html | |
header | |
title hot reload | |
body | |
#app | |
script(src="/dist/app.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
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) |
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
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