├── .gitignore ├── .npmignore ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.log 3 | .idea/ 4 | .ipr 5 | .iws 6 | *~ 7 | ~* 8 | *.diff 9 | *.patch 10 | *.bak 11 | .DS_Store 12 | Thumbs.db 13 | .project 14 | .*proj 15 | .svn/ 16 | *.swp 17 | *.swo 18 | *.pyc 19 | *.pyo 20 | .build 21 | node_modules 22 | _site 23 | sea-modules 24 | spm_modules 25 | .cache 26 | dist 27 | assets/**/*.css 28 | examples/**/*.css 29 | build/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components/ 2 | *.cfg 3 | node_modules/ 4 | nohup.out 5 | *.iml 6 | .idea/ 7 | .ipr 8 | .iws 9 | *~ 10 | ~* 11 | *.diff 12 | *.log 13 | *.patch 14 | *.bak 15 | .DS_Store 16 | Thumbs.db 17 | .project 18 | .*proj 19 | .svn/ 20 | *.swp 21 | out/ 22 | .build 23 | node_modules 24 | _site 25 | sea-modules 26 | spm_modules 27 | .cache 28 | dist 29 | build/ -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-webpack-hot-middleware", 3 | "version": "1.0.3", 4 | "description": "webpack hot reload middleware for koa", 5 | "keywords": [ 6 | "koa", 7 | "webpack", 8 | "middleware" 9 | ], 10 | "homepage": "http://github.com/dayAlone/koa-webpack-hot-middleware", 11 | "author": "andrey.slider@gmail.com", 12 | "repository": { 13 | "type": "git", 14 | "url": "git@github.com:dayAlone/koa-webpack-hot-middleware.git" 15 | }, 16 | "bugs": { 17 | "url": "http://github.com/dayAlone/koa-webpack-hot-middleware/issues" 18 | }, 19 | "licenses": "MIT", 20 | "dependencies": { 21 | "webpack-hot-middleware": "2.x" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var webpackHotMiddleware = require('webpack-hot-middleware'); 2 | 3 | function middleware(doIt, req, res) { 4 | var originalEnd = res.end; 5 | return function(done) { 6 | res.end = function() { 7 | originalEnd.apply(this, arguments); 8 | done(null, 0); 9 | }; 10 | doIt(req, res, function() { 11 | done(null, 1); 12 | }); 13 | }; 14 | } 15 | 16 | module.exports = function(compiler, option) { 17 | var action = webpackHotMiddleware(compiler, option); 18 | return function (ctx, next) { 19 | var nextStep = yield middleware(action, ctx.req, ctx.res); 20 | if (nextStep && next) { 21 | next(); 22 | } 23 | }; 24 | }; 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # koa-webpack-hot-middleware 2 | 3 | webpack hot reload middleware for koa 4 | 5 | ## Usage 6 | 7 | same with https://github.com/glenjamin/webpack-hot-middleware 8 | 9 | # Webpack Hot Middleware 10 | 11 | Webpack hot reloading using only [webpack-dev-middleware](http://webpack.github.io/docs/webpack-dev-middleware.html). This allows you to add hot reloading into an existing server without [webpack-dev-server](http://webpack.github.io/docs/webpack-dev-server.html). 12 | 13 | ## Installation & Usage 14 | 15 | See [example/](./example/) for an example of usage. 16 | 17 | First, install the npm module. 18 | 19 | ```sh 20 | npm install --save-dev koa-webpack-hot-middleware 21 | ``` 22 | 23 | Next, enable hot reloading in your webpack config: 24 | 25 | 1. Add the following three plugins to the `plugins` array: 26 | ```js 27 | plugins: [ 28 | new webpack.optimize.OccurenceOrderPlugin(), 29 | new webpack.HotModuleReplacementPlugin(), 30 | new webpack.NoErrorsPlugin() 31 | ] 32 | ``` 33 | Occurence ensures consistent build hashes, hot module replacement is 34 | somewhat self-explanatory, no errors is used to handle errors more cleanly. 35 | 36 | 3. Add `'webpack-hot-middleware/client'` into the `entry` array. 37 | This connects to the server to receive notifications when the bundle 38 | rebuilds and then updates your client bundle accordingly. 39 | 40 | Now add the middleware into your server: 41 | 42 | 1. Add `webpack-dev-middleware` the usual way 43 | ```js 44 | var webpack = require('webpack'); 45 | var webpackConfig = require('./webpack.config'); 46 | var compiler = webpack(webpackConfig); 47 | 48 | app.use(require("webpack-dev-middleware")(compiler, { 49 | noInfo: true, publicPath: webpackConfig.output.publicPath 50 | })); 51 | ``` 52 | 53 | 2. Add `koa-webpack-hot-middleware` attached to the same compiler instance 54 | ```js 55 | app.use(require("koa-webpack-hot-middleware")(compiler)); 56 | ``` 57 | 58 | And you're all set! 59 | 60 | ## License 61 | 62 | Copyright 2015 Glen Mailer. 63 | 64 | MIT Licened. 65 | --------------------------------------------------------------------------------