Skip to content

Commit c97ab57

Browse files
committed
show raw babel output
1 parent 8c721a3 commit c97ab57

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/components/repl.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export const Repl: typeof ReplProps = (props) => {
3636
const [store, setStore] = createStore({
3737
error: '',
3838
compiled: '',
39+
compiledTabs: {
40+
[`./${props.current}`]: '',
41+
},
3942
mode: 'DOM' as keyof typeof compileMode,
4043
isCompiling: false,
4144
get compileMode(): ValueOf<typeof compileMode> {
@@ -50,8 +53,8 @@ export const Repl: typeof ReplProps = (props) => {
5053
if (idx < 0) return;
5154
props.setCurrent(current);
5255
},
53-
setCompiled(compiled: string) {
54-
setStore({ compiled, isCompiling: false });
56+
setCompiled(compiled: string, tabs: Record<string, string>) {
57+
setStore({ compiled, isCompiling: false, compiledTabs: tabs });
5558
},
5659
setTabName(id1: string, name: string) {
5760
const idx = props.tabs.findIndex((tab) => id(tab) === id1);
@@ -136,12 +139,12 @@ export const Repl: typeof ReplProps = (props) => {
136139
const { event } = data;
137140

138141
if (event === 'RESULT') {
139-
const { compiled, error } = data;
142+
const { compiled, tabs, error } = data;
140143

141144
if (error) return setStore({ error });
142145
if (!compiled) return;
143146

144-
actions.setCompiled(compiled);
147+
actions.setCompiled(compiled, tabs);
145148

146149
console.log('Compilation took:', formatMs(performance.now() - now));
147150
}
@@ -396,7 +399,7 @@ export const Repl: typeof ReplProps = (props) => {
396399
</TabList>
397400

398401
<div class="h-full row-start-2 flex flex-col overflow-hidden">
399-
<MonacoTabs tabs={props.tabs} compiled={store.compiled} folder={props.id} />
402+
<MonacoTabs tabs={props.tabs} compiled={store.compiledTabs[`./${props.current}`] || ''} folder={props.id} />
400403

401404
<Editor
402405
url={`file:///${props.id}/${props.current}`}

src/workers/compiler.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import babelPresetSolid from 'babel-preset-solid';
66
// @ts-ignore
77
import { rollup } from 'rollup/dist/es/rollup.browser.js';
88
import dd from 'dedent';
9+
import type { Plugin } from 'rollup';
910

1011
const CDN_URL = 'https://cdn.skypack.dev';
1112

1213
const tabsLookup = new Map<string, Tab>();
14+
let tabsOutput: { [key: string]: string } = {};
1315

1416
function uid(str: string) {
1517
return Array.from(str)
@@ -24,7 +26,7 @@ function uid(str: string) {
2426
*
2527
* Note: Passing in the Solid Version for later use
2628
*/
27-
function virtual({ solidOptions = {} }: { solidOptions: unknown }) {
29+
function virtual({ solidOptions = {} }: { solidOptions: unknown }): Plugin {
2830
return {
2931
name: 'repl-plugin',
3032

@@ -75,24 +77,31 @@ function virtual({ solidOptions = {} }: { solidOptions: unknown }) {
7577

7678
// Compile solid code
7779
if (/\.(j|t)sx$/.test(filename)) {
78-
return transform(code, {
80+
let transformed = transform(code, {
7981
presets: [
8082
[babelPresetSolid, solidOptions],
8183
['typescript', { onlyRemoveTypeImports: true }],
8284
],
8385
filename,
8486
});
87+
tabsOutput[filename] = transformed.code;
88+
return transformed;
8589
}
8690
},
8791
};
8892
}
8993

90-
async function compile(tabs: Tab[], solidOptions = {}): Promise<{ compiled: string } | { error: string }> {
91-
try {
92-
for (const tab of tabs) {
93-
tabsLookup.set(`./${tab.name}.${tab.type}`, tab);
94-
}
94+
async function compile(
95+
tabs: Tab[],
96+
solidOptions = {},
97+
): Promise<{ event: 'RESULT' } & ({ compiled: string; tabs: { [key: string]: string } } | { error: string })> {
98+
tabsOutput = {};
99+
tabsLookup.clear();
100+
for (const tab of tabs) {
101+
tabsLookup.set(`./${tab.name}.${tab.type}`, tab);
102+
}
95103

104+
try {
96105
const compiler = await rollup({
97106
input: `./${tabs[0].name}`,
98107
plugins: [virtual({ solidOptions })],
@@ -102,9 +111,9 @@ async function compile(tabs: Tab[], solidOptions = {}): Promise<{ compiled: stri
102111
output: [{ code }],
103112
} = await compiler.generate({ format: 'esm', inlineDynamicImports: true });
104113

105-
return { compiled: code as string };
114+
return { event: 'RESULT', compiled: code as string, tabs: tabsOutput };
106115
} catch (e) {
107-
return { error: (e as Error).message };
116+
return { event: 'RESULT', error: (e as Error).message };
108117
}
109118
}
110119

@@ -114,10 +123,7 @@ self.addEventListener('message', async ({ data }) => {
114123
switch (event) {
115124
case 'COMPILE':
116125
// @ts-ignore
117-
self.postMessage({
118-
event: 'RESULT',
119-
...(await compile(tabs, compileOpts)),
120-
});
126+
self.postMessage(await compile(tabs, compileOpts));
121127
break;
122128
}
123129
});

0 commit comments

Comments
 (0)