From 845a120688f06120f19ce1651704f84918ed1ac0 Mon Sep 17 00:00:00 2001 From: Jon Ambas Date: Sun, 7 Apr 2024 12:14:22 -0400 Subject: [PATCH] feat: add debug logging (#15) --- src/__tests__/fixtures.ts | 5 ++++- src/codegen.ts | 6 ++++++ src/context.ts | 1 + src/index.ts | 3 +++ src/map.ts | 7 +------ src/parser.ts | 12 +++++++----- src/types.ts | 2 ++ src/utils.ts | 8 ++++++++ tsup.config.ts | 7 ++++--- 9 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/__tests__/fixtures.ts b/src/__tests__/fixtures.ts index 0f6615a..ae9c93c 100644 --- a/src/__tests__/fixtures.ts +++ b/src/__tests__/fixtures.ts @@ -5,4 +5,7 @@ export const tokens = { bar: { 100: 'red', 200: 'blue' }, }; -export const context = createContext(tokens); +const ctx = createContext(tokens); +ctx.debug = vi.fn(); + +export const context = ctx; diff --git a/src/codegen.ts b/src/codegen.ts index a1de0d4..31c51f3 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -6,6 +6,7 @@ import type { } from '@pandacss/types'; import { makePaths, mapTemplate } from './map'; import type { PluginContext } from './types'; +import { serializeValue } from './utils'; export const codegen = ( args: CodegenPrepareHookArgs, @@ -39,5 +40,10 @@ export const codegen = ( indexFile.code += `\nexport * from './ct.mjs';`; indexDtsFile.code += `\nexport * from './ct';`; + if (context.debug) { + context.debug('plugin:ct', 'codegen complete'); + context.debug('plugin:ct', map); + } + return args.artifacts; }; diff --git a/src/context.ts b/src/context.ts index a50f000..5b66138 100644 --- a/src/context.ts +++ b/src/context.ts @@ -21,4 +21,5 @@ export const createContext = (tokens: ComponentTokens): PluginContext => ({ }), tokens, map: makeMap(tokens), + debug: undefined, }); diff --git a/src/index.ts b/src/index.ts index 44db809..91c7afa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,9 @@ const pluginComponentTokens = (tokens: ComponentTokens): PandaPlugin => { return { name: 'panda-plugin-ct', hooks: { + 'context:created': (args) => { + context.debug = args.logger?.debug; + }, 'parser:before': (args) => { return parser(args, context); }, diff --git a/src/map.ts b/src/map.ts index 400c175..6442dde 100644 --- a/src/map.ts +++ b/src/map.ts @@ -1,5 +1,5 @@ import type { ComponentTokens } from './types'; -import { isObject, isObjectWithValue } from './utils'; +import { isObject, isObjectWithValue, serializeMap } from './utils'; export const get = (tokens: ComponentTokens, path: string) => { const parts = path.split('.'); @@ -49,11 +49,6 @@ export const makeMap = (tokens: ComponentTokens) => { return map; }; -// Serialize a Map to a JSON string. -const serializeMap = (map: Map) => { - return JSON.stringify(Array.from(map.entries())); -}; - // Generate a template string for the token alias Map. export const mapTemplate = (map: Map) => `\nconst pluginCtMap = new Map(${serializeMap(map)});\n`; diff --git a/src/parser.ts b/src/parser.ts index 1f573e9..331b5bb 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,6 +1,6 @@ import type { ParserResultBeforeHookArgs } from '@pandacss/types'; import type { PluginContext } from './types'; -import { isObject } from './utils'; +import { serializeValue } from './utils'; import { ts } from 'ts-morph'; export const parser = ( @@ -37,11 +37,13 @@ export const parser = ( for (const node of calls) { const path = node.getArguments()[0]?.getText().replace(/['"]/g, ''); const value = map.get(path); - - node.replaceWithText( - isObject(value) ? JSON.stringify(value) : `'${value}'`, - ); + node.replaceWithText(serializeValue(value)); } + context.debug?.( + `plugin:ct`, + `Replaced ${calls.length} aliases in: ${args.filePath.split('/').at(-1)}`, + ); + return calls.length ? source.getText() : undefined; }; diff --git a/src/types.ts b/src/types.ts index f8e7238..ffa1776 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,4 @@ +import { LoggerInterface, LogLevel } from '@pandacss/types'; import { type Project } from 'ts-morph'; export type ComponentTokens = { [k: string]: string | ComponentTokens }; @@ -6,4 +7,5 @@ export type PluginContext = { project: Project; tokens: ComponentTokens; map: Map; + debug?: LoggerInterface['debug']; }; diff --git a/src/utils.ts b/src/utils.ts index 39b245c..1648a93 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,3 +5,11 @@ export const isObject = (obj: any) => { export const isObjectWithValue = (obj: any) => { return isObject(obj) && 'value' in obj; }; + +export const serializeMap = (map: Map) => { + return JSON.stringify(Array.from(map.entries())); +}; + +export const serializeValue = (value: any) => { + return isObject(value) ? JSON.stringify(value) : `'${value}'`; +}; diff --git a/tsup.config.ts b/tsup.config.ts index 0d98725..620b51f 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,8 +1,9 @@ -import { defineConfig } from "tsup"; +import { defineConfig } from 'tsup'; export default defineConfig({ clean: true, - entry: ["src/index.ts"], - format: ["cjs", "esm"], + entry: ['src/index.ts'], + format: ['cjs', 'esm'], dts: true, + minify: true, });