@nishantvij/vite-plugin-hmr-gate 1.1.0 → 1.1.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.
package/dist/index.cjs CHANGED
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
6
10
  var __copyProps = (to, from, except, desc) => {
7
11
  if (from && typeof from === "object" || typeof from === "function") {
8
12
  for (let key of __getOwnPropNames(from))
@@ -14,5 +18,101 @@ var __copyProps = (to, from, except, desc) => {
14
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
19
 
16
20
  // src/index.ts
17
- var index_exports = {};
18
- module.exports = __toCommonJS(index_exports);
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ hmrGatePlugin: () => hmrGatePlugin
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ function hmrGatePlugin(options = {}) {
27
+ const fullReload = options.fullReload ?? true;
28
+ const gatedEvents = new Set(options.events ?? ["change", "add", "unlink"]);
29
+ const passthroughPatterns = options.passthrough ?? [];
30
+ const pending = /* @__PURE__ */ new Map();
31
+ let originalEmit = null;
32
+ let server = null;
33
+ function isPassthrough(filePath) {
34
+ if (typeof filePath !== "string")
35
+ return true;
36
+ if (filePath.includes("node_modules/.vite"))
37
+ return true;
38
+ return passthroughPatterns.some((p) => filePath.includes(p));
39
+ }
40
+ return {
41
+ name: "hmr-gate",
42
+ apply: "serve",
43
+ configureServer(srv) {
44
+ server = srv;
45
+ const _emit = srv.watcher.emit.bind(srv.watcher);
46
+ originalEmit = _emit;
47
+ srv.watcher.emit = (event, ...args) => {
48
+ if (typeof event === "string" && gatedEvents.has(event) && !isPassthrough(args[0])) {
49
+ const filePath = args[0];
50
+ let events = pending.get(filePath);
51
+ if (!events) {
52
+ events = /* @__PURE__ */ new Set();
53
+ pending.set(filePath, events);
54
+ }
55
+ events.add(event);
56
+ return false;
57
+ }
58
+ return _emit(event, ...args);
59
+ };
60
+ srv.middlewares.use((req, res, next) => {
61
+ if (req.method !== "POST" || req.url !== "/__hmr_flush")
62
+ return next();
63
+ const entries = Array.from(pending.entries());
64
+ pending.clear();
65
+ for (const [f] of entries) {
66
+ srv.moduleGraph.onFileChange(f);
67
+ }
68
+ if (entries.length > 0) {
69
+ if (fullReload) {
70
+ srv.ws.send({ type: "full-reload" });
71
+ } else {
72
+ for (const [f, events] of entries) {
73
+ for (const event of events) {
74
+ _emit(event, f);
75
+ }
76
+ }
77
+ }
78
+ }
79
+ const files = entries.map(([f]) => f);
80
+ const body = JSON.stringify({
81
+ flushed: files,
82
+ count: entries.length,
83
+ mode: fullReload ? "full-reload" : "granular"
84
+ });
85
+ res.writeHead(200, { "Content-Type": "application/json" });
86
+ res.end(body);
87
+ });
88
+ srv.middlewares.use((req, res, next) => {
89
+ if (req.method !== "GET" || req.url !== "/__hmr_gate")
90
+ return next();
91
+ const serialized = {};
92
+ for (const [f, events] of pending) {
93
+ serialized[f] = Array.from(events);
94
+ }
95
+ const body = JSON.stringify({
96
+ enabled: true,
97
+ pending: serialized,
98
+ count: pending.size,
99
+ mode: fullReload ? "full-reload" : "granular"
100
+ });
101
+ res.writeHead(200, { "Content-Type": "application/json" });
102
+ res.end(body);
103
+ });
104
+ },
105
+ closeWatcher() {
106
+ if (server && originalEmit) {
107
+ server.watcher.emit = originalEmit;
108
+ }
109
+ pending.clear();
110
+ server = null;
111
+ originalEmit = null;
112
+ }
113
+ };
114
+ }
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ hmrGatePlugin
118
+ });
package/dist/index.js CHANGED
@@ -0,0 +1,93 @@
1
+ // src/index.ts
2
+ function hmrGatePlugin(options = {}) {
3
+ const fullReload = options.fullReload ?? true;
4
+ const gatedEvents = new Set(options.events ?? ["change", "add", "unlink"]);
5
+ const passthroughPatterns = options.passthrough ?? [];
6
+ const pending = /* @__PURE__ */ new Map();
7
+ let originalEmit = null;
8
+ let server = null;
9
+ function isPassthrough(filePath) {
10
+ if (typeof filePath !== "string")
11
+ return true;
12
+ if (filePath.includes("node_modules/.vite"))
13
+ return true;
14
+ return passthroughPatterns.some((p) => filePath.includes(p));
15
+ }
16
+ return {
17
+ name: "hmr-gate",
18
+ apply: "serve",
19
+ configureServer(srv) {
20
+ server = srv;
21
+ const _emit = srv.watcher.emit.bind(srv.watcher);
22
+ originalEmit = _emit;
23
+ srv.watcher.emit = (event, ...args) => {
24
+ if (typeof event === "string" && gatedEvents.has(event) && !isPassthrough(args[0])) {
25
+ const filePath = args[0];
26
+ let events = pending.get(filePath);
27
+ if (!events) {
28
+ events = /* @__PURE__ */ new Set();
29
+ pending.set(filePath, events);
30
+ }
31
+ events.add(event);
32
+ return false;
33
+ }
34
+ return _emit(event, ...args);
35
+ };
36
+ srv.middlewares.use((req, res, next) => {
37
+ if (req.method !== "POST" || req.url !== "/__hmr_flush")
38
+ return next();
39
+ const entries = Array.from(pending.entries());
40
+ pending.clear();
41
+ for (const [f] of entries) {
42
+ srv.moduleGraph.onFileChange(f);
43
+ }
44
+ if (entries.length > 0) {
45
+ if (fullReload) {
46
+ srv.ws.send({ type: "full-reload" });
47
+ } else {
48
+ for (const [f, events] of entries) {
49
+ for (const event of events) {
50
+ _emit(event, f);
51
+ }
52
+ }
53
+ }
54
+ }
55
+ const files = entries.map(([f]) => f);
56
+ const body = JSON.stringify({
57
+ flushed: files,
58
+ count: entries.length,
59
+ mode: fullReload ? "full-reload" : "granular"
60
+ });
61
+ res.writeHead(200, { "Content-Type": "application/json" });
62
+ res.end(body);
63
+ });
64
+ srv.middlewares.use((req, res, next) => {
65
+ if (req.method !== "GET" || req.url !== "/__hmr_gate")
66
+ return next();
67
+ const serialized = {};
68
+ for (const [f, events] of pending) {
69
+ serialized[f] = Array.from(events);
70
+ }
71
+ const body = JSON.stringify({
72
+ enabled: true,
73
+ pending: serialized,
74
+ count: pending.size,
75
+ mode: fullReload ? "full-reload" : "granular"
76
+ });
77
+ res.writeHead(200, { "Content-Type": "application/json" });
78
+ res.end(body);
79
+ });
80
+ },
81
+ closeWatcher() {
82
+ if (server && originalEmit) {
83
+ server.watcher.emit = originalEmit;
84
+ }
85
+ pending.clear();
86
+ server = null;
87
+ originalEmit = null;
88
+ }
89
+ };
90
+ }
91
+ export {
92
+ hmrGatePlugin
93
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nishantvij/vite-plugin-hmr-gate",
3
- "version": "1.1.0",
4
- "description": "Vite plugin that gates HMR updates behind an HTTP flush signal",
3
+ "version": "1.1.1",
4
+ "description": "Vite plugin that gates HMR updates behind an HTTP flush signal (rescoped fork).",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -9,37 +9,8 @@
9
9
  "files": [
10
10
  "dist"
11
11
  ],
12
- "scripts": {
13
- "build": "tsup src/index.ts --format cjs,esm --dts --outDir dist",
14
- "typecheck": "tsgo --noEmit",
15
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
16
- "test": "vitest",
17
- "prepublishOnly": "npm run clean && npm run build",
18
- "clean": "rimraf dist"
19
- },
20
- "keywords": [
21
- "vite",
22
- "plugin",
23
- "hmr",
24
- "hot-module-replacement"
25
- ],
26
12
  "license": "MIT",
27
13
  "peerDependencies": {
28
14
  "vite": ">=5.0.0 <9.0.0"
29
- },
30
- "peerDependenciesMeta": {
31
- "vite": {
32
- "optional": false
33
- }
34
- },
35
- "devDependencies": {
36
- "@types/node": "catalog:",
37
- "rimraf": "^5.0.0",
38
- "tsup": "^7.2.0",
39
- "typescript": "catalog:",
40
- "vitest": "catalog:"
41
- },
42
- "directories": {
43
- "test": "tests"
44
15
  }
45
16
  }