wip - speed up markdown
This commit is contained in:
parent
f46afa98c6
commit
d3d1f994f2
1 changed files with 31 additions and 1 deletions
|
@ -4,17 +4,30 @@ import type { AstroConfig } from '../@types/astro';
|
||||||
import esbuild from 'esbuild';
|
import esbuild from 'esbuild';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { transform } from '@astrojs/compiler';
|
import { transform } from '@astrojs/compiler';
|
||||||
|
import resolve from 'resolve';
|
||||||
|
|
||||||
interface AstroPluginOptions {
|
interface AstroPluginOptions {
|
||||||
config: AstroConfig;
|
config: AstroConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let count = 0;
|
||||||
|
const buildCache: Record<string, () => Promise<string>> = {};
|
||||||
|
|
||||||
/** Transform .astro files for Vite */
|
/** Transform .astro files for Vite */
|
||||||
export default function markdown({ config }: AstroPluginOptions): Plugin {
|
export default function markdown({ config }: AstroPluginOptions): Plugin {
|
||||||
return {
|
return {
|
||||||
name: 'astro:markdown',
|
name: 'astro:markdown',
|
||||||
enforce: 'pre', // run transforms before other plugins can
|
enforce: 'pre', // run transforms before other plugins can
|
||||||
|
async resolveId(id) {
|
||||||
|
if (buildCache[id]) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
},
|
||||||
async load(id) {
|
async load(id) {
|
||||||
|
if (buildCache[id]) {
|
||||||
|
console.log('LOAD', id);
|
||||||
|
return {code: await buildCache[id]()};
|
||||||
|
}
|
||||||
if (id.endsWith('.md')) {
|
if (id.endsWith('.md')) {
|
||||||
let source = await fs.promises.readFile(id, 'utf8');
|
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 renderResult = await render(source, renderOpts);
|
||||||
let { frontmatter, metadata, code: astroResult } = renderResult;
|
let { frontmatter, metadata, code: astroResult } = renderResult;
|
||||||
|
|
||||||
|
const cacheId = `MD_BUILD_${count++}`;
|
||||||
|
console.log('CREATE', cacheId, id);
|
||||||
|
buildCache[cacheId] = (async () => {
|
||||||
|
|
||||||
// Extract special frontmatter keys
|
// Extract special frontmatter keys
|
||||||
const { layout = '', components = '', setup = '', ...content } = frontmatter;
|
const { layout = '', components = '', setup = '', ...content } = frontmatter;
|
||||||
content.astro = metadata;
|
content.astro = metadata;
|
||||||
|
@ -68,11 +85,24 @@ ${tsResult}`;
|
||||||
|
|
||||||
// Compile from `.ts` to `.js`
|
// Compile from `.ts` to `.js`
|
||||||
const { code, map } = await esbuild.transform(tsResult, { loader: 'ts', sourcemap: 'inline', sourcefile: id });
|
const { code, map } = await esbuild.transform(tsResult, { loader: 'ts', sourcemap: 'inline', sourcefile: id });
|
||||||
|
return code;
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
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,
|
map: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// return {
|
||||||
|
// code,
|
||||||
|
// map: null,
|
||||||
|
// };
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue