@@ -21,12 +21,13 @@ export interface ImportReference {
2121}
2222
2323export interface Transpile {
24- // TODO: Create a better type for the transpiled result.
25- cell : Record < string , any > ;
26- // TODO: Replace usages of js with cell and remove js here.
27- js : string ;
28- files : FileReference [ ] ;
29- imports : ImportReference [ ] ;
24+ id : string ;
25+ inputs ?: string [ ] ;
26+ outputs ?: string [ ] ;
27+ inline ?: boolean ;
28+ body : string ;
29+ files ?: FileReference [ ] ;
30+ imports ?: ImportReference [ ] ;
3031}
3132
3233export interface TranspileOptions {
@@ -41,7 +42,7 @@ export function transpileJavaScript(input: string, {id, root, ...options}: Trans
4142 . filter ( ( f ) => f . type === "FileAttachment" )
4243 . filter ( ( f ) => canReadSync ( join ( root , f . name ) ) )
4344 . map ( ( f ) => ( { name : f . name , mimeType : mime . getType ( f . name ) } ) ) ;
44- const inputs = Array . from ( new Set ( node . references . map ( ( r ) => r . name ) ) ) ;
45+ const inputs = Array . from ( new Set < string > ( node . references . map ( ( r ) => r . name ) ) ) ;
4546 const output = new Sourcemap ( input ) ;
4647 trim ( output , input ) ;
4748 if ( node . expression && ! inputs . includes ( "display" ) ) {
@@ -51,28 +52,16 @@ export function transpileJavaScript(input: string, {id, root, ...options}: Trans
5152 }
5253 rewriteImports ( output , node ) ;
5354 rewriteFetches ( output , node ) ;
54- const cell = {
55+ return {
5556 id : `${ id } ` ,
5657 ...( inputs . length ? { inputs} : null ) ,
5758 ...( options . inline ? { inline : true } : null ) ,
5859 ...( node . declarations ?. length ? { outputs : node . declarations . map ( ( { name} ) => name ) } : null ) ,
5960 ...( files . length ? { files} : null ) ,
6061 body : `${ node . async ? "async " : "" } (${ inputs } ) => {
6162${ String ( output ) } ${ node . declarations ?. length ? `\nreturn {${ node . declarations . map ( ( { name} ) => name ) } };` : "" }
62- }`
63- } ;
64- return {
65- cell,
66- js : `define({id: ${ id } ${ inputs . length ? `, inputs: ${ JSON . stringify ( inputs ) } ` : "" } ${
67- options . inline ? `, inline: true` : ""
68- } ${ node . declarations ?. length ? `, outputs: ${ JSON . stringify ( node . declarations . map ( ( { name} ) => name ) ) } ` : "" } ${
69- files . length ? `, files: ${ JSON . stringify ( files ) } ` : ""
70- } , body: ${ node . async ? "async " : "" } (${ inputs } ) => {
71- ${ String ( output ) } ${ node . declarations ?. length ? `\nreturn {${ node . declarations . map ( ( { name} ) => name ) } };` : "" }
72- }});
73- ` ,
74- files,
75- imports : node . imports
63+ }` ,
64+ ...( node . imports . length ? { imports : node . imports } : null )
7665 } ;
7766 } catch ( error ) {
7867 if ( ! ( error instanceof SyntaxError ) ) throw error ;
@@ -89,12 +78,8 @@ ${String(output)}${node.declarations?.length ? `\nreturn {${node.declarations.ma
8978 // whether we want to show the file name here.
9079 console . error ( `${ error . name } : ${ message } ` ) ;
9180 return {
92- cell : { } ,
93- // TODO: Add error details to the response to improve code rendering.
94- js : `define({id: ${ id } , body: () => { throw new SyntaxError(${ JSON . stringify ( error . message ) } ); }});
95- ` ,
96- files : [ ] ,
97- imports : [ ]
81+ id : `${ id } ` ,
82+ body : `() => { throw new SyntaxError(${ JSON . stringify ( error . message ) } ); }`
9883 } ;
9984 }
10085}
0 commit comments