-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
101 lines (91 loc) · 2.75 KB
/
test.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
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
92
93
94
95
96
97
98
99
100
101
import { readFile } from "node:fs/promises";
import init, { transform } from "./pkg/tsx.js";
import { CompressionStream } from "node:stream/web";
async function load() {
const wasmData = await readFile(new URL("./pkg/tsx_bg.wasm", import.meta.url));
await init({ module_or_path: wasmData });
let gzSize = 0;
await new Response(wasmData).body.pipeThrough(new CompressionStream("gzip")).pipeTo(
new WritableStream({
write(chunk) {
gzSize += chunk.byteLength;
},
}),
);
console.log(
`%c✔ wasm loaded: ${(wasmData.byteLength / 1024 / 1024).toFixed(2)}MB (gzip: ${Math.ceil(gzSize / 1024)}KB)`,
"color: green;",
);
}
async function test() {
const enc = new TextEncoder();
const dec = new TextDecoder();
const source = `
import { createRoot } from "react-dom/client"
import App from "./App.tsx"
createRoot(document.getElementById("app")).render(<App />)
`;
const ret = transform({
filename: "/source.tsx",
code: enc.encode(source),
importMap: {
"$src": "file:///index.html",
"imports": {
"react": "https://esm.sh/react@18",
"react-dom": "https://esm.sh/react-dom@18",
},
},
sourceMap: "inline",
});
const code = dec.decode(ret.code);
if (!code.includes(`import { jsx as _jsx } from "https://esm.sh/react@18/jsx-runtime"`)) {
console.log(code);
throw new Error("jsx-runtime not imported");
}
if (!code.includes(`import { createRoot } from "https://esm.sh/react-dom@18/client"`)) {
console.log(code);
throw new Error("'react-dom/client' not resolved");
}
if (!code.includes(`import App from "/App.tsx?im=L2luZGV4Lmh0bWw"`)) {
console.log(code);
throw new Error("'/App.tsx' not resolved");
}
if (!code.includes(`_jsx(App`)) {
console.log(code);
throw new Error("jsx not transformed");
}
if (!code.includes("//# sourceMappingURL=data:application/json;charset=utf-8;base64,")) {
console.log(code);
throw new Error("source map not generated");
}
// catch syntax error
try {
const source = `export App() {}`;
transform({
filename: "/source.ts",
code: enc.encode(source),
});
} catch (error) {
if (error.message !== "Expected '{', got 'App' at /source.ts:1:7") {
throw error;
}
}
// use `lang` option
{
const ret = transform({
filename: "/index",
code: enc.encode(`const s: string = "hello"`),
lang: "ts",
});
const code = dec.decode(ret.code);
if (!code.includes(`const s = "hello";`)) {
console.log(code);
throw new Error("lang option not working");
}
}
console.log("%c✔ test passed", "color: green;");
}
if (import.meta.main || process.argv[1] === new URL(import.meta.url).pathname) {
await load();
await test();
}