Skip to content

Commit

Permalink
support alias for emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Jun 15, 2024
1 parent 375b217 commit 64a6469
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/happy-css-modules/src/emitter/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ function dashesCamelCase(str: string): string {
}

function formatTokens(tokens: Token[], localsConvention: LocalsConvention): Token[] {
function formatToken(token: Token, formatter: (str: string) => string): Token {
if ('importedName' in token && typeof token.importedName === 'string') {
return { ...token, name: formatter(token.name), importedName: formatter(token.importedName) };
} else {
return { ...token, name: formatter(token.name) };
}
}

const result: Token[] = [];
for (const token of tokens) {
if (localsConvention === 'camelCaseOnly') {
result.push({ ...token, name: camelcase(token.name) });
result.push(formatToken(token, camelcase));
} else if (localsConvention === 'camelCase') {
result.push(token);
result.push({ ...token, name: camelcase(token.name) });
result.push(formatToken(token, camelcase));
} else if (localsConvention === 'dashesOnly') {
result.push({ ...token, name: dashesCamelCase(token.name) });
result.push(formatToken(token, dashesCamelCase));
} else if (localsConvention === 'dashes') {
result.push(token);
result.push({ ...token, name: dashesCamelCase(token.name) });
result.push(formatToken(token, dashesCamelCase));
} else {
result.push(token); // asIs
}
Expand Down Expand Up @@ -89,11 +97,10 @@ function generateTokenDeclarations(
: // Imported tokens in non-external files are typed by dynamic import.
// See https://github.com/mizdra/happy-css-modules/issues/106.
new SourceNode(null, null, null, [
'& Readonly<Pick<(typeof import(',
`"${getRelativePath(filePath, originalLocation.filePath)}"`,
'))["default"], ',
`"${token.name}"`,
'>>',
`& Readonly<{ ${token.name}: (typeof import("${getRelativePath(
filePath,
originalLocation.filePath,
)}"))["default"]["${token.importedName ?? token.name}"] }>`,
]),
);
}
Expand Down

0 comments on commit 64a6469

Please sign in to comment.