-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): add JS runtime transformer for @pandabox/unplugin (#…
…21)
- Loading branch information
Showing
8 changed files
with
157 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/node_modules | ||
/dist | ||
/app | ||
/apps | ||
/coverage |
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,81 @@ | ||
import { transform } from '../transform'; | ||
import { tokens } from './fixtures'; | ||
|
||
describe('transform', () => { | ||
it('returns a function', () => { | ||
expect(transform(tokens)).toBeTypeOf('function'); | ||
}); | ||
|
||
it('replaces ct', () => { | ||
expect( | ||
transform(tokens)({ | ||
configure: () => {}, | ||
filePath: 'test.tsx', | ||
content: ` | ||
import { css, ct, cva } from '@/styled-system/css'; | ||
const styles = cva({ | ||
base: { | ||
// background: ct('foo.200'), | ||
color: ct('bar.200'), | ||
}, | ||
}); | ||
export const Component = () => { | ||
return (<div | ||
className={ | ||
css({ | ||
bg: ct('foo.200'), | ||
color: ct('bar.100') | ||
})} | ||
/>); | ||
`, | ||
}), | ||
).toMatchInlineSnapshot(` | ||
"import { css, ct, cva } from '@/styled-system/css'; | ||
const styles = cva({ | ||
base: { | ||
// background: ct('foo.200'), | ||
color: 'blue', | ||
}, | ||
}); | ||
export const Component = () => { | ||
return (<div | ||
className={ | ||
css({ | ||
bg: {"base":"#000","lg":"#111"}, | ||
color: 'red' | ||
})} | ||
/>); | ||
" | ||
`); | ||
}); | ||
|
||
it('skips without imports, expressions, content', () => { | ||
expect( | ||
transform(tokens)({ | ||
configure: () => {}, | ||
filePath: 'test.tsx', | ||
content: `<div className={css({ bg: ct("foo.200") })/>`, | ||
}), | ||
).toBeUndefined(); | ||
|
||
expect( | ||
transform(tokens)({ | ||
configure: () => {}, | ||
filePath: 'test.tsx', | ||
content: `import { ct } from '@/styled-system/css`, | ||
}), | ||
).toBeUndefined(); | ||
|
||
expect( | ||
transform(tokens)({ | ||
configure: () => {}, | ||
filePath: 'test.tsx', | ||
content: ``, | ||
}), | ||
).toBeUndefined(); | ||
}); | ||
}); |
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,21 @@ | ||
import type { ParserResultBeforeHookArgs } from '@pandacss/types'; | ||
import { createContext } from './context'; | ||
import type { ComponentTokens } from './types'; | ||
import { parser } from './parser'; | ||
|
||
/** | ||
* Transformer for @pandabox/unplugin. | ||
* Replaces JS runtime calls to `ct` with their resulting class names. | ||
* | ||
* @see https://github.com/jonambas/panda-plugin-ct | ||
* @see https://github.com/astahmer/pandabox/tree/main/packages/unplugin | ||
*/ | ||
export const transform = (tokens: ComponentTokens) => { | ||
const context = createContext(tokens); | ||
return (args: ParserResultBeforeHookArgs) => { | ||
// This doesn't have `args.configure` | ||
|
||
if (!args.content) return; | ||
return parser(args, context); | ||
}; | ||
}; |