Skip to content

Commit

Permalink
refactor: remove ct file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas committed Apr 6, 2024
1 parent 012200a commit 7638437
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 82 deletions.
11 changes: 5 additions & 6 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
`);
Expand Down
45 changes: 0 additions & 45 deletions src/__tests__/ct.test.ts

This file was deleted.

27 changes: 26 additions & 1 deletion src/__tests__/map.test.ts
Original file line number Diff line number Diff line change
@@ -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(`
Expand Down
8 changes: 6 additions & 2 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
25 changes: 0 additions & 25 deletions src/ct.ts

This file was deleted.

20 changes: 17 additions & 3 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends string>(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 = (
Expand All @@ -26,7 +40,7 @@ export const makeMap = (tokens: ComponentTokens) => {
const map = new Map<string, string | object>();

for (const path of makePaths(tokens)) {
const value = ct(tokens, path);
const value = get(tokens, path);
if (value) {
map.set(path, value);
}
Expand Down

0 comments on commit 7638437

Please sign in to comment.