@@ -2,83 +2,95 @@ import type * as types from '../shared/types';
2
2
import Go from './wasm_exec.js' ;
3
3
4
4
export const transform : typeof types . transform = ( input , options ) => {
5
- return ensureServiceIsRunning ( ) . transform ( input , options ) ;
5
+ return ensureServiceIsRunning ( ) . transform ( input , options ) ;
6
6
} ;
7
7
8
8
export const parse : typeof types . parse = ( input , options ) => {
9
- return ensureServiceIsRunning ( ) . parse ( input , options ) ;
9
+ return ensureServiceIsRunning ( ) . parse ( input , options ) ;
10
10
} ;
11
11
12
12
export const convertToTSX : typeof types . convertToTSX = ( input , options ) => {
13
- return ensureServiceIsRunning ( ) . convertToTSX ( input , options ) ;
13
+ return ensureServiceIsRunning ( ) . convertToTSX ( input , options ) ;
14
14
} ;
15
15
16
16
interface Service {
17
- transform : typeof types . transform ;
18
- parse : typeof types . parse ;
19
- convertToTSX : typeof types . convertToTSX ;
17
+ transform : typeof types . transform ;
18
+ parse : typeof types . parse ;
19
+ convertToTSX : typeof types . convertToTSX ;
20
20
}
21
21
22
22
let initializePromise : Promise < Service > | undefined ;
23
23
let longLivedService : Service | undefined ;
24
24
25
25
export const teardown : typeof types . teardown = ( ) => {
26
- initializePromise = undefined ;
27
- longLivedService = undefined ;
28
- ( globalThis as any ) [ '@astrojs/compiler' ] = undefined ;
26
+ initializePromise = undefined ;
27
+ longLivedService = undefined ;
28
+ ( globalThis as any ) [ '@astrojs/compiler' ] = undefined ;
29
29
} ;
30
30
31
31
export const initialize : typeof types . initialize = async ( options ) => {
32
- let wasmURL = options . wasmURL ;
33
- if ( ! wasmURL ) throw new Error ( 'Must provide the "wasmURL" option' ) ;
34
- wasmURL += '' ;
35
- if ( ! initializePromise ) {
36
- initializePromise = startRunningService ( wasmURL ) . catch ( ( err ) => {
37
- // Let the caller try again if this fails.
38
- initializePromise = void 0 ;
39
- // But still, throw the error back up the caller.
40
- throw err ;
41
- } ) ;
42
- }
43
- longLivedService = longLivedService || ( await initializePromise ) ;
32
+ let wasmURL = options . wasmURL ;
33
+ if ( ! wasmURL ) throw new Error ( 'Must provide the "wasmURL" option' ) ;
34
+ wasmURL += '' ;
35
+ if ( ! initializePromise ) {
36
+ initializePromise = startRunningService ( wasmURL ) . catch ( ( err ) => {
37
+ // Let the caller try again if this fails.
38
+ initializePromise = void 0 ;
39
+ // But still, throw the error back up the caller.
40
+ throw err ;
41
+ } ) ;
42
+ }
43
+ longLivedService = longLivedService || ( await initializePromise ) ;
44
44
} ;
45
45
46
46
const ensureServiceIsRunning = ( ) : Service => {
47
- if ( ! initializePromise ) throw new Error ( 'You need to call "initialize" before calling this' ) ;
48
- if ( ! longLivedService ) throw new Error ( 'You need to wait for the promise returned from "initialize" to be resolved before calling this' ) ;
49
- return longLivedService ;
47
+ if ( ! initializePromise ) throw new Error ( 'You need to call "initialize" before calling this' ) ;
48
+ if ( ! longLivedService )
49
+ throw new Error (
50
+ 'You need to wait for the promise returned from "initialize" to be resolved before calling this'
51
+ ) ;
52
+ return longLivedService ;
50
53
} ;
51
54
52
- const instantiateWASM = async ( wasmURL : string , importObject : Record < string , any > ) : Promise < WebAssembly . WebAssemblyInstantiatedSource > => {
53
- let response = undefined ;
55
+ const instantiateWASM = async (
56
+ wasmURL : string ,
57
+ importObject : Record < string , any >
58
+ ) : Promise < WebAssembly . WebAssemblyInstantiatedSource > => {
59
+ let response = undefined ;
54
60
55
- if ( WebAssembly . instantiateStreaming ) {
56
- response = await WebAssembly . instantiateStreaming ( fetch ( wasmURL ) , importObject ) ;
57
- } else {
58
- const fetchAndInstantiateTask = async ( ) => {
59
- const wasmArrayBuffer = await fetch ( wasmURL ) . then ( ( res ) => res . arrayBuffer ( ) ) ;
60
- return WebAssembly . instantiate ( wasmArrayBuffer , importObject ) ;
61
- } ;
62
- response = await fetchAndInstantiateTask ( ) ;
63
- }
61
+ if ( WebAssembly . instantiateStreaming ) {
62
+ response = await WebAssembly . instantiateStreaming ( fetch ( wasmURL ) , importObject ) ;
63
+ } else {
64
+ const fetchAndInstantiateTask = async ( ) => {
65
+ const wasmArrayBuffer = await fetch ( wasmURL ) . then ( ( res ) => res . arrayBuffer ( ) ) ;
66
+ return WebAssembly . instantiate ( wasmArrayBuffer , importObject ) ;
67
+ } ;
68
+ response = await fetchAndInstantiateTask ( ) ;
69
+ }
64
70
65
- return response ;
71
+ return response ;
66
72
} ;
67
73
68
74
const startRunningService = async ( wasmURL : string ) : Promise < Service > => {
69
- const go = new Go ( ) ;
70
- const wasm = await instantiateWASM ( wasmURL , go . importObject ) ;
71
- go . run ( wasm . instance ) ;
75
+ const go = new Go ( ) ;
76
+ const wasm = await instantiateWASM ( wasmURL , go . importObject ) ;
77
+ go . run ( wasm . instance ) ;
72
78
73
- const service : any = ( globalThis as any ) [ '@astrojs/compiler' ] ;
79
+ const service : any = ( globalThis as any ) [ '@astrojs/compiler' ] ;
74
80
75
- return {
76
- transform : ( input , options ) => new Promise ( ( resolve ) => resolve ( service . transform ( input , options || { } ) ) ) ,
77
- convertToTSX : ( input , options ) =>
78
- new Promise ( ( resolve ) => resolve ( service . convertToTSX ( input , options || { } ) ) ) . then ( ( result : any ) => ( {
79
- ...result ,
80
- map : JSON . parse ( result . map ) ,
81
- } ) ) ,
82
- parse : ( input , options ) => new Promise ( ( resolve ) => resolve ( service . parse ( input , options || { } ) ) ) . then ( ( result : any ) => ( { ...result , ast : JSON . parse ( result . ast ) } ) ) ,
83
- } ;
81
+ return {
82
+ transform : ( input , options ) =>
83
+ new Promise ( ( resolve ) => resolve ( service . transform ( input , options || { } ) ) ) ,
84
+ convertToTSX : ( input , options ) =>
85
+ new Promise ( ( resolve ) => resolve ( service . convertToTSX ( input , options || { } ) ) ) . then (
86
+ ( result : any ) => ( {
87
+ ...result ,
88
+ map : JSON . parse ( result . map ) ,
89
+ } )
90
+ ) ,
91
+ parse : ( input , options ) =>
92
+ new Promise ( ( resolve ) => resolve ( service . parse ( input , options || { } ) ) ) . then (
93
+ ( result : any ) => ( { ...result , ast : JSON . parse ( result . ast ) } )
94
+ ) ,
95
+ } ;
84
96
} ;
0 commit comments