• Stars
    star
    215
  • Rank 183,925 (Top 4 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

webpack dev&hot middleware for koa2

koa-webpack-middleware

npm version Circle CI js-standard-style

webpack-dev-middleware for koa2 with HMR(hot module replacement) supports.

Install

$ npm i koa-webpack-middleware -D

Depends

This middleware designd for koa2 ecosystem, make sure installed the right version:

npm i koa@next -S

Usage

See example/ for an example of usage.

import webpack from 'webpack'
import { devMiddleware, hotMiddleware } from 'koa-webpack-middleware'
import devConfig from './webpack.config.dev'
const compile = webpack(devConfig)
app.use(devMiddleware(compile, {
    // display no info to console (only warnings and errors)
    noInfo: false,

    // display nothing to the console
    quiet: false,

    // switch into lazy mode
    // that means no watching, but recompilation on every request
    lazy: true,

    // watch options (only lazy: false)
    watchOptions: {
        aggregateTimeout: 300,
        poll: true
    },

    // public path to bind the middleware to
    // use the same as in webpack
    publicPath: "/assets/",

    // custom headers
    headers: { "X-Custom-Header": "yes" },

    // options for formating the statistics
    stats: {
        colors: true
    }
}))
app.use(hotMiddleware(compile, {
  // log: console.log,
  // path: '/__webpack_hmr',
  // heartbeat: 10 * 1000
}))

HMR configure

  1. webpack plugins configure

    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    ]
  2. webpack entry configure

    $ npm i eventsource-polyfill -D
    entry: {
      'index': [
        // For old browsers
        'eventsource-polyfill',
        'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
        'index.js']
    },
  3. webpack loader configure

    $ npm i babel-preset-es2015 babel-preset-stage-0 -D
    {
      test: /\.js$/,
      loader: 'babel',
      query: {
        'presets': ['es2015', 'stage-0']
        }
      },
      include: './src'
    }

    HMR for react project

    $ npm i babel-preset-react babel-preset-react-hmre -D
    {
      test: /\.jsx?$/,
      loader: 'babel',
      query: {
        'presets': ['es2015', 'stage-0', 'react'],
        'env': {
          'development': {
            'presets': ['react-hmre']
          }
        }
      },
      include: './src'
    }
  4. put the code in your entry file to enable HMR

    React project do not need

    if (module.hot) {
      module.hot.accept()
    }

That's all, you're all set!

More Repositories

1

react-native-swiper

The best Swiper component for React Native.
JavaScript
10,416
star
2

ios-dev-flow

iOS 开发流程笔记
1,227
star
3

date

Javascript历法算法类
JavaScript
76
star
4

es6-study

JavaScript
36
star
5

caniuse_local

Instant, Offline, Non-breaking compatibility check of HTML5, CSS3, SVG, New JS API.
JavaScript
32
star
6

imagine

imagine collection
JavaScript
24
star
7

fe

🐶 Provide Font-End 2017 Tech Stack and standalone build environment
JavaScript
15
star
8

weixin-node

微信(wechat)签名等后端接口封装(nodejs)
JavaScript
11
star
9

react-i

A tiny (1 KB) <I icon='🍧' /> Component for React(-like).
JavaScript
10
star
10

react-native-practice

8
star
11

node-deploy

node 生产环境部署
JavaScript
7
star
12

react-native-alert

Use AlertIOS as a simple way just like native javascript.
JavaScript
7
star
13

echarts

fork echarts for fis.
JavaScript
6
star
14

suggest

suggest
JavaScript
5
star
15

react-native-unlock

JavaScript
4
star
16

soulema

Dockerize Google reverse proxy
Nginx
3
star
17

react-native-schedule

Objective-C
3
star
18

wunderlist

The awesome and free GTD Tool Wunderlist
JavaScript
2
star
19

leecade.github.com

JavaScript
2
star
20

docker-yii

Write and render smarty template with no environment depend.
PHP
2
star
21

ender-bootstrap

Build tool to assemble Ender-compatible Twitter Bootstrap 2.0 JS packages
JavaScript
2
star
22

javascript

JavaScript
2
star
23

jquery-mobile-starter

jquery mobile starter
JavaScript
1
star
24

keydown

keydown
JavaScript
1
star
25

webapp-debug

1
star
26

yuji

1
star
27

preact-boilerplate

JavaScript
1
star
28

_G

JavaScript
1
star
29

tower

test create from tower
1
star
30

fuck-comma

A safe way to remove redundant commas for IE6/7.
JavaScript
1
star
31

helloworld

流行语言的 HELLO WORLD
Assembly
1
star
32

try_git

1
star
33

react-native-html-view

1
star
34

mock-server

1
star
35

send2kindle

Send a file to your kindle in a single command.
JavaScript
1
star
36

dotvim

sync vim config
Vim Script
1
star