diff --git a/.changeset/happy-tips-burn.md b/.changeset/happy-tips-burn.md new file mode 100644 index 0000000000..6cffe4d913 --- /dev/null +++ b/.changeset/happy-tips-burn.md @@ -0,0 +1,11 @@ +--- +'@astrojs/starlight': minor +--- + +Add the `@astrojs/sitemap` and `@astrojs/mdx` integrations only if they are not detected in the Astro configuration. + +⚠️ **BREAKING CHANGE** The minimum supported version of Astro is now v3.2.0. Make sure you update Astro at the same time as updating Starlight: + +```sh +npm install astro@latest +``` diff --git a/packages/starlight/index.ts b/packages/starlight/index.ts index 50e37d046c..a52b994f20 100644 --- a/packages/starlight/index.ts +++ b/packages/starlight/index.ts @@ -10,7 +10,7 @@ import { errorMap } from './utils/error-map'; import { StarlightConfigSchema, type StarlightUserConfig } from './utils/user-config'; import { rehypeRtlCodeSupport } from './integrations/code-rtl-support'; -export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration[] { +export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration { const parsedConfig = StarlightConfigSchema.safeParse(opts, { errorMap }); if (!parsedConfig.success) { @@ -34,7 +34,15 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn pattern: '[...slug]', entryPoint: '@astrojs/starlight/index.astro', }); + const integrations: AstroIntegration[] = []; + if (!config.integrations.find(({ name }) => name === '@astrojs/sitemap')) { + integrations.push(starlightSitemap(userConfig)); + } + if (!config.integrations.find(({ name }) => name === '@astrojs/mdx')) { + integrations.push(mdx()); + } const newConfig: AstroUserConfig = { + integrations, vite: { plugins: [vitePluginStarlightUserConfig(userConfig, config)], }, @@ -50,19 +58,6 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn updateConfig(newConfig); }, - 'astro:config:done': ({ config }) => { - const integrations = config.integrations.map(({ name }) => name); - for (const builtin of ['@astrojs/mdx', '@astrojs/sitemap']) { - if (integrations.filter((name) => name === builtin).length > 1) { - throw new Error( - `Found more than one instance of ${builtin}.\n` + - `Starlight includes ${builtin} by default.\n` + - 'Please remove it from your integrations array in astro.config.mjs' - ); - } - } - }, - 'astro:build:done': ({ dir }) => { const targetDir = fileURLToPath(dir); const cwd = dirname(fileURLToPath(import.meta.url)); @@ -78,5 +73,5 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn }, }; - return [starlightSitemap(userConfig), Starlight, mdx()]; + return Starlight; } diff --git a/packages/starlight/package.json b/packages/starlight/package.json index 5e3dfc6325..dc9f928a74 100644 --- a/packages/starlight/package.json +++ b/packages/starlight/package.json @@ -32,7 +32,7 @@ "./404.astro": "./404.astro" }, "peerDependencies": { - "astro": "^3.0.0" + "astro": "^3.2.0" }, "devDependencies": { "@types/node": "^18.16.19",