@@ -5,12 +5,10 @@ import {
5
5
} from '@shikijs/twoslash' ;
6
6
import type { ElementContent } from 'hast' ;
7
7
import type { ShikiTransformer } from 'shiki/types' ;
8
- import { createTwoslasher , type TwoslashInstance } from 'twoslash' ;
8
+ import { type TwoslashInstance } from 'twoslash' ;
9
9
import { createTwoslashFromCDN , TwoslashCdnReturn } from 'twoslash-cdn' ;
10
10
import * as ts from 'typescript' ;
11
11
12
- type TransformerFactory = ( options ?: TransformerTwoslashOptions ) => ShikiTransformer ;
13
-
14
12
const twoslashCompilerOptions : ts . CompilerOptions = {
15
13
target : ts . ScriptTarget . ESNext ,
16
14
lib : [ 'ESNext' , 'DOM' , 'esnext' , 'dom' , 'es2020' ] ,
@@ -20,29 +18,15 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
20
18
alwaysStrict : false ,
21
19
} ;
22
20
23
- const fsMap : Map < string , string > = new Map ( ) ;
24
- const twoslashStorageMap = new Map ( ) ;
25
-
26
- export const cdnTwoslash = createTwoslashFromCDN ( {
27
- compilerOptions : twoslashCompilerOptions ,
28
- fsMap,
29
- storage : {
30
- getItemRaw ( key ) {
31
- return twoslashStorageMap . get ( key ) ;
32
- } ,
33
- setItemRaw ( key , value ) {
34
- twoslashStorageMap . set ( key , value ) ;
35
- } ,
36
- } ,
37
- } ) ;
38
-
39
21
let cachedInstance : TwoslashCdnReturn | undefined ;
40
22
41
- export const cdnTwoslashTransformer : TransformerFactory = createTransformerFactory (
42
- cdnTwoslash . runSync
43
- ) ;
23
+ export async function getCdnTwoslashTransformer (
24
+ code : string ,
25
+ options : TransformerTwoslashOptions
26
+ ) : Promise < ShikiTransformer > {
27
+ const fsMap : Map < string , string > = new Map ( ) ;
28
+ const twoslashStorageMap = new Map ( ) ;
44
29
45
- export function getCdnTwoslashTransformer ( options : TransformerTwoslashOptions ) : ShikiTransformer {
46
30
function getInstance ( ) {
47
31
cachedInstance ??= createTwoslashFromCDN ( {
48
32
compilerOptions : twoslashCompilerOptions ,
@@ -59,6 +43,10 @@ export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions):
59
43
return cachedInstance ;
60
44
}
61
45
46
+ const instance = getInstance ( ) ;
47
+ await instance . init ( ) ;
48
+ await instance . prepareTypes ( code ) ;
49
+
62
50
return createTransformerFactory (
63
51
// lazy load Twoslash instance so it works on serverless platforms
64
52
( ( ...args ) => getInstance ( ) . runSync ( ...args ) ) as TwoslashInstance
0 commit comments