From 94503a7fb387f45ad92c3ee8293e68d4be03b1d6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 23 Jun 2023 16:28:07 -0400 Subject: [PATCH 1/6] deps: vite-plugin-restart --- packages/integrations/markdoc/package.json | 1 + pnpm-lock.yaml | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/integrations/markdoc/package.json b/packages/integrations/markdoc/package.json index 8604bda5ad36..fcc4bb77ce07 100644 --- a/packages/integrations/markdoc/package.json +++ b/packages/integrations/markdoc/package.json @@ -70,6 +70,7 @@ "gray-matter": "^4.0.3", "kleur": "^4.1.5", "shiki": "^0.14.1", + "vite-plugin-restart": "^0.3.1", "zod": "^3.17.3" }, "peerDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b9f479ae3025..ee5250db928a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3989,6 +3989,9 @@ importers: shiki: specifier: ^0.14.1 version: 0.14.1 + vite-plugin-restart: + specifier: ^0.3.1 + version: 0.3.1(vite@4.3.9) zod: specifier: ^3.17.3 version: 3.20.6 @@ -17285,6 +17288,18 @@ packages: - supports-color dev: false + /vite-plugin-restart@0.3.1(vite@4.3.9): + resolution: {integrity: sha512-LVe74MLUwaOzXVUgRGiphYbAYNgdGF6mP5vxj5BKcKkdmzvuuw79qcNgn9kHKKaIlCCjmOUiY3qlJ8jBSBUCoA==} + peerDependencies: + vite: ^2.9.0 || ^3.0.0 || ^4.0.0 + peerDependenciesMeta: + vite: + optional: true + dependencies: + micromatch: 4.0.5 + vite: 4.3.9(@types/node@18.16.18)(sass@1.63.4) + dev: false + /vite@4.3.9(@types/node@14.18.51): resolution: {integrity: sha512-qsTNZjO9NoJNW7KnOrgYwczm0WctJ8m/yqYAMAK9Lxt4SoySUfS5S8ia9K7JHpa3KEeMfyF8LoJ3c5NeBJy6pg==} engines: {node: ^14.18.0 || >=16.0.0} From ebf889cd6ab74841d69fe66db1e63aa6e379adbb Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 23 Jun 2023 16:28:55 -0400 Subject: [PATCH 2/6] feat: restart on markdoc config change --- packages/integrations/markdoc/src/index.ts | 31 ++++++++----------- .../integrations/markdoc/src/load-config.ts | 2 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index 5274d36820de..c822cfbe2ed5 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,16 @@ 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 vitePluginRestart from 'vite-plugin-restart'; 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'> & { @@ -204,15 +209,16 @@ 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, }, plugins: [ + vitePluginRestart({ + restart: SUPPORTED_MARKDOC_CONFIG_FILES, + }), { name: '@astrojs/markdoc:astro-propagated-assets', enforce: 'pre', @@ -231,17 +237,6 @@ 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.` - ) - ); - } - }); - }, }, }; } diff --git a/packages/integrations/markdoc/src/load-config.ts b/packages/integrations/markdoc/src/load-config.ts index e04cc441bb93..a912051b54fc 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', From d75d44dd5ea66b17ef107892422f37836d7dad3a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 23 Jun 2023 16:34:08 -0400 Subject: [PATCH 3/6] chore: changeset --- .changeset/wet-apples-smoke.md | 5 +++++ 1 file changed, 5 insertions(+) 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 000000000000..f13bb957d525 --- /dev/null +++ b/.changeset/wet-apples-smoke.md @@ -0,0 +1,5 @@ +--- +'@astrojs/markdoc': patch +--- + +Restart the dev server whenever your markdoc config changes. From 1397bd4b278389637dbe8a0c11cce3107083dace Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 26 Jun 2023 17:59:49 -0400 Subject: [PATCH 4/6] chore: roll our own restarter! --- packages/integrations/markdoc/src/index.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index c822cfbe2ed5..f2849fc8729d 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -16,7 +16,6 @@ import { // @ts-expect-error Cannot find module 'astro/assets' or its corresponding type declarations. import { emitESMImage } from 'astro/assets'; import { bold, red } from 'kleur/colors'; -import vitePluginRestart from 'vite-plugin-restart'; import path from 'node:path'; import type * as rollup from 'rollup'; import { normalizePath } from 'vite'; @@ -50,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) { @@ -216,9 +213,6 @@ export const Content = createComponent({ rollupOptions, }, plugins: [ - vitePluginRestart({ - restart: SUPPORTED_MARKDOC_CONFIG_FILES, - }), { name: '@astrojs/markdoc:astro-propagated-assets', enforce: 'pre', @@ -237,6 +231,16 @@ export const Content = createComponent({ }, }); }, + 'astro:server:setup': async ({ server }) => { + server.watcher.on('all', (event, entry) => { + for (const markdocConfigBase of SUPPORTED_MARKDOC_CONFIG_FILES) { + const absolutePath = new URL(markdocConfigBase, astroConfig.root); + if (entry === fileURLToPath(absolutePath)) { + server.restart(); + } + } + }); + }, }, }; } From 23d1ebf6b374ca88a8b974e08356673699bc0122 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 26 Jun 2023 18:01:11 -0400 Subject: [PATCH 5/6] deps: remove vite-plugin-restart --- packages/integrations/markdoc/package.json | 1 - pnpm-lock.yaml | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/packages/integrations/markdoc/package.json b/packages/integrations/markdoc/package.json index fcc4bb77ce07..8604bda5ad36 100644 --- a/packages/integrations/markdoc/package.json +++ b/packages/integrations/markdoc/package.json @@ -70,7 +70,6 @@ "gray-matter": "^4.0.3", "kleur": "^4.1.5", "shiki": "^0.14.1", - "vite-plugin-restart": "^0.3.1", "zod": "^3.17.3" }, "peerDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee5250db928a..b9f479ae3025 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3989,9 +3989,6 @@ importers: shiki: specifier: ^0.14.1 version: 0.14.1 - vite-plugin-restart: - specifier: ^0.3.1 - version: 0.3.1(vite@4.3.9) zod: specifier: ^3.17.3 version: 3.20.6 @@ -17288,18 +17285,6 @@ packages: - supports-color dev: false - /vite-plugin-restart@0.3.1(vite@4.3.9): - resolution: {integrity: sha512-LVe74MLUwaOzXVUgRGiphYbAYNgdGF6mP5vxj5BKcKkdmzvuuw79qcNgn9kHKKaIlCCjmOUiY3qlJ8jBSBUCoA==} - peerDependencies: - vite: ^2.9.0 || ^3.0.0 || ^4.0.0 - peerDependenciesMeta: - vite: - optional: true - dependencies: - micromatch: 4.0.5 - vite: 4.3.9(@types/node@18.16.18)(sass@1.63.4) - dev: false - /vite@4.3.9(@types/node@14.18.51): resolution: {integrity: sha512-qsTNZjO9NoJNW7KnOrgYwczm0WctJ8m/yqYAMAK9Lxt4SoySUfS5S8ia9K7JHpa3KEeMfyF8LoJ3c5NeBJy6pg==} engines: {node: ^14.18.0 || >=16.0.0} From ce2f157f0507f6aac2719ea7fd91500e8c7fe42e Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 27 Jun 2023 07:28:35 -0400 Subject: [PATCH 6/6] refactor: use good enough option --- packages/integrations/markdoc/src/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index f2849fc8729d..8f48dec41a86 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -233,11 +233,8 @@ export const Content = createComponent({ }, 'astro:server:setup': async ({ server }) => { server.watcher.on('all', (event, entry) => { - for (const markdocConfigBase of SUPPORTED_MARKDOC_CONFIG_FILES) { - const absolutePath = new URL(markdocConfigBase, astroConfig.root); - if (entry === fileURLToPath(absolutePath)) { - server.restart(); - } + if (SUPPORTED_MARKDOC_CONFIG_FILES.some((f) => entry.endsWith(f))) { + server.restart(); } }); },