webpack-middleware.js 1.3 KB
'use strict';

const expressMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')

function middleware(doIt, req, res) {
    const originalEnd = res.end

    return function (done) {
        res.end = function () {
            originalEnd.apply(this, arguments);
            done(null, 0)
        }
        doIt(req, res, function () {
            done(null, 1)
        })
    }
}

module.exports = function (compiler, option, callbacks) {
    const doIt = expressMiddleware(compiler, option);
    var action = webpackHotMiddleware(compiler, option.hotConfig);
    for (const key in callbacks) {
        const exist = doIt[key] && typeof doIt[key] === 'function'
        exist && doIt[key].call(doIt, callbacks[key])
    }

    return function* (next) {
        const ctx = this
        const req = this.req
        const res = this.res
        const runNext = yield middleware(doIt, req, {
            end: function (content) {
                ctx.body = content
            },
            setHeader: function () {
                ctx.set.apply(ctx, arguments)
            },
        });
        const nextStep = yield middleware(action, req, res);
        if (runNext && nextStep && next) {
            yield* next
        }
    }
}