Skip to content

Commit bcc9947

Browse files
authored
fix(codegen): match file extensions (#19)
1 parent 93f7efb commit bcc9947

File tree

5 files changed

+75
-22
lines changed

5 files changed

+75
-22
lines changed

src/__tests__/codegen.test.ts

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ describe('codegen', () => {
3838
},
3939
{
4040
"code": "type PluginCtMapType = {
41-
'foo.100': '#fff',
42-
'foo.200': {"base":"#000","lg":"#111"},
43-
'bar.100': 'red',
44-
'bar.200': 'blue',
45-
}
46-
47-
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
41+
'foo.100': '#fff';
42+
'foo.200': {"base":"#000","lg":"#111"};
43+
'bar.100': 'red';
44+
'bar.200': 'blue';};
45+
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
4846
"file": "ct.d.ts",
4947
},
5048
],
@@ -69,6 +67,60 @@ describe('codegen', () => {
6967
`);
7068
});
7169

70+
it('generates ct runtime code with outExtension set to "js"', () => {
71+
const result = codegen(
72+
{
73+
artifacts: [
74+
{
75+
id: 'css-fn',
76+
files: [],
77+
},
78+
{
79+
id: 'css-index',
80+
files: [
81+
{ file: 'index.js', code: '' },
82+
{ file: 'index.d.ts', code: '' },
83+
],
84+
},
85+
],
86+
changed: [],
87+
},
88+
context,
89+
) as any[];
90+
91+
expect(result.at(0).files[0].file).toEqual('ct.js');
92+
expect(result.at(0).files[1].file).toEqual('ct.d.ts');
93+
expect(result.at(1).files[0].code).includes('./ct.js');
94+
expect(result.at(1).files[1].code).includes('./ct');
95+
});
96+
97+
it('generates ct runtime code with outExtension set to "mjs" and force type extension', () => {
98+
const result = codegen(
99+
{
100+
artifacts: [
101+
{
102+
id: 'css-fn',
103+
files: [],
104+
},
105+
{
106+
id: 'css-index',
107+
files: [
108+
{ file: 'index.mjs', code: '' },
109+
{ file: 'index.d.mts', code: '' },
110+
],
111+
},
112+
],
113+
changed: [],
114+
},
115+
context,
116+
) as any[];
117+
118+
expect(result.at(0).files[0].file).toEqual('ct.mjs');
119+
expect(result.at(0).files[1].file).toEqual('ct.d.mts');
120+
expect(result.at(1).files[0].code).includes('./ct.mjs');
121+
expect(result.at(1).files[1].code).includes('./ct');
122+
});
123+
72124
it('skips if artifacts dont exist', () => {
73125
const result = codegen(
74126
{

src/codegen.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ import type {
44
Artifact,
55
ArtifactContent,
66
} from '@pandacss/types';
7-
import { makePaths, mapTemplate } from './map';
7+
import { mapTemplate } from './map';
88
import type { PluginContext } from './types';
9-
import { serializeMapTypes, serializeValue } from './utils';
9+
import { serializeMapTypes } from './utils';
1010

1111
export const codegen = (
1212
args: CodegenPrepareHookArgs,
1313
context: PluginContext,
1414
): MaybeAsyncReturn<void | Artifact[]> => {
15-
const { tokens, map } = context;
15+
const { map } = context;
16+
1617
const cssFn = args.artifacts.find((a) => a.id === 'css-fn');
1718
const index = args.artifacts.find((a) => a.id === 'css-index');
18-
const indexFile = index?.files.find((f) => f.file.includes('index.mjs'));
19-
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.ts'));
19+
const indexFile = index?.files.find((f) => f.file.match(/^index\.(mjs|js)/));
20+
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.'));
21+
const ext = indexFile?.file.split('.').at(-1);
22+
const dtsExt = indexDtsFile?.file.split('.').at(-1);
2023

2124
if (!cssFn || !indexFile || !indexDtsFile) return args.artifacts;
2225

2326
const ctFile: ArtifactContent = {
24-
file: 'ct.mjs',
27+
file: `ct.${ext}`,
2528
code: `${mapTemplate(map)}
2629
export const ct = (path) => {
2730
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
@@ -30,13 +33,13 @@ export const codegen = (
3033
};
3134

3235
const ctDtsFile: ArtifactContent = {
33-
file: 'ct.d.ts',
34-
code: `${serializeMapTypes(map)}
35-
\nexport const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
36+
file: `ct.d.${dtsExt}`,
37+
code: `type PluginCtMapType = {${serializeMapTypes(map)}};
38+
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
3639
};
3740

3841
cssFn.files.push(ctFile, ctDtsFile);
39-
indexFile.code += `\nexport * from './ct.mjs';`;
42+
indexFile.code += `\nexport * from './ct.${ext}';`;
4043
indexDtsFile.code += `\nexport * from './ct';`;
4144

4245
if (context.debug) {

src/context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ export const createContext = (tokens: ComponentTokens): PluginContext => ({
2121
}),
2222
tokens,
2323
map: makeMap(tokens),
24-
debug: undefined,
2524
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LoggerInterface, LogLevel } from '@pandacss/types';
1+
import type { LoggerInterface } from '@pandacss/types';
22
import { type Project } from 'ts-morph';
33

44
export type ComponentTokens = { [k: string]: string | ComponentTokens };

src/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ export const serializeValue = (value: any) => {
1515
};
1616

1717
export const serializeMapTypes = (map: Map<any, any>) => {
18-
let code = 'type PluginCtMapType = {';
18+
let code = '';
1919
for (const [key, value] of map.entries()) {
20-
code += `\n '${key}': ${serializeValue(value)},`;
20+
code += `\n '${key}': ${serializeValue(value)};`;
2121
}
22-
code += '\n}';
2322
return code;
2423
};

0 commit comments

Comments
 (0)