summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2024-11-15 13:28:40 +0000
committerGravatar GitHub <noreply@github.com> 2024-11-15 13:28:40 +0000
commitc8f877cad2d8f1780f70045413872d5b9d32ebed (patch)
treec792a31e5d15cf8f56988a82610859de88ca35cb
parent671f50c7d3c02ab9d23d8d38ecb8934d080b6a40 (diff)
downloadastro-c8f877cad2d8f1780f70045413872d5b9d32ebed.tar.gz
astro-c8f877cad2d8f1780f70045413872d5b9d32ebed.tar.zst
astro-c8f877cad2d8f1780f70045413872d5b9d32ebed.zip
fix: skip legacy typegen by default (#12438)
-rw-r--r--.changeset/clean-moles-rest.md5
-rw-r--r--packages/astro/src/config/index.ts10
-rw-r--r--packages/astro/src/content/types-generator.ts47
-rw-r--r--packages/astro/test/astro-sync.test.js15
-rw-r--r--packages/astro/test/data-collections-schema.test.js5
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/config.ts2
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/columbia-copy.md (renamed from packages/astro/test/fixtures/content-layer/content/space/columbia-copy.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/columbia.md (renamed from packages/astro/test/fixtures/content-layer/content/space/columbia.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/endeavour.md (renamed from packages/astro/test/fixtures/content-layer/content/space/endeavour.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/enterprise.md (renamed from packages/astro/test/fixtures/content-layer/content/space/enterprise.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/index.md (renamed from packages/astro/test/fixtures/content-layer/content/space/index.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/lunar-module.md (renamed from packages/astro/test/fixtures/content-layer/content/space/lunar-module.md)0
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/space/shuttle.jpg (renamed from packages/astro/test/fixtures/content-layer/content/space/shuttle.jpg)bin174008 -> 174008 bytes
-rw-r--r--packages/astro/test/fixtures/data-collections-schema/src/content/config.ts4
14 files changed, 53 insertions, 35 deletions
diff --git a/.changeset/clean-moles-rest.md b/.changeset/clean-moles-rest.md
new file mode 100644
index 000000000..6925bd90c
--- /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 534dc4330..5e9fcddfa 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 12b54483b..c5123acef 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 d6b343616..94e6b326b 100644
--- a/packages/astro/test/astro-sync.test.js
+++ b/packages/astro/test/astro-sync.test.js
@@ -76,6 +76,14 @@ const createFixture = () => {
},
/**
* @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
*/
thenFileShouldBeValidTypescript(path) {
try {
@@ -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 bd7bf598e..119cf8542 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 5a1f2ae1d..65c0c5df0 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
index fba81378e..fba81378e 100644
--- 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
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
index e8a7bf248..e8a7bf248 100644
--- a/packages/astro/test/fixtures/content-layer/content/space/columbia.md
+++ b/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
index 51d6e8c42..51d6e8c42 100644
--- a/packages/astro/test/fixtures/content-layer/content/space/endeavour.md
+++ b/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
index 3131e6d5d..3131e6d5d 100644
--- a/packages/astro/test/fixtures/content-layer/content/space/enterprise.md
+++ b/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
index 4971108e3..4971108e3 100644
--- a/packages/astro/test/fixtures/content-layer/content/space/index.md
+++ b/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
index 780106de4..780106de4 100644
--- 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
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
index 80b8ea67b..80b8ea67b 100644
--- a/packages/astro/test/fixtures/content-layer/content/space/shuttle.jpg
+++ b/packages/astro/test/fixtures/content-layer/src/content/space/shuttle.jpg
Binary files differ
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 378a3dcf2..7cd0854d1 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 };