Skip to content

Commit

Permalink
Fix Starlight component imports with EC disabled (#1626)
Browse files Browse the repository at this point in the history
* Fix Starlight component imports with EC disabled

* Remove superfluous `async` modifier
  • Loading branch information
hippotastic authored Mar 19, 2024
1 parent 4096e1b commit 67459cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-timers-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes a bundling issue that caused imports from `@astrojs/starlight/components` to fail when using the config setting `expressiveCode: false`.
35 changes: 34 additions & 1 deletion packages/starlight/integrations/expressive-code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,40 @@ export const starlightExpressiveCode = ({
starlightConfig,
useTranslations,
}: StarlightEcIntegrationOptions): AstroIntegration[] => {
if (starlightConfig.expressiveCode === false) return [];
// If Expressive Code is disabled, add a shim to prevent build errors and provide
// a helpful error message in case the user tries to use the `<Code>` component
if (starlightConfig.expressiveCode === false) {
const modules: Record<string, string> = {
'virtual:astro-expressive-code/api': 'export default {}',
'virtual:astro-expressive-code/config': 'export default {}',
'virtual:astro-expressive-code/preprocess-config': `throw new Error("Starlight's
Code component requires Expressive Code, which is disabled in your Starlight config.
Please remove \`expressiveCode: false\` from your config or import Astro's built-in
Code component from 'astro:components' instead.")`.replace(/\s+/g, ' '),
};

return [
{
name: 'astro-expressive-code-shim',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
updateConfig({
vite: {
plugins: [
{
name: 'vite-plugin-astro-expressive-code-shim',
enforce: 'post',
resolveId: (id) => (id in modules ? `\0${id}` : undefined),
load: (id) => (id?.[0] === '\0' ? modules[id.slice(1)] : undefined),
},
],
},
});
},
},
},
];
}

const configArgs =
typeof starlightConfig.expressiveCode === 'object'
Expand Down

0 comments on commit 67459cb

Please sign in to comment.