vite-plugin-enter-dev 0.0.10-alpha.0 → 0.0.11
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 +5 -0
- package/dist/index.js +64 -5
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# vite-plugin-enter-dev
|
|
2
|
+
|
|
3
|
+
Version 0.0.11 adds recovery for full reloads missed before the first HMR connection or while disconnected. Each served HTML document includes the server instance and reload revision. The client compares that generation on initial connection and reconnection; a stale document reloads once, while a current document remains in place.
|
|
4
|
+
|
|
5
|
+
The plugin uses Vite's public WebSocket and HTML transform hooks. It does not depend on optimizer internals or an arbitrary sleep. Existing workspaces must install this version and restart Vite to activate it. Publish 0.0.11 before deploying a Gateway that requires that version.
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import react from '@vitejs/plugin-react';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
2
3
|
|
|
3
4
|
// src/index.ts
|
|
4
5
|
|
|
@@ -283,13 +284,64 @@ function getParentElementType(parentNode) {
|
|
|
283
284
|
}
|
|
284
285
|
return null;
|
|
285
286
|
}
|
|
287
|
+
function reloadGenerationPlugin() {
|
|
288
|
+
const instance = randomUUID();
|
|
289
|
+
let revision = 0;
|
|
290
|
+
let base = "/";
|
|
291
|
+
const generation = () => `${instance}:${revision}`;
|
|
292
|
+
return {
|
|
293
|
+
name: "enter-dev-reload-generation",
|
|
294
|
+
apply: "serve",
|
|
295
|
+
configResolved(config) {
|
|
296
|
+
base = config.base;
|
|
297
|
+
},
|
|
298
|
+
configureServer(server) {
|
|
299
|
+
const originalSend = server.ws.send.bind(server.ws);
|
|
300
|
+
const send = (payload, data) => {
|
|
301
|
+
if (typeof payload === "string") {
|
|
302
|
+
originalSend(payload, data);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (payload.type === "full-reload") revision += 1;
|
|
306
|
+
originalSend(payload);
|
|
307
|
+
};
|
|
308
|
+
server.ws.send = send;
|
|
309
|
+
server.ws.on("enter:check-generation", (data, client) => {
|
|
310
|
+
if (data?.generation !== generation()) {
|
|
311
|
+
client.send("enter:reload-required", { generation: generation() });
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
server.httpServer?.once("close", () => {
|
|
315
|
+
if (server.ws.send === send) server.ws.send = originalSend;
|
|
316
|
+
});
|
|
317
|
+
},
|
|
318
|
+
transformIndexHtml() {
|
|
319
|
+
return [
|
|
320
|
+
{
|
|
321
|
+
tag: "script",
|
|
322
|
+
attrs: { type: "module" },
|
|
323
|
+
injectTo: "head-prepend",
|
|
324
|
+
children: `import { createHotContext } from ${JSON.stringify(`${base}@vite/client`)};
|
|
325
|
+
const generation = ${JSON.stringify(generation())};
|
|
326
|
+
const hot = createHotContext('/@enter/reload-generation');
|
|
327
|
+
let reloading = false;
|
|
328
|
+
hot.on('enter:reload-required', () => {
|
|
329
|
+
if (reloading) return;
|
|
330
|
+
reloading = true;
|
|
331
|
+
window.location.reload();
|
|
332
|
+
});
|
|
333
|
+
const check = () => hot.send('enter:check-generation', { generation });
|
|
334
|
+
hot.on('vite:ws:connect', check);
|
|
335
|
+
check();`
|
|
336
|
+
}
|
|
337
|
+
];
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
}
|
|
286
341
|
|
|
287
342
|
// src/index.ts
|
|
288
343
|
function enterDevPlugin(options = {}) {
|
|
289
|
-
const {
|
|
290
|
-
injectMessageListener = true,
|
|
291
|
-
react: enableReact = true
|
|
292
|
-
} = options;
|
|
344
|
+
const { injectMessageListener = true, react: enableReact = true } = options;
|
|
293
345
|
const reactPlugin = enableReact ? react({
|
|
294
346
|
// 确保在开发模式下启用JSX源码映射
|
|
295
347
|
jsxImportSource: void 0,
|
|
@@ -502,7 +554,14 @@ function enterDevPlugin(options = {}) {
|
|
|
502
554
|
}
|
|
503
555
|
}
|
|
504
556
|
};
|
|
505
|
-
return [
|
|
557
|
+
return [
|
|
558
|
+
...reactPlugin,
|
|
559
|
+
htmlInjectPlugin,
|
|
560
|
+
sandboxServerPlugin,
|
|
561
|
+
routerBasePlugin,
|
|
562
|
+
publicAssetReloadPlugin,
|
|
563
|
+
reloadGenerationPlugin()
|
|
564
|
+
];
|
|
506
565
|
}
|
|
507
566
|
function enterProdPlugin(options = {}) {
|
|
508
567
|
const {
|