-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve shiki twoslash options (#350)
- Loading branch information
1 parent
f994d2c
commit d5aa2e3
Showing
15 changed files
with
367 additions
and
192 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
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
36 changes: 36 additions & 0 deletions
36
tools/shiki-twoslash/src/node/createFileSystemTypesCache.ts
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,36 @@ | ||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' | ||
import { join } from 'node:path' | ||
import type { TwoslashReturn } from 'twoslash' | ||
import { hash as createHash } from 'vuepress/utils' | ||
import type { TwoslashTypesCache } from './options.js' | ||
|
||
export interface FileSystemTypeResultCacheOptions { | ||
/** | ||
* The directory to store the cache files. | ||
*/ | ||
dir: string | ||
} | ||
|
||
export const createFileSystemTypesCache = ({ | ||
dir, | ||
}: FileSystemTypeResultCacheOptions): TwoslashTypesCache => ({ | ||
init() { | ||
mkdirSync(dir, { recursive: true }) | ||
}, | ||
read(code) { | ||
const hash = createHash(code) | ||
const filePath = join(dir, `${hash}.json`) | ||
if (!existsSync(filePath)) { | ||
return null | ||
} | ||
return JSON.parse( | ||
readFileSync(filePath, { encoding: 'utf-8' }), | ||
) as TwoslashReturn | ||
}, | ||
write(code, data) { | ||
const hash = createHash(code) | ||
const filePath = join(dir, `${hash}.json`) | ||
const json = JSON.stringify(data) | ||
writeFileSync(filePath, json, { encoding: 'utf-8' }) | ||
}, | ||
}) |
Oops, something went wrong.