From 4262ced9f0499de54c9befef005a2a653459f063 Mon Sep 17 00:00:00 2001 From: Jon Ambas Date: Sat, 6 Apr 2024 09:58:25 -0400 Subject: [PATCH] fix: remove uneeded JSON.parse, add codegen banner (#10) --- src/__tests__/codegen.test.ts | 9 +++++---- src/__tests__/map.test.ts | 2 +- src/codegen.ts | 6 +++--- src/map.ts | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/__tests__/codegen.test.ts b/src/__tests__/codegen.test.ts index dcbee13..04b0c17 100644 --- a/src/__tests__/codegen.test.ts +++ b/src/__tests__/codegen.test.ts @@ -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", } `); diff --git a/src/__tests__/map.test.ts b/src/__tests__/map.test.ts index ec05e5f..16ca7ae 100644 --- a/src/__tests__/map.test.ts +++ b/src/__tests__/map.test.ts @@ -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"}]]); " `, ); diff --git a/src/codegen.ts b/src/codegen.ts index fb6ce41..5cb8075 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -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; diff --git a/src/map.ts b/src/map.ts index f7fda8d..400c175 100644 --- a/src/map.ts +++ b/src/map.ts @@ -56,4 +56,4 @@ const serializeMap = (map: Map) => { // Generate a template string for the token alias Map. export const mapTemplate = (map: Map) => - `\nconst pluginCtMap = new Map(JSON.parse('${serializeMap(map)}'));\n`; + `\nconst pluginCtMap = new Map(${serializeMap(map)});\n`;