-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmr.cjs
More file actions
56 lines (52 loc) · 1.56 KB
/
Copy pathhmr.cjs
File metadata and controls
56 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
/**
* Escape a string as a JSON string literal without using the JSON global.
* @param {string} value
* @returns {string}
*/
function jsonString(value) {
return (
'"' +
value
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r") +
'"'
);
}
/**
* Vite HMR accept snippet for Lattish mount modules.
*
* `@tishlang/vite-plugin-tish` injects a self-accepting boundary in compiled `.tish`
* output. Because `.tish` source cannot reference `import.meta`, apps/plugins append
* this snippet (emitted JS only) so hot accept notifies Lattish remount handlers.
*
* @param {string} moduleRelId Project-relative `.tish` path (same id passed to
* `registerLattishHmrRemount`).
* @returns {string}
*/
function viteHmrAcceptSnippet(moduleRelId) {
if (typeof moduleRelId !== "string") {
throw new TypeError("moduleRelId must be a string");
}
var idJson = jsonString(moduleRelId);
return (
"if (import.meta.hot) {\n" +
" import.meta.hot.accept(() => {\n" +
' if (typeof globalThis.__LATTISH_HMR_ACCEPT__ === "function") {\n' +
" globalThis.__LATTISH_HMR_ACCEPT__(" +
idJson +
");\n" +
" }\n" +
" });\n" +
"}\n"
);
}
/** Strip the plugin's bare accept boundary so it can be replaced with viteHmrAcceptSnippet. */
var VITE_PLUGIN_BARE_ACCEPT_RE =
/\nif \(import\.meta\.hot\) \{ import\.meta\.hot\.accept\(\); \}\n?$/;
module.exports = {
viteHmrAcceptSnippet: viteHmrAcceptSnippet,
VITE_PLUGIN_BARE_ACCEPT_RE: VITE_PLUGIN_BARE_ACCEPT_RE,
};