-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vitepress-twoslash): cache type informations to improve performa…
…nce (#798) Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
Showing
10 changed files
with
160 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { TwoslashTypesCache } from './types' | ||
import { createHash } from 'node:crypto' | ||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' | ||
import { join, resolve } from 'node:path' | ||
import process from 'node:process' | ||
|
||
export interface FileSystemTypeResultCacheOptions { | ||
/** | ||
* The directory to store the cache files. | ||
* | ||
* @default '.vitepress/cache/twoslash' | ||
*/ | ||
dir?: string | ||
} | ||
|
||
export function createFileSystemTypesCache(options: FileSystemTypeResultCacheOptions = {}): TwoslashTypesCache { | ||
const dir = resolve(process.cwd(), options.dir ?? '.vitepress/cache/twoslash') | ||
|
||
return { | ||
init() { | ||
mkdirSync(dir, { recursive: true }) | ||
}, | ||
read(code) { | ||
const hash = createHash('SHA256').update(code).digest('hex').slice(0, 12) | ||
const filePath = join(dir, `${hash}.json`) | ||
if (!existsSync(filePath)) { | ||
return null | ||
} | ||
return JSON.parse(readFileSync(filePath, { encoding: 'utf-8' })) | ||
}, | ||
write(code, data) { | ||
const hash = createHash('SHA256').update(code).digest('hex').slice(0, 12) | ||
const filePath = join(dir, `${hash}.json`) | ||
const json = JSON.stringify(data) | ||
writeFileSync(filePath, json, { encoding: 'utf-8' }) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { TransformerTwoslashOptions } from '@shikijs/twoslash/core' | ||
import type { TwoslashReturn } from 'twoslash' | ||
import type { VueSpecificOptions } from 'twoslash-vue' | ||
import type { TwoslashFloatingVueRendererOptions } from './renderer-floating-vue' | ||
|
||
interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions { | ||
twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions | ||
} | ||
|
||
export interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions { | ||
/** | ||
* Requires adding `twoslash` to the code block explicitly to run twoslash | ||
* @default true | ||
*/ | ||
explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'] | ||
|
||
/** | ||
* The options for caching resolved types | ||
* | ||
* @example | ||
* ```ts | ||
* import { transformerTwoslash } from '@shikijs/vitepress-twoslash' | ||
* import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs' | ||
* | ||
* transformerTwoslash({ | ||
* typesCache: createFileSystemTypesCache({ | ||
* dir: './my-cache-dir' | ||
* }) | ||
* }) | ||
* ``` | ||
*/ | ||
typesCache?: TwoslashTypesCache | ||
} | ||
|
||
export interface TwoslashTypesCache { | ||
/** | ||
* Read cached result | ||
* | ||
* @param code Source code | ||
*/ | ||
read: (code: string) => TwoslashReturn | null | ||
|
||
/** | ||
* Save result to cache | ||
* | ||
* @param code Source code | ||
* @param data Twoslash data | ||
*/ | ||
write: (code: string, data: TwoslashReturn) => void | ||
|
||
/** | ||
* On initialization | ||
*/ | ||
init?: () => void | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters