webpack-dev-service 0.15.16 → 0.16.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.
Files changed (79) hide show
  1. package/README.md +35 -36
  2. package/cjs/client/HotUpdate.cjs +78 -0
  3. package/cjs/client/HotUpdate.d.cts +26 -0
  4. package/cjs/client/client.cjs +15 -17
  5. package/cjs/client/events.cjs +1 -1
  6. package/cjs/client/index.cjs +1 -1
  7. package/cjs/client/main.cjs +1 -1
  8. package/cjs/client/ui/Overlay.cjs +3 -3
  9. package/cjs/client/ui/Progress.cjs +5 -5
  10. package/cjs/client/ui/images/webpack-logo.svg.cjs +1 -1
  11. package/cjs/client/ui/utils.cjs +12 -11
  12. package/cjs/client/ui/utils.d.cts +6 -2
  13. package/cjs/server/compose.cjs +1 -1
  14. package/cjs/server/dev/ReadStream.cjs +1 -1
  15. package/cjs/server/dev/Service.cjs +1 -1
  16. package/cjs/server/dev/index.cjs +3 -10
  17. package/cjs/server/dev/index.d.cts +1 -1
  18. package/cjs/server/dev/interface.d.cts +0 -3
  19. package/cjs/server/dev/middleware.cjs +8 -15
  20. package/cjs/server/dev/middleware.d.cts +1 -1
  21. package/cjs/server/dev/utils/fs.cjs +1 -1
  22. package/cjs/server/dev/utils/hash.cjs +1 -1
  23. package/cjs/server/dev/utils/http.cjs +1 -1
  24. package/cjs/server/dev/utils/path.cjs +1 -1
  25. package/cjs/server/dev/utils/paths.cjs +1 -1
  26. package/cjs/server/dev/utils/ready.cjs +1 -1
  27. package/cjs/server/dev/utils/setupHooks.cjs +9 -6
  28. package/cjs/server/dev/utils/setupOutputFileSystem.cjs +1 -1
  29. package/cjs/server/dev/utils/setupWatching.cjs +1 -1
  30. package/cjs/server/dev/utils/setupWriteToDisk.cjs +1 -1
  31. package/cjs/server/hot/Socket.cjs +8 -1
  32. package/cjs/server/hot/index.cjs +3 -3
  33. package/cjs/server/hot/utils.cjs +1 -1
  34. package/cjs/server/index.cjs +4 -5
  35. package/cjs/server/index.d.cts +1 -1
  36. package/cjs/server/schema.cjs +1 -1
  37. package/cjs/server/utils.cjs +1 -1
  38. package/esm/client/HotUpdate.d.ts +26 -0
  39. package/esm/client/HotUpdate.js +76 -0
  40. package/esm/client/client.js +16 -18
  41. package/esm/client/events.js +1 -1
  42. package/esm/client/index.js +1 -1
  43. package/esm/client/main.js +1 -1
  44. package/esm/client/ui/Overlay.js +4 -4
  45. package/esm/client/ui/Progress.js +6 -6
  46. package/esm/client/ui/images/webpack-logo.svg.js +1 -1
  47. package/esm/client/ui/utils.d.ts +6 -2
  48. package/esm/client/ui/utils.js +11 -10
  49. package/esm/server/compose.js +1 -1
  50. package/esm/server/dev/ReadStream.js +1 -1
  51. package/esm/server/dev/Service.js +1 -1
  52. package/esm/server/dev/index.d.ts +1 -1
  53. package/esm/server/dev/index.js +3 -10
  54. package/esm/server/dev/interface.d.ts +0 -3
  55. package/esm/server/dev/middleware.d.ts +1 -1
  56. package/esm/server/dev/middleware.js +8 -15
  57. package/esm/server/dev/utils/fs.js +1 -1
  58. package/esm/server/dev/utils/hash.js +1 -1
  59. package/esm/server/dev/utils/http.js +1 -1
  60. package/esm/server/dev/utils/path.js +1 -1
  61. package/esm/server/dev/utils/paths.js +1 -1
  62. package/esm/server/dev/utils/ready.js +1 -1
  63. package/esm/server/dev/utils/setupHooks.js +9 -6
  64. package/esm/server/dev/utils/setupOutputFileSystem.js +1 -1
  65. package/esm/server/dev/utils/setupWatching.js +1 -1
  66. package/esm/server/dev/utils/setupWriteToDisk.js +1 -1
  67. package/esm/server/hot/Socket.js +8 -1
  68. package/esm/server/hot/index.js +3 -3
  69. package/esm/server/hot/utils.js +1 -1
  70. package/esm/server/index.d.ts +1 -1
  71. package/esm/server/index.js +4 -5
  72. package/esm/server/schema.js +1 -1
  73. package/esm/server/utils.js +1 -1
  74. package/global.d.ts +0 -2
  75. package/package.json +5 -5
  76. package/cjs/client/hot.cjs +0 -111
  77. package/cjs/client/hot.d.cts +0 -16
  78. package/esm/client/hot.d.ts +0 -16
  79. package/esm/client/hot.js +0 -108
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -16,11 +16,6 @@ import { getPaths } from './utils/paths.js';
16
16
  * @module middleware
