1 | import { ViteNodeRunner } from "__VITE_NODE_CLIENT__"
|
2 |
|
3 | function handleError(msg, error) {
|
4 | console.error(msg, error)
|
5 | process.send("exit")
|
6 | }
|
7 | const runner = new ViteNodeRunner({
|
8 | root: ["__ROOT__"],
|
9 | base: ["__BASE__"],
|
10 | debug: true,
|
11 | fetchModule: async function (id) {
|
12 | return new Promise((resolve) => {
|
13 | try {
|
14 | process.once('message', (msg) => {
|
15 | const message = JSON.parse(msg)
|
16 | if (message.type == "fetched" && id == message.id)
|
17 | resolve(message.md)
|
18 | })
|
19 | process.send({ type: 'fetch', id })
|
20 | } catch (error) {
|
21 | handleError(`Error fetching module {id}`, error)
|
22 | }
|
23 | })
|
24 |
|
25 | },
|
26 | resolveId: async function (id, importer) {
|
27 | return new Promise((resolve) => {
|
28 | try {
|
29 | process.once('message', msg => {
|
30 | const { input, output, type } = JSON.parse(msg)
|
31 | if (type === "resolved") {
|
32 | if (input.id == id && input.importer == importer)
|
33 | resolve(output)
|
34 | }
|
35 | })
|
36 | process.send({ type: 'resolve', payload: { id, importer } })
|
37 | } catch (error) {
|
38 | handleError(`Error fetching module {id}`, error)
|
39 | }
|
40 | })
|
41 | }
|
42 | }
|
43 | );
|
44 | const file = '__FILE__'
|
45 |
|
46 | await runner.executeFile(file).catch(function (error) {
|
47 | handleError(`Error executing file ${file}`, error)
|
48 | });
|