Skip to content

Commit

Permalink
fix(codegen): match file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas committed Apr 8, 2024
1 parent 6372c25 commit 75f3b91
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
34 changes: 2 additions & 32 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('codegen', () => {
],
changed: [],
},
{ ...context, config: { outExtension: 'js' } },
context,
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.js');
Expand All @@ -112,7 +112,7 @@ describe('codegen', () => {
],
changed: [],
},
{ ...context, config: { forceConsistentTypeExtension: true } },
context,
) as any[];

expect(result.at(0).files[0].file).toEqual('ct.mjs');
Expand All @@ -121,36 +121,6 @@ describe('codegen', () => {
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
21 changes: 12 additions & 9 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ export const codegen = (
args: CodegenPrepareHookArgs,
context: PluginContext,
): MaybeAsyncReturn<void | Artifact[]> => {
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 { map } = context;
let ext = 'mjs';
let dtsExt = 'ts';

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

for (const artifact of index?.files || []) {
if (artifact.file.includes('.d.')) {
dtsExt = artifact.file.split('.').at(-1) || dtsExt;
} else {
ext = artifact.file.split('.').at(-1) || ext;
}
}

const indexFile = index?.files.find((f) => f.file.includes(`index.${ext}`));
const indexDtsFile = index?.files.find((f) => f.file.includes('index.d.'));

Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, LoggerInterface, LogLevel } from '@pandacss/types';
import type { LoggerInterface } from '@pandacss/types';
import { type Project } from 'ts-morph';

export type ComponentTokens = { [k: string]: string | ComponentTokens };
Expand All @@ -8,5 +8,4 @@ export type PluginContext = {
tokens: ComponentTokens;
map: Map<string, string | object>;
debug?: LoggerInterface['debug'];
config?: Config;
};

0 comments on commit 75f3b91

Please sign in to comment.