@samitouri / QOS-React-2 / commits / 43686787a0

[playground] add link to view sourcemap (#798)

Jan Kassens committed Nov 21, 2022 at 13:19 UTC 43686787a0b9f62d896037485a2d7e8d9601fccf
1 file changed +49 -11
compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx
+49 -11
@@ -58,17 +58,43 @@ export default function HIRTabContent({ source }: { source: string }) {
58 if (flags.leaveSSA) {
59 leaveSSA(ir);
60 }
61 - const output = flags.codegen
62 - ? prettier.format(
63 - generate(codegen(ir)).code.replace("\n\n", "\n"),
64 - {
65 - semi: true,
66 - parser: "babel",
67 - plugins: [prettierParserBabel],
68 - }
69 - )
70 - : printHIR(ir.body);
71 - body = <pre>{output}</pre>;
61 +
62 + if (flags.codegen) {
63 + const ast = codegen(ir);
64 + // TODO: codegen should set this correctly directly
65 + ast.loc = func.node.loc;
66 + const generated = generate(
67 + ast,
68 + {
69 + sourceMaps: true,
70 + sourceFileName: "input.js",
71 + },
72 + source
73 + );
74 + const formatted = prettier.format(generated.code, {
75 + semi: true,
76 + parser: "babel",
77 + plugins: [prettierParserBabel],
78 + });
79 + const sourceMapUrl = getSourceMapUrl(
80 + generated.code,
81 + JSON.stringify(generated.map)
82 + );
83 + body = (
84 + <>
85 + <pre>{formatted}</pre>
86 + {sourceMapUrl && (
87 + <iframe
88 + src={sourceMapUrl}
89 + className="w-full h-96"
90 + title="Generated Code"
91 + />
92 + )}
93 + </>
94 + );
95 + } else {
96 + body = <pre>{printHIR(ir.body)}</pre>;
97 + }
98 } catch (e: any) {
99 body = <div>error: ${e.toString()}</div>;
100 }
@@ -86,6 +112,18 @@ export default function HIRTabContent({ source }: { source: string }) {
112 );
113 }
114
115 +function utf16ToUTF8(s: string): string {
116 + return unescape(encodeURIComponent(s));
117 +}
118 +
119 +function getSourceMapUrl(code: string, map: string): string | null {
120 + code = utf16ToUTF8(code);
121 + map = utf16ToUTF8(map);
122 + return `https://evanw.github.io/source-map-visualization/#${btoa(
123 + `${code.length}\0${code}${map.length}\0${map}`
124 + )}`;
125 +}
126 +
127 type Flags = {
128 eliminateRedundantPhi: boolean;
129 inferReferenceEffects: boolean;