webpack-dev-service 0.8.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -35
- package/{server/cjs → cjs/server}/index.cjs +17 -7
- package/package.json +23 -34
- package/types/client/client.d.ts +1 -1
- package/types/client/events.d.ts +6 -6
- package/types/client/message.d.ts +6 -5
- package/types/client/ui/overlay.d.ts +1 -1
- package/types/server/dev/Service.d.ts +82 -0
- package/types/server/dev/index.d.ts +4 -4
- package/types/server/dev/interface.d.ts +26 -34
- package/types/server/dev/utils/fs.d.ts +27 -0
- package/types/server/dev/utils/hash.d.ts +9 -0
- package/types/server/dev/utils/http.d.ts +31 -14
- package/types/server/dev/utils/path.d.ts +22 -0
- package/types/server/dev/utils/paths.d.ts +7 -0
- package/types/server/dev/utils/ready.d.ts +2 -2
- package/types/server/hot/Socket.d.ts +6 -10
- package/types/server/hot/index.d.ts +4 -9
- package/types/server/hot/interface.d.ts +20 -0
- package/types/server/hot/utils.d.ts +13 -0
- package/types/server/index.d.ts +8 -8
- package/types/server/interface.d.ts +9 -0
- package/types/server/schema.d.ts +7 -0
- package/types/server/utils.d.ts +12 -0
- package/client/cjs/client.cjs +0 -129
- package/client/cjs/events.cjs +0 -64
- package/client/cjs/hot.cjs +0 -111
- package/client/cjs/index.cjs +0 -15
- package/client/cjs/main.cjs +0 -78
- package/client/cjs/ui/overlay.cjs +0 -233
- package/client/cjs/ui/progress.cjs +0 -92
- package/client/cjs/ui/utils.cjs +0 -136
- package/client/esm/client.js +0 -127
- package/client/esm/events.js +0 -60
- package/client/esm/hot.js +0 -106
- package/client/esm/index.js +0 -10
- package/client/esm/main.js +0 -76
- package/client/esm/ui/overlay.js +0 -231
- package/client/esm/ui/progress.js +0 -90
- package/client/esm/ui/utils.js +0 -123
- package/global.d.ts +0 -16
- package/server/cjs/dev/Files.cjs +0 -394
- package/server/cjs/dev/index.cjs +0 -62
- package/server/cjs/dev/middleware.cjs +0 -69
- package/server/cjs/dev/utils/boundary.cjs +0 -42
- package/server/cjs/dev/utils/common.cjs +0 -98
- package/server/cjs/dev/utils/compose.cjs +0 -58
- package/server/cjs/dev/utils/getPaths.cjs +0 -71
- package/server/cjs/dev/utils/http.cjs +0 -69
- package/server/cjs/dev/utils/ready.cjs +0 -26
- package/server/cjs/dev/utils/setupHooks.cjs +0 -95
- package/server/cjs/dev/utils/setupOutputFileSystem.cjs +0 -64
- package/server/cjs/dev/utils/setupWatching.cjs +0 -43
- package/server/cjs/dev/utils/setupWriteToDisk.cjs +0 -62
- package/server/cjs/hot/Socket.cjs +0 -197
- package/server/cjs/hot/index.cjs +0 -36
- package/server/esm/dev/Files.js +0 -384
- package/server/esm/dev/index.js +0 -60
- package/server/esm/dev/middleware.js +0 -67
- package/server/esm/dev/utils/boundary.js +0 -40
- package/server/esm/dev/utils/common.js +0 -98
- package/server/esm/dev/utils/compose.js +0 -56
- package/server/esm/dev/utils/getPaths.js +0 -69
- package/server/esm/dev/utils/http.js +0 -65
- package/server/esm/dev/utils/ready.js +0 -24
- package/server/esm/dev/utils/setupHooks.js +0 -87
- package/server/esm/dev/utils/setupOutputFileSystem.js +0 -62
- package/server/esm/dev/utils/setupWatching.js +0 -41
- package/server/esm/dev/utils/setupWriteToDisk.js +0 -60
- package/server/esm/hot/Socket.js +0 -188
- package/server/esm/hot/index.js +0 -34
- package/server/esm/index.js +0 -27
- package/types/server/dev/Files.d.ts +0 -83
- package/types/server/dev/utils/boundary.d.ts +0 -8
- package/types/server/dev/utils/common.d.ts +0 -44
- package/types/server/dev/utils/getPaths.d.ts +0 -10
- /package/types/server/{dev/utils/compose.d.ts → compose.d.ts} +0 -0
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @module compose
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* @function dispatch
|
|
17
|
-
* @param middlewares 中间件数组
|
|
18
|
-
* @param index 要执行的中间件索引
|
|
19
|
-
* @param stack 调用栈信息
|
|
20
|
-
* @param context 执行上下文
|
|
21
|
-
* @param [next] 下一个中间件
|
|
22
|
-
*/
|
|
23
|
-
async function dispatch(middlewares, index, stack, context, next) {
|
|
24
|
-
if (index <= stack.index) {
|
|
25
|
-
throw new Error('next() called multiple times');
|
|
26
|
-
}
|
|
27
|
-
stack.index = index;
|
|
28
|
-
const { length } = middlewares;
|
|
29
|
-
if (index <= length) {
|
|
30
|
-
if (index < length) {
|
|
31
|
-
const middleware = middlewares[index];
|
|
32
|
-
await middleware(context, () => {
|
|
33
|
-
return dispatch(middlewares, index + 1, stack, context, next);
|
|
34
|
-
});
|
|
35
|
-
} else if (next) {
|
|
36
|
-
await next();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* @function compose
|
|
42
|
-
* @description 生成融合中间件
|
|
43
|
-
* @param middlewares 中间件数组
|
|
44
|
-
*/
|
|
45
|
-
function compose(...middlewares) {
|
|
46
|
-
/**
|
|
47
|
-
* @function middleware
|
|
48
|
-
* @description 融合中间件
|
|
49
|
-
* @param context 执行上下文
|
|
50
|
-
* @param [next] 下一个中间件
|
|
51
|
-
*/
|
|
52
|
-
return (context, next) => {
|
|
53
|
-
const stack = { index: -1 };
|
|
54
|
-
return dispatch(middlewares, 0, stack, context, next);
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
exports.compose = compose;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const url = require('url');
|
|
13
|
-
const ready = require('./ready.cjs');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @module getPaths
|
|
17
|
-
*/
|
|
18
|
-
const cache = new WeakMap();
|
|
19
|
-
function getOutputPath(compilation) {
|
|
20
|
-
const { path } = compilation.outputOptions;
|
|
21
|
-
return compilation.getPath(path != null ? path : '');
|
|
22
|
-
}
|
|
23
|
-
function getPublicPath(compilation) {
|
|
24
|
-
const { publicPath } = compilation.outputOptions;
|
|
25
|
-
const path = compilation.getPath(publicPath != null ? publicPath : '');
|
|
26
|
-
try {
|
|
27
|
-
return new url.URL(path).pathname;
|
|
28
|
-
} catch (_a) {
|
|
29
|
-
return path;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function getStats(stats) {
|
|
33
|
-
if (stats == null) {
|
|
34
|
-
return [];
|
|
35
|
-
}
|
|
36
|
-
if (isMultiStatsMode(stats)) {
|
|
37
|
-
return stats.stats;
|
|
38
|
-
}
|
|
39
|
-
return [stats];
|
|
40
|
-
}
|
|
41
|
-
function isMultiStatsMode(stats) {
|
|
42
|
-
return 'stats' in stats;
|
|
43
|
-
}
|
|
44
|
-
function getPaths(context, name) {
|
|
45
|
-
return new Promise(resolve => {
|
|
46
|
-
const { compiler } = context;
|
|
47
|
-
const paths = cache.get(compiler);
|
|
48
|
-
if (paths != null) {
|
|
49
|
-
resolve(paths);
|
|
50
|
-
} else {
|
|
51
|
-
ready.ready(
|
|
52
|
-
context,
|
|
53
|
-
stats => {
|
|
54
|
-
const paths = [];
|
|
55
|
-
const childStats = getStats(stats);
|
|
56
|
-
for (const { compilation } of childStats) {
|
|
57
|
-
// The `output.path` is always present and always absolute
|
|
58
|
-
const outputPath = getOutputPath(compilation);
|
|
59
|
-
const publicPath = getPublicPath(compilation);
|
|
60
|
-
paths.push({ outputPath, publicPath });
|
|
61
|
-
}
|
|
62
|
-
cache.set(compiler, paths);
|
|
63
|
-
resolve(paths);
|
|
64
|
-
},
|
|
65
|
-
name
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
exports.getPaths = getPaths;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @method http
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* @function parseTokens
|
|
17
|
-
* @description Parse HTTP tokens.
|
|
18
|
-
* @param value The tokens value string.
|
|
19
|
-
*/
|
|
20
|
-
function parseTokens(value) {
|
|
21
|
-
let end = 0;
|
|
22
|
-
let start = 0;
|
|
23
|
-
let tokens = [];
|
|
24
|
-
// Gather tokens.
|
|
25
|
-
for (let i = 0, length = value.length; i < length; i++) {
|
|
26
|
-
switch (value.charCodeAt(i)) {
|
|
27
|
-
case 0x20:
|
|
28
|
-
// ' '.
|
|
29
|
-
if (start === end) {
|
|
30
|
-
start = end = i + 1;
|
|
31
|
-
}
|
|
32
|
-
break;
|
|
33
|
-
case 0x2c:
|
|
34
|
-
// ','.
|
|
35
|
-
tokens.push(value.substring(start, end));
|
|
36
|
-
start = end = i + 1;
|
|
37
|
-
break;
|
|
38
|
-
default:
|
|
39
|
-
end = i + 1;
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
// Final token.
|
|
44
|
-
tokens.push(value.substring(start, end));
|
|
45
|
-
return tokens;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* @function isETag
|
|
49
|
-
* @description Check if etag is valid.
|
|
50
|
-
* @param value The value to check.
|
|
51
|
-
*/
|
|
52
|
-
function isETag(value) {
|
|
53
|
-
return /^(?:W\/)?"[\s\S]+"$/.test(value);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @function isETagFresh
|
|
57
|
-
* @description Check if etag is fresh.
|
|
58
|
-
* @param match The match value.
|
|
59
|
-
* @param etag The etag value.
|
|
60
|
-
*/
|
|
61
|
-
function isETagFresh(match, etag) {
|
|
62
|
-
return parseTokens(match).some(match => {
|
|
63
|
-
return match === etag || match === 'W/' + etag || 'W/' + match === etag;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
exports.isETag = isETag;
|
|
68
|
-
exports.isETagFresh = isETagFresh;
|
|
69
|
-
exports.parseTokens = parseTokens;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @method ready
|
|
14
|
-
*/
|
|
15
|
-
function ready(context, callback, name) {
|
|
16
|
-
if (context.state) {
|
|
17
|
-
callback(context.stats);
|
|
18
|
-
} else {
|
|
19
|
-
context.callbacks.push(callback);
|
|
20
|
-
if (name) {
|
|
21
|
-
context.logger.info(`wait until bundle finished: ${name}`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
exports.ready = ready;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const supportsColor = require('supports-color');
|
|
13
|
-
const common = require('./common.cjs');
|
|
14
|
-
|
|
15
|
-
function _interopDefault(e) {
|
|
16
|
-
return e && e.__esModule ? e : { default: e };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const supportsColor__default = /*#__PURE__*/ _interopDefault(supportsColor);
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @module setupHooks
|
|
23
|
-
*/
|
|
24
|
-
function normalizeStatsOptions(statsOptions) {
|
|
25
|
-
if (statsOptions == null) {
|
|
26
|
-
return { preset: 'normal' };
|
|
27
|
-
} else if (common.isString(statsOptions)) {
|
|
28
|
-
return { preset: statsOptions };
|
|
29
|
-
} else if (common.isBoolean(statsOptions)) {
|
|
30
|
-
return statsOptions ? { preset: 'normal' } : { preset: 'none' };
|
|
31
|
-
}
|
|
32
|
-
if (statsOptions.colors == null) {
|
|
33
|
-
const { stdout, stderr } = supportsColor__default.default;
|
|
34
|
-
statsOptions.colors = stdout !== false && stderr !== false;
|
|
35
|
-
}
|
|
36
|
-
return statsOptions;
|
|
37
|
-
}
|
|
38
|
-
function getStatsOptions(context) {
|
|
39
|
-
const { compiler } = context;
|
|
40
|
-
const { stats } = context.options;
|
|
41
|
-
if (stats != null) {
|
|
42
|
-
if (!common.isMultiCompilerMode(compiler)) {
|
|
43
|
-
return normalizeStatsOptions(stats);
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
children: compiler.compilers.map(() => normalizeStatsOptions(stats))
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
if (!common.isMultiCompilerMode(compiler)) {
|
|
50
|
-
return normalizeStatsOptions(compiler.options.stats);
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
children: compiler.compilers.map(({ options }) => normalizeStatsOptions(options.stats))
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function setupHooks(context) {
|
|
57
|
-
function invalid() {
|
|
58
|
-
if (context.state) {
|
|
59
|
-
context.logger.log('Compilation starting...');
|
|
60
|
-
}
|
|
61
|
-
// We are now in invalid state
|
|
62
|
-
context.stats = null;
|
|
63
|
-
context.state = false;
|
|
64
|
-
}
|
|
65
|
-
function done(stats) {
|
|
66
|
-
// We are now on valid state
|
|
67
|
-
context.state = true;
|
|
68
|
-
context.stats = stats;
|
|
69
|
-
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
|
|
70
|
-
process.nextTick(() => {
|
|
71
|
-
const { state } = context;
|
|
72
|
-
// Check if still in valid state
|
|
73
|
-
if (state) {
|
|
74
|
-
const { logger, callbacks } = context;
|
|
75
|
-
logger.log('Compilation finished');
|
|
76
|
-
context.callbacks = [];
|
|
77
|
-
for (const callback of callbacks) {
|
|
78
|
-
callback(stats);
|
|
79
|
-
}
|
|
80
|
-
const statsOptions = getStatsOptions(context);
|
|
81
|
-
const printedStats = stats.toString(statsOptions);
|
|
82
|
-
// Avoid extra empty line when `stats: 'none'`
|
|
83
|
-
if (printedStats) {
|
|
84
|
-
logger.log(printedStats);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
const { hooks } = context.compiler;
|
|
90
|
-
hooks.done.tap(common.PLUGIN_NAME, done);
|
|
91
|
-
hooks.invalid.tap(common.PLUGIN_NAME, invalid);
|
|
92
|
-
hooks.watchRun.tap(common.PLUGIN_NAME, invalid);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
exports.setupHooks = setupHooks;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const memfs = require('memfs');
|
|
13
|
-
const common = require('./common.cjs');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @module setupOutputFileSystem
|
|
17
|
-
*/
|
|
18
|
-
function createMemfs() {
|
|
19
|
-
const volume = new memfs.Volume();
|
|
20
|
-
return memfs.createFsFromVolume(volume);
|
|
21
|
-
}
|
|
22
|
-
function hasReadStream(fs) {
|
|
23
|
-
return common.isFunction(fs.createReadStream);
|
|
24
|
-
}
|
|
25
|
-
function getOutputFileSystem({ options, compiler }) {
|
|
26
|
-
if (options.outputFileSystem) {
|
|
27
|
-
return options.outputFileSystem;
|
|
28
|
-
}
|
|
29
|
-
if (options.writeToDisk !== true) {
|
|
30
|
-
return createMemfs();
|
|
31
|
-
}
|
|
32
|
-
if (common.isMultiCompilerMode(compiler)) {
|
|
33
|
-
const { compilers } = compiler;
|
|
34
|
-
for (const compiler of compilers) {
|
|
35
|
-
if ('devServer' in compiler) {
|
|
36
|
-
if (compiler.outputFileSystem) {
|
|
37
|
-
return compiler.outputFileSystem;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (compiler.outputFileSystem) {
|
|
42
|
-
return compiler.outputFileSystem;
|
|
43
|
-
}
|
|
44
|
-
for (const compiler of compilers) {
|
|
45
|
-
if (compiler.outputFileSystem) {
|
|
46
|
-
return compiler.outputFileSystem;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return createMemfs();
|
|
51
|
-
}
|
|
52
|
-
function setupOutputFileSystem(context) {
|
|
53
|
-
const compilers = common.getCompilers(context.compiler);
|
|
54
|
-
const outputFileSystem = getOutputFileSystem(context);
|
|
55
|
-
if (!hasReadStream(outputFileSystem)) {
|
|
56
|
-
throw new Error('Compiler outputFileSystem must support createReadStream');
|
|
57
|
-
}
|
|
58
|
-
for (const compiler of compilers) {
|
|
59
|
-
compiler.outputFileSystem = outputFileSystem;
|
|
60
|
-
}
|
|
61
|
-
context.outputFileSystem = outputFileSystem;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
exports.setupOutputFileSystem = setupOutputFileSystem;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const common = require('./common.cjs');
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @module setupWatching
|
|
16
|
-
*/
|
|
17
|
-
function getWatching({ compiler, logger }) {
|
|
18
|
-
const isMulti = common.isMultiCompilerMode(compiler);
|
|
19
|
-
if (!isMulti && compiler.watching) {
|
|
20
|
-
return compiler.watching;
|
|
21
|
-
}
|
|
22
|
-
const errorHandler = error => {
|
|
23
|
-
if (error) {
|
|
24
|
-
// For example - `writeToDisk` can throw an error and right now it is ends watching.
|
|
25
|
-
// We can improve that and keep watching active, but it is require API on webpack side.
|
|
26
|
-
// Let's implement that in webpack@5 because it is rare case.
|
|
27
|
-
logger.error(error);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
if (!isMulti) {
|
|
31
|
-
const { watchOptions } = compiler.options;
|
|
32
|
-
return compiler.watch(watchOptions, errorHandler);
|
|
33
|
-
}
|
|
34
|
-
const watchOptions = compiler.compilers.map(({ options }) => {
|
|
35
|
-
return options.watchOptions;
|
|
36
|
-
});
|
|
37
|
-
return compiler.watch(watchOptions, errorHandler);
|
|
38
|
-
}
|
|
39
|
-
function setupWatching(context) {
|
|
40
|
-
context.watching = getWatching(context);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
exports.setupWatching = setupWatching;
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const path = require('path');
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
const common = require('./common.cjs');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @module setupWriteToDisk
|
|
18
|
-
*/
|
|
19
|
-
const assetEmitted = Symbol('assetEmitted');
|
|
20
|
-
function getCompilerName({ options: { name } }) {
|
|
21
|
-
return name ? `Child "${name}": ` : '';
|
|
22
|
-
}
|
|
23
|
-
function isAllowWrite(targetPath, filter) {
|
|
24
|
-
return filter && common.isFunction(filter) ? filter(targetPath) : true;
|
|
25
|
-
}
|
|
26
|
-
function setupWriteToDisk(context) {
|
|
27
|
-
const { logger, options } = context;
|
|
28
|
-
const { writeToDisk: filter } = options;
|
|
29
|
-
const compilers = common.getCompilers(context.compiler);
|
|
30
|
-
for (const compiler of compilers) {
|
|
31
|
-
const { hooks } = compiler;
|
|
32
|
-
hooks.emit.tap(common.PLUGIN_NAME, () => {
|
|
33
|
-
if (!compiler[assetEmitted]) {
|
|
34
|
-
hooks.assetEmitted.tapAsync(common.PLUGIN_NAME, (_file, { targetPath, content }, callback) => {
|
|
35
|
-
if (!isAllowWrite(targetPath, filter)) {
|
|
36
|
-
return callback();
|
|
37
|
-
}
|
|
38
|
-
const dir = path.dirname(targetPath);
|
|
39
|
-
const name = getCompilerName(compiler);
|
|
40
|
-
return fs.mkdir(dir, { recursive: true }, mkdirError => {
|
|
41
|
-
if (mkdirError) {
|
|
42
|
-
logger.error(`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`);
|
|
43
|
-
return callback(mkdirError);
|
|
44
|
-
}
|
|
45
|
-
fs.writeFile(targetPath, content, writeFileError => {
|
|
46
|
-
if (writeFileError) {
|
|
47
|
-
logger.error(`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`);
|
|
48
|
-
return callback(writeFileError);
|
|
49
|
-
}
|
|
50
|
-
logger.log(`${name}Asset written to disk: "${targetPath}"`);
|
|
51
|
-
return callback();
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
// Set asset emitted to true
|
|
56
|
-
compiler[assetEmitted] = true;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
exports.setupWriteToDisk = setupWriteToDisk;
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package webpack-dev-service
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.8.1
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
-
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
const WebSocket = require('ws');
|
|
13
|
-
const webpack = require('webpack');
|
|
14
|
-
|
|
15
|
-
function _interopDefault(e) {
|
|
16
|
-
return e && e.__esModule ? e : { default: e };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const WebSocket__default = /*#__PURE__*/ _interopDefault(WebSocket);
|
|
20
|
-
const webpack__default = /*#__PURE__*/ _interopDefault(webpack);
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @module Socket
|
|
24
|
-
*/
|
|
25
|
-
const WEBSOCKET_RE = /^websocket$/i;
|
|
26
|
-
const { toString } = Object.prototype;
|
|
27
|
-
function isObject(value) {
|
|
28
|
-
return toString.call(value) === '[object Object]';
|
|
29
|
-
}
|
|
30
|
-
function resolveStatsOptions(compiler) {
|
|
31
|
-
const options = {
|
|
32
|
-
all: false,
|
|
33
|
-
hash: true,
|
|
34
|
-
colors: true,
|
|
35
|
-
errors: true,
|
|
36
|
-
assets: false,
|
|
37
|
-
builtAt: true,
|
|
38
|
-
warnings: true,
|
|
39
|
-
errorDetails: false
|
|
40
|
-
};
|
|
41
|
-
const { stats } = compiler.options;
|
|
42
|
-
if (isObject(stats)) {
|
|
43
|
-
const { warningsFilter } = stats;
|
|
44
|
-
if (warningsFilter !== undefined) {
|
|
45
|
-
options.warningsFilter = warningsFilter;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return options;
|
|
49
|
-
}
|
|
50
|
-
function normalize(path) {
|
|
51
|
-
const segments = [];
|
|
52
|
-
const parts = path.split(/[\\/]+/);
|
|
53
|
-
for (const segment of parts) {
|
|
54
|
-
switch (segment) {
|
|
55
|
-
case '.':
|
|
56
|
-
break;
|
|
57
|
-
case '..':
|
|
58
|
-
segments.pop();
|
|
59
|
-
break;
|
|
60
|
-
default:
|
|
61
|
-
segments.push(segment);
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const pathname = segments.join('/');
|
|
66
|
-
return pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
67
|
-
}
|
|
68
|
-
function resolveOptions(options) {
|
|
69
|
-
const settings = {
|
|
70
|
-
hmr: true,
|
|
71
|
-
path: '/hot',
|
|
72
|
-
progress: true,
|
|
73
|
-
...options
|
|
74
|
-
};
|
|
75
|
-
settings.path = normalize(settings.path);
|
|
76
|
-
return settings;
|
|
77
|
-
}
|
|
78
|
-
function isUpgradable(context, detector) {
|
|
79
|
-
const { upgrade } = context.headers;
|
|
80
|
-
return !!upgrade && detector.test(upgrade.trim());
|
|
81
|
-
}
|
|
82
|
-
function hasProblems(problems) {
|
|
83
|
-
return !!problems && problems.length > 0;
|
|
84
|
-
}
|
|
85
|
-
class Socket {
|
|
86
|
-
constructor(compiler, options) {
|
|
87
|
-
this.name = 'webpack-hot-middleware';
|
|
88
|
-
this.compiler = compiler;
|
|
89
|
-
this.options = resolveOptions(options);
|
|
90
|
-
this.logger = compiler.getInfrastructureLogger(this.name);
|
|
91
|
-
this.server = new WebSocket.WebSocketServer({ path: this.options.path, noServer: true });
|
|
92
|
-
this.setupWss();
|
|
93
|
-
this.setupHooks();
|
|
94
|
-
this.setupPlugins();
|
|
95
|
-
}
|
|
96
|
-
setupWss() {
|
|
97
|
-
const { server, logger } = this;
|
|
98
|
-
server.on('error', error => {
|
|
99
|
-
logger.error(error.message);
|
|
100
|
-
});
|
|
101
|
-
server.on('connection', client => {
|
|
102
|
-
if (this.stats) {
|
|
103
|
-
this.broadcastStats([client], this.stats);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
setupHooks() {
|
|
108
|
-
const { compiler } = this;
|
|
109
|
-
const { hooks } = compiler;
|
|
110
|
-
const statsOptions = resolveStatsOptions(compiler);
|
|
111
|
-
hooks.done.tapAsync(this.name, (stats, next) => {
|
|
112
|
-
next();
|
|
113
|
-
this.stats = stats.toJson(statsOptions);
|
|
114
|
-
this.broadcastStats(this.clients(), this.stats);
|
|
115
|
-
});
|
|
116
|
-
hooks.invalid.tap(this.name, (path, builtAt) => {
|
|
117
|
-
this.broadcast(this.clients(), 'invalid', { path, builtAt });
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
setupPlugins() {
|
|
121
|
-
const { options, compiler } = this;
|
|
122
|
-
const plugins = [
|
|
123
|
-
new webpack__default.default.NoEmitOnErrorsPlugin(),
|
|
124
|
-
new webpack__default.default.DefinePlugin({
|
|
125
|
-
__WDS_HOT_OPTIONS__: JSON.stringify({
|
|
126
|
-
...options,
|
|
127
|
-
name: compiler.name
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
];
|
|
131
|
-
if (options.hmr) {
|
|
132
|
-
plugins.push(new webpack__default.default.HotModuleReplacementPlugin());
|
|
133
|
-
}
|
|
134
|
-
if (options.progress) {
|
|
135
|
-
let value = 0;
|
|
136
|
-
plugins.push(
|
|
137
|
-
new webpack__default.default.ProgressPlugin((percentage, status, message) => {
|
|
138
|
-
const nextValue = Math.floor(percentage * 100);
|
|
139
|
-
if (nextValue > value || nextValue === 0) {
|
|
140
|
-
value = nextValue;
|
|
141
|
-
switch (value) {
|
|
142
|
-
case 0:
|
|
143
|
-
status = 'start';
|
|
144
|
-
message = 'end idle';
|
|
145
|
-
break;
|
|
146
|
-
case 100:
|
|
147
|
-
status = 'finish';
|
|
148
|
-
message = 'begin idle';
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
this.broadcast(this.clients(), 'progress', { status, message, value });
|
|
152
|
-
}
|
|
153
|
-
})
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
for (const plugin of plugins) {
|
|
157
|
-
plugin.apply(compiler);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
clients() {
|
|
161
|
-
return this.server.clients;
|
|
162
|
-
}
|
|
163
|
-
upgrade(context) {
|
|
164
|
-
const { server } = this;
|
|
165
|
-
const { req: request } = context;
|
|
166
|
-
if (isUpgradable(context, WEBSOCKET_RE) && server.shouldHandle(request)) {
|
|
167
|
-
context.respond = false;
|
|
168
|
-
const { socket } = context;
|
|
169
|
-
const head = Buffer.alloc(0);
|
|
170
|
-
server.handleUpgrade(request, socket, head, client => {
|
|
171
|
-
server.emit('connection', client, request);
|
|
172
|
-
});
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
return false;
|
|
176
|
-
}
|
|
177
|
-
broadcast(clients, action, payload) {
|
|
178
|
-
for (const client of clients) {
|
|
179
|
-
if (client.readyState === WebSocket__default.default.OPEN) {
|
|
180
|
-
client.send(JSON.stringify({ action, payload }));
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
broadcastStats(clients, stats) {
|
|
185
|
-
if (clients.size > 0 || clients.length > 0) {
|
|
186
|
-
const { hash, builtAt, errors, warnings } = stats;
|
|
187
|
-
this.broadcast(clients, 'hash', { hash });
|
|
188
|
-
if (hasProblems(errors) || hasProblems(warnings)) {
|
|
189
|
-
this.broadcast(clients, 'problems', { errors, warnings, builtAt });
|
|
190
|
-
} else {
|
|
191
|
-
this.broadcast(clients, 'ok', { builtAt });
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
exports.Socket = Socket;
|