From c8f877cad2d8f1780f70045413872d5b9d32ebed Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Fri, 15 Nov 2024 13:28:40 +0000 Subject: [PATCH] fix: skip legacy typegen by default (#12438) --- .changeset/clean-moles-rest.md | 5 ++ packages/astro/src/config/index.ts | 10 ++-- packages/astro/src/content/types-generator.ts | 47 +++++++----------- packages/astro/test/astro-sync.test.js | 15 ++++++ .../test/data-collections-schema.test.js | 5 +- .../content-layer/src/content/config.ts | 2 +- .../{ => src}/content/space/columbia-copy.md | 0 .../{ => src}/content/space/columbia.md | 0 .../{ => src}/content/space/endeavour.md | 0 .../{ => src}/content/space/enterprise.md | 0 .../{ => src}/content/space/index.md | 0 .../{ => src}/content/space/lunar-module.md | 0 .../{ => src}/content/space/shuttle.jpg | Bin .../src/content/config.ts | 4 +- 14 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 .changeset/clean-moles-rest.md rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/columbia-copy.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/columbia.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/endeavour.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/enterprise.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/index.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/lunar-module.md (100%) rename packages/astro/test/fixtures/content-layer/{ => src}/content/space/shuttle.jpg (100%) diff --git a/.changeset/clean-moles-rest.md b/.changeset/clean-moles-rest.md new file mode 100644 index 0000000000..6925bd90cf --- /dev/null +++ b/.changeset/clean-moles-rest.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where legacy content types were generated for content layer collections if they were in the content directory diff --git a/packages/astro/src/config/index.ts b/packages/astro/src/config/index.ts index 534dc43303..5e9fcddfa3 100644 --- a/packages/astro/src/config/index.ts +++ b/packages/astro/src/config/index.ts @@ -51,10 +51,12 @@ export function getViteConfig( const devSSRManifest = createDevelopmentManifest(settings); const viteConfig = await createVite( { - plugins: [ - // Initialize the content listener - astroContentListenPlugin({ settings, logger, fs }), - ], + plugins: config.legacy.collections + ? [ + // Initialize the content listener + astroContentListenPlugin({ settings, logger, fs }), + ] + : [], }, { settings, command: cmd, logger, mode, sync: false, manifest, ssrManifest: devSSRManifest }, ); diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 12b54483bd..c5123acef2 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -92,24 +92,26 @@ export async function createContentTypesGenerator({ events.push({ name: 'add', entry: contentPaths.config.url }); - const globResult = await glob('**', { - cwd: fileURLToPath(contentPaths.contentDir), - fs: { - readdir: fs.readdir.bind(fs), - readdirSync: fs.readdirSync.bind(fs), - }, - onlyFiles: false, - objectMode: true, - }); + if (settings.config.legacy.collections) { + const globResult = await glob('**', { + cwd: fileURLToPath(contentPaths.contentDir), + fs: { + readdir: fs.readdir.bind(fs), + readdirSync: fs.readdirSync.bind(fs), + }, + onlyFiles: false, + objectMode: true, + }); - for (const entry of globResult) { - const fullPath = path.join(fileURLToPath(contentPaths.contentDir), entry.path); - const entryURL = pathToFileURL(fullPath); - if (entryURL.href.startsWith(contentPaths.config.url.href)) continue; - if (entry.dirent.isFile()) { - events.push({ name: 'add', entry: entryURL }); - } else if (entry.dirent.isDirectory()) { - events.push({ name: 'addDir', entry: entryURL }); + for (const entry of globResult) { + const fullPath = path.join(fileURLToPath(contentPaths.contentDir), entry.path); + const entryURL = pathToFileURL(fullPath); + if (entryURL.href.startsWith(contentPaths.config.url.href)) continue; + if (entry.dirent.isFile()) { + events.push({ name: 'add', entry: entryURL }); + } else if (entry.dirent.isDirectory()) { + events.push({ name: 'addDir', entry: entryURL }); + } } } await runEvents(); @@ -487,7 +489,6 @@ async function writeContentFiles({ // This ensures `getCollection('empty-collection')` doesn't raise a type error (collectionConfig?.type ?? 'data') : collection.type; - const collectionEntryKeys = Object.keys(collection.entries).sort(); const dataType = await typeForCollection(collectionConfig, collectionKey); switch (resolvedType) { @@ -525,20 +526,10 @@ async function writeContentFiles({ dataTypesStr += `};\n`; } - if (collectionConfig?.schema) { - await generateJSONSchema( - fs, - collectionConfig, - collectionKey, - collectionSchemasDir, - logger, - ); - } break; } if ( - settings.config.experimental.contentIntellisense && collectionConfig && (collectionConfig.schema || (await getContentLayerSchema(collectionConfig, collectionKey))) ) { diff --git a/packages/astro/test/astro-sync.test.js b/packages/astro/test/astro-sync.test.js index d6b343616e..94e6b326b7 100644 --- a/packages/astro/test/astro-sync.test.js +++ b/packages/astro/test/astro-sync.test.js @@ -74,6 +74,14 @@ const createFixture = () => { thenFileContentShouldInclude(path, content, error = undefined) { assert.equal(writtenFiles[getExpectedPath(path)].includes(content), true, error); }, + /** + * @param {string} path + * @param {string} content + * @param {string | undefined} error + */ + thenFileContentShouldNotInclude(path, content, error = undefined) { + assert.equal(writtenFiles[getExpectedPath(path)].includes(content), false, error); + }, /** * @param {string} path */ @@ -164,6 +172,13 @@ describe('astro sync', () => { 'Types file does not include empty collection type', ); }); + + it('does not write individual types for entries when emulating legacy collections', async () => { + await fixture.load('./fixtures/content-collections/'); + fixture.clean(); + await fixture.whenSyncing(); + fixture.thenFileContentShouldNotInclude('.astro/content.d.ts', 'id: "one.md"'); + }); }); describe('astro:env', () => { diff --git a/packages/astro/test/data-collections-schema.test.js b/packages/astro/test/data-collections-schema.test.js index bd7bf598e4..119cf85427 100644 --- a/packages/astro/test/data-collections-schema.test.js +++ b/packages/astro/test/data-collections-schema.test.js @@ -1,12 +1,15 @@ +// @ts-check import assert from 'node:assert/strict'; import { before, describe, it } from 'node:test'; import { loadFixture } from './test-utils.js'; +import { removeDir } from '@astrojs/internal-helpers/fs'; describe('Content Collections - data collections', () => { let fixture; before(async () => { fixture = await loadFixture({ root: './fixtures/data-collections-schema/' }); - await fixture.build(); + removeDir(new URL('./fixtures/data-collections-schema/.astro', import.meta.url)); + await fixture.build({}); }); describe('Translations Collection', () => { diff --git a/packages/astro/test/fixtures/content-layer/src/content/config.ts b/packages/astro/test/fixtures/content-layer/src/content/config.ts index 5a1f2ae1d9..65c0c5df05 100644 --- a/packages/astro/test/fixtures/content-layer/src/content/config.ts +++ b/packages/astro/test/fixtures/content-layer/src/content/config.ts @@ -141,7 +141,7 @@ const birds = defineCollection({ }); // Absolute paths should also work -const absoluteRoot = new URL('../../content/space', import.meta.url); +const absoluteRoot = new URL('space', import.meta.url); const spacecraft = defineCollection({ loader: glob({ pattern: '*.md', base: absoluteRoot }), diff --git a/packages/astro/test/fixtures/content-layer/content/space/columbia-copy.md b/packages/astro/test/fixtures/content-layer/src/content/space/columbia-copy.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/columbia-copy.md rename to packages/astro/test/fixtures/content-layer/src/content/space/columbia-copy.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/columbia.md b/packages/astro/test/fixtures/content-layer/src/content/space/columbia.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/columbia.md rename to packages/astro/test/fixtures/content-layer/src/content/space/columbia.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/endeavour.md b/packages/astro/test/fixtures/content-layer/src/content/space/endeavour.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/endeavour.md rename to packages/astro/test/fixtures/content-layer/src/content/space/endeavour.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/enterprise.md b/packages/astro/test/fixtures/content-layer/src/content/space/enterprise.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/enterprise.md rename to packages/astro/test/fixtures/content-layer/src/content/space/enterprise.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/index.md b/packages/astro/test/fixtures/content-layer/src/content/space/index.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/index.md rename to packages/astro/test/fixtures/content-layer/src/content/space/index.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/lunar-module.md b/packages/astro/test/fixtures/content-layer/src/content/space/lunar-module.md similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/lunar-module.md rename to packages/astro/test/fixtures/content-layer/src/content/space/lunar-module.md diff --git a/packages/astro/test/fixtures/content-layer/content/space/shuttle.jpg b/packages/astro/test/fixtures/content-layer/src/content/space/shuttle.jpg similarity index 100% rename from packages/astro/test/fixtures/content-layer/content/space/shuttle.jpg rename to packages/astro/test/fixtures/content-layer/src/content/space/shuttle.jpg diff --git a/packages/astro/test/fixtures/data-collections-schema/src/content/config.ts b/packages/astro/test/fixtures/data-collections-schema/src/content/config.ts index 378a3dcf24..7cd0854d1e 100644 --- a/packages/astro/test/fixtures/data-collections-schema/src/content/config.ts +++ b/packages/astro/test/fixtures/data-collections-schema/src/content/config.ts @@ -38,6 +38,8 @@ const image = defineCollection({ }), }); -const authors = defineCollection({}); +const authors = defineCollection({ + type: 'data', +}); export const collections = { docs, func, image, i18n, authors };