From d3d1f994f23b562206435715169bc1ad3c8c16aa Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Sat, 29 Jan 2022 23:58:04 -0800 Subject: [PATCH] wip - speed up markdown --- .../astro/src/vite-plugin-markdown/index.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 99888e8b9..e405b6249 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -4,17 +4,30 @@ import type { AstroConfig } from '../@types/astro'; import esbuild from 'esbuild'; import fs from 'fs'; import { transform } from '@astrojs/compiler'; +import resolve from 'resolve'; interface AstroPluginOptions { config: AstroConfig; } +let count = 0; +const buildCache: Record Promise> = {}; + /** Transform .astro files for Vite */ export default function markdown({ config }: AstroPluginOptions): Plugin { return { name: 'astro:markdown', enforce: 'pre', // run transforms before other plugins can + async resolveId(id) { + if (buildCache[id]) { + return id; + } + }, async load(id) { + if (buildCache[id]) { + console.log('LOAD', id); + return {code: await buildCache[id]()}; + } if (id.endsWith('.md')) { let source = await fs.promises.readFile(id, 'utf8'); @@ -31,6 +44,10 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { let renderResult = await render(source, renderOpts); let { frontmatter, metadata, code: astroResult } = renderResult; + const cacheId = `MD_BUILD_${count++}`; + console.log('CREATE', cacheId, id); + buildCache[cacheId] = (async () => { + // Extract special frontmatter keys const { layout = '', components = '', setup = '', ...content } = frontmatter; content.astro = metadata; @@ -68,11 +85,24 @@ ${tsResult}`; // Compile from `.ts` to `.js` const { code, map } = await esbuild.transform(tsResult, { loader: 'ts', sourcemap: 'inline', sourcefile: id }); + return code; + }) return { - code, + code: ` + export const frontmatter = ${JSON.stringify(frontmatter)}; + export default async function render(...args) { + return (await import(${JSON.stringify(cacheId)})).default(...args); + }; + render.isAstroComponentFactory = true;`, + map: null, }; + + // return { + // code, + // map: null, + // }; } return null;