From 52209ca2ad72a30854947dcb3a90ab4db0ac0a6f Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Wed, 11 Jan 2023 12:51:31 -0500 Subject: [PATCH] [Content collections] Remove experimental flag (#5825) * refactor: remove experimental.cc from core * chore: remove experimental flag from tests * fix: mock contentDir in remark tests * fix: check vfile.path in rel-image-error plugin * fix: move .astro/ excludes to all test/fixtures * fix: include test/**/fixtures in ignore * chore: changeset --- .changeset/thin-beers-drive.md | 14 ++++++++++ .gitignore | 4 +++ examples/with-content/astro.config.mjs | 3 --- packages/astro/src/@types/astro.ts | 24 +---------------- packages/astro/src/core/build/generate.ts | 1 - packages/astro/src/core/build/static-build.ts | 3 +-- .../astro/src/core/build/vite-plugin-css.ts | 27 +++++++------------ .../astro/src/core/build/vite-plugin-ssr.ts | 1 - packages/astro/src/core/config/config.ts | 5 ---- packages/astro/src/core/config/schema.ts | 13 +-------- packages/astro/src/core/create-vite.ts | 10 +++---- .../astro/src/core/render/dev/environment.ts | 1 - .../astro/src/vite-plugin-markdown/index.ts | 1 - packages/astro/test/fixtures/.gitignore | 2 -- .../content-collections/astro.config.mjs | 3 --- .../content-ssr-integration/astro.config.mjs | 3 --- .../astro.config.mjs | 3 --- .../test/fixtures/content/astro.config.mjs | 1 - .../units/dev/collections-renderentry.test.js | 4 --- packages/integrations/mdx/src/plugins.ts | 5 ++-- packages/markdown/remark/src/index.ts | 5 +--- .../src/remark-content-rel-image-error.ts | 2 ++ packages/markdown/remark/src/types.ts | 2 -- .../markdown/remark/test/autolinking.test.js | 13 ++++++--- .../markdown/remark/test/entities.test.js | 6 ++++- packages/markdown/remark/test/plugins.test.js | 2 ++ packages/markdown/remark/test/test-utils.js | 3 +++ 27 files changed, 58 insertions(+), 103 deletions(-) create mode 100644 .changeset/thin-beers-drive.md delete mode 100644 packages/astro/test/fixtures/.gitignore create mode 100644 packages/markdown/remark/test/test-utils.js diff --git a/.changeset/thin-beers-drive.md b/.changeset/thin-beers-drive.md new file mode 100644 index 000000000..d006e95fa --- /dev/null +++ b/.changeset/thin-beers-drive.md @@ -0,0 +1,14 @@ +--- +'astro': major +'@astrojs/mdx': major +'@astrojs/markdown-remark': major +--- + +Baseline the experimental `contentCollections` flag. You're free to remove this from your astro config! + +```diff +import { defineConfig } from 'astro/config'; + +export default defineConfig({ +- experimental: { contentCollections: true } +}) diff --git a/.gitignore b/.gitignore index 0f5c68284..df661ac83 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,7 @@ packages/integrations/**/.netlify/ # exclude IntelliJ/WebStorm stuff .idea + +# ignore content collection generated files +packages/**/test/**/fixtures/**/.astro/ +packages/**/test/**/fixtures/**/env.d.ts diff --git a/examples/with-content/astro.config.mjs b/examples/with-content/astro.config.mjs index e27ea0285..3b2f75c84 100644 --- a/examples/with-content/astro.config.mjs +++ b/examples/with-content/astro.config.mjs @@ -7,7 +7,4 @@ import sitemap from '@astrojs/sitemap'; export default defineConfig({ site: 'https://example.com', integrations: [mdx(), sitemap()], - experimental: { - contentCollections: true, - }, }); diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 2f62409d3..75e75f6a3 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -83,7 +83,6 @@ export interface CLIFlags { port?: number; config?: string; drafts?: boolean; - experimentalContentCollections?: boolean; } export interface BuildConfig { @@ -911,34 +910,13 @@ export interface AstroUserConfig { legacy?: object; /** - * @docs * @kind heading * @name Experimental Flags * @description * Astro offers experimental flags to give users early access to new features. * These flags are not guaranteed to be stable. */ - experimental?: { - /** - * @docs - * @name experimental.contentCollections - * @type {boolean} - * @default `false` - * @version 1.7.0 - * @description - * Enable experimental support for [Content Collections](https://docs.astro.build/en/guides/content-collections/). This makes the `src/content/` directory a reserved directory for Astro to manage, and introduces the `astro:content` module for querying this content. - * - * To enable this feature, set `experimental.contentCollections` to `true` in your Astro config: - * - * ```js - * { - * experimental: { - * contentCollections: true, - * }, - * } - */ - contentCollections?: boolean; - }; + experimental?: object; // Legacy options to be removed diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 047831860..1a708aab3 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -349,7 +349,6 @@ async function generatePath( logging, markdown: { ...settings.config.markdown, - isExperimentalContentCollections: settings.config.experimental.contentCollections, contentDir: getContentPaths(settings.config).contentDir, }, mode: opts.mode, diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 747ef360f..deb8a0b05 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -157,8 +157,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp }), vitePluginPrerender(opts, internals), ...(viteConfig.plugins || []), - settings.config.experimental.contentCollections && - astroBundleDelayedAssetPlugin({ internals }), + astroBundleDelayedAssetPlugin({ internals }), // SSR needs to be last ssr && vitePluginSSR(internals, settings.adapter!), ], diff --git a/packages/astro/src/core/build/vite-plugin-css.ts b/packages/astro/src/core/build/vite-plugin-css.ts index 68012948a..9398cf60a 100644 --- a/packages/astro/src/core/build/vite-plugin-css.ts +++ b/packages/astro/src/core/build/vite-plugin-css.ts @@ -76,15 +76,13 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] // For CSS, create a hash of all of the pages that use it. // This causes CSS to be built into shared chunks when used by multiple pages. if (isCSSRequest(id)) { - if (settings.config.experimental.contentCollections) { - for (const [pageInfo] of walkParentInfos(id, { - getModuleInfo: args[0].getModuleInfo, - })) { - if (new URL(pageInfo.id, 'file://').searchParams.has(DELAYED_ASSET_FLAG)) { - // Split delayed assets to separate modules - // so they can be injected where needed - return createNameHash(id, [id]); - } + for (const [pageInfo] of walkParentInfos(id, { + getModuleInfo: args[0].getModuleInfo, + })) { + if (new URL(pageInfo.id, 'file://').searchParams.has(DELAYED_ASSET_FLAG)) { + // Split delayed assets to separate modules + // so they can be injected where needed + return createNameHash(id, [id]); } } return createNameForParentPages(id, args[0]); @@ -174,17 +172,10 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] id, this, function until(importer) { - if (settings.config.experimental.contentCollections) { - // Short circuit when `contentCollections` is enabled. - return new URL(importer, 'file://').searchParams.has(DELAYED_ASSET_FLAG); - } - return false; + return new URL(importer, 'file://').searchParams.has(DELAYED_ASSET_FLAG); } )) { - if ( - settings.config.experimental.contentCollections && - new URL(pageInfo.id, 'file://').searchParams.has(DELAYED_ASSET_FLAG) - ) { + if (new URL(pageInfo.id, 'file://').searchParams.has(DELAYED_ASSET_FLAG)) { for (const parent of walkParentInfos(id, this)) { const parentInfo = parent[0]; if (moduleIsTopLevelPage(parentInfo)) { diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index d9df28af2..817881c36 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -210,7 +210,6 @@ function buildManifest( base: settings.config.base, markdown: { ...settings.config.markdown, - isExperimentalContentCollections: settings.config.experimental.contentCollections, contentDir: getContentPaths(settings.config).contentDir, }, pageMap: null as any, diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 5be492b40..394a968db 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -100,10 +100,6 @@ export function resolveFlags(flags: Partial): CLIFlags { host: typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, drafts: typeof flags.drafts === 'boolean' ? flags.drafts : undefined, - experimentalContentCollections: - typeof flags.experimentalContentCollections === 'boolean' - ? flags.experimentalContentCollections - : undefined, }; } @@ -132,7 +128,6 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) { // TODO: Come back here and refactor to remove this expected error. astroConfig.server.host = flags.host; } - if (flags.experimentalContentCollections) astroConfig.experimental.contentCollections = true; return astroConfig; } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index bdf7d4e5d..eff69ca4a 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -34,9 +34,6 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = { }, vite: {}, legacy: {}, - experimental: { - contentCollections: false, - }, }; export const AstroConfigSchema = z.object({ @@ -167,15 +164,7 @@ export const AstroConfigSchema = z.object({ vite: z .custom((data) => data instanceof Object && !Array.isArray(data)) .default(ASTRO_CONFIG_DEFAULTS.vite), - experimental: z - .object({ - contentCollections: z - .boolean() - .optional() - .default(ASTRO_CONFIG_DEFAULTS.experimental.contentCollections), - }) - .optional() - .default({}), + experimental: z.object({}).optional().default({}), legacy: z.object({}).optional().default({}), }); diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 05d591f42..1d33ba28f 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -104,13 +104,9 @@ export async function createVite( astroHeadPropagationPlugin({ settings }), astroScannerPlugin({ settings, logging }), astroInjectEnvTsPlugin({ settings, logging, fs }), - ...(settings.config.experimental.contentCollections - ? [ - astroContentVirtualModPlugin({ settings }), - astroContentServerPlugin({ fs, settings, logging, mode }), - astroDelayedAssetPlugin({ mode }), - ] - : []), + astroContentVirtualModPlugin({ settings }), + astroContentServerPlugin({ fs, settings, logging, mode }), + astroDelayedAssetPlugin({ mode }), ], publicDir: fileURLToPath(settings.config.publicDir), root: fileURLToPath(settings.config.root), diff --git a/packages/astro/src/core/render/dev/environment.ts b/packages/astro/src/core/render/dev/environment.ts index c12495bf5..1704c6f29 100644 --- a/packages/astro/src/core/render/dev/environment.ts +++ b/packages/astro/src/core/render/dev/environment.ts @@ -24,7 +24,6 @@ export function createDevelopmentEnvironment( logging, markdown: { ...settings.config.markdown, - isExperimentalContentCollections: settings.config.experimental.contentCollections, contentDir: getContentPaths(settings.config).contentDir, }, mode, diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 34ed3647b..6891bc0d5 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -71,7 +71,6 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu const renderResult = await renderMarkdown(raw.content, { ...settings.config.markdown, fileURL: new URL(`file://${fileId}`), - isExperimentalContentCollections: settings.config.experimental.contentCollections, contentDir: getContentPaths(settings.config).contentDir, frontmatter: raw.data, }); diff --git a/packages/astro/test/fixtures/.gitignore b/packages/astro/test/fixtures/.gitignore deleted file mode 100644 index 7f0fa55b0..000000000 --- a/packages/astro/test/fixtures/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.astro/ -env.d.ts diff --git a/packages/astro/test/fixtures/content-collections/astro.config.mjs b/packages/astro/test/fixtures/content-collections/astro.config.mjs index 6f20dd805..3f7b88523 100644 --- a/packages/astro/test/fixtures/content-collections/astro.config.mjs +++ b/packages/astro/test/fixtures/content-collections/astro.config.mjs @@ -4,7 +4,4 @@ import mdx from '@astrojs/mdx'; // https://astro.build/config export default defineConfig({ integrations: [mdx()], - experimental: { - contentCollections: true, - }, }); diff --git a/packages/astro/test/fixtures/content-ssr-integration/astro.config.mjs b/packages/astro/test/fixtures/content-ssr-integration/astro.config.mjs index 6f20dd805..3f7b88523 100644 --- a/packages/astro/test/fixtures/content-ssr-integration/astro.config.mjs +++ b/packages/astro/test/fixtures/content-ssr-integration/astro.config.mjs @@ -4,7 +4,4 @@ import mdx from '@astrojs/mdx'; // https://astro.build/config export default defineConfig({ integrations: [mdx()], - experimental: { - contentCollections: true, - }, }); diff --git a/packages/astro/test/fixtures/content-static-paths-integration/astro.config.mjs b/packages/astro/test/fixtures/content-static-paths-integration/astro.config.mjs index 6f20dd805..3f7b88523 100644 --- a/packages/astro/test/fixtures/content-static-paths-integration/astro.config.mjs +++ b/packages/astro/test/fixtures/content-static-paths-integration/astro.config.mjs @@ -4,7 +4,4 @@ import mdx from '@astrojs/mdx'; // https://astro.build/config export default defineConfig({ integrations: [mdx()], - experimental: { - contentCollections: true, - }, }); diff --git a/packages/astro/test/fixtures/content/astro.config.mjs b/packages/astro/test/fixtures/content/astro.config.mjs index b331e201a..f6e805117 100644 --- a/packages/astro/test/fixtures/content/astro.config.mjs +++ b/packages/astro/test/fixtures/content/astro.config.mjs @@ -3,5 +3,4 @@ import mdx from '@astrojs/mdx'; export default defineConfig({ integrations: [mdx()], - experimental: { contentCollections: true }, }); diff --git a/packages/astro/test/units/dev/collections-renderentry.test.js b/packages/astro/test/units/dev/collections-renderentry.test.js index 072d004fa..fa720f97b 100644 --- a/packages/astro/test/units/dev/collections-renderentry.test.js +++ b/packages/astro/test/units/dev/collections-renderentry.test.js @@ -47,7 +47,6 @@ describe('Content Collections - render()', () => { userConfig: { integrations: [mdx()], vite: { server: { middlewareMode: true } }, - experimental: { contentCollections: true }, }, }, async (container) => { @@ -121,7 +120,6 @@ describe('Content Collections - render()', () => { userConfig: { integrations: [mdx()], vite: { server: { middlewareMode: true } }, - experimental: { contentCollections: true }, }, }, async (container) => { @@ -193,7 +191,6 @@ describe('Content Collections - render()', () => { userConfig: { integrations: [mdx()], vite: { server: { middlewareMode: true } }, - experimental: { contentCollections: true }, }, }, async (container) => { @@ -259,7 +256,6 @@ describe('Content Collections - render()', () => { userConfig: { integrations: [mdx()], vite: { server: { middlewareMode: true } }, - experimental: { contentCollections: true }, }, }, async (container) => { diff --git a/packages/integrations/mdx/src/plugins.ts b/packages/integrations/mdx/src/plugins.ts index 6637b57d7..6b97bc77c 100644 --- a/packages/integrations/mdx/src/plugins.ts +++ b/packages/integrations/mdx/src/plugins.ts @@ -161,9 +161,8 @@ export async function getRemarkPlugins( remarkPlugins = [...remarkPlugins, ...ignoreStringPlugins(mdxOptions.remarkPlugins)]; // Apply last in case user plugins resolve relative image paths - if (config.experimental.contentCollections) { - remarkPlugins.push(toRemarkContentRelImageError(config)); - } + remarkPlugins.push(toRemarkContentRelImageError(config)); + return remarkPlugins; } diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index 91680f3fd..fdd669280 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -53,7 +53,6 @@ export async function renderMarkdown( remarkRehype = markdownConfigDefaults.remarkRehype, gfm = markdownConfigDefaults.gfm, smartypants = markdownConfigDefaults.smartypants, - isExperimentalContentCollections = false, contentDir, frontmatter: userFrontmatter = {}, } = opts; @@ -91,9 +90,7 @@ export async function renderMarkdown( } // Apply later in case user plugins resolve relative image paths - if (isExperimentalContentCollections) { - parser.use([toRemarkContentRelImageError({ contentDir })]); - } + parser.use([toRemarkContentRelImageError({ contentDir })]); parser.use([ [ diff --git a/packages/markdown/remark/src/remark-content-rel-image-error.ts b/packages/markdown/remark/src/remark-content-rel-image-error.ts index 1a0870c22..3e3664b20 100644 --- a/packages/markdown/remark/src/remark-content-rel-image-error.ts +++ b/packages/markdown/remark/src/remark-content-rel-image-error.ts @@ -10,6 +10,8 @@ import type { VFile } from 'vfile'; export default function toRemarkContentRelImageError({ contentDir }: { contentDir: URL }) { return function remarkContentRelImageError() { return (tree: any, vfile: VFile) => { + if (typeof vfile?.path !== 'string') return; + const isContentFile = pathToFileURL(vfile.path).href.startsWith(contentDir.href); if (!isContentFile) return; diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts index 40b2ac841..ff3060704 100644 --- a/packages/markdown/remark/src/types.ts +++ b/packages/markdown/remark/src/types.ts @@ -59,8 +59,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions { scopedClassName: string | null; }; /** Used to prevent relative image imports from `src/content/` */ - isExperimentalContentCollections?: boolean; - /** Used to prevent relative image imports from `src/content/` */ contentDir: URL; /** Used for frontmatter injection plugins */ frontmatter?: Record; diff --git a/packages/markdown/remark/test/autolinking.test.js b/packages/markdown/remark/test/autolinking.test.js index 48bf894be..b1e567bb4 100644 --- a/packages/markdown/remark/test/autolinking.test.js +++ b/packages/markdown/remark/test/autolinking.test.js @@ -1,10 +1,14 @@ import { renderMarkdown } from '../dist/index.js'; import chai from 'chai'; +import { mockRenderMarkdownParams } from './test-utils.js'; describe('autolinking', () => { describe('plain md', () => { it('autolinks URLs starting with a protocol in plain text', async () => { - const { code } = await renderMarkdown(`See https://example.com for more.`, {}); + const { code } = await renderMarkdown( + `See https://example.com for more.`, + mockRenderMarkdownParams + ); chai .expect(code.replace(/\n/g, '')) @@ -12,7 +16,10 @@ describe('autolinking', () => { }); it('autolinks URLs starting with "www." in plain text', async () => { - const { code } = await renderMarkdown(`See www.example.com for more.`, {}); + const { code } = await renderMarkdown( + `See www.example.com for more.`, + mockRenderMarkdownParams + ); chai .expect(code.trim()) @@ -22,7 +29,7 @@ describe('autolinking', () => { it('does not autolink URLs in code blocks', async () => { const { code } = await renderMarkdown( 'See `https://example.com` or `www.example.com` for more.', - {} + mockRenderMarkdownParams ); chai diff --git a/packages/markdown/remark/test/entities.test.js b/packages/markdown/remark/test/entities.test.js index d35ed3a89..acaf71be1 100644 --- a/packages/markdown/remark/test/entities.test.js +++ b/packages/markdown/remark/test/entities.test.js @@ -1,9 +1,13 @@ import { renderMarkdown } from '../dist/index.js'; import { expect } from 'chai'; +import { mockRenderMarkdownParams } from './test-utils.js'; describe('entities', () => { it('should not unescape entities in regular Markdown', async () => { - const { code } = await renderMarkdown(`<i>This should NOT be italic</i>`, {}); + const { code } = await renderMarkdown( + `<i>This should NOT be italic</i>`, + mockRenderMarkdownParams + ); expect(code).to.equal(`

<i>This should NOT be italic</i>

`); }); diff --git a/packages/markdown/remark/test/plugins.test.js b/packages/markdown/remark/test/plugins.test.js index a1abeb2ed..35e5dcaf8 100644 --- a/packages/markdown/remark/test/plugins.test.js +++ b/packages/markdown/remark/test/plugins.test.js @@ -1,4 +1,5 @@ import { renderMarkdown } from '../dist/index.js'; +import { mockRenderMarkdownParams } from './test-utils.js'; import chai from 'chai'; import { fileURLToPath } from 'node:url'; @@ -8,6 +9,7 @@ describe('plugins', () => { it('should be able to get file path when passing fileURL', async () => { let context; await renderMarkdown(`test`, { + ...mockRenderMarkdownParams, fileURL: new URL('virtual.md', import.meta.url), remarkPlugins: [ function () { diff --git a/packages/markdown/remark/test/test-utils.js b/packages/markdown/remark/test/test-utils.js new file mode 100644 index 000000000..10b779a7d --- /dev/null +++ b/packages/markdown/remark/test/test-utils.js @@ -0,0 +1,3 @@ +export const mockRenderMarkdownParams = { + contentDir: new URL('file:///src/content/'), +};