17
17
  */
18
18
  function getFileServices(context, stats) {
19
- const cached = context.services;
20
- // Cache hit.
21
- if (cached) {
22
- return cached;
23
- }
24
19
  const { options } = context;
25
20
  const paths = getPaths(stats);
26
21
  const services = [];
@@ -28,8 +23,6 @@ function getFileServices(context, stats) {
28
23
  for (const [outputPath, publicPath] of paths) {
29
24
  services.push([publicPath, new Service(outputPath, options)]);
30
25
  }
31
- // Cache services.
32
- context.services = services;
33
26
  // Return services.
34
27
  return services;
35
28
  }
@@ -47,24 +40,24 @@ function getFileServicesAsync(context) {
47
40
  }
48
41
  });
49
42
  }
50
- function middleware(context) {
43
+ async function middleware(context) {
44
+ // Get the file services.
45
+ const services = await getFileServicesAsync(context);
51
46
  // Middleware.
52
- return async (ctx, next) => {
53
- const { request } = ctx;
47
+ return async (context, next) => {
48
+ const { request } = context;
54
49
  const pathname = decodeURI(request.path);
55
50
  // Pathname decode failed or includes null byte(s).
56
51
  if (pathname === -1 || pathname.includes('\0')) {
57
- return ctx.throw(400);
52
+ return context.throw(400);
58
53
  }
59
54
  // Get request method.
60
55
  const { method } = request;
61
56
  // Only support GET and HEAD (405).
62
57
  if (method === 'GET' || method === 'HEAD') {
63
- // Get the file services.
64
- const services = await getFileServicesAsync(context);
65
58
  // Try to respond.
66
59
  for (const [publicPath, service] of services) {
67
- if (await service.respond(pathname, ctx, publicPath)) {
60
+ if (await service.respond(pathname, context, publicPath)) {
68
61
  return;
69
62
  }
70
63
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -79,11 +79,14 @@ function setupHooks(context) {
79
79
  onCompilationDone(stats, statsOptions);
80
80
  // Callbacks.
81
81
  const { callbacks } = context;
82
- // Clear callbacks.
83
- context.callbacks = [];
84
- // Call callbacks.
85
- for (const callback of callbacks) {
86
- callback(stats);
82
+ // Call callbacks if there are any.
83
+ if (callbacks.length > 0) {
84
+ // Clear callbacks.
85
+ context.callbacks = [];
86
+ // Call callbacks.
87
+ for (const callback of callbacks) {
88
+ callback(stats);
89
+ }
87
90
  }
88
91
  // Log compilation finished.
89
92
  context.logger.log('compilation finished');
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -44,11 +44,18 @@ class Socket {
44
44
  clients: new Set()
45
45
  };
46
46
  contexts.set(uuid, context);
47
+ this.#setupOutput(compiler, uuid);
47
48
  this.#setupHooks(compiler, context);
48
49
  this.#setupPlugins(compiler, context);
49
50
  }
50
51
  this.#setupWss(contexts);
51
52
  }
53
+ #setupOutput(compiler, uuid) {
54
+ const { output } = compiler.options;
55
+ // Override hot update filename.
56
+ output.hotUpdateChunkFilename = `[id].${uuid}.hot-update.js`;
57
+ output.hotUpdateMainFilename = `[runtime].${uuid}.hot-update.json`;
58
+ }
52
59
  #setupHooks(compiler, context) {
53
60
  const { hooks } = compiler;
54
61
  const statsOptions = getStatsOptions(compiler);
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -15,8 +15,8 @@ import { Socket } from './Socket.js';
15
15
  function hot(compiler, options) {
16
16
  const socket = new Socket(compiler, options);
17
17
  // Middleware.
18
- return async (ctx, next) => {
19
- if (!socket.upgrade(ctx)) {
18
+ return async (context, next) => {
19
+ if (!socket.upgrade(context)) {
20
20
  await next();
21
21
  }
22
22
  };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -14,4 +14,4 @@ export type Options = DevOptions & {
14
14
  * @param compiler The rspack compiler instance.
15
15
  * @param options Options.
16
16
  */
17
- export declare function server(compiler: UnionCompiler, options?: Options): Middleware & Expose;
17
+ export declare function server(compiler: UnionCompiler, options?: Options): Promise<Middleware & Expose>;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -23,7 +23,7 @@ import { dev } from './dev/index.js';
23
23
  * @param compiler The rspack compiler instance.
24
24
  * @param options Options.
25
25
  */
26
- function server(compiler, options = {}) {
26
+ async function server(compiler, options = {}) {
27
27
  validate(schema, options, {
28
28
  name: PLUGIN_NAME,
29
29
  baseDataPath: 'options'
@@ -35,9 +35,8 @@ function server(compiler, options = {}) {
35
35
  // All plugins must be initialized before watching.
36
36
  // Because dev will start watching, so call hot before dev.
37
37
  const hotMiddleware = hot(compiler, hotOptions);
38
- const devMiddleware = dev(compiler, options);
39
- const middleware = compose(devMiddleware, hotMiddleware);
40
- return Object.assign(middleware, devMiddleware, hotMiddleware);
38
+ const devMiddleware = await dev(compiler, options);
39
+ return Object.assign(compose(devMiddleware, hotMiddleware), devMiddleware);
41
40
  }
42
41
 
43
42
  export { server };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package webpack-dev-service
3
3
  * @license MIT
4
- * @version 0.15.16
4
+ * @version 0.16.0
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A koa 2 middleware for webpack development and hot reloading.
7
7
  * @see https://github.com/nuintun/webpack-dev-service#readme
package/global.d.ts CHANGED
@@ -15,5 +15,3 @@ declare const __ESM__: boolean;
15
15
  declare const __HOT_CLIENT__: string;
16
16
 
17
17
  declare const __PLUGIN_NAME__: string;
18
-
19
- declare type HotUpdateStatus = `${webpack.HotUpdateStatus}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpack-dev-service",
3
- "version": "0.15.16",
3
+ "version": "0.16.0",
4
4
  "description": "A koa 2 middleware for webpack development and hot reloading.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -54,7 +54,7 @@
54
54
  "@types/koa": "^3.0.0",
55
55
  "@types/ws": "^8.18.1",
56
56
  "etag": "^1.8.1",
57
- "memfs": "^4.39.0",
57
+ "memfs": "^4.46.1",
58
58
  "range-parser": "^1.2.1",
59
59
  "schema-utils": "^4.3.2",
60
60
  "supports-color": "^10.2.2",
@@ -66,10 +66,10 @@
66
66
  "@rollup/plugin-replace": "^6.0.2",
67
67
  "@rollup/plugin-typescript": "^12.1.4",
68
68
  "@rollup/plugin-url": "^8.0.2",
69
- "@swc/core": "^1.13.5",
69
+ "@swc/core": "^1.13.19",
70
70
  "@swc/helpers": "^0.5.17",
71
71
  "@types/etag": "^1.8.4",
72
- "@types/node": "^24.4.0",
72
+ "@types/node": "^24.5.2",
73
73
  "@types/range-parser": "^1.2.7",
74
74
  "@types/react": "^19.1.13",
75
75
  "@types/react-dom": "^19.1.9",
@@ -83,7 +83,7 @@
83
83
  "react": "^19.1.1",
84
84
  "react-dom": "^19.1.1",
85
85
  "rimraf": "^6.0.1",
86
- "rollup": "^4.50.2",
86
+ "rollup": "^4.52.2",
87
87
  "swc-loader": "^0.2.6",
88
88
  "typescript": "^5.9.2",
89
89
  "webpack": "^5.101.3"
@@ -1,111 +0,0 @@
1
- /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.15.16
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 hot
14
- */
15
- // Last error.
16
- let error;
17
- // Last update hash.
18
- let hash = __webpack_hash__;
19
- // HMR status.
20
- let status = 'idle';
21
- /**
22
- * @function isUpdateAvailable
23
- * @description Is there a newer version of this code available.
24
- */
25
- function isUpdateAvailable() {
26
- // __webpack_hash__ is the hash of the current compilation.
27
- // It's a global variable injected by webpack.
28
- return hash !== __webpack_hash__;
29
- }
30
- /**
31
- * @function updateStatus
32
- * @description Update hot status.
33
- * @param value The new status of the hot update.
34
- */
35
- function updateStatus(value) {
36
- status = value;
37
- }
38
- // Initialize status.
39
- if (module.hot) {
40
- updateStatus(module.hot.status());
41
- }
42
- /**
43
- * @function updateHash
44
- * @description Update webpack hash.
45
- * @param value The new hash value.
46
- */
47
- function updateHash(value) {
48
- hash = value;
49
- }
50
- /**
51
- * @function applyUpdate
52
- * @description Apply update.
53
- * @param hmr Whether to enable HMR.
54
- * @param fallback Fallback function when HMR fail.
55
- */
56
- function applyUpdate(hmr, fallback) {
57
- // Update available.
58
- if (isUpdateAvailable()) {
59
- // HMR enabled.
60
- if (hmr && module.hot) {
61
- switch (status) {
62
- case 'idle':
63
- // Update status.
64
- updateStatus('check');
65
- // Auto check and apply updates.
66
- module.hot
67
- .check(true)
68
- .then(updated => {
69
- // Update status.
70
- updateStatus(module.hot.status());
71
- // When updated modules is available,
72
- // it indicates server is ready to serve new bundle.
73
- if (updated) {
74
- // While update completed, do it again until no update available.
75
- applyUpdate(hmr, fallback);
76
- }
77
- })
78
- .catch(exception => {
79
- // Get status.
80
- const status = module.hot.status();
81
- // Update status.
82
- switch (status) {
83
- case 'fail':
84
- case 'abort':
85
- updateStatus(status);
86
- break;
87
- default:
88
- updateStatus('fail');
89
- break;
90
- }
91
- // Cache error.
92
- error = exception;
93
- // Call fallback.
94
- fallback(error);
95
- });
96
- break;
97
- case 'fail':
98
- case 'abort':
99
- // Call fallback.
100
- fallback(error);
101
- break;
102
- }
103
- } else {
104
- // HMR disabled.
105
- fallback();
106
- }
107
- }
108
- }
109
-
110
- exports.applyUpdate = applyUpdate;
111
- exports.updateHash = updateHash;
@@ -1,16 +0,0 @@
1
- /**
2
- * @module hot
3
- */
4
- /**
5
- * @function updateHash
6
- * @description Update webpack hash.
7
- * @param value The new hash value.
8
- */
9
- export declare function updateHash(value: string): void;
10
- /**
11
- * @function applyUpdate
12
- * @description Apply update.
13
- * @param hmr Whether to enable HMR.
14
- * @param fallback Fallback function when HMR fail.
15
- */
16
- export declare function applyUpdate(hmr: boolean, fallback: (error?: Error) => void): void;
@@ -1,16 +0,0 @@
1
- /**
2
- * @module hot
3
- */
4
- /**
5
- * @function updateHash
6
- * @description Update webpack hash.
7
- * @param value The new hash value.
8
- */
9
- export declare function updateHash(value: string): void;
10
- /**
11
- * @function applyUpdate
12
- * @description Apply update.
13
- * @param hmr Whether to enable HMR.
14
- * @param fallback Fallback function when HMR fail.
15
- */
16
- export declare function applyUpdate(hmr: boolean, fallback: (error?: Error) => void): void;
package/esm/client/hot.js DELETED
@@ -1,108 +0,0 @@
1
- /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.15.16
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
- /**
11
- * @module hot
12
- */
13
- // Last error.
14
- let error;
15
- // Last update hash.
16
- let hash = __webpack_hash__;
17
- // HMR status.
18
- let status = 'idle';
19
- /**
20
- * @function isUpdateAvailable
21
- * @description Is there a newer version of this code available.
22
- */
23
- function isUpdateAvailable() {
24
- // __webpack_hash__ is the hash of the current compilation.
25
- // It's a global variable injected by webpack.
26
- return hash !== __webpack_hash__;
27
- }
28
- /**
29
- * @function updateStatus
30
- * @description Update hot status.
31
- * @param value The new status of the hot update.
32
- */
33
- function updateStatus(value) {
34
- status = value;
35
- }
36
- // Initialize status.
37
- if (import.meta.webpackHot) {
38
- updateStatus(import.meta.webpackHot.status());
39
- }
40
- /**
41
- * @function updateHash
42
- * @description Update webpack hash.
43
- * @param value The new hash value.
44
- */
45
- function updateHash(value) {
46
- hash = value;
47
- }
48
- /**
49
- * @function applyUpdate
50
- * @description Apply update.
51
- * @param hmr Whether to enable HMR.
52
- * @param fallback Fallback function when HMR fail.
53
- */
54
- function applyUpdate(hmr, fallback) {
55
- // Update available.
56
- if (isUpdateAvailable()) {
57
- // HMR enabled.
58
- if (hmr && import.meta.webpackHot) {
59
- switch (status) {
60
- case 'idle':
61
- // Update status.
62
- updateStatus('check');
63
- // Auto check and apply updates.
64
- import.meta.webpackHot
65
- .check(true)
66
- .then(updated => {
67
- // Update status.
68
- updateStatus(import.meta.webpackHot.status());
69
- // When updated modules is available,
70
- // it indicates server is ready to serve new bundle.
71
- if (updated) {
72
- // While update completed, do it again until no update available.
73
- applyUpdate(hmr, fallback);
74
- }
75
- })
76
- .catch(exception => {
77
- // Get status.
78
- const status = import.meta.webpackHot.status();
79
- // Update status.
80
- switch (status) {
81
- case 'fail':
82
- case 'abort':
83
- updateStatus(status);
84
- break;
85
- default:
86
- updateStatus('fail');
87
- break;
88
- }
89
- // Cache error.
90
- error = exception;
91
- // Call fallback.
92
- fallback(error);
93
- });
94
- break;
95
- case 'fail':
96
- case 'abort':
97
- // Call fallback.
98
- fallback(error);
99
- break;
100
- }
101
- } else {
102
- // HMR disabled.
103
- fallback();
104
- }
105
- }
106
- }
107
-
108
- export { applyUpdate, updateHash };