Skip to content

Commit 2d1d973

Browse files
authored
refactor(codegen): generate separate ct files (#14)
1 parent 48b2cf2 commit 2d1d973

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

src/__tests__/codegen.test.ts

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { CodegenPrepareHookArgs } from '@pandacss/types';
21
import { codegen } from '../codegen';
32
import { createContext } from '../context';
43

@@ -7,43 +6,76 @@ const context = createContext({
76
bar: { 100: 'red', 200: 'blue' },
87
});
98

10-
const args: CodegenPrepareHookArgs = {
11-
artifacts: [
12-
{
13-
id: 'css-fn',
14-
files: [
15-
{ file: 'css.mjs', code: '' },
16-
{ file: 'css.d.ts', code: '' },
17-
],
18-
},
19-
],
20-
changed: [],
21-
};
22-
239
describe('codegen', () => {
2410
it('generates ct runtime code', () => {
25-
const result = codegen(args, context) as any[];
26-
expect(result[0].files[0]).toMatchInlineSnapshot(`
11+
const result = codegen(
2712
{
28-
"code": "
29-
30-
/* panda-plugin-ct codegen */
13+
artifacts: [
14+
{
15+
id: 'css-fn',
16+
files: [],
17+
},
18+
{
19+
id: 'css-index',
20+
files: [
21+
{ file: 'index.mjs', code: '// ...panda code' },
22+
{ file: 'index.d.ts', code: '// ...panda code' },
23+
],
24+
},
25+
],
26+
changed: [],
27+
},
28+
context,
29+
);
30+
expect(result).toMatchInlineSnapshot(`
31+
[
32+
{
33+
"files": [
34+
{
35+
"code": "
3136
const pluginCtMap = new Map([["foo.100","#fff"],["foo.200",{"base":"#000"}],["bar.100","red"],["bar.200","blue"]]);
3237
3338
export const ct = (path) => {
3439
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
3540
return pluginCtMap.get(path);
3641
};",
37-
"file": "css.mjs",
38-
}
42+
"file": "ct.mjs",
43+
},
44+
{
45+
"code": "export const ct: (alias: "foo.100" | "foo.200" | "bar.100" | "bar.200") => string;",
46+
"file": "ct.d.ts",
47+
},
48+
],
49+
"id": "css-fn",
50+
},
51+
{
52+
"files": [
53+
{
54+
"code": "// ...panda code
55+
export * from './ct.mjs';",
56+
"file": "index.mjs",
57+
},
58+
{
59+
"code": "// ...panda code
60+
export * from './ct';",
61+
"file": "index.d.ts",
62+
},
63+
],
64+
"id": "css-index",
65+
},
66+
]
3967
`);
68+
});
4069

41-
expect(result[0].files[1]).toMatchInlineSnapshot(`
70+
it('skips if artifacts dont exist', () => {
71+
const result = codegen(
4272
{
43-
"code": "
44-
export const ct: (alias: "foo.100" | "foo.200" | "bar.100" | "bar.200") => string;",
45-
"file": "css.d.ts",
46-
}
47-
`);
73+
artifacts: [],
74+
changed: [],
75+
},
76+
context,
77+
);
78+
79+
expect(result).toEqual([]);
4880
});
4981
});

src/codegen.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
CodegenPrepareHookArgs,
33
MaybeAsyncReturn,
44
Artifact,
5+
ArtifactContent,
56
} from '@pandacss/types';
67
import { makePaths, mapTemplate } from './map';
78
import type { PluginContext } from './types';
@@ -11,28 +12,32 @@ export const codegen = (
1112
context: PluginContext,
1213
): MaybeAsyncReturn<void | Artifact[]> => {
1314
const { tokens, map } = context;
14-
1515
const cssFn = args.artifacts.find((a) => a.id === 'css-fn');
16-
if (!cssFn) return args.artifacts;
16+
const index = args.artifacts.find((a) => a.id === 'css-index');
17+
const indexFile = index?.files.find((f) => f.file.includes('index.mjs'));
18+
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.ts'));
1719

18-
const cssFile = cssFn.files.find((f) => f.file.includes('css.mjs'));
19-
if (!cssFile) return args.artifacts;
20+
if (!cssFn || !indexFile || !indexDtsFile) return args.artifacts;
2021

21-
cssFile.code += '\n\n/* panda-plugin-ct codegen */';
22-
cssFile.code += mapTemplate(map);
23-
cssFile.code += `
22+
const code = `${mapTemplate(map)}
2423
export const ct = (path) => {
2524
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
2625
return pluginCtMap.get(path);
2726
};`;
2827

29-
const cssDtsFile = cssFn.files.find((f) => f.file.includes('css.d.'));
30-
if (!cssDtsFile) return args.artifacts;
31-
3228
const paths = makePaths(tokens)
3329
.map((key) => `"${key}"`)
3430
.join(' | ');
35-
cssDtsFile.code += `\nexport const ct: (alias: ${paths}) => string;`;
31+
32+
const ctFile: ArtifactContent = { file: 'ct.mjs', code };
33+
const ctDtsFile: ArtifactContent = {
34+
file: 'ct.d.ts',
35+
code: `export const ct: (alias: ${paths}) => string;`,
36+
};
37+
38+
cssFn.files.push(ctFile, ctDtsFile);
39+
indexFile.code += `\nexport * from './ct.mjs';`;
40+
indexDtsFile.code += `\nexport * from './ct';`;
3641

3742
return args.artifacts;
3843
};

0 commit comments

Comments
 (0)