Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): add ct return value hints #16

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ describe('codegen', () => {
"file": "ct.mjs",
},
{
"code": "export const ct: (alias: "foo.100" | "foo.200" | "bar.100" | "bar.200") => string;",
"code": "const pluginCtMap = {
'foo.100': '#fff',
'foo.200': {"base":"#000","lg":"#111"},
'bar.100': 'red',
'bar.200': 'blue',
} as const;

export const ct: <T extends keyof typeof pluginCtMap>(alias: T) => typeof pluginCtMap[T];",
"file": "ct.d.ts",
},
],
Expand Down
17 changes: 8 additions & 9 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from '@pandacss/types';
import { makePaths, mapTemplate } from './map';
import type { PluginContext } from './types';
import { serializeValue } from './utils';
import { serializeMapTypes, serializeValue } from './utils';

export const codegen = (
args: CodegenPrepareHookArgs,
Expand All @@ -20,20 +20,19 @@ export const codegen = (

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

const code = `${mapTemplate(map)}
const ctFile: ArtifactContent = {
file: 'ct.mjs',
code: `${mapTemplate(map)}
export const ct = (path) => {
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
return pluginCtMap.get(path);
};`;

const paths = makePaths(tokens)
.map((key) => `"${key}"`)
.join(' | ');
};`,
};

const ctFile: ArtifactContent = { file: 'ct.mjs', code };
const ctDtsFile: ArtifactContent = {
file: 'ct.d.ts',
code: `export const ct: (alias: ${paths}) => string;`,
code: `${serializeMapTypes(map)}
\nexport const ct: <T extends keyof typeof pluginCtMap>(alias: T) => typeof pluginCtMap[T];`,
};

cssFn.files.push(ctFile, ctDtsFile);
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ export const serializeMap = (map: Map<any, any>) => {
export const serializeValue = (value: any) => {
return isObject(value) ? JSON.stringify(value) : `'${value}'`;
};

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