Skip to content

Commit

Permalink
feat: add debug logging (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas authored Apr 7, 2024
1 parent 83f5d6a commit 845a120
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const createContext = (tokens: ComponentTokens): PluginContext => ({
}),
tokens,
map: makeMap(tokens),
debug: undefined,
});
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
7 changes: 1 addition & 6 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -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('.');
Expand Down Expand Up @@ -49,11 +49,6 @@ export const makeMap = (tokens: ComponentTokens) => {
return map;
};

// Serialize a Map to a JSON string.
const serializeMap = (map: Map<any, any>) => {
return JSON.stringify(Array.from(map.entries()));
};

// Generate a template string for the token alias Map.
export const mapTemplate = (map: Map<string, any>) =>
`\nconst pluginCtMap = new Map(${serializeMap(map)});\n`;
12 changes: 7 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down Expand Up @@ -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;
};
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoggerInterface, LogLevel } from '@pandacss/types';
import { type Project } from 'ts-morph';

export type ComponentTokens = { [k: string]: string | ComponentTokens };
Expand All @@ -6,4 +7,5 @@ export type PluginContext = {
project: Project;
tokens: ComponentTokens;
map: Map<string, string | object>;
debug?: LoggerInterface['debug'];
};
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>) => {
return JSON.stringify(Array.from(map.entries()));
};

export const serializeValue = (value: any) => {
return isObject(value) ? JSON.stringify(value) : `'${value}'`;
};
7 changes: 4 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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,
});

0 comments on commit 845a120

Please sign in to comment.