Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions in theme config are always undefined #176

Open
Yuhanawa opened this issue Dec 5, 2024 · 1 comment
Open

Functions in theme config are always undefined #176

Yuhanawa opened this issue Dec 5, 2024 · 1 comment

Comments

@Yuhanawa
Copy link
Contributor

Yuhanawa commented Dec 5, 2024

problem

index.ts in theme

export default defineTheme({
  schema: z.object({
    customScript: z.function(),
  })
})

astro.config.ts in playground

integrations: [
  theme({
    config: {
      customScript: () => {
        console.log("test");
      },
    },
  }),
],

file in theme

import config from "theme:config";

console.log(config.customScript); // undefined

possible reason

[`${themeName}:config`]: `export default ${JSON.stringify(userConfig)}`,

using JSON.stringify in this line

possible solution

JSON.stringify(
  Object.fromEntries(
    Object.entries(userConfig).map(([key, value]) => 
      typeof value === 'function' 
        ? [key, value.toString()] 
        : [key, value]
        )
    )
)
@BryceRussell
Copy link
Member

This is a limitation of the config option / ATP in general. The value has to be JSON serializable so that the config can be turned into a virtual module and functions cannot be serialized into JSON.

Supporting something like this may be possible using @inox-tools/inline-mod, more details in this issue here: #32

Yuhanawa added a commit to Yuhanawa/astro-theme-provider that referenced this issue Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants