Skip to content

Commit

Permalink
fix: remove uneeded JSON.parse, add codegen banner (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas authored Apr 6, 2024
1 parent 25af4ac commit 4262ced
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
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`;

0 comments on commit 4262ced

Please sign in to comment.