@@ -5,10 +5,12 @@ import {
5
5
} from '@shikijs/twoslash' ;
6
6
import type { ElementContent } from 'hast' ;
7
7
import type { ShikiTransformer } from 'shiki/types' ;
8
- import { type TwoslashInstance } from 'twoslash' ;
8
+ import { createTwoslasher , 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
+
12
14
const twoslashCompilerOptions : ts . CompilerOptions = {
13
15
target : ts . ScriptTarget . ESNext ,
14
16
lib : [ 'ESNext' , 'DOM' , 'esnext' , 'dom' , 'es2020' ] ,
@@ -18,15 +20,29 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
18
20
alwaysStrict : false ,
19
21
} ;
20
22
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
+
21
39
let cachedInstance : TwoslashCdnReturn | undefined ;
22
40
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 ( ) ;
41
+ export const cdnTwoslashTransformer : TransformerFactory = createTransformerFactory (
42
+ cdnTwoslash . runSync
43
+ ) ;
29
44
45
+ export function getCdnTwoslashTransformer ( options : TransformerTwoslashOptions ) : ShikiTransformer {
30
46
function getInstance ( ) {
31
47
cachedInstance ??= createTwoslashFromCDN ( {
32
48
compilerOptions : twoslashCompilerOptions ,
@@ -43,10 +59,6 @@ export async function getCdnTwoslashTransformer(
43
59
return cachedInstance ;
44
60
}
45
61
46
- const instance = getInstance ( ) ;
47
- await instance . init ( ) ;
48
- await instance . prepareTypes ( code ) ;
49
-
50
62
return createTransformerFactory (
51
63
// lazy load Twoslash instance so it works on serverless platforms
52
64
( ( ...args ) => getInstance ( ) . runSync ( ...args ) ) as TwoslashInstance
0 commit comments