Skip to content

Commit c7c769a

Browse files
feat: classnames sorting worker
1 parent e7debac commit c7c769a

File tree

7 files changed

+101
-4
lines changed

7 files changed

+101
-4
lines changed

src/util/tailwindcss-api/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { createSyncFn } from "synckit";
22

33
import { type Theme } from "./types";
44

5-
export const loadThemeWorker: (twConfigPath: string) => Theme = createSyncFn(
5+
export const loadThemeWorker: (cssConfigPath: string) => Theme = createSyncFn(
66
require.resolve("./worker/load-theme.mjs")
77
);
8+
export const getSortedClassNamesWorker: (
9+
cssConfigPath: string,
10+
unorderedClassNames: Array<string>
11+
) => Array<string> = createSyncFn(
12+
require.resolve("./worker/get-sorted-class-names.mjs")
13+
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* ⚠️ This is a worker script which is ran in node's `worker_threads`.
3+
* 🤓 This means that it is not executed in the main thread, but in a separate thread.
4+
* 😅 Because of this, expect some disturbances, like:
5+
* - `console.log` won't work `vitest`... (seems to work with `jest`).
6+
* - You cannot pass complex objects as arguments, only serializable ones.
7+
* - You cannot retun complex objects, only serializable ones.
8+
* - e.g. You cannot return the `utils.context` directly, but you can return some of its properties...
9+
*
10+
* ℹ️ It uses the `*.mjs` extension to indicate that it is an ES module.
11+
* ✅ We still check the syntax with TypeScript, but it is not a TypeScript file.
12+
*/
13+
14+
// @ts-check
15+
16+
import { runAsWorker } from "synckit";
17+
import { TailwindUtils } from "tailwind-api-utils";
18+
19+
runAsWorker(
20+
async (
21+
/**
22+
* @type {string} The path to the Tailwind CSS config file
23+
*/
24+
cssConfigPath,
25+
/**
26+
* @type {Array<string>} List of class names to sort
27+
*/
28+
unorderedClassNames
29+
) => {
30+
const utils = new TailwindUtils();
31+
await utils.loadConfigV4(cssConfigPath);
32+
if (!utils.context) {
33+
throw new Error(
34+
`Failed to load the Tailwind CSS theme using: "${cssConfigPath}"`
35+
);
36+
}
37+
const sorted = await utils.getSortedClassNames(unorderedClassNames);
38+
return sorted;
39+
}
40+
);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect, test } from "vitest";
2+
3+
import { getSortedClassNamesWorker } from "..";
4+
5+
test(`Sort classnames based on "normal.css"`, () => {
6+
const path = require.resolve("../../../../tests/stubs/css/normal.css");
7+
const unorderdClassNames = [
8+
"lg:flex",
9+
"block",
10+
"top-0",
11+
"unkown",
12+
"text-red-100",
13+
];
14+
const sorted = getSortedClassNamesWorker(path, unorderdClassNames);
15+
// Unknown classnames should be first
16+
expect(sorted[0]).toBe("unkown");
17+
// The rest should be sorted as well
18+
expect(sorted).toStrictEqual([
19+
"unkown",
20+
"top-0",
21+
"block",
22+
"text-red-100",
23+
"lg:flex",
24+
]);
25+
});
26+
27+
test(`Sort "tw:" prefixed classnames based on "tiny-prefixed.css"`, () => {
28+
const path = require.resolve("../../../../tests/stubs/css/tiny-prefixed.css");
29+
const unorderdClassNames = [
30+
"tw:flex",
31+
"tw:unkown",
32+
"tw:hover:text-tiny",
33+
"tw:top-0",
34+
];
35+
const sorted = getSortedClassNamesWorker(path, unorderdClassNames);
36+
// Unknown classnames should be first
37+
expect(sorted[0]).toBe("tw:unkown");
38+
// The rest should be sorted as well
39+
expect(sorted).toStrictEqual([
40+
"tw:unkown",
41+
"tw:top-0",
42+
"tw:flex",
43+
"tw:hover:text-tiny",
44+
]);
45+
});

src/util/tailwindcss-api/worker/load-theme.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ runAsWorker(
2727
await utils.loadConfigV4(cssConfigPath);
2828
if (!utils.context) {
2929
throw new Error(
30-
`Failed to load the Talwind CSS theme using: "${cssConfigPath}"`
30+
`Failed to load the Tailwind CSS theme using: "${cssConfigPath}"`
3131
);
3232
}
3333
// @ts-expect-error Property 'theme' does not exist on type 'DesignSystem'.ts(2339)

src/util/tailwindcss-api/worker/load-theme.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { expect, test } from "vitest";
22

33
import { loadThemeWorker } from "..";
44

5-
test(`load theme from "tiny.css"`, () => {
6-
const path = require.resolve("../../../../tests/stubs/css/tiny.css");
5+
test(`load theme from "tiny-prefixed.css"`, () => {
6+
const path = require.resolve("../../../../tests/stubs/css/tiny-prefixed.css");
77
const theme = loadThemeWorker(path);
88
expect(theme.prefix).toBe("tw");
99
expect(theme.keyframes.size).toBe(

tests/stubs/css/normal.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import "tailwindcss";
2+
3+
/* Minimalist theme used for testing */
4+
@theme {
5+
--color-primary: #123456;
6+
}
File renamed without changes.

0 commit comments

Comments
 (0)