@@ -5,10 +5,12 @@ import {
55} from '@shikijs/twoslash' ;
66import type { ElementContent } from 'hast' ;
77import type { ShikiTransformer } from 'shiki/types' ;
8- import { type TwoslashInstance } from 'twoslash' ;
8+ import { createTwoslasher , type TwoslashInstance } from 'twoslash' ;
99import { createTwoslashFromCDN , TwoslashCdnReturn } from 'twoslash-cdn' ;
1010import * as ts from 'typescript' ;
1111
12+ type TransformerFactory = ( options ?: TransformerTwoslashOptions ) => ShikiTransformer ;
13+
1214const twoslashCompilerOptions : ts . CompilerOptions = {
1315 target : ts . ScriptTarget . ESNext ,
1416 lib : [ 'ESNext' , 'DOM' , 'esnext' , 'dom' , 'es2020' ] ,
@@ -18,15 +20,29 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
1820 alwaysStrict : false ,
1921} ;
2022
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+
2139let cachedInstance : TwoslashCdnReturn | undefined ;
2240
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+ ) ;
2944
45+ export function getCdnTwoslashTransformer ( options : TransformerTwoslashOptions ) : ShikiTransformer {
3046 function getInstance ( ) {
3147 cachedInstance ??= createTwoslashFromCDN ( {
3248 compilerOptions : twoslashCompilerOptions ,
@@ -43,10 +59,6 @@ export async function getCdnTwoslashTransformer(
4359 return cachedInstance ;
4460 }
4561
46- const instance = getInstance ( ) ;
47- await instance . init ( ) ;
48- await instance . prepareTypes ( code ) ;
49-
5062 return createTransformerFactory (
5163 // lazy load Twoslash instance so it works on serverless platforms
5264 ( ( ...args ) => getInstance ( ) . runSync ( ...args ) ) as TwoslashInstance
0 commit comments