webpack-dev-service 0.7.2 → 0.8.1

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.
Files changed (71) hide show
  1. package/README.md +6 -10
  2. package/client/cjs/client.cjs +1 -1
  3. package/client/cjs/events.cjs +1 -1
  4. package/client/cjs/hot.cjs +1 -1
  5. package/client/cjs/index.cjs +1 -1
  6. package/client/cjs/main.cjs +1 -1
  7. package/client/cjs/ui/overlay.cjs +1 -1
  8. package/client/cjs/ui/progress.cjs +1 -1
  9. package/client/cjs/ui/utils.cjs +1 -1
  10. package/client/esm/client.js +1 -1
  11. package/client/esm/events.js +1 -1
  12. package/client/esm/hot.js +1 -1
  13. package/client/esm/index.js +1 -1
  14. package/client/esm/main.js +1 -1
  15. package/client/esm/ui/overlay.js +1 -1
  16. package/client/esm/ui/progress.js +1 -1
  17. package/client/esm/ui/utils.js +1 -1
  18. package/global.d.ts +2 -0
  19. package/package.json +13 -6
  20. package/server/cjs/dev/Files.cjs +394 -0
  21. package/server/cjs/dev/index.cjs +62 -0
  22. package/server/cjs/dev/middleware.cjs +69 -0
  23. package/server/cjs/dev/utils/boundary.cjs +42 -0
  24. package/server/cjs/dev/utils/common.cjs +98 -0
  25. package/server/cjs/dev/utils/compose.cjs +58 -0
  26. package/server/cjs/dev/utils/getPaths.cjs +71 -0
  27. package/server/cjs/dev/utils/http.cjs +69 -0
  28. package/server/cjs/dev/utils/ready.cjs +26 -0
  29. package/server/cjs/dev/utils/setupHooks.cjs +95 -0
  30. package/server/cjs/dev/utils/setupOutputFileSystem.cjs +64 -0
  31. package/server/cjs/dev/utils/setupWatching.cjs +43 -0
  32. package/server/cjs/dev/utils/setupWriteToDisk.cjs +62 -0
  33. package/server/cjs/{hot.cjs → hot/Socket.cjs} +6 -20
  34. package/server/cjs/hot/index.cjs +36 -0
  35. package/server/cjs/index.cjs +11 -15
  36. package/server/esm/dev/Files.js +384 -0
  37. package/server/esm/dev/index.js +60 -0
  38. package/server/esm/dev/middleware.js +67 -0
  39. package/server/esm/dev/utils/boundary.js +40 -0
  40. package/server/esm/dev/utils/common.js +98 -0
  41. package/server/esm/dev/utils/compose.js +56 -0
  42. package/server/esm/dev/utils/getPaths.js +69 -0
  43. package/server/esm/dev/utils/http.js +65 -0
  44. package/server/esm/dev/utils/ready.js +24 -0
  45. package/server/esm/dev/utils/setupHooks.js +87 -0
  46. package/server/esm/dev/utils/setupOutputFileSystem.js +62 -0
  47. package/server/esm/dev/utils/setupWatching.js +41 -0
  48. package/server/esm/dev/utils/setupWriteToDisk.js +60 -0
  49. package/server/esm/{hot.js → hot/Socket.js} +6 -20
  50. package/server/esm/hot/index.js +34 -0
  51. package/server/esm/index.js +11 -9
  52. package/types/server/dev/Files.d.ts +83 -0
  53. package/types/server/dev/index.d.ts +8 -0
  54. package/types/server/dev/interface.d.ts +46 -0
  55. package/types/server/dev/middleware.d.ts +6 -0
  56. package/types/server/dev/utils/boundary.d.ts +8 -0
  57. package/types/server/dev/utils/common.d.ts +44 -0
  58. package/types/server/dev/utils/compose.d.ts +18 -0
  59. package/types/server/dev/utils/getPaths.d.ts +10 -0
  60. package/types/server/dev/utils/http.d.ts +22 -0
  61. package/types/server/dev/utils/ready.d.ts +5 -0
  62. package/types/server/dev/utils/setupHooks.d.ts +5 -0
  63. package/types/server/dev/utils/setupOutputFileSystem.d.ts +5 -0
  64. package/types/server/dev/utils/setupWatching.d.ts +5 -0
  65. package/types/server/dev/utils/setupWriteToDisk.d.ts +5 -0
  66. package/types/server/hot/Socket.d.ts +27 -0
  67. package/types/server/{hot.d.ts → hot/index.d.ts} +7 -10
  68. package/types/server/index.d.ts +4 -4
  69. package/server/cjs/dev.cjs +0 -50
  70. package/server/esm/dev.js +0 -42
  71. package/types/server/dev.d.ts +0 -17
