|
1 | 1 | import { |
2 | | - createTransformerFactory, |
3 | 2 | rendererRich, |
| 3 | + createTransformerFactory, |
4 | 4 | type TransformerTwoslashOptions, |
5 | 5 | } from '@shikijs/twoslash'; |
6 | 6 | import type { ElementContent } from 'hast'; |
7 | 7 | import type { ShikiTransformer } from 'shiki/types'; |
8 | | -import { createTwoslashFromCDN, type TwoslashCdnReturn } from 'twoslash-cdn'; |
9 | | -import ts from 'typescript'; |
| 8 | +import { createTwoslasher, type TwoslashInstance } from 'twoslash'; |
| 9 | +import { createTwoslashFromCDN, TwoslashCdnReturn } from 'twoslash-cdn'; |
| 10 | +import * as ts from 'typescript'; |
10 | 11 |
|
11 | 12 | type TransformerFactory = (options?: TransformerTwoslashOptions) => ShikiTransformer; |
12 | 13 |
|
13 | 14 | const twoslashCompilerOptions: ts.CompilerOptions = { |
14 | 15 | target: ts.ScriptTarget.ESNext, |
15 | 16 | lib: ['ESNext', 'DOM', 'esnext', 'dom', 'es2020'], |
| 17 | + allowJs: true, |
| 18 | + allowSyntheticDefaultImports: true, |
| 19 | + allowUnreachableCode: true, |
| 20 | + alwaysStrict: false, |
16 | 21 | }; |
17 | 22 |
|
18 | | -export const cdnTwoslash: TwoslashCdnReturn = createTwoslashFromCDN({ |
| 23 | +const fsMap: Map<string, string> = new Map(); |
| 24 | +const twoslashStorageMap = new Map(); |
| 25 | + |
| 26 | +export const cdnTwoslash = createTwoslashFromCDN({ |
19 | 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 | + }, |
20 | 37 | }); |
21 | | -export const cdnTransformerTwoslash: TransformerFactory = createTransformerFactory( |
| 38 | + |
| 39 | +let cachedInstance: TwoslashCdnReturn | undefined; |
| 40 | + |
| 41 | +export const cdnTwoslashTransformer: TransformerFactory = createTransformerFactory( |
22 | 42 | cdnTwoslash.runSync |
23 | 43 | ); |
24 | 44 |
|
| 45 | +export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions): ShikiTransformer { |
| 46 | + function getInstance() { |
| 47 | + cachedInstance ??= createTwoslashFromCDN({ |
| 48 | + compilerOptions: twoslashCompilerOptions, |
| 49 | + fsMap, |
| 50 | + storage: { |
| 51 | + getItemRaw(key) { |
| 52 | + return twoslashStorageMap.get(key); |
| 53 | + }, |
| 54 | + setItemRaw(key, value) { |
| 55 | + twoslashStorageMap.set(key, value); |
| 56 | + }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + return cachedInstance; |
| 60 | + } |
| 61 | + |
| 62 | + return createTransformerFactory( |
| 63 | + // lazy load Twoslash instance so it works on serverless platforms |
| 64 | + ((...args) => getInstance().runSync(...args)) as TwoslashInstance |
| 65 | + )({ |
| 66 | + ...options, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
25 | 70 | function onTwoslashError(err: unknown, code: string, lang: string) { |
26 | 71 | console.error(JSON.stringify({ err, code, lang })); |
27 | 72 | } |
@@ -69,6 +114,7 @@ export function getTwoslashOptions( |
69 | 114 | langs: ['ts', 'typescript', 'js', 'javascript', 'tsx', 'jsx'], |
70 | 115 | explicitTrigger: /mint-twoslash/, |
71 | 116 | twoslashOptions: { |
| 117 | + tsModule: ts, |
72 | 118 | compilerOptions: twoslashCompilerOptions, |
73 | 119 | }, |
74 | 120 | }; |
|
0 commit comments