Skip to content

Commit 013f07a

Browse files
authored
Improve <Code> error messages, fix theme option edge case (#328)
1 parent 876a8cd commit 013f07a

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

.changeset/fifty-llamas-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro-expressive-code': patch
3+
---
4+
5+
Fixes an issue where the deprecated, but still available `theme` option was not being taken into account during SSR bundle trimming.

.changeset/hot-dodos-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro-expressive-code': patch
3+
---
4+
5+
Improves the error message when the `<Code>` component is being used on a page without having the Astro integration enabled in the project.

packages/astro-expressive-code/components/renderer.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ async function createRenderer() {
1111
const { astroConfig, ecConfigFileOptions, ecIntegrationOptions = {} } = await import('virtual:astro-expressive-code/config')
1212
const { createAstroRenderer, mergeEcConfigOptions } = await import('virtual:astro-expressive-code/api')
1313

14+
if (typeof mergeEcConfigOptions !== 'function') {
15+
throw new Error(
16+
`You are trying to use Expressive Code's \`<Code>\` component, but the Expressive Code
17+
Astro integration is not enabled in your project.
18+
19+
If you are using Starlight, ensure that its default Expressive Code integration
20+
was not disabled by you or your themes or plugins.
21+
22+
If you are not a Starlight user, ensure that Expressive Code was added to
23+
\`integrations\` in your Astro config file.
24+
25+
In case you don't want to use Expressive Code, you can import Astro's own \`<Code>\`
26+
component from \`astro:components\` instead.`
27+
.split(/\r?\n[ \t]*\r?\n/)
28+
.map((s) => s.replace(/\s+/g, ' ').trim())
29+
.join('\n\n')
30+
)
31+
}
32+
1433
const strIntegrationOptions = JSON.stringify(ecIntegrationOptions)
1534
if (strIntegrationOptions.includes('"[Function]"') || strIntegrationOptions.includes("'[Circular]'")) {
1635
throw new Error(

packages/astro-expressive-code/src/vite-plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export function vitePluginAstroExpressiveCode({
5858
// Prepare Shiki-related SSR bundle trimming
5959
const shikiConfig = typeof processedEcConfig.shiki === 'object' ? processedEcConfig.shiki : {}
6060
const configuredEngine = shikiConfig.engine === 'javascript' ? 'javascript' : 'oniguruma'
61-
const configuredBundledThemes: string[] = (processedEcConfig.themes ?? []).filter((theme) => typeof theme === 'string')
61+
// Extract any bundled theme names from the config, supporting the deprecated `theme` option
62+
// and the current `themes` option
63+
const anyThemeOrThemes = processedEcConfig as unknown as { theme: string | string[]; themes: string | string[] }
64+
const effectiveThemesOrTheme = anyThemeOrThemes.themes ?? anyThemeOrThemes.theme ?? []
65+
const effectiveThemes = Array.isArray(effectiveThemesOrTheme) ? effectiveThemesOrTheme : [effectiveThemesOrTheme]
66+
const configuredBundledThemes = effectiveThemes.filter((theme) => typeof theme === 'string')
6267
const shikiAssetRegExp = /(?<=\n)\s*\{[\s\S]*?"id": "(.*?)",[\s\S]*?\n\s*\},?\s*\n/g
6368

6469
const noQuery = (source: string) => source.split('?')[0]

0 commit comments

Comments
 (0)