• Stars
    star
    441
  • Rank 98,861 (Top 2 %)
  • Language
    JavaScript
  • License
    Mozilla Public Li...
  • Created about 8 years ago
  • Updated 8 months ago

Reviews

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

Repository Details

Development and Hot Reload Middleware for Koa2

koa-webpack

tests cover size

Development and Hot Module Reload Middleware for Koa2, in a single middleware module.

This module wraps and composes webpack-dev-middleware and webpack-hot-client into a single middleware module, allowing for quick and concise implementation.

As an added bonus, it'll also use the installed webpack module from your project, and the webpack.config.js file in the root of your project, automagically, should you choose to let it. This negates the need for all of the repetitive setup and config that you get with koa-webpack-middleware.

Install

Using npm:

npm install koa-webpack --save-dev

Requirements

koa-webpack is an evergreen module. 🌲 This module requires an Active LTS Node version (v8.0.0+ or v10.0.0+), and Webpack v4.0.0+.

Usage

const Koa = require('koa');
const koaWebpack = require('koa-webpack');

const app = new Koa();
const options = { .. };
const middleware = await koaWebpack(options);

app.use(middleware);

API

koaWebpack([options])

Returns a Promise which resolves the server middleware containing the following additional properties:

  • close(callback) (Function) - Closes both the instance of webpack-dev-middleware and webpack-hot-client. Accepts a single Function callback parameter that is executed when complete.
  • hotClient (Object) - An instance of webpack-hot-client.
  • devMiddleware (Object) - An instance of webpack-dev-middleware

Options

The middleware accepts an options Object, which can contain options for the webpack-dev-middleware and webpack-hot-client bundled with this module. The following is a property reference for the Object:

compiler

Type: Object
optional

Should you rather that the middleware use an instance of webpack that you've already init'd [with webpack config], you can pass it to the middleware using this option.

Example:

const webpack = require('webpack');
const config = require('./webpack.config.js');
const koaWebpack = require('koa-webpack');

const compiler = webpack(config);
const middleware = await koaWebpack({ compiler });

app.use(middleware);

config

Type: Object

Should you rather that the middleware use an instance of webpack configuration that you've already required/imported, you can pass it to the middleware using this option.

Example:

const koaWebpack = require('koa-webpack');
const config = require('./webpack.config.js');

const middleware = await koaWebpack({ config });

app.use(middleware);

configPath

Type: String

Allows you to specify the absolute path to the Webpack config file to be used.

Example:

const path = require('path');
const koaWebpack = require('koa-webpack');

// The Webpack config file would be at "./client/webpack.config.js".
const middleware = await koaWebpack({
  configPath: path.join(__dirname, 'client', 'webpack.config.js')
});

app.use(middleware);

devMiddleware

Type: Object

The devMiddleware property should contain options for webpack-dev-middleware, a list of which is available at webpack-dev-middleware. Omitting this property will result in webpack-dev-middleware using its default options.

hotClient

Type: Object|Boolean

The hotClient property should contain options for webpack-hot-client, a list of which is available at webpack-hot-client. Omitting this property will result in webpack-hot-client using its default options.

As of v3.0.1 setting this to false will completely disable webpack-hot-client and all automatic Hot Module Replacement functionality.

Using with koa-compress

When using koa-webpack with koa-compress, you may experience issues with saving files and hot module reload. Please review this issue for more information and a workaround.

Server-Side-Rendering

When serverSideRender is set to true in config.devMiddleware, webpackStats is accessible from ctx.state.webpackStats.

app.use(async (ctx, next) => {
  const assetsByChunkName = ctx.state.webpackStats.toJson().assetsByChunkName;
  // do something with assetsByChunkName
})

For more details please refer to: webpack-dev-middleware

Using with html-webpack-plugin

When using with html-webpack-plugin, you can access dev-middleware in-memory filesystem to serve index.html file:

const middleware = await koaWebpack({ config });

app.use(middleware);

app.use(async (ctx) => {
  const filename = path.resolve(webpackConfig.output.path, 'index.html')
  ctx.response.type = 'html'
  ctx.response.body = middleware.devMiddleware.fileSystem.createReadStream(filename)
});

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

Attribution

This module started as a fork of koa-webpack-middleware

License

MPL

More Repositories

1

webpack-manifest-plugin

