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

fix: remove uneeded JSON.parse, add codegen banner #10

Merged
merged 1 commit into from
Apr 6, 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: 5 additions & 4 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('codegen', () => {
expect(result[0].files[0]).toMatchInlineSnapshot(`
{
"code": "
const pluginCtMap = new Map(JSON.parse('[["foo.100","#fff"],["foo.200",{"base":"#000"}],["bar.100","red"],["bar.200","blue"]]'));

/* panda-plugin-ct codegen */
const pluginCtMap = new Map([["foo.100","#fff"],["foo.200",{"base":"#000"}],["bar.100","red"],["bar.200","blue"]]);

export const ct = (path) => {
if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found';
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
return pluginCtMap.get(path);
};
",
};",
"file": "css.mjs",
}
`);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('mapTemplate', () => {
expect(mapTemplate(map)).toMatchInlineSnapshot(
`
"
const pluginCtMap = new Map(JSON.parse('[["foo.100","#fff"],["foo.200",{"base":"#000"}]]'));
const pluginCtMap = new Map([["foo.100","#fff"],["foo.200",{"base":"#000"}]]);
"
`,
);
Expand Down
6 changes: 3 additions & 3 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const codegen = (
const cssFile = cssFn.files.find((f) => f.file.includes('css.mjs'));
if (!cssFile) return args.artifacts;

cssFile.code += '\n\n/* panda-plugin-ct codegen */';
cssFile.code += mapTemplate(map);
cssFile.code += `
export const ct = (path) => {
if (!pluginCtMap.has(path)) return 'panda-plugin-ct-alias-not-found';
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
2 changes: 1 addition & 1 deletion src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ const serializeMap = (map: Map<any, any>) => {

// Generate a template string for the token alias Map.
export const mapTemplate = (map: Map<string, any>) =>
`\nconst pluginCtMap = new Map(JSON.parse('${serializeMap(map)}'));\n`;
`\nconst pluginCtMap = new Map(${serializeMap(map)});\n`;