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

refactor: transform css only in component #129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@babel/core": "^7.22.17",
"@babel/parser": "^7.22.16",
"@babel/plugin-transform-typescript": "^7.22.15",
"@vue/compiler-sfc": "^3.3.6",
"chalk": "5.3.0",
"commander": "^11.0.0",
"cosmiconfig": "^8.3.6",
Expand Down
38 changes: 25 additions & 13 deletions packages/cli/src/utils/transformers/transform-css-vars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SyntaxKind } from 'ts-morph'
import type * as z from 'zod'
import MagicString from 'magic-string'
import { parse, walk } from '@vue/compiler-sfc'
import { SyntaxKind } from 'ts-morph'
import type { registryBaseColorSchema } from '@/src/utils/registry/schema'
import type { Transformer } from '@/src/utils/transformers'

Expand All @@ -9,22 +11,32 @@ export const transformCssVars: Transformer = async ({
baseColor,
}) => {
// No transform if using css variables.
if (config.tailwind?.cssVariables || !baseColor?.inlineColors)
if (config.tailwind?.cssVariables || !baseColor?.inlineColors || sourceFile.getFilePath().endsWith('ts'))
return sourceFile

const parsed = parse(sourceFile.getText())
const template = parsed.descriptor.template

if (!template)
return sourceFile

sourceFile.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach((node) => {
if (template.loc.start.offset >= node.getPos())
return sourceFile

const value = node.getText()

if (value.includes('cn(')) {
const splitted = value.split('\'').map(i => applyColorMapping(i, baseColor.inlineColors))
node.replaceWithText(`${splitted.join('\'')}`)
const hasClosingDoubleQuote = value.match(/"/g)?.length === 2
if (value.search('\'') === -1 && hasClosingDoubleQuote) {
const mapped = applyColorMapping(value.replace(/"/g, ''), baseColor.inlineColors)
node.replaceWithText(`"${mapped}"`)
}
else if (value) {
const valueWithColorMapping = applyColorMapping(
value.replace(/"/g, ''),
baseColor.inlineColors,
)
node.replaceWithText(`"${valueWithColorMapping.trim()}"`)
else {
const s = new MagicString(value)
s.replace(/'(.*?)'/g, (substring) => {
return `'${applyColorMapping(substring.replace(/\'/g, ''), baseColor.inlineColors)}'`
})
node.replaceWithText(s.toString())
}
})

Expand Down Expand Up @@ -103,6 +115,6 @@ export function applyColorMapping(
if (!lightMode.includes(className))
lightMode.push(className)
}

return `${lightMode.join(' ')} ${darkMode.join(' ').trim()}`
const combined = `${lightMode.join(' ').replace(/\'/g, '')} ${darkMode.join(' ').trim()}`.trim()
return `${combined}`
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`handle tailwind config template correctly 1`] = `
"
import animate from \\"tailwindcss-animate\\"
"import animate from \\"tailwindcss-animate\\"

/** @type {import('tailwindcss').Config} */
export default {
Expand Down Expand Up @@ -43,9 +42,7 @@ export default {
`;

exports[`handle tailwind config template correctly 2`] = `
"

import animate from \\"tailwindcss-animate\\"
"import animate from \\"tailwindcss-animate\\"

/** @type {import('tailwindcss').Config} */
export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ exports[`transform css vars 2`] = `
exports[`transform css vars 3`] = `
"<script setup lang=\\"ts\\"></script>
<template>
<div :class=\\"cn( 'bg-white hover:bg-stone-100 dark:bg-stone-950 dark:hover:bg-stone-800', true && 'text-stone-50 sm:focus:text-stone-900 dark:text-stone-900 dark:sm:focus:text-stone-50')\\" >foo</div>
<div :class=\\"cn('bg-white hover:bg-stone-100 dark:bg-stone-950 dark:hover:bg-stone-800', true && 'text-stone-50 sm:focus:text-stone-900 dark:text-stone-900 dark:sm:focus:text-stone-50')\\">foo</div>
</template>\\""
`;
82 changes: 76 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading