-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
91 lines (74 loc) · 2.39 KB
/
main.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { WebContainer } from "@webcontainer/api";
import { sample } from "./sample";
import "./style.css";
/** @type {import('@webcontainer/api').WebContainer} */
let webcontainerInstance;
document.querySelector("body").innerHTML = `
<div class="header">
<div>
Paste <a href="https://webcontainers.io/api#filesystemtree" target="_blank" rel="noopener noreferrer">FileSystemTree</a> JSON object here and click
<button id="run">Boot</button>
</div>
<div>
Sample:
<select>
<option value=""></option>
<option value="viteReactJs">Vite React + JavaScript</option>
</select>
</div>
</div>
<div class="container">
<div class="editor">
<textarea></textarea>
</div>
<div class="preview">
<iframe src="loading.html"></iframe>
</div>
</div>
<div id="log">`;
window.addEventListener("load", async () => {
/** @type {HTMLIFrameElement | null} */
const iframeEl = document.querySelector("iframe");
/** @type {HTMLTextAreaElement | null} */
const textareaEl = document.querySelector("textarea");
/** @type {HTMLButtonElement | null} */
const buttonEl = document.querySelector("button");
/** @type {HTMLButtonElement | null} */
const logEl = document.querySelector("#log");
/** @type {HTMLSelectElement | null} */
const selectEl = document.querySelector("select");
// Call only once
webcontainerInstance = await WebContainer.boot();
webcontainerInstance.on("server-ready", (port, url) => {
iframeEl.src = url;
});
buttonEl.addEventListener("click", async () => {
const files = textareaEl?.value;
try {
await webcontainerInstance?.mount(JSON.parse(files));
} catch (error) {
console.error(error);
return;
}
const installProcess = await webcontainerInstance.spawn("npm", ["install"]);
installProcess.output.pipeTo(
new WritableStream({
write(data) {
const div = document.createElement("div");
div.textContent = data;
logEl.append(div);
},
})
);
const installExitCode = await installProcess.exit;
if (installExitCode !== 0) {
throw new Error("Unable to run npm install");
}
await webcontainerInstance.spawn("npm", ["run", "dev"]);
});
selectEl.addEventListener("change", (event) => {
const value = event.target.value;
const files = sample[value];
textareaEl.value = files ? JSON.stringify(files, null, 2) : "";
});
});