-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
111 lines (90 loc) · 3.75 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
const webpackIsomorphicCompiler = require('webpack-isomorphic-compiler');
const startReporting = require('webpack-isomorphic-compiler-reporter');
const startNotifying = require('webpack-sane-compiler-notifier');
const compose = require('compose-middleware').compose;
const merge = require('lodash.merge');
const omitBy = require('lodash.omitby');
const castArray = require('lodash.castarray');
const memoryFs = require('./lib/util/memoryFs');
const mainMiddleware = require('./lib/mainMiddleware');
const devMiddleware = require('./lib/devMiddleware');
const renderErrorMiddleware = require('./lib/renderErrorMiddleware');
const checkHashes = require('./lib/util/checkHashes');
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),
};
}
// Separate 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
watchDelay: 0,
watchOptions: undefined, // Options to pass to .watch()
report: { stats: 'once' }, // Enable reporting, see https://github.com/moxystudio/webpack-isomorphic-compiler-reporter
notify: false, // Enable OS notifications, see https://github.com/moxystudio/webpack-sane-compiler-notifier
headers: { 'Cache-Control': 'max-age=0, must-revalidate' }, // Headers to set when serving compiled files
findServerAssetName: (stats) => {
const entrypoint = Object.keys(stats.entrypoints)[0];
return castArray(stats.assetsByChunkName[entrypoint])
.find((asset) => /\.js$/.test(asset));
},
}, options);
// Normalize some options
/* istanbul ignore next */
options.report = options.report === true ? {} : options.report;
options.notify = options.notify === true ? {} : options.notify;
options.headers = omitBy(options.headers, (value) => value == null);
return options;
}
function webpackIsomorphicDevMiddleware(...args) {
const { compiler, options } = parseArgs(args);
// Use an in-memory filesystem
if (options.memoryFs) {
const fs = memoryFs();
compiler.client.webpackCompiler.outputFileSystem = fs;
compiler.server.webpackCompiler.outputFileSystem = fs;
}
// Enable reporting
if (options.report !== false) {
options.report = startReporting(compiler, options.report).options;
}
// Notify build status through OS notifications
if (options.notify !== false) {
options.notify = startNotifying(compiler, options.notify).options;
}
// Check if hashes are present in emitted files
options.memoryFs && checkHashes(compiler, options);
// Create middleware by composing our parts
const middleware = compose([
mainMiddleware(compiler, options),
devMiddleware(compiler, options),
renderErrorMiddleware(compiler, options),
]);
// Expose isomorphic compiler
middleware.compiler = compiler;
// Start watching
setTimeout(() => compiler.watch(options.watchOptions), options.watchDelay);
return middleware;
}
module.exports = webpackIsomorphicDevMiddleware;