From f6feff7a2991fb94e11ee1b70ac606e4c053062b Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 27 Jun 2023 10:46:59 -0400 Subject: [PATCH] feat: restart server on markdoc config change (#7467) * deps: vite-plugin-restart * feat: restart on markdoc config change * chore: changeset * chore: roll our own restarter! * deps: remove vite-plugin-restart * refactor: use good enough option --- .changeset/wet-apples-smoke.md | 5 +++ packages/integrations/markdoc/src/index.ts | 32 ++++++++----------- .../integrations/markdoc/src/load-config.ts | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 .changeset/wet-apples-smoke.md diff --git a/.changeset/wet-apples-smoke.md b/.changeset/wet-apples-smoke.md new file mode 100644 index 0000000000..f13bb957d5 --- /dev/null +++ b/.changeset/wet-apples-smoke.md @@ -0,0 +1,5 @@ +--- +'@astrojs/markdoc': patch +--- + +Restart the dev server whenever your markdoc config changes. diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index 5274d36820..8f48dec41a 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -4,7 +4,7 @@ import Markdoc from '@markdoc/markdoc'; import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro'; import crypto from 'node:crypto'; import fs from 'node:fs'; -import { fileURLToPath, pathToFileURL } from 'node:url'; +import { fileURLToPath } from 'node:url'; import { hasContentFlag, isValidUrl, @@ -15,11 +15,15 @@ import { } from './utils.js'; // @ts-expect-error Cannot find module 'astro/assets' or its corresponding type declarations. import { emitESMImage } from 'astro/assets'; -import { bold, red, yellow } from 'kleur/colors'; +import { bold, red } from 'kleur/colors'; import path from 'node:path'; import type * as rollup from 'rollup'; import { normalizePath } from 'vite'; -import { loadMarkdocConfig, type MarkdocConfigResult } from './load-config.js'; +import { + loadMarkdocConfig, + SUPPORTED_MARKDOC_CONFIG_FILES, + type MarkdocConfigResult, +} from './load-config.js'; import { setupConfig } from './runtime.js'; type SetupHookParams = HookParameters<'astro:config:setup'> & { @@ -45,15 +49,13 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration } let markdocConfigResult: MarkdocConfigResult | undefined; let markdocConfigResultId = ''; + let astroConfig: AstroConfig; return { name: '@astrojs/markdoc', hooks: { 'astro:config:setup': async (params) => { - const { - config: astroConfig, - updateConfig, - addContentEntryType, - } = params as SetupHookParams; + const { updateConfig, addContentEntryType } = params as SetupHookParams; + astroConfig = params.config; markdocConfigResult = await loadMarkdocConfig(astroConfig); if (markdocConfigResult) { @@ -204,10 +206,8 @@ export const Content = createComponent({ updateConfig({ vite: { - vite: { - ssr: { - external: ['@astrojs/markdoc/prism', '@astrojs/markdoc/shiki'], - }, + ssr: { + external: ['@astrojs/markdoc/prism', '@astrojs/markdoc/shiki'], }, build: { rollupOptions, @@ -233,12 +233,8 @@ export const Content = createComponent({ }, 'astro:server:setup': async ({ server }) => { server.watcher.on('all', (event, entry) => { - if (prependForwardSlash(pathToFileURL(entry).pathname) === markdocConfigResultId) { - console.log( - yellow( - `${bold('[Markdoc]')} Restart the dev server for config changes to take effect.` - ) - ); + if (SUPPORTED_MARKDOC_CONFIG_FILES.some((f) => entry.endsWith(f))) { + server.restart(); } }); }, diff --git a/packages/integrations/markdoc/src/load-config.ts b/packages/integrations/markdoc/src/load-config.ts index e04cc441bb..a912051b54 100644 --- a/packages/integrations/markdoc/src/load-config.ts +++ b/packages/integrations/markdoc/src/load-config.ts @@ -4,7 +4,7 @@ import * as fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import type { AstroMarkdocConfig } from './config.js'; -const SUPPORTED_MARKDOC_CONFIG_FILES = [ +export const SUPPORTED_MARKDOC_CONFIG_FILES = [ 'markdoc.config.js', 'markdoc.config.mjs', 'markdoc.config.mts',