From 6c2482560dcf5882781500aa580d6474f9e21d3d Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 16 Feb 2023 14:03:54 -0500 Subject: [PATCH] feat: add `--perf` flag to skip md plugins --- packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/build/generate.ts | 1 + .../src/core/build/plugins/plugin-ssr.ts | 1 + packages/astro/src/core/config/settings.ts | 4 +++ .../astro/src/core/render/dev/environment.ts | 1 + .../astro/src/vite-plugin-markdown/index.ts | 1 + packages/markdown/remark/src/index.ts | 35 ++++++++++++------- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 818384c99..38c52ba6e 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1010,6 +1010,7 @@ export interface AstroSettings { watchFiles: string[]; forceDisableTelemetry: boolean; timer: AstroTimer; + performanceRun?: boolean; } export type AsyncRendererComponentFn = ( diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 7ae4de6a1..4d5c7fd6b 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -350,6 +350,7 @@ async function generatePath( markdown: { ...settings.config.markdown, contentDir: getContentPaths(settings.config).contentDir, + performanceRun: settings.performanceRun, }, mode: opts.mode, renderers, diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index d4536b92c..ba473431e 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -209,6 +209,7 @@ function buildManifest( markdown: { ...settings.config.markdown, contentDir: getContentPaths(settings.config).contentDir, + performanceRun: settings.performanceRun, }, pageMap: null as any, propagation: Array.from(internals.propagation), diff --git a/packages/astro/src/core/config/settings.ts b/packages/astro/src/core/config/settings.ts index c92222b04..d11ab4d1b 100644 --- a/packages/astro/src/core/config/settings.ts +++ b/packages/astro/src/core/config/settings.ts @@ -27,6 +27,9 @@ export function createBaseSettings(config: AstroConfig): AstroSettings { } export function createSettings(config: AstroConfig, cwd?: string): AstroSettings { + // TODO: hoisted to flags handler + const performanceRun = process.argv.some((arg) => arg === '--perf'); + const tsconfig = loadTSConfig(cwd); const settings = createBaseSettings(config); @@ -39,6 +42,7 @@ export function createSettings(config: AstroConfig, cwd?: string): AstroSettings settings.tsConfig = tsconfig?.config; settings.tsConfigPath = tsconfig?.path; settings.watchFiles = watchFiles; + settings.performanceRun = performanceRun; return settings; } diff --git a/packages/astro/src/core/render/dev/environment.ts b/packages/astro/src/core/render/dev/environment.ts index 7f9fe3195..d4bdc4779 100644 --- a/packages/astro/src/core/render/dev/environment.ts +++ b/packages/astro/src/core/render/dev/environment.ts @@ -25,6 +25,7 @@ export function createDevelopmentEnvironment( markdown: { ...settings.config.markdown, contentDir: getContentPaths(settings.config).contentDir, + performanceRun: settings.performanceRun, }, mode, // This will be overridden in the dev server diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 746bfde36..fe9a65e66 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -72,6 +72,7 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu ...settings.config.markdown, fileURL: new URL(`file://${fileId}`), contentDir: getContentPaths(settings.config).contentDir, + performanceRun: settings.performanceRun, frontmatter: raw.data, }); diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index fdd669280..7a89d084e 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -55,6 +55,7 @@ export async function renderMarkdown( smartypants = markdownConfigDefaults.smartypants, contentDir, frontmatter: userFrontmatter = {}, + performanceRun = false, } = opts; const input = new VFile({ value: content, path: fileURL }); const scopedClassName = opts.$?.scopedClassName; @@ -64,8 +65,10 @@ export async function renderMarkdown( .use(toRemarkInitializeAstroData({ userFrontmatter })) .use([]); - if (gfm) { - parser.use(remarkGfm); + if (!performanceRun) { + if (gfm) { + parser.use(remarkGfm); + } } if (smartypants) { @@ -79,18 +82,20 @@ export async function renderMarkdown( parser.use([[plugin, pluginOpts]]); }); - if (scopedClassName) { - parser.use([scopedStyles(scopedClassName)]); - } + if (!performanceRun) { + if (scopedClassName) { + parser.use([scopedStyles(scopedClassName)]); + } - if (syntaxHighlight === 'shiki') { - parser.use([await remarkShiki(shikiConfig, scopedClassName)]); - } else if (syntaxHighlight === 'prism') { - parser.use([remarkPrism(scopedClassName)]); - } + if (syntaxHighlight === 'shiki') { + parser.use([await remarkShiki(shikiConfig, scopedClassName)]); + } else if (syntaxHighlight === 'prism') { + parser.use([remarkPrism(scopedClassName)]); + } - // Apply later in case user plugins resolve relative image paths - parser.use([toRemarkContentRelImageError({ contentDir })]); + // Apply later in case user plugins resolve relative image paths + parser.use([toRemarkContentRelImageError({ contentDir })]); + } parser.use([ [ @@ -107,7 +112,11 @@ export async function renderMarkdown( parser.use([[plugin, pluginOpts]]); }); - parser.use([rehypeHeadingIds, rehypeRaw]).use(rehypeStringify, { allowDangerousHtml: true }); + if (!performanceRun) { + parser.use([rehypeHeadingIds]); + } + + parser.use([rehypeRaw]).use(rehypeStringify, { allowDangerousHtml: true }); let vfile: MarkdownVFile; try {