Skip to content

Commit

Permalink
Store theme config in globalThis for virtual imports(astrolicious#176)(
Browse files Browse the repository at this point in the history
…astrolicious#32)

- add function example usage in playground config
  • Loading branch information
Yuhanawa committed Dec 7, 2024
1 parent dd63fc8 commit 86dfc0e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(

const projectRoot = resolveDirectory("./", config.root);

(globalThis as any)[themeName] = { userConfig, userConfigPartial };
// Record of virtual imports and their content
const virtualImports: Parameters<typeof createVirtualResolver>[0]["imports"] = {
[`${themeName}:config`]: `export default ${JSON.stringify(userConfig)}`,
[`${themeName}:config`]: `export default globalThis["${themeName}"].userConfig;`,
[`${themeName}:context`]: "",
};

Expand Down
7 changes: 6 additions & 1 deletion playground/astro.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default defineConfig({
config: {
title: "Hey!",
description: "This is a theme created using",
// sitemap: false
// sitemap: false,
customCornerMessage: (url: URL) => {
if (url.pathname === "/") return "you are on home page";
if (url.pathname.startsWith("/cats")) return `you are on cats page, it's cute!`;
return `current url: ${url}`;
},
},
pages: {
// '/cats': '/dogs',
Expand Down
1 change: 1 addition & 0 deletions tests/themes/theme-playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineTheme({
title: z.string(),
description: z.string().optional(),
sitemap: z.boolean().optional().default(true),
customCornerMessage: z.function().args(z.instanceof(URL)).returns(z.string()).optional(),
}),
imports: {
test: {
Expand Down
34 changes: 21 additions & 13 deletions tests/themes/theme-playground/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
---
import "theme-playground:styles";
import config from "theme-playground:config";
---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="stylesheet" href="">
<title>Theme</title>
</head>
<body>
{Astro.url.pathname !== '/' && <a href="/">Home</a>}
<main>
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Theme</title>
</head>
<body>
{Astro.url.pathname !== "/" && <a href="/">Home</a>}
<span>{config.customCornerMessage?.(Astro.url) ?? `current url: ${Astro.url}`}</span>
<main>
<slot />
</main>
</body>
</main>
</body>
</html>
<style>
span {
position: absolute;
top: 2rem;
left: 1rem;
}
</style>

0 comments on commit 86dfc0e

Please sign in to comment.