Skip to content

Commit

Permalink
Fix cssModuleToJS with kebab case classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Dec 10, 2022
1 parent b18cdd6 commit 6630a86
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix cssModuleToJS with kebab case classes

## 0.4.2

- Fix crash when using arbitrary values for `animate` rule
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { themeGet } from "./utils/themeGet";
import { getVariants, Variant } from "./variants";

export const VERSION = __VERSION__;
export { cssModuleToJS } from "./utils/modules";
export { cssModuleToJS } from "./utils/cssModuleToJS";
export { convertTargets } from "./utils/convertTargets";

const arbitraryValueRE = /-\[.+]$/;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/cssModuleToJS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { cssModuleToJS as cssModuleToJSDeclaration } from "../types";

export const cssModuleToJS: typeof cssModuleToJSDeclaration = (cssModule) => {
let namedExport = "";
let defaultExport = "export default {\n";
for (const key in cssModule) {
if (!key.includes("-")) {
namedExport += `export const ${key} = "${cssModule[key].name}";\n`;
defaultExport += ` ${key},\n`;
} else {
defaultExport += ` "${key}": "${cssModule[key].name}",\n`;
}
}
defaultExport += "};";
return namedExport + defaultExport;
};
7 changes: 0 additions & 7 deletions src/utils/modules.ts

This file was deleted.

27 changes: 27 additions & 0 deletions tests/cssModuleToJS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as assert from "node:assert";
import test from "node:test";

import { cssModuleToJS } from "../src/utils/cssModuleToJS";

test("convertTargets", () => {
assert.equal(
cssModuleToJS({
"container": {
name: "_container_11zaj_1",
composes: [],
isReferenced: false,
},
"kebab-case": {
name: "_kebab-case_11zaj_6",
composes: [],
isReferenced: false,
},
}),
`
export const container = "_container_11zaj_1";
export default {
container,
"kebab-case": "_kebab-case_11zaj_6",
};`.trim(),
);
});
1 change: 1 addition & 0 deletions tests/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
require("./generate.test");
require("./preTransform.test");
require("./convertTargets.test");
require("./cssModuleToJS.test");
require("./codegen.test");
require("./esbuildPlugin.test");
require("./vitePlugin.test");

0 comments on commit 6630a86

Please sign in to comment.