forked from moxystudio/webpack-isomorphic-dev-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (64 loc) · 2.58 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const webpackIsomorphicCompiler = require('webpack-isomorphic-compiler');
const compose = require('compose-middleware').compose;
const merge = require('lodash.merge');
const standardFs = require('./lib/fs/standardFs');
const memoryFs = require('./lib/fs/memoryFs');
const mainMiddleware = require('./lib/mainMiddleware');
const devMiddleware = require('./lib/devMiddleware');
const renderErrorMiddleware = require('./lib/renderErrorMiddleware');
function parseArgs(args) {
const [firstArg = {}, secondArg, thirdArg] = args;
// Webpack multi compiler?
if (firstArg.compilers && firstArg.run) {
return {
compiler: webpackIsomorphicCompiler(firstArg.compilers[0], firstArg.compilers[1]),
options: parseOptions(secondArg),
};
}
// Isomorphic compiler?
if (firstArg.resolve && firstArg.client) {
return {
compiler: firstArg,
options: parseOptions(secondArg),
};
}
// Seperate webpack compilers
if (firstArg.run && secondArg && secondArg.run) {
return {
compiler: webpackIsomorphicCompiler(firstArg, secondArg),
options: parseOptions(thirdArg),
};
}
throw new TypeError('Invalid arguments passed to middleware');
}
function parseOptions(options) {
options = merge({
memoryFs: true, // Enable memory fs
report: { stats: 'once' }, // Enable reporting, see https://github.com/moxystudio/webpack-isomorphic-compiler/blob/master/README.md#reporter
watchOptions: {}, // Options to pass to .watch()
headers: null, // Headers to set when serving compiled files, see https://github.com/webpack/webpack-dev-middleware
}, options);
Object.assign(options.watchOptions, { report: options.report });
return options;
}
// -----------------------------------------------------------
function middleware(...args) {
const { compiler, options } = parseArgs(args);
// Set output filesystems
const fs = options.memoryFs ? memoryFs() : standardFs();
compiler.client.webpackCompiler.outputFileSystem = fs;
compiler.server.webpackCompiler.outputFileSystem = fs;
// Create middleware by composing our parts
const middleware = compose([
mainMiddleware(compiler, options),
devMiddleware(compiler, options),
renderErrorMiddleware,
]);
// Start watching
options.watchOptions !== false && compiler.watch(options.watchOptions);
// Expose isomorphic compiler
middleware.isomorphicCompiler = compiler;
return middleware;
}
module.exports = middleware;