# kwm

> webpack dev&amp;hot middleware for koa2

Latest version **1.0.4** (published 2019-04-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install kwm
pnpm add kwm
yarn add kwm
bun add kwm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2019-04-23 |
| First published | 2019-01-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 213 |
| Author | leecade |
| Maintainers | langshu, zhangyuang, zhaoxingyue |
| Keywords | react, hot, middleware, webpack, koa, koa2, hmr |

## Links

- npm: https://www.npmjs.com/package/kwm
- Repository: https://github.com/leecade/koa-webpack-middleware
- Homepage: https://github.com/leecade/koa-webpack-middleware#readme
- Issues: https://github.com/leecade/koa-webpack-middleware/issues
- npm.io page: https://npm.io/package/kwm

## Dependencies (4)

- [isobject](https://npm.io/package/isobject.md) 3.0.1
- [koa-compose](https://npm.io/package/koa-compose.md) 4.1.0
- [webpack-dev-middleware](https://npm.io/package/webpack-dev-middleware.md) 3.4.0
- [webpack-hot-middleware](https://npm.io/package/webpack-hot-middleware.md) 2.24.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2019-04-23
- 1.0.3 — 2019-01-04
- 1.0.2 — 2019-01-03
- 1.0.1 — 2019-01-03
- 1.0.0 — 2019-01-03

## README

# koa-webpack-middleware

[![npm version](http://img.shields.io/npm/v/koa-webpack-middleware.svg?style=flat-square)](https://npmjs.org/package/koa-webpack-middleware "View this project on npm")
[![Circle CI](https://circleci.com/gh/leecade/koa-webpack-middleware.svg)](https://circleci.com/gh/leecade/koa-webpack-middleware)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

[webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) for [koa2](https://github.com/koajs/koa/tree/v2.x) with [HMR](http://webpack.github.io/docs/hot-module-replacement-with-webpack.html)(hot module replacement) supports.

## Install

```sh
$ npm i kwm -D
```

## Exmaple

```
$ npm i
$ npm run dev
```

change  `example/react/src/components/Layout.js` #render() && save! 

## Depends

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

```sh
npm i koa@next -S
```

## Usage

> See [example/](./example/) for an example of usage.

```js
import webpack from 'webpack'
import kwm from 'kwm'
import devConfig from './webpack.config.dev'
const compile = webpack(devConfig)

// kwm(compile, devConf, hotConf) 
app.use(kwm(compile)

app.use( (ctx, next ) => {
    if (ctx.path === '/'){
        const jsx = ( <Layout /> );
        const reactDom = renderToString( jsx );
        ctx.styles = ctx.entry('app').styles
        ctx.scripts = ctx.entry('app').scripts
        ctx.type = 'html';
        ctx.body = htmlTemplate( ctx, reactDom ) ;
    }
} );

```

- ctx.webpackStats = ctx.state.webpackStats
- ctx.webpackFs = ctx.state.fs
- ctx.entry('webpack_entry_key') return {styles, scripts}

## HMR configure

1. webpack `plugins` configure

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

    ```sh
    $ npm i eventsource-polyfill -D
    ```

    ```js
    entry: {
      'index': [
        // For old browsers
        'eventsource-polyfill',
        'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
        'index.js']
    },
    ```

3. webpack `output` configure 
    
    ```js
    output: {
        path: path.resolve( __dirname, "dist" ),
        // filename: "[name].bundle.js",
        filename: '[name].[hash].js'
    },
    ```

That's all, you're all set!

---
_Source: https://npm.io/package/kwm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
