Skip to content

Commit 372ec96

Browse files
HiDeoodelucis
andauthored
Add built-in integrations conditionally (#796)
Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 9eebaf7 commit 372ec96

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

.changeset/happy-tips-burn.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@astrojs/starlight': minor
3+
---
4+
5+
Add the `@astrojs/sitemap` and `@astrojs/mdx` integrations only if they are not detected in the Astro configuration.
6+
7+
⚠️ **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:
8+
9+
```sh
10+
npm install astro@latest
11+
```

packages/starlight/index.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { errorMap } from './utils/error-map';
1010
import { StarlightConfigSchema, type StarlightUserConfig } from './utils/user-config';
1111
import { rehypeRtlCodeSupport } from './integrations/code-rtl-support';
1212

13-
export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration[] {
13+
export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration {
1414
const parsedConfig = StarlightConfigSchema.safeParse(opts, { errorMap });
1515

1616
if (!parsedConfig.success) {
@@ -34,7 +34,15 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
3434
pattern: '[...slug]',
3535
entryPoint: '@astrojs/starlight/index.astro',
3636
});
37+
const integrations: AstroIntegration[] = [];
38+
if (!config.integrations.find(({ name }) => name === '@astrojs/sitemap')) {
39+
integrations.push(starlightSitemap(userConfig));
40+
}
41+
if (!config.integrations.find(({ name }) => name === '@astrojs/mdx')) {
42+
integrations.push(mdx());
43+
}
3744
const newConfig: AstroUserConfig = {
45+
integrations,
3846
vite: {
3947
plugins: [vitePluginStarlightUserConfig(userConfig, config)],
4048
},
@@ -50,19 +58,6 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
5058
updateConfig(newConfig);
5159
},
5260

53-
'astro:config:done': ({ config }) => {
54-
const integrations = config.integrations.map(({ name }) => name);
55-
for (const builtin of ['@astrojs/mdx', '@astrojs/sitemap']) {
56-
if (integrations.filter((name) => name === builtin).length > 1) {
57-
throw new Error(
58-
`Found more than one instance of ${builtin}.\n` +
59-
`Starlight includes ${builtin} by default.\n` +
60-
'Please remove it from your integrations array in astro.config.mjs'
61-
);
62-
}
63-
}
64-
},
65-
6661
'astro:build:done': ({ dir }) => {
6762
const targetDir = fileURLToPath(dir);
6863
const cwd = dirname(fileURLToPath(import.meta.url));
@@ -78,5 +73,5 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
7873
},
7974
};
8075

81-
return [starlightSitemap(userConfig), Starlight, mdx()];
76+
return Starlight;
8277
}

packages/starlight/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"./404.astro": "./404.astro"
3333
},
3434
"peerDependencies": {
35-
"astro": "^3.0.0"
35+
"astro": "^3.2.0"
3636
},
3737
"devDependencies": {
3838
"@types/node": "^18.16.19",

0 commit comments

Comments
 (0)