From 7638437d203c716ff5463c921b95db655524ee88 Mon Sep 17 00:00:00 2001 From: jonambas Date: Sat, 6 Apr 2024 00:06:43 -0400 Subject: [PATCH] refactor: remove ct file --- src/__tests__/codegen.test.ts | 11 ++++----- src/__tests__/ct.test.ts | 45 ----------------------------------- src/__tests__/map.test.ts | 27 ++++++++++++++++++++- src/codegen.ts | 8 +++++-- src/ct.ts | 25 ------------------- src/map.ts | 20 +++++++++++++--- 6 files changed, 54 insertions(+), 82 deletions(-) delete mode 100644 src/__tests__/ct.test.ts delete mode 100644 src/ct.ts diff --git a/src/__tests__/codegen.test.ts b/src/__tests__/codegen.test.ts index f5d7d36..dcbee13 100644 --- a/src/__tests__/codegen.test.ts +++ b/src/__tests__/codegen.test.ts @@ -28,12 +28,11 @@ describe('codegen', () => { "code": " const pluginCtMap = new Map(JSON.parse('[["foo.100","#fff"],["foo.200",{"base":"#000"}],["bar.100","red"],["bar.200","blue"]]')); - export const ct = (path) => { - if (!path) return 'panda-plugin-ct-path-empty'; - if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found'; - return pluginCtMap.get(path); - }; - ", + export const ct = (path) => { + if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found'; + return pluginCtMap.get(path); + }; + ", "file": "css.mjs", } `); diff --git a/src/__tests__/ct.test.ts b/src/__tests__/ct.test.ts deleted file mode 100644 index 3885e56..0000000 --- a/src/__tests__/ct.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { ct, ctTemplate } from '../ct'; - -const tokens = { - foo: { 100: { value: '#fff' }, 200: { value: { base: '#000' } } }, - bar: { 100: 'red', 200: 'blue' }, -}; - -describe('ct', () => { - it('gets a string', () => { - expect(ct(tokens, 'bar.100')).toBe('red'); - }); - - it('gets a value object', () => { - expect(ct(tokens, 'foo.200')).toMatchInlineSnapshot( - ` - { - "base": "#000", - } - `, - ); - }); - - it('gets a value string', () => { - expect(ct(tokens, 'foo.100')).toMatchInlineSnapshot(`"#fff"`); - }); - - it('gets an undefined token', () => { - expect(ct(tokens, 'nope.nope')).toBeUndefined(); - expect(ct(tokens, 'foo.baz')).toBeUndefined(); - }); -}); - -describe('getTemplate', () => { - it('generates a ct function', () => { - expect(ctTemplate).toMatchInlineSnapshot(` - " - export const ct = (path) => { - if (!path) return 'panda-plugin-ct-path-empty'; - if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found'; - return pluginCtMap.get(path); - }; - " - `); - }); -}); diff --git a/src/__tests__/map.test.ts b/src/__tests__/map.test.ts index 1d8b897..ec05e5f 100644 --- a/src/__tests__/map.test.ts +++ b/src/__tests__/map.test.ts @@ -1,10 +1,35 @@ -import { makeMap, makePaths, mapTemplate } from '../map'; +import { get, makeMap, makePaths, mapTemplate } from '../map'; const tokens = { foo: { 100: { value: '#fff' }, 200: { value: { base: '#000' } } }, bar: { 100: 'red', 200: 'blue' }, }; +describe('get', () => { + it('gets a string', () => { + expect(get(tokens, 'bar.100')).toBe('red'); + }); + + it('gets a value object', () => { + expect(get(tokens, 'foo.200')).toMatchInlineSnapshot( + ` + { + "base": "#000", + } + `, + ); + }); + + it('gets a value string', () => { + expect(get(tokens, 'foo.100')).toMatchInlineSnapshot(`"#fff"`); + }); + + it('gets an undefined token', () => { + expect(get(tokens, 'nope.nope')).toBeUndefined(); + expect(get(tokens, 'foo.baz')).toBeUndefined(); + }); +}); + describe('makePaths', () => { it('makes paths', () => { expect(makePaths(tokens)).toMatchInlineSnapshot(` diff --git a/src/codegen.ts b/src/codegen.ts index e6e02e4..fb6ce41 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -5,7 +5,6 @@ import type { } from '@pandacss/types'; import { makePaths, mapTemplate } from './map'; import type { PluginContext } from './types'; -import { ctTemplate } from './ct'; export const codegen = ( args: CodegenPrepareHookArgs, @@ -20,7 +19,12 @@ export const codegen = ( if (!cssFile) return args.artifacts; cssFile.code += mapTemplate(map); - cssFile.code += ctTemplate; + cssFile.code += ` + export const ct = (path) => { + if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found'; + return pluginCtMap.get(path); + }; + `; const cssDtsFile = cssFn.files.find((f) => f.file.includes('css.d.')); if (!cssDtsFile) return args.artifacts; diff --git a/src/ct.ts b/src/ct.ts deleted file mode 100644 index aea95f0..0000000 --- a/src/ct.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { ComponentTokens } from './types'; -import { isObjectWithValue } from './utils'; - -export const ct = (tokens: ComponentTokens, path: T) => { - const parts = path.split('.'); - let current = tokens; - - for (const part of parts) { - if (!current[part]) break; - current = current[part] as ComponentTokens; - } - - if (typeof current === 'string') return current; - if (isObjectWithValue(current)) return current.value; - - return; -}; - -export const ctTemplate = ` -export const ct = (path) => { - if (!path) return 'panda-plugin-ct-path-empty'; - if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found'; - return pluginCtMap.get(path); -}; -`; diff --git a/src/map.ts b/src/map.ts index 92fc92f..39e2378 100644 --- a/src/map.ts +++ b/src/map.ts @@ -1,6 +1,20 @@ -import { ct } from './ct'; import type { ComponentTokens } from './types'; -import { isObject } from './utils'; +import { isObject, isObjectWithValue } from './utils'; + +export const get = (tokens: ComponentTokens, path: T) => { + const parts = path.split('.'); + let current = tokens; + + for (const part of parts) { + if (!current[part]) break; + current = current[part] as ComponentTokens; + } + + if (typeof current === 'string') return current; + if (isObjectWithValue(current)) return current.value; + + return; +}; // Create an array of all string paths from an object. export const makePaths = ( @@ -26,7 +40,7 @@ export const makeMap = (tokens: ComponentTokens) => { const map = new Map(); for (const path of makePaths(tokens)) { - const value = ct(tokens, path); + const value = get(tokens, path); if (value) { map.set(path, value); }