webpack plugin for generating asset manifests
JavaScript
1,433
star
2

jsx-email

Build emails with a delightful DX
TypeScript
924
star
3

webpack-plugin-serve

A Development Server in a Webpack Plugin
JavaScript
337
star
4

webpack-nano

A teensy, squeaky 🐤 clean Webpack CLI
JavaScript
237
star
5

gmail-classic

CSS for reverting Gmail to the Classic Theme
CSS
216
star
6

postcss-less

PostCSS Syntax for parsing LESS
JavaScript
123
star
7

webpack-plugin-ramdisk

🐏 A webpack plugin for blazing fast builds on a RAM disk / drive
JavaScript
118
star
8

Gmail-Notifier-Plus

Gmail Notifier Plus
C#
89
star
9

prettier-plugin-package

An opinionated package.json formatter plugin for Prettier
JavaScript
81
star
10

mocha-chrome

☕ Run Mocha tests using headless Google Chrome
JavaScript
72
star
11

apollo-log

A logging extension for the Apollo GraphQL Server
TypeScript
66
star
12

postcss-values-parser

A CSS property value parser for PostCSS
JavaScript
58
star
13

webpack-serve

A CLI for webpack-plugin-serve, providing a premier webpack development server
JavaScript
51
star
14

loglevelnext

A modern logging library for Node.js that provides log level mapping to the console
TypeScript
38
star
15

OctoGerrit

A modern, clean, and usable theme for Gerrit
CSS
29
star
16

koa-ws

Empower your koa.js application with realtime
JavaScript
26
star
17

nanoid-cli

A tiny cli wrapper for nanoid
JavaScript
24
star
18

LiberaManifesto

A Manifesto for Open Source Software and Free Services
HTML
20
star
19

webpack-log

A logger for the Webpack ecosystem
JavaScript
18
star
20

doiuse-email

Lint HTML and CSS for email support against the `Can I email?` database.
TypeScript
14
star
21

Snarf

Snarf is an NFS server implementation written in C# with .NET 4.5.
C#
13
star
22

Brew

Brew is an open source library of interface components for ASP.NET which utilitizes jQuery and jQuery UI.
JavaScript
10
star
23

github-notifications-classic

A CSS project that restores the classic Github Notifications appearance.
CSS
9
star
24

Shellscape.Common

A .NET Library I use in a lot of my projects. Written in C#
C#
7
star
25

harmonica

A module that allows use of Node.js --harmony flags, programmatically.
JavaScript
7
star
26

webpack-hot-client

A client for enabling, and interacting with, webpack Hot Module Replacement
JavaScript
7
star
27

bundler-serve

A Platform for Bundler Development Servers
JavaScript
6
star
28

fwv

A ⚡️ crazy fast streaming ⚡️ Fixed Width Value Parser
JavaScript
5
star
29

gulp-scan

A Gulp plugin to scan a file for a particular string or expression
JavaScript
5
star
30

piilist

A list of Personally Identifiable Information keys / words / phrases used for redaction or removal
JavaScript
3
star
31

thank-you

A place for me to thank the outstanding people who support my work
3
star
32

Lumen

Everything that Start should have been.
C#
3
star
33

dot

Tools for monorepos, serverless, and more
TypeScript
2
star
34

shellscape.github.io

The codebase for shellscape.org
HTML
2
star
35

Gcal-Notifier-Plus

A Google Calendar Notifier for Windows 7
C#
2
star
36

gulp-assist

Display helpful information for your Gulp tasks
JavaScript
2
star
37

babel-plugin-async-to-plain-generator

Transform async functions into non-wrapped ES2015 generators
JavaScript
2
star
38

npm-version-tree

Fetch a dependency version tree for a package
JavaScript
2
star
39

Nubs

Fancy Tabs for the Windows Desktop
C#
1
star
40

travis-target

A Node.js module to determine the file or directory targets of a Travis CI build.
JavaScript
1
star
41

gulp-mocha-chrome

☕ Run Mocha tests using Google Chrome via Gulp
JavaScript
1
star
42

prettier-tree

Prettier directory trees
1
star
43

Rumshot

1
star
44

eslint-config-shellscape

ESLint shareable config for my projects
JavaScript
1
star
45

gulp-version-conflicts

Check for and report on module version conflicts for a package
JavaScript
1
star