@@ -0,0 +1,62 @@
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 ready = require('./utils/ready.cjs');
13
+ const middleware = require('./middleware.cjs');
14
+ const common = require('./utils/common.cjs');
15
+ const setupHooks = require('./utils/setupHooks.cjs');
16
+ const setupWatching = require('./utils/setupWatching.cjs');
17
+ const setupWriteToDisk = require('./utils/setupWriteToDisk.cjs');
18
+ const setupOutputFileSystem = require('./utils/setupOutputFileSystem.cjs');
19
+
20
+ /**
21
+ * @module index
22
+ */
23
+ function setup(compiler, options) {
24
+ const context = {
25
+ options,
26
+ compiler,
27
+ stats: null,
28
+ state: false,
29
+ callbacks: [],
30
+ logger: compiler.getInfrastructureLogger(common.PLUGIN_NAME)
31
+ };
32
+ setupHooks.setupHooks(context);
33
+ if (options.writeToDisk) {
34
+ setupWriteToDisk.setupWriteToDisk(context);
35
+ }
36
+ setupOutputFileSystem.setupOutputFileSystem(context);
37
+ setupWatching.setupWatching(context);
38
+ return context;
39
+ }
40
+ function dev(compiler, options = {}) {
41
+ const context = setup(compiler, options);
42
+ return Object.assign(middleware.middleware(context), {
43
+ isReady() {
44
+ return context.state;
45
+ },
46
+ get logger() {
47
+ return context.logger;
48
+ },
49
+ ready(callback) {
50
+ ready.ready(context, callback);
51
+ },
52
+ invalidate(callback) {
53
+ ready.ready(context, callback);
54
+ context.watching.invalidate();
55
+ },
56
+ close(callback) {
57
+ context.watching.close(callback);
58
+ }
59
+ });
60
+ }
61
+
62
+ exports.dev = dev;
@@ -0,0 +1,69 @@
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 Files = require('./Files.cjs');
13
+ const common = require('./utils/common.cjs');
14
+ const getPaths = require('./utils/getPaths.cjs');
15
+
16
+ /**
17
+ * @module middleware
18
+ */
19
+ const cache = new WeakMap();
20
+ async function getFilesInstances(context, name) {
21
+ const cached = cache.get(context.compiler);
22
+ if (cached != null) {
23
+ return cached;
24
+ }
25
+ const { options } = context;
26
+ const instances = [];
27
+ const paths = await getPaths.getPaths(context, name);
28
+ for (const { outputPath, publicPath } of paths) {
29
+ instances.push({
30
+ publicPath,
31
+ files: new Files(outputPath, {
32
+ etag: options.etag,
33
+ headers: options.headers,
34
+ fs: context.outputFileSystem,
35
+ acceptRanges: options.acceptRanges,
36
+ cacheControl: options.cacheControl,
37
+ lastModified: options.lastModified
38
+ })
39
+ });
40
+ }
41
+ return instances;
42
+ }
43
+ function middleware(context) {
44
+ return async (ctx, next) => {
45
+ const path = common.decodeURI(ctx.path);
46
+ // Path -1 or null byte(s)
47
+ if (path === -1 || path.includes('\0')) {
48
+ return ctx.throw(400);
49
+ }
50
+ let respond = false;
51
+ const instances = await getFilesInstances(context, path);
52
+ for (const { files, publicPath } of instances) {
53
+ if (path.startsWith(publicPath)) {
54
+ ctx.path = path.slice(publicPath.length);
55
+ respond = await files.response(ctx);
56
+ if (respond) {
57
+ return;
58
+ } else {
59
+ ctx.path = path;
60
+ }
61
+ }
62
+ }
63
+ if (!respond) {
64
+ await next();
65
+ }
66
+ };
67
+ }
68
+
69
+ exports.middleware = middleware;
@@ -0,0 +1,42 @@
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 boundary
14
+ */
15
+ // prettier-ignore
16
+ const CHARS = [
17
+ // 0-9
18
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
19
+ // A-M
20
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
21
+ // N-Z
22
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
23
+ // a-m
24
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
25
+ // n-z
26
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
27
+ ];
28
+ /**
29
+ * @function generate
30
+ * @description Generate a boundary.
31
+ */
32
+ function generate() {
33
+ let boundary = '';
34
+ // Create boundary.
35
+ for (let i = 0; i < 38; i++) {
36
+ boundary += CHARS[Math.floor(Math.random() * 62)];
37
+ }
38
+ // Return boundary.
39
+ return boundary;
40
+ }
41
+
42
+ exports.generate = generate;
@@ -0,0 +1,98 @@
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
+
14
+ /**
15
+ * @module common
16
+ */
17
+ const { toString } = Object.prototype;
18
+ const PLUGIN_NAME = 'webpack-dev-service';
19
+ /**
20
+ * @function unixify
21
+ * @description Convert path to unix style.
22
+ * @param path The path to convert.
23
+ */
24
+ function unixify(path) {
25
+ return path.replace(/\\/g, '/');
26
+ }
27
+ /**
28
+ * @function decodeURI
29
+ * @description Decode URI component.
30
+ * @param URI The URI to decode.
31
+ */
32
+ function decodeURI(URI) {
33
+ try {
34
+ return decodeURIComponent(URI);
35
+ } catch (_a) {
36
+ return -1;
37
+ }
38
+ }
39
+ /**
40
+ * @function hasTrailingSlash
41
+ * @description Check if path has trailing slash.
42
+ * @param path The path to check.
43
+ */
44
+ function hasTrailingSlash(path) {
45
+ return /\/$/.test(path);
46
+ }
47
+ function isString(value) {
48
+ return toString.call(value) === '[object String]';
49
+ }
50
+ function isBoolean(value) {
51
+ return toString.call(value) === '[object Boolean]';
52
+ }
53
+ function isFunction(value) {
54
+ return typeof value === 'function';
55
+ }
56
+ /**
57
+ * @function isOutRoot
58
+ * @description Check if path is out of root.
59
+ * @param path The path to check.
60
+ * @param root The root path.
61
+ */
62
+ function isOutRoot(path$1, root) {
63
+ path$1 = path.relative(root, path$1);
64
+ return /\.\.(?:[\\/]|$)/.test(path$1) || path.isAbsolute(path$1);
65
+ }
66
+ function getCompilers(compiler) {
67
+ if (isMultiCompilerMode(compiler)) {
68
+ return compiler.compilers;
69
+ }
70
+ return [compiler];
71
+ }
72
+ /**
73
+ * @function fstat
74
+ * @description Get file stats.
75
+ * @param path The file path.
76
+ */
77
+ function fstat(fs, path) {
78
+ return new Promise((resolve, reject) => {
79
+ fs.stat(path, (error, stats) => {
80
+ error ? reject(error) : resolve(stats);
81
+ });
82
+ });
83
+ }
84
+ function isMultiCompilerMode(compiler) {
85
+ return 'compilers' in compiler;
86
+ }
87
+
88
+ exports.PLUGIN_NAME = PLUGIN_NAME;
89
+ exports.decodeURI = decodeURI;
90
+ exports.fstat = fstat;
91
+ exports.getCompilers = getCompilers;
92
+ exports.hasTrailingSlash = hasTrailingSlash;
93
+ exports.isBoolean = isBoolean;
94
+ exports.isFunction = isFunction;
95
+ exports.isMultiCompilerMode = isMultiCompilerMode;
96
+ exports.isOutRoot = isOutRoot;
97
+ exports.isString = isString;
98
+ exports.unixify = unixify;
@@ -0,0 +1,58 @@
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;
@@ -0,0 +1,71 @@
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;
@@ -0,0 +1,69 @@
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;
@@ -0,0 +1,26 @@
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;
@@ -0,0 +1,95 @@
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;
@@ -0,0 +1,64 @@
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;
@@ -0,0 +1,43 @@
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;