-
-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svelte-scoped): Add theme() to transform style block pipeline (#…
- Loading branch information
Showing
9 changed files
with
172 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { UnoGenerator } from '@unocss/core' | ||
import { toArray } from '@unocss/core' | ||
import type { Processed } from 'svelte/types/compiler/preprocess' | ||
import MagicString from 'magic-string' | ||
import type { TransformApplyOptions } from './types' | ||
import { transformApply } from './transformApply' | ||
import { themeRE, transformTheme } from './transformTheme' | ||
|
||
const DEFAULT_APPLY_VARIABLES = ['--at-apply'] | ||
|
||
export async function transformStyle({ content, uno, prepend, applyVariables, filename }: { | ||
content: string | ||
uno: UnoGenerator | ||
prepend?: string | ||
applyVariables?: TransformApplyOptions['applyVariables'] | ||
filename?: string | ||
}): Promise<Processed | void> { | ||
applyVariables = toArray(applyVariables || DEFAULT_APPLY_VARIABLES) | ||
const hasApply = content.includes('@apply') || applyVariables.some(v => content.includes(v)) | ||
|
||
const hasThemeFn = content.match(themeRE) | ||
|
||
if (!hasApply && !hasThemeFn) | ||
return | ||
|
||
const s = new MagicString(content) | ||
|
||
if (hasApply) | ||
await transformApply({ s, uno, applyVariables }) | ||
|
||
if (hasThemeFn) | ||
transformTheme(s, uno.config.theme) | ||
|
||
if (!s.hasChanged()) | ||
return | ||
|
||
if (prepend) | ||
s.prepend(prepend) | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ hires: true, source: filename || '' }), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import MagicString from 'magic-string' | ||
|
||
interface Theme { [key: string]: any } | ||
|
||
export const themeRE = /theme\((.+?)\)/g | ||
|
||
export function transformTheme(s: MagicString, theme: Theme): MagicString { | ||
return s.replace(themeRE, (_, match) => { | ||
const argumentsWithoutQuotes = match.slice(1, -1) | ||
return getThemeValue(argumentsWithoutQuotes, theme) | ||
}) | ||
} | ||
|
||
export function getThemeValue(rawArguments: string, theme: Theme): string { | ||
const keys = rawArguments.split('.') | ||
|
||
let current = theme | ||
|
||
for (const key of keys) { | ||
if (current[key] === undefined) | ||
throw new Error(`"${rawArguments}" is not found in your theme`) | ||
|
||
else | ||
current = current[key] | ||
} | ||
|
||
return current as unknown as string | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { describe, expect, it } = import.meta.vitest | ||
|
||
const theme = { | ||
colors: { | ||
blue: { | ||
500: '#3b82f6', | ||
}, | ||
}, | ||
spacing: { | ||
sm: '0.875rem', | ||
}, | ||
} | ||
|
||
describe('transformTheme', () => { | ||
it('loops over all occurrences and replaces', () => { | ||
const code = ` | ||
div { | ||
background: theme('colors.blue.500'); | ||
margin-right: theme("spacing.sm"); | ||
}`.trim() | ||
expect(transformTheme(new MagicString(code), theme).toString()).toMatchInlineSnapshot(` | ||
"div { | ||
background: #3b82f6; | ||
margin-right: 0.875rem; | ||
}" | ||
`) | ||
}) | ||
|
||
it('does nothing if contains no arguments', () => { | ||
const noArgument = 'div { background: theme() }' | ||
expect(transformTheme(new MagicString(noArgument), theme).toString()).toBe(noArgument) | ||
}) | ||
}) | ||
|
||
describe('getThemeValue', () => { | ||
it('splits string into keys and traverses theme', () => { | ||
expect(getThemeValue('colors.blue.500', theme)).toBe('#3b82f6') | ||
expect(getThemeValue('spacing.sm', theme)).toBe('0.875rem') | ||
}) | ||
|
||
it('throws error if not found', () => { | ||
expect(async () => getThemeValue('size.lg', theme)).rejects.toMatchInlineSnapshot('[Error: "size.lg" is not found in your theme]') | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div class="mr-1" /> | ||
|
||
<style> | ||
div { | ||
--at-apply: mb-1; | ||
background: theme("colors.blue.500"); | ||
margin-right: theme("spacing.sm"); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class="_mr-1_gvdwv7" /> | ||
|
||
<style> | ||
:global(._mr-1_gvdwv7) { | ||
margin-right: 0.25rem; | ||
} | ||
div { | ||
margin-bottom: 0.25rem; | ||
background: #3b82f6; | ||
margin-right: 0.875rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class="spu-xjcfo6" /> | ||
|
||
<style> | ||
:global(.spu-xjcfo6) { | ||
margin-right: 0.25rem; | ||
} | ||
div { | ||
margin-bottom: 0.25rem; | ||
background: #3b82f6; | ||
margin-right: 0.875rem; | ||
} | ||
</style> |