@@ -5,12 +5,10 @@ import {
55} from '@shikijs/twoslash' ;
66import type { ElementContent } from 'hast' ;
77import type { ShikiTransformer } from 'shiki/types' ;
8- import { createTwoslasher , type TwoslashInstance } from 'twoslash' ;
8+ import { type TwoslashInstance } from 'twoslash' ;
99import { createTwoslashFromCDN , TwoslashCdnReturn } from 'twoslash-cdn' ;
1010import * as ts from 'typescript' ;
1111
12- type TransformerFactory = ( options ?: TransformerTwoslashOptions ) => ShikiTransformer ;
13-
1412const twoslashCompilerOptions : ts . CompilerOptions = {
1513 target : ts . ScriptTarget . ESNext ,
1614 lib : [ 'ESNext' , 'DOM' , 'esnext' , 'dom' , 'es2020' ] ,
@@ -20,29 +18,15 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
2018 alwaysStrict : false ,
2119} ;
2220
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-
3921let cachedInstance : TwoslashCdnReturn | undefined ;
4022
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 ( ) ;
4429
45- export function getCdnTwoslashTransformer ( options : TransformerTwoslashOptions ) : ShikiTransformer {
4630 function getInstance ( ) {
4731 cachedInstance ??= createTwoslashFromCDN ( {
4832 compilerOptions : twoslashCompilerOptions ,
@@ -59,6 +43,10 @@ export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions):
5943 return cachedInstance ;
6044 }
6145
46+ const instance = getInstance ( ) ;
47+ await instance . init ( ) ;
48+ await instance . prepareTypes ( code ) ;
49+
6250 return createTransformerFactory (
6351 // lazy load Twoslash instance so it works on serverless platforms
6452 ( ( ...args ) => getInstance ( ) . runSync ( ...args ) ) as TwoslashInstance
0 commit comments