@@ -6,10 +6,12 @@ import babelPresetSolid from 'babel-preset-solid';
6
6
// @ts -ignore
7
7
import { rollup } from 'rollup/dist/es/rollup.browser.js' ;
8
8
import dd from 'dedent' ;
9
+ import type { Plugin } from 'rollup' ;
9
10
10
11
const CDN_URL = 'https://cdn.skypack.dev' ;
11
12
12
13
const tabsLookup = new Map < string , Tab > ( ) ;
14
+ let tabsOutput : { [ key : string ] : string } = { } ;
13
15
14
16
function uid ( str : string ) {
15
17
return Array . from ( str )
@@ -24,7 +26,7 @@ function uid(str: string) {
24
26
*
25
27
* Note: Passing in the Solid Version for later use
26
28
*/
27
- function virtual ( { solidOptions = { } } : { solidOptions : unknown } ) {
29
+ function virtual ( { solidOptions = { } } : { solidOptions : unknown } ) : Plugin {
28
30
return {
29
31
name : 'repl-plugin' ,
30
32
@@ -75,24 +77,31 @@ function virtual({ solidOptions = {} }: { solidOptions: unknown }) {
75
77
76
78
// Compile solid code
77
79
if ( / \. ( j | t ) s x $ / . test ( filename ) ) {
78
- return transform ( code , {
80
+ let transformed = transform ( code , {
79
81
presets : [
80
82
[ babelPresetSolid , solidOptions ] ,
81
83
[ 'typescript' , { onlyRemoveTypeImports : true } ] ,
82
84
] ,
83
85
filename,
84
86
} ) ;
87
+ tabsOutput [ filename ] = transformed . code ;
88
+ return transformed ;
85
89
}
86
90
} ,
87
91
} ;
88
92
}
89
93
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
+ }
95
103
104
+ try {
96
105
const compiler = await rollup ( {
97
106
input : `./${ tabs [ 0 ] . name } ` ,
98
107
plugins : [ virtual ( { solidOptions } ) ] ,
@@ -102,9 +111,9 @@ async function compile(tabs: Tab[], solidOptions = {}): Promise<{ compiled: stri
102
111
output : [ { code } ] ,
103
112
} = await compiler . generate ( { format : 'esm' , inlineDynamicImports : true } ) ;
104
113
105
- return { compiled : code as string } ;
114
+ return { event : 'RESULT' , compiled : code as string , tabs : tabsOutput } ;
106
115
} catch ( e ) {
107
- return { error : ( e as Error ) . message } ;
116
+ return { event : 'RESULT' , error : ( e as Error ) . message } ;
108
117
}
109
118
}
110
119
@@ -114,10 +123,7 @@ self.addEventListener('message', async ({ data }) => {
114
123
switch ( event ) {
115
124
case 'COMPILE' :
116
125
// @ts -ignore
117
- self . postMessage ( {
118
- event : 'RESULT' ,
119
- ...( await compile ( tabs , compileOpts ) ) ,
120
- } ) ;
126
+ self . postMessage ( await compile ( tabs , compileOpts ) ) ;
121
127
break ;
122
128
}
123
129
} ) ;
0 commit comments