This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var watchify = require('watchify-middleware');
var createBuilder = require('browserify-builder');
var merge = require('lodash/fp/merge');
module.exports = function createMiddleware(config, middlewareConfig) {
var builder = createBuilder(merge(config, {parallel: false, options: {cache: {}, packageCache: {}}}));
var bundles = builder.getBundles();
var sharedBundles = bundles.filter(function(bundle) {
return bundle.type === 'shared';
}).reduce(function(bundleMap, bundle) {
var emitter = watchify.emitter(bundle.bundle, config.watchOptions);
bundleMap[(middlewareConfig.publicPath + bundle.path.replace('\\', '/')).toLowerCase()] = emitter.middleware;
console.log(bundle.path);
return bundleMap;
}, {});
var entryBundles = bundles.filter(function(bundle) {
return bundle.type !== 'shared';
}).reduce(function(bundleMap, bundle) {
var emitter,
middleware;
if (middlewareConfig.lazy) {
middleware = function middlewareMemoize(req, res, next) {
if (!emitter) {
emitter = watchify.emitter(bundle.bundle, config.watchOptions);
emitter.emit('pending');
}
return emitter.middleware(req, res, next);
};
bundleMap[(middlewareConfig.publicPath + bundle.path.replace('\\', '/')).toLowerCase()] = middleware;
} else {
emitter = watchify.emitter(bundle.bundle, config.watchOptions);
bundleMap[(middlewareConfig.publicPath + bundle.path.replace('\\', '/')).toLowerCase()] = emitter.middleware;
}
return bundleMap;
}, sharedBundles);
return function middlewareFn(req, res, next) {
var middleware = entryBundles[req.path.toLowerCase()];
if (middleware) {
middleware(req, res, next);
} else {
next();
}
};
};