Code Monkey home page Code Monkey logo

koa-webpack-hot-middleware's Introduction

koa-webpack-hot-middleware

webpack hot reload middleware for koa

Usage

same with https://github.com/glenjamin/webpack-hot-middleware

Webpack Hot Middleware

Webpack hot reloading using only webpack-dev-middleware. This allows you to add hot reloading into an existing server without webpack-dev-server.

Installation & Usage

See example/ for an example of usage.

First, install the npm module.

npm install --save-dev koa-webpack-hot-middleware

Next, enable hot reloading in your webpack config:

  1. Add the following three plugins to the plugins array:

    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    ]

    Occurence ensures consistent build hashes, hot module replacement is somewhat self-explanatory, no errors is used to handle errors more cleanly.

  2. Add 'webpack-hot-middleware/client' into the entry array. This connects to the server to receive notifications when the bundle rebuilds and then updates your client bundle accordingly.

Now add the middleware into your server:

  1. Add webpack-dev-middleware the usual way

    var webpack = require('webpack');
    var webpackConfig = require('./webpack.config');
    var compiler = webpack(webpackConfig);
    
    app.use(require("webpack-dev-middleware")(compiler, {
        noInfo: true, publicPath: webpackConfig.output.publicPath
    }));
  2. Add koa-webpack-hot-middleware attached to the same compiler instance

    app.use(require("koa-webpack-hot-middleware")(compiler));

And you're all set!

License

Copyright 2015 Glen Mailer.

MIT Licened.

koa-webpack-hot-middleware's People

Contributors

dayalone avatar dzcpy avatar matthewmueller avatar mull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

koa-webpack-hot-middleware's Issues

webpack can't find `webpack-hot-middleware`

ERROR in multi app
Module not found: Error: Cannot resolve module 'webpack-hot-middleware/client' in /Users/iam4x/github/isomorphic-flux-boilerplate
 @ multi app

When I add in my entry array: webpack-hot-middleware/client?path=//${HOST}:${PORT}/__webpack_hmr

workaround: install as dep webpack-hot-middleware as well.

NodeJS 4.0.0 / latest babel && webpack

app.use() requires a generator function

I have the following code:

var path = require('path');
var koa = require('koa');
var app = module.exports = koa();
var webpack = require('webpack');
var webpackConfig = require('../webpack/webpack.config.dev');
var compiler = webpack(webpackConfig);

app.use(require("webpack-dev-middleware")(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
}));

app.use(require('webpack-hot-middleware')(compiler));

app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(3000, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}

console.log('Listening at http://localhost:3000');
});

The error is on the "app.use(require("webpack-dev-middleware")(compiler ..."

Any help please.

[Question] Hot to use this middleware

I need a little help for the understanding the concept of use.

My application has the server-side rendering for the basic frame of the page. I'm using a template engine (nunjucks).

for example for the route /login it uses my middleware

app.get('/login', function* () {
  yield this.render('login');
});

and the template looks something like this:

{% extends "main.njk" %}

{% block pageTitle %}Login{% endblock %}

{% block pageBodyContent %}
  <script type="application/javascript" src="/login.js"></script>
  <h1>I'm login page</h1>
{% endblock %}

login.js is bundled with webpack.

As i know, webpack-dev-server serves the files directly from the memory, instead of usual serving from my /build folder, which is served by koa-static module.

So basicly I have two general problems.

  1. If I just run the server, the rendered page can't load assests, because there is nothing in the /build folder. As i understood the login.js file exists only in the memory on that perticular moment.

Here how it looks on the client
asset

and on the server
server

It looks like it can't get the resources or it is a long-polling request.
If I run webpack in my terminal, it will make the files and will put them to the /build folder.
As result the sutiaton will change and, here comes my second problem.

2.When i have made the assests manualy, it seems that LR and HMR is working, but it can't get any updates from the server. It gets the notification "something changed, you need to update" (i supose from the route /__webpack_hmr) correctly, but then it askes the [publicPath][hash].hot-update.json from the server.

Here is the first run
Client
first_run_client
Server
first_run_server

And after some changes in the login.js
Client
second_client_run
and server
second_server_run

The [publicPath][hash].hot-update.json request seems has no middleware to handle it.
I've made a new middleware for it, maybe you guys can say me where I should provide it (see the comment in the code below)

app.use(function*(next) {
     let hotModuleUpdate = /\.hot-update\.json?$/.test(this.url);
     if (!hotModuleUpdate) yield next;
     //What should I do here. Maybe call `hotMiddleware()` ?
})

My OS - Ubuntu 16.04
Node - 6.3.1
Koa - 2@beta (as it required in the documentation)

Here is my webpack config and the module for koa to serve the case
hot-reload.txt
webpack.config.txt

Can't get it to work

I can't get it to work. The client shows [HMR] Waiting for update signal... But there are no emitted updates.

Below is code for Koa v2, but I've also tried it with v1 (without convert) but it appears to behave the same.

Can you spot a mistake?

var webpack = require('webpack')
var path = require('path')
var HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {

  entry: {
    app: ['webpack/hot/dev-server', './app/main.js']
  },

  output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '',
    filename: '[name].[hash].js'
  },

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /(node_modules|vendor)/
      }
    ]
  },

  resolve: {
    extensions: ['', '.js', '.json']
  },

  plugins: [
    new HtmlWebpackPlugin({
      title: 'Dagr Client',
      filename: 'index.html',
      template: 'template.html',
      inject: true
    }),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ]
}
import Koa from 'koa'
import convert from 'koa-convert'
import webpack from 'webpack'
import webpackConfig from './webpack.config'
import Router from 'koa-66'

const compiler = webpack(webpackConfig)
const app = new Koa()
const router = new Router()

app.use(router.routes())

app.use(convert(require('koa-webpack-dev-middleware')(compiler, {
  noInfo: true,
  publicPath: webpackConfig.output.publicPath
})))

app.use(convert(require('koa-webpack-hot-middleware')(compiler)))

app.listen(3000)
console.log('App listening on port', 3000)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.