0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

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
This commit is contained in:
Ben Holmes 2023-06-27 10:46:59 -04:00 committed by GitHub
parent 2726098bc8
commit f6feff7a29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 19 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/markdoc': patch
---
Restart the dev server whenever your markdoc config changes.

View file

@ -4,7 +4,7 @@ import Markdoc from '@markdoc/markdoc';
import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro'; import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import fs from 'node:fs'; import fs from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url'; import { fileURLToPath } from 'node:url';
import { import {
hasContentFlag, hasContentFlag,
isValidUrl, isValidUrl,
@ -15,11 +15,15 @@ import {
} from './utils.js'; } from './utils.js';
// @ts-expect-error Cannot find module 'astro/assets' or its corresponding type declarations. // @ts-expect-error Cannot find module 'astro/assets' or its corresponding type declarations.
import { emitESMImage } from 'astro/assets'; import { emitESMImage } from 'astro/assets';
import { bold, red, yellow } from 'kleur/colors'; import { bold, red } from 'kleur/colors';
import path from 'node:path'; import path from 'node:path';
import type * as rollup from 'rollup'; import type * as rollup from 'rollup';
import { normalizePath } from 'vite'; 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'; import { setupConfig } from './runtime.js';
type SetupHookParams = HookParameters<'astro:config:setup'> & { type SetupHookParams = HookParameters<'astro:config:setup'> & {
@ -45,15 +49,13 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
} }
let markdocConfigResult: MarkdocConfigResult | undefined; let markdocConfigResult: MarkdocConfigResult | undefined;
let markdocConfigResultId = ''; let markdocConfigResultId = '';
let astroConfig: AstroConfig;
return { return {
name: '@astrojs/markdoc', name: '@astrojs/markdoc',
hooks: { hooks: {
'astro:config:setup': async (params) => { 'astro:config:setup': async (params) => {
const { const { updateConfig, addContentEntryType } = params as SetupHookParams;
config: astroConfig, astroConfig = params.config;
updateConfig,
addContentEntryType,
} = params as SetupHookParams;
markdocConfigResult = await loadMarkdocConfig(astroConfig); markdocConfigResult = await loadMarkdocConfig(astroConfig);
if (markdocConfigResult) { if (markdocConfigResult) {
@ -204,10 +206,8 @@ export const Content = createComponent({
updateConfig({ updateConfig({
vite: { vite: {
vite: { ssr: {
ssr: { external: ['@astrojs/markdoc/prism', '@astrojs/markdoc/shiki'],
external: ['@astrojs/markdoc/prism', '@astrojs/markdoc/shiki'],
},
}, },
build: { build: {
rollupOptions, rollupOptions,
@ -233,12 +233,8 @@ export const Content = createComponent({
}, },
'astro:server:setup': async ({ server }) => { 'astro:server:setup': async ({ server }) => {
server.watcher.on('all', (event, entry) => { server.watcher.on('all', (event, entry) => {
if (prependForwardSlash(pathToFileURL(entry).pathname) === markdocConfigResultId) { if (SUPPORTED_MARKDOC_CONFIG_FILES.some((f) => entry.endsWith(f))) {
console.log( server.restart();
yellow(
`${bold('[Markdoc]')} Restart the dev server for config changes to take effect.`
)
);
} }
}); });
}, },

View file

@ -4,7 +4,7 @@ import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import type { AstroMarkdocConfig } from './config.js'; import type { AstroMarkdocConfig } from './config.js';
const SUPPORTED_MARKDOC_CONFIG_FILES = [ export const SUPPORTED_MARKDOC_CONFIG_FILES = [
'markdoc.config.js', 'markdoc.config.js',
'markdoc.config.mjs', 'markdoc.config.mjs',
'markdoc.config.mts', 'markdoc.config.mts',