-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdev-build-html.mjs
47 lines (44 loc) · 1.67 KB
/
dev-build-html.mjs
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
import { BUNDLE_NAME, AUTO_REFRESH_MODULE } from "./dev-config.mjs";
import getRuntimeModules from "./runtime-modules.mjs";
export default (indexFile) =>
Promise.all([
import("jsdom").then((module) => module.default.JSDOM.fromFile(indexFile)),
getRuntimeModules(),
]).then(([dom, runTimeModules]) => {
const { window } = dom;
const { document } = window;
const script = document.createElement("script");
script.textContent = `process={env:${JSON.stringify(process.env)}}`;
document.head.append(script);
document.head.append(
...[BUNDLE_NAME, AUTO_REFRESH_MODULE]
.map((name) => `./${name}`)
.concat(runTimeModules.map(([url]) => url))
.map((relativePath) => {
const scriptTag = document.createElement("script");
scriptTag.type = "module";
scriptTag.src = relativePath;
return scriptTag;
})
);
const noScriptAlert = document.createElement("noscript");
const removeDialogScript = document.createElement("script");
const dialog = document.createElement("dialog");
dialog.open = true;
dialog.id = "placeholder-dialog";
dialog.append(
document.createElement("progress"),
" Building application...",
noScriptAlert
);
noScriptAlert.append(
document.createElement("br"),
document.createElement("br"),
"Warning, your browser in on no-script mode!"
);
removeDialogScript.textContent = `import("./${BUNDLE_NAME}")
.then(()=>document.getElementById("${dialog.id}").remove())
.catch(e=>{document.body.textContent=e.message})`;
document.body.append(dialog, removeDialogScript);
return dom.serialize();
});