Skip to content

invalidate snippet cache when code changes #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { codeToHtml, createCssVariablesTheme } from 'shiki';
import { transformerTwoslash } from '@shikijs/twoslash';
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, smart_quotes, transform } from './utils';
import type { Modules } from './index';
import { fileURLToPath } from 'node:url';

interface SnippetOptions {
file: string | null;
Expand Down Expand Up @@ -508,6 +509,37 @@ function find_nearest_node_modules(file: string): string | null {
return null;
}

/**
* Get the `mtime` of the most recently modified file in a dependency graph,
* excluding imports from `node_modules`
*/
function get_mtime(file: string, seen = new Set<string>()) {
if (seen.has(file)) return -1;
seen.add(file);

let mtime = fs.statSync(file).mtimeMs;
const content = fs.readFileSync(file, 'utf-8');

for (const [_, source] of content.matchAll(/^import(?:.+?\s+from\s+)?['"](.+)['"];?$/gm)) {
if (source[0] !== '.') continue;

let resolved = path.resolve(file, '..', source);
if (!fs.existsSync(resolved)) resolved += '.ts';
if (!fs.existsSync(resolved))
throw new Error(`Could not resolve ${source} relative to ${file}`);

mtime = Math.max(mtime, get_mtime(resolved, seen));
}

return mtime;
}

const mtime = Math.max(
get_mtime(fileURLToPath(import.meta.url)),
fs.statSync('node_modules').mtimeMs,
fs.statSync('../../pnpm-lock.yaml').mtimeMs
);

/**
* Utility function to work with code snippet caching.
*
Expand All @@ -526,12 +558,26 @@ async function create_snippet_cache(should: boolean) {
const cache = new Map();
const directory = find_nearest_node_modules(import.meta.url) + '/.snippets';

if (fs.existsSync(directory)) {
for (const dir of fs.readdirSync(directory)) {
if (!fs.statSync(`${directory}/${dir}`).isDirectory() || +dir < mtime) {
fs.rmSync(`${directory}/${dir}`, { force: true, recursive: true });
}
}
} else {
fs.mkdirSync(directory);
}

try {
fs.mkdirSync(`${directory}/${mtime}`);
} catch {}

function get_file(source: string) {
const hash = createHash('sha256');
hash.update(source);
const digest = hash.digest().toString('base64').replace(/\//g, '-');

return `${directory}/${digest}.html`;
return `${directory}/${mtime}/${digest}.html`;
}

return {
Expand Down Expand Up @@ -681,7 +727,15 @@ async function syntax_highlight({
html = await codeToHtml(source, {
lang: 'ts',
theme,
transformers: [transformerTwoslash({})]
transformers: [
transformerTwoslash({
twoslashOptions: {
compilerOptions: {
types: ['svelte', '@sveltejs/kit']
}
}
})
]
});
} catch (e) {
console.error(`Error compiling snippet in ${filename}`);
Expand Down
Loading