Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codegen): match file extensions #18

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 89 additions & 7 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ describe('codegen', () => {
},
{
"code": "type PluginCtMapType = {
'foo.100': '#fff',
'foo.200': {"base":"#000","lg":"#111"},
'bar.100': 'red',
'bar.200': 'blue',
}

export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
'foo.100': '#fff';
'foo.200': {"base":"#000","lg":"#111"};
'bar.100': 'red';
'bar.200': 'blue';};
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
"file": "ct.d.ts",
},
],
Expand All @@ -69,6 +67,90 @@ describe('codegen', () => {
`);
});

it('generates ct runtime code with outExtension set to "js"', () => {
const result = codegen(
{
artifacts: [
{
id: 'css-fn',
files: [],
},
{
id: 'css-index',
files: [
{ file: 'index.js', code: '' },
{ file: 'index.d.ts', code: '' },
],
},
],
changed: [],
},
{ ...context, config: { outExtension: 'js' } },
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.js');
expect(result.at(0).files[1].file).toEqual('ct.d.ts');
expect(result.at(1).files[0].code).includes('./ct.js');
expect(result.at(1).files[1].code).includes('./ct');
});

it('generates ct runtime code with outExtension set to "mjs" and force type extension', () => {
const result = codegen(
{
artifacts: [
{
id: 'css-fn',
files: [],
},
{
id: 'css-index',
files: [
{ file: 'index.mjs', code: '' },
{ file: 'index.d.mts', code: '' },
],
},
],
changed: [],
},
{ ...context, config: { forceConsistentTypeExtension: true } },
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.mjs');
expect(result.at(0).files[1].file).toEqual('ct.d.mts');
expect(result.at(1).files[0].code).includes('./ct.mjs');
expect(result.at(1).files[1].code).includes('./ct');
});

it('generates ct runtime code with outExtension set to "js" and force type extension', () => {
const result = codegen(
{
artifacts: [
{
id: 'css-fn',
files: [],
},
{
id: 'css-index',
files: [
{ file: 'index.js', code: '' },
{ file: 'index.d.ts', code: '' },
],
},
],
changed: [],
},
{
...context,
config: { outExtension: 'js', forceConsistentTypeExtension: true },
},
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.js');
expect(result.at(0).files[1].file).toEqual('ct.d.ts');
expect(result.at(1).files[0].code).includes('./ct.js');
expect(result.at(1).files[1].code).includes('./ct');
});

it('skips if artifacts dont exist', () => {
const result = codegen(
{
Expand Down
29 changes: 19 additions & 10 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ import type {
Artifact,
ArtifactContent,
} from '@pandacss/types';
import { makePaths, mapTemplate } from './map';
import { mapTemplate } from './map';
import type { PluginContext } from './types';
import { serializeMapTypes, serializeValue } from './utils';
import { serializeMapTypes } from './utils';

export const codegen = (
args: CodegenPrepareHookArgs,
context: PluginContext,
): MaybeAsyncReturn<void | Artifact[]> => {
const { tokens, map } = context;
const { config, map } = context;

// @see https://panda-css.com/docs/references/config#forceconsistenttypeextension
const ext = config?.outExtension ?? 'mjs';
const dtsExt = !config?.forceConsistentTypeExtension
? 'ts'
: ext === 'js'
? 'ts'
: 'mts';

const cssFn = args.artifacts.find((a) => a.id === 'css-fn');
const index = args.artifacts.find((a) => a.id === 'css-index');
const indexFile = index?.files.find((f) => f.file.includes('index.mjs'));
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.ts'));
const indexFile = index?.files.find((f) => f.file.includes(`index.${ext}`));
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.'));

if (!cssFn || !indexFile || !indexDtsFile) return args.artifacts;

const ctFile: ArtifactContent = {
file: 'ct.mjs',
file: `ct.${ext}`,
code: `${mapTemplate(map)}
export const ct = (path) => {
if (!pluginCtMap.has(path)) return 'panda-plugin-ct_alias-not-found';
Expand All @@ -30,13 +39,13 @@ export const codegen = (
};

const ctDtsFile: ArtifactContent = {
file: 'ct.d.ts',
code: `${serializeMapTypes(map)}
\nexport const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
file: `ct.d.${dtsExt}`,
code: `type PluginCtMapType = {${serializeMapTypes(map)}};
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
};

cssFn.files.push(ctFile, ctDtsFile);
indexFile.code += `\nexport * from './ct.mjs';`;
indexFile.code += `\nexport * from './ct.${ext}';`;
indexDtsFile.code += `\nexport * from './ct';`;

if (context.debug) {
Expand Down
1 change: 0 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ export const createContext = (tokens: ComponentTokens): PluginContext => ({
}),
tokens,
map: makeMap(tokens),
debug: undefined,
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const pluginComponentTokens = (tokens: ComponentTokens): PandaPlugin => {
hooks: {
'context:created': (args) => {
context.debug = args.logger?.debug;
context.config = args.ctx.config;
},
'parser:before': (args) => {
return parser(args, context);
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoggerInterface, LogLevel } from '@pandacss/types';
import { Config, LoggerInterface, LogLevel } from '@pandacss/types';
import { type Project } from 'ts-morph';

export type ComponentTokens = { [k: string]: string | ComponentTokens };
Expand All @@ -8,4 +8,5 @@ export type PluginContext = {
tokens: ComponentTokens;
map: Map<string, string | object>;
debug?: LoggerInterface['debug'];
config?: Config;
};
5 changes: 2 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ export const serializeValue = (value: any) => {
};

export const serializeMapTypes = (map: Map<any, any>) => {
let code = 'type PluginCtMapType = {';
let code = '';
for (const [key, value] of map.entries()) {
code += `\n '${key}': ${serializeValue(value)},`;
code += `\n '${key}': ${serializeValue(value)};`;
}
code += '\n}';
return code;
};