Skip to content

Commit

Permalink
fix(themes): dynamic import of theme json
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Nov 13, 2024
1 parent eec1488 commit 2bc12d2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .changeset/happy-radios-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@pandacss/generator': patch
---

Fix multi-theme issue where calling the `getTheme` function throws a Vite error due to invalid dynamic import format.

```js
import { getTheme } from 'styled-system/themes'

getTheme('default')
// -> The above dynamic import cannot be analyzed by Vite.
```
6 changes: 3 additions & 3 deletions packages/generator/__tests__/generate-themes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ describe('generate themes', () => {
"id": "panda-theme-default",
"css": " [data-panda-theme=default] {\\n --colors-primary: blue;\\n --colors-simple: var(--colors-red-600);\\n --colors-text: var(--colors-blue-600)\\n}\\n\\n@media (prefers-color-scheme: dark) {\\n [data-panda-theme=default] {\\n --colors-text: var(--colors-blue-400)\\n }\\n }"
}",
"name": "default",
"name": "theme-default",
},
{
"json": "{
"name": "pink",
"id": "panda-theme-pink",
"css": " [data-panda-theme=pink] {\\n --colors-primary: pink;\\n --colors-text: var(--colors-pink-600)\\n}\\n\\n@media (prefers-color-scheme: dark) {\\n [data-panda-theme=pink] {\\n --colors-text: var(--colors-pink-400)\\n }\\n }"
}",
"name": "pink",
"name": "theme-pink",
},
]
`)

expect(generateThemesIndex(ctx, files)).toMatchInlineSnapshot(`
[
{
"code": "export const getTheme = (themeName) => import('./' + themeName + '.json').then((m) => m.default)
"code": "export const getTheme = (themeName) => import(\`./theme-\${themeName}.json\`).then((m) => m.default)
export function injectTheme(el, theme) {
const doc = el.ownerDocument || document
Expand Down
8 changes: 4 additions & 4 deletions packages/generator/src/artifacts/js/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function generateThemes(ctx: Context) {
}

return {
name,
name: `theme-${name}`,
json: JSON.stringify(
compact({
name,
Expand All @@ -51,7 +51,7 @@ export function generateThemesIndex(ctx: Context, files: ReturnType<typeof gener
{
file: ctx.file.ext('index'),
code: outdent`
export const getTheme = (themeName) => import('./' + themeName + '.json').then((m) => m.default)
export const getTheme = (themeName) => import(\`./theme-\$\{themeName}.json\`).then((m) => m.default)
export function injectTheme(el, theme) {
const doc = el.ownerDocument || document
Expand Down Expand Up @@ -86,9 +86,9 @@ export function generateThemesIndex(ctx: Context, files: ReturnType<typeof gener
.map((f) => {
const theme = JSON.parse(f.json) as GeneratedTheme
if (!theme.css) return ''
return `'${f.name}': {
return `'${theme.name}': {
id: string,
name: '${f.name}',
name: '${theme.name}',
css: string
}`
})
Expand Down

0 comments on commit 2bc12d2

Please sign in to comment.