Skip to content

Commit

Permalink
refactor: move ts-morph project to context, remove import traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Ambas authored and jonambas committed Apr 5, 2024
1 parent 15e6284 commit 957a621
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import type {
Artifact,
} from '@pandacss/types';
import { makePaths } from './utils';
import type { ComponentTokens } from './types';
import type { PluginContext } from './types';

export const codegen = (
args: CodegenPrepareHookArgs,
tokens: ComponentTokens,
context: Partial<PluginContext>,
): MaybeAsyncReturn<void | Artifact[]> => {
const tokens = context.tokens ?? {};
if (!tokens) return;

const cssFn = args.artifacts.find((a) => a.id === 'css-fn');
if (!cssFn) return args.artifacts;

Expand All @@ -31,7 +34,7 @@ export const codegen = (
}
if (typeof current !== "string") {
return "alias-not-found";
return "panda-plugin-ct-alias-not-found";
}
return current;
Expand Down
20 changes: 20 additions & 0 deletions src/create-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Project, ts } from 'ts-morph';

export const createProject = () => {
return new Project({
compilerOptions: {
jsx: ts.JsxEmit.React,
jsxFactory: 'React.createElement',
jsxFragmentFactory: 'React.Fragment',
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ESNext,
noUnusedParameters: false,
noEmit: true,
useVirtualFileSystem: true,
allowJs: true,
},
skipAddingFilesFromTsConfig: true,
skipFileDependencyResolution: true,
skipLoadingLibFiles: true,
});
};
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import type { PandaPlugin } from '@pandacss/types';
import { parser } from './parser';
import { codegen } from './codegen';
import type { ComponentTokens } from './types';
import { createProject } from './create-project';
import type { ComponentTokens, PluginContext } from './types';

/**
*
*/
const pluginComponentTokens = (tokens: ComponentTokens): PandaPlugin => {
const context: Partial<PluginContext> = {};
return {
name: 'component-tokens',
name: 'panda-plugin-ct',
hooks: {
'config:resolved': () => {
context.project = createProject();
context.tokens = tokens;
},
'parser:before': (args) => {
return parser(args, tokens);
return parser(args, context);
},
'codegen:prepare': (args) => {
return codegen(args, tokens);
return codegen(args, context);
},
},
};
Expand Down
32 changes: 13 additions & 19 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import type { ParserResultBeforeHookArgs } from '@pandacss/types';
import { Project } from 'ts-morph';
import type { ComponentTokens } from './types';
import type { ComponentTokens, PluginContext } from './types';

export const parser = (
args: ParserResultBeforeHookArgs,
tokens: ComponentTokens,
context: Partial<PluginContext>,
): string | void => {
const { content } = args;
const project = new Project();
const source = project.createSourceFile('__temp-ct-parser.ts', content, {
overwrite: true,
});
const tokens = context.tokens ?? {};
const project = context.project;

let hasCt = false;
if (!tokens || !project) return;

for (const node of source.getImportDeclarations()) {
for (const named of node.getNamedImports()) {
if (named.getName() === 'ct') {
hasCt = true;
}
}
if (hasCt) break;
}
// TODO: handle `import { ct as xyz }` aliasing
const content = args.content;
if (!content.includes('ct(')) return;

if (!hasCt) return;
const source = project.createSourceFile('__temp-ct-parser.ts', content, {
overwrite: true,
});

const text = source.getText();
const calls = text.match(/ct\(['"][\w.]+['"]\)/g) ?? [];
Expand All @@ -38,8 +31,9 @@ export const parser = (
current = current[part] as ComponentTokens;
}

// TODO: allow passing through style objects
if (typeof current !== 'string') {
return 'alias-not-found';
return 'panda-plugin-ct-alias-not-found';
}

return current as unknown as string;
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import { type Project } from 'ts-morph';

export type ComponentTokens = { [k: string]: string | ComponentTokens };

export type PluginContext = {
project: Project;
tokens: ComponentTokens;
};

0 comments on commit 957a621

Please sign in to comment.