Skip to content

Commit 2e834e2

Browse files
feat: is valid class name worker (#411)
1 parent 0b9f2ac commit 2e834e2

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

src/util/tailwindcss-api/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ export const getSortedClassNamesWorker: (
1111
) => Array<string> = createSyncFn(
1212
require.resolve("./worker/get-sorted-class-names.mjs")
1313
);
14+
export const isValidClassNameWorker: (
15+
cssConfigPath: string,
16+
className: string
17+
) => boolean = createSyncFn(
18+
require.resolve("./worker/is-valid-class-name.mjs")
19+
);

src/util/tailwindcss-api/worker/get-sorted-class-names.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ test(`Sort classnames based on "normal.css"`, () => {
88
"lg:flex",
99
"block",
1010
"top-0",
11-
"unkown",
11+
"unknown",
1212
"text-red-100",
1313
];
1414
const sorted = getSortedClassNamesWorker(path, unorderdClassNames);
1515
// Unknown classnames should be first
16-
expect(sorted[0]).toBe("unkown");
16+
expect(sorted[0]).toBe("unknown");
1717
// The rest should be sorted as well
1818
expect(sorted).toStrictEqual([
19-
"unkown",
19+
"unknown",
2020
"top-0",
2121
"block",
2222
"text-red-100",
@@ -28,16 +28,16 @@ test(`Sort "tw:" prefixed classnames based on "tiny-prefixed.css"`, () => {
2828
const path = require.resolve("../../../../tests/stubs/css/tiny-prefixed.css");
2929
const unorderdClassNames = [
3030
"tw:flex",
31-
"tw:unkown",
31+
"tw:unknown",
3232
"tw:hover:text-tiny",
3333
"tw:top-0",
3434
];
3535
const sorted = getSortedClassNamesWorker(path, unorderdClassNames);
3636
// Unknown classnames should be first
37-
expect(sorted[0]).toBe("tw:unkown");
37+
expect(sorted[0]).toBe("tw:unknown");
3838
// The rest should be sorted as well
3939
expect(sorted).toStrictEqual([
40-
"tw:unkown",
40+
"tw:unknown",
4141
"tw:top-0",
4242
"tw:flex",
4343
"tw:hover:text-tiny",
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 {string} Class name to validate
27+
*/
28+
className
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.isValidClassName(className);
38+
return sorted;
39+
}
40+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, test } from "vitest";
2+
3+
import { isValidClassNameWorker } from "..";
4+
5+
test(`Validate classnames based on "normal.css"`, () => {
6+
const path = require.resolve("../../../../tests/stubs/css/normal.css");
7+
// Unknown classnames should be invalid
8+
expect(isValidClassNameWorker(path, "unknown")).toBe(false);
9+
// Known classnames should be valid
10+
expect(isValidClassNameWorker(path, "flex")).toBe(true);
11+
});
12+
13+
test(`Validate "tw:" prefixed classnames based on "tiny-prefixed.css"`, () => {
14+
const path = require.resolve("../../../../tests/stubs/css/tiny-prefixed.css");
15+
// Unknown classnames should be invalid
16+
expect(isValidClassNameWorker(path, "tw:unknown")).toBe(false);
17+
// Known classnames should be valid
18+
expect(isValidClassNameWorker(path, "tw:flex")).toBe(true);
19+
expect(isValidClassNameWorker(path, "tw:border-tiny")).toBe(true);
20+
// Unprefixed classnames should be invalid
21+
expect(isValidClassNameWorker(path, "flex")).toBe(false);
22+
});

0 commit comments

Comments
 (0)