From 309d787409cae4198cdcbe57bd271a84ffb73d73 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 15 Aug 2024 09:06:00 +0200 Subject: [PATCH 1/6] fix: content types sync in dev --- .changeset/angry-plants-guess.md | 5 +++++ packages/astro/src/core/sync/index.ts | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 .changeset/angry-plants-guess.md diff --git a/.changeset/angry-plants-guess.md b/.changeset/angry-plants-guess.md new file mode 100644 index 000000000000..8798ef7038d2 --- /dev/null +++ b/.changeset/angry-plants-guess.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes content types sync in dev diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index c9b2ec235b98..9a193502f518 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -3,11 +3,11 @@ import { performance } from 'node:perf_hooks'; import { dim } from 'kleur/colors'; import { type HMRPayload, createServer } from 'vite'; import type { AstroConfig, AstroInlineConfig, AstroSettings } from '../../@types/astro.js'; -import { DATA_STORE_FILE } from '../../content/consts.js'; +import { CONTENT_TYPES_FILE, DATA_STORE_FILE } from '../../content/consts.js'; import { globalContentLayer } from '../../content/content-layer.js'; import { DataStore, globalDataStore } from '../../content/data-store.js'; import { createContentTypesGenerator } from '../../content/index.js'; -import { globalContentConfigObserver } from '../../content/utils.js'; +import { getContentPaths, globalContentConfigObserver } from '../../content/utils.js'; import { syncAstroEnv } from '../../env/sync.js'; import { telemetry } from '../../events/index.js'; import { eventCliSession } from '../../events/session.js'; @@ -124,6 +124,14 @@ export async function syncInternal({ }); await contentLayer.sync(); settings.timer.end('Sync content layer'); + } else if (fs.existsSync(getContentPaths(settings.config, fs).contentDir)) { + // Content is synced after writeFiles. That means references are not created + // To work around it, we create a stub so the reference is created and content + // sync will override the empty file + settings.injectedTypes.push({ + filename: CONTENT_TYPES_FILE, + content: '', + }); } syncAstroEnv(settings, fs); From d93fbc99a9cc92bc86e1f9a12a6c232cdfd26458 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 15 Aug 2024 09:59:27 +0200 Subject: [PATCH 2/6] fix: test --- packages/astro/src/core/sync/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 9a193502f518..9d9e5bc23703 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -124,7 +124,7 @@ export async function syncInternal({ }); await contentLayer.sync(); settings.timer.end('Sync content layer'); - } else if (fs.existsSync(getContentPaths(settings.config, fs).contentDir)) { + } else if (fs.existsSync(getContentPaths(settings.config, fs).contentDir.href)) { // Content is synced after writeFiles. That means references are not created // To work around it, we create a stub so the reference is created and content // sync will override the empty file From 36214a4d07b085508604b1f6011f249870b7983b Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 15 Aug 2024 15:40:18 +0200 Subject: [PATCH 3/6] fix: content layer types in dev, take 2 --- .changeset/ten-buckets-matter.md | 5 +++++ packages/astro/src/core/sync/index.ts | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/ten-buckets-matter.md diff --git a/.changeset/ten-buckets-matter.md b/.changeset/ten-buckets-matter.md new file mode 100644 index 000000000000..b70a244a0591 --- /dev/null +++ b/.changeset/ten-buckets-matter.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a type issue when using the Content Layer in dev diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 9d9e5bc23703..2aafc263e088 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -1,7 +1,7 @@ import fsMod, { existsSync } from 'node:fs'; import { performance } from 'node:perf_hooks'; import { dim } from 'kleur/colors'; -import { type HMRPayload, createServer } from 'vite'; +import { type HMRPayload, createServer, normalizePath } from 'vite'; import type { AstroConfig, AstroInlineConfig, AstroSettings } from '../../@types/astro.js'; import { CONTENT_TYPES_FILE, DATA_STORE_FILE } from '../../content/consts.js'; import { globalContentLayer } from '../../content/content-layer.js'; @@ -29,6 +29,7 @@ import type { Logger } from '../logger/core.js'; import { formatErrorMessage } from '../messages.js'; import { ensureProcessNodeEnv } from '../util.js'; import { writeFiles } from './write-files.js'; +import { fileURLToPath } from 'node:url'; export type SyncOptions = { /** @@ -124,7 +125,9 @@ export async function syncInternal({ }); await contentLayer.sync(); settings.timer.end('Sync content layer'); - } else if (fs.existsSync(getContentPaths(settings.config, fs).contentDir.href)) { + } else if ( + fs.existsSync(normalizePath(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) + ) { // Content is synced after writeFiles. That means references are not created // To work around it, we create a stub so the reference is created and content // sync will override the empty file From 9f7ddb629bc23c54efbf370fc4d87e1f5f8be62f Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 15 Aug 2024 15:41:52 +0200 Subject: [PATCH 4/6] Delete .changeset/angry-plants-guess.md --- .changeset/angry-plants-guess.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changeset/angry-plants-guess.md diff --git a/.changeset/angry-plants-guess.md b/.changeset/angry-plants-guess.md deleted file mode 100644 index 8798ef7038d2..000000000000 --- a/.changeset/angry-plants-guess.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'astro': patch ---- - -Fixes content types sync in dev From fa0ec842bc3f971dd874fbb6b0388b827bb6cad6 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 15 Aug 2024 15:58:40 +0200 Subject: [PATCH 5/6] feat: try without normalizePath --- packages/astro/src/core/sync/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 2aafc263e088..349abb36fa93 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -1,7 +1,7 @@ import fsMod, { existsSync } from 'node:fs'; import { performance } from 'node:perf_hooks'; import { dim } from 'kleur/colors'; -import { type HMRPayload, createServer, normalizePath } from 'vite'; +import { type HMRPayload, createServer } from 'vite'; import type { AstroConfig, AstroInlineConfig, AstroSettings } from '../../@types/astro.js'; import { CONTENT_TYPES_FILE, DATA_STORE_FILE } from '../../content/consts.js'; import { globalContentLayer } from '../../content/content-layer.js'; @@ -125,9 +125,7 @@ export async function syncInternal({ }); await contentLayer.sync(); settings.timer.end('Sync content layer'); - } else if ( - fs.existsSync(normalizePath(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) - ) { + } else if (fs.existsSync(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) { // Content is synced after writeFiles. That means references are not created // To work around it, we create a stub so the reference is created and content // sync will override the empty file From 8036d977c6e3df1f67eabd76393283bf6ed47299 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:32:34 +0200 Subject: [PATCH 6/6] fix: fix memfs tests --- packages/astro/src/content/types-generator.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 973a892fa17f..faf02d95d915 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -590,11 +590,9 @@ async function writeContentFiles({ // If it's the first time, we inject types the usual way. sync() will handle creating files and references. If it's not the first time, we just override the dts content if (settings.injectedTypes.some((t) => t.filename === CONTENT_TYPES_FILE)) { - fs.promises.writeFile( - new URL(CONTENT_TYPES_FILE, settings.dotAstroDir), - typeTemplateContent, - 'utf-8', - ); + const filePath = fileURLToPath(new URL(CONTENT_TYPES_FILE, settings.dotAstroDir)); + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); + await fs.promises.writeFile(filePath, typeTemplateContent, 'utf-8'); } else { settings.injectedTypes.push({ filename: CONTENT_TYPES_FILE,