Skip to content

Commit

Permalink
fix(codegen): match file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas committed Apr 8, 2024
1 parent 93f7efb commit a1630a1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 22 deletions.
66 changes: 59 additions & 7 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ describe('codegen', () => {
},
{
"code": "type PluginCtMapType = {
'foo.100': '#fff',
'foo.200': {"base":"#000","lg":"#111"},
'bar.100': 'red',
'bar.200': 'blue',
}
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
'foo.100': '#fff';
'foo.200': {"base":"#000","lg":"#111"};
'bar.100': 'red';
'bar.200': 'blue';};
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
"file": "ct.d.ts",
},
],
Expand All @@ -69,6 +67,60 @@ describe('codegen', () => {
`);
});

it('generates ct runtime code with outExtension set to "js"', () => {
const result = codegen(
{
artifacts: [
{
id: 'css-fn',
files: [],
},
{
id: 'css-index',
files: [
{ file: 'index.js', code: '' },
{ file: 'index.d.ts', code: '' },
],
},
],
changed: [],
},
context,
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.js');
expect(result.at(0).files[1].file).toEqual('ct.d.ts');
expect(result.at(1).files[0].code).includes('./ct.js');
expect(result.at(1).files[1].code).includes('./ct');
});

it('generates ct runtime code with outExtension set to "mjs" and force type extension', () => {
const result = codegen(
{
artifacts: [
{
id: 'css-fn',
files: [],
},
{
id: 'css-index',
files: [
{ file: 'index.mjs', code: '' },
{ file: 'index.d.mts', code: '' },
],
},
],
changed: [],
},
context,
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.mjs');
expect(result.at(0).files[1].file).toEqual('ct.d.mts');
expect(result.at(1).files[0].code).includes('./ct.mjs');
expect(result.at(1).files[1].code).includes('./ct');
});

it('skips if artifacts dont exist', () => {
const result = codegen(
{
Expand Down
25 changes: 15 additions & 10 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import type {
Artifact,
ArtifactContent,
} from '@pandacss/types';
import { makePaths, mapTemplate } from './map';
import { mapTemplate } from './map';
import type { PluginContext } from './types';
import { serializeMapTypes, serializeValue } from './utils';
import { serializeMapTypes } from './utils';

export const codegen = (
args: CodegenPrepareHookArgs,
context: PluginContext,
): MaybeAsyncReturn<void | Artifact[]> => {
const { tokens, map } = context;
const { map } = context;

const cssFn = args.artifacts.find((a) => a.id === 'css-fn');
const index = args.artifacts.find((a) => a.id === 'css-index');
const indexFile = index?.files.find((f) => f.file.includes('index.mjs'));
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.ts'));
const indexFile = index?.files.find(
(f) => f.file.includes('index.mjs') || f.file.includes('index.js'),
);
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.'));
const ext = indexFile?.file.split('.').at(-1);
const dtsExt = indexDtsFile?.file.split('.').at(-1);

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

const ctFile: ArtifactContent = {
file: 'ct.mjs',
file: `ct.${ext}`,
code: `${mapTemplate(map)}
export const ct = (path) => {
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
Expand All @@ -30,13 +35,13 @@ export const codegen = (
};

const ctDtsFile: ArtifactContent = {
file: 'ct.d.ts',
code: `${serializeMapTypes(map)}
\nexport const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
file: `ct.d.${dtsExt}`,
code: `type PluginCtMapType = {${serializeMapTypes(map)}};
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
};

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

if (context.debug) {
Expand Down
1 change: 0 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ export const createContext = (tokens: ComponentTokens): PluginContext => ({
}),
tokens,
map: makeMap(tokens),
debug: undefined,
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoggerInterface, LogLevel } from '@pandacss/types';
import type { LoggerInterface } from '@pandacss/types';
import { type Project } from 'ts-morph';

export type ComponentTokens = { [k: string]: string | ComponentTokens };
Expand Down
5 changes: 2 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ export const serializeValue = (value: any) => {
};

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

0 comments on commit a1630a1

Please sign in to comment.