Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip legacy typegen by default #12438

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-moles-rest.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions packages/astro/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
Expand Down
47 changes: 19 additions & 28 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)))
) {
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/test/astro-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/test/data-collections-schema.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const image = defineCollection({
}),
});

const authors = defineCollection({});
const authors = defineCollection({
type: 'data',
});

export const collections = { docs, func, image, i18n, authors };
Loading