Skip to content

Commit f6c9218

Browse files
loadTheme
1 parent e7f3742 commit f6c9218

File tree

10 files changed

+110
-29
lines changed

10 files changed

+110
-29
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"source.fixAll.eslint": "explicit"
44
},
55
"editor.defaultFormatter": "esbenp.prettier-vscode",
6-
"editor.formatOnSave": true
6+
"editor.formatOnSave": true,
7+
"files.associations": {
8+
"*.css": "tailwindcss"
9+
}
710
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"eslint-plugin-unicorn": "^56.0.1",
6565
"globals": "^15.13.0",
6666
"jest": "^29.7.0",
67+
"tailwindcss": "^4.0.0",
6768
"ts-jest": "^29.1.4",
6869
"tsup": "^8.1.0",
6970
"typescript": "^5.4.5",

pnpm-lock.yaml

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/my-rule.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { RuleCreator } from "@typescript-eslint/utils/eslint-utils";
2-
import { createSyncFn } from "synckit";
32

43
import { PluginSharedSettings } from "../types";
54
import urlCreator from "../url-creator";
5+
import { loadThemeWorker } from "../util/tailwindcss-api";
66

77
export { ESLintUtils } from "@typescript-eslint/utils";
88

@@ -17,8 +17,6 @@ type Options = [
1717
}
1818
];
1919

20-
const syncFunction = createSyncFn(require.resolve("../worker.mjs"));
21-
2220
// The Rule creator returns a function that is used to create a well-typed ESLint rule
2321
// The parameter passed into RuleCreator is a URL generator function.
2422
export const createRule = RuleCreator(urlCreator);
@@ -69,18 +67,21 @@ export const myRule = createRule<Options, MessageIds>({
6967
VariableDeclaration: (node) => {
7068
if (node.kind === "var") {
7169
console.log("!!VAR!!!");
72-
console.log("!!VAR!!!");
73-
const asyncResult = syncFunction();
74-
console.log(asyncResult);
70+
const result = loadThemeWorker(
71+
require.resolve("../../tests/stubs/css/tiny.css")
72+
);
73+
console.log("Tailwind config result:");
74+
console.log("=======================");
75+
console.log(result);
7576
// Reading inline configuration
76-
console.log(options[0]);
77+
console.log("\n", "Options:", "\n", options[0]);
7778

7879
// Shared settings
7980
const sharedSettings = (context.settings?.tailwindcss || {
8081
stylesheet: "",
8182
functions: [],
8283
}) as PluginSharedSettings;
83-
console.log(sharedSettings);
84+
console.log("\n", "sharedSettings:", "\n", sharedSettings);
8485

8586
const rangeStart = node.range[0];
8687
const range: readonly [number, number] = [

src/util/tailwindcss-api/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createSyncFn } from "synckit";
2+
3+
import { type Theme } from "./types";
4+
5+
export const loadThemeWorker: (twConfigPath: string) => Theme = createSyncFn(
6+
require.resolve("./worker/load-theme.mjs")
7+
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export type TailwindConfig = {
2+
separator?: string;
3+
prefix?: string;
4+
content?: {
5+
files?: Array<string>;
6+
};
7+
};
8+
9+
export interface DesignSystem {
10+
getClassOrder: (classes: Array<string>) => Array<[string, bigint | null]>;
11+
candidatesToCss: (classes: Array<string>) => Array<string | null>;
12+
tailwindConfig?: TailwindConfig;
13+
}
14+
15+
type ThemeValue = {
16+
value: string;
17+
options: number;
18+
src: string | undefined;
19+
};
20+
21+
export type Theme = {
22+
prefix: string;
23+
values: Map<string, ThemeValue>;
24+
// @ts-expect-error Unexpected any. Specify a different type
25+
keyframes: Set<any>;
26+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
const utils = new TailwindUtils();
27+
// TODO use the config manager ?
28+
if (!utils.context) {
29+
await utils.loadConfigV4(cssConfigPath);
30+
}
31+
if (!utils.context) {
32+
return;
33+
}
34+
// @ts-expect-error Property 'theme' does not exist on type 'DesignSystem'.ts(2339)
35+
return utils.context?.theme;
36+
}
37+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect, test } from "vitest";
2+
3+
import { loadThemeWorker } from "..";
4+
5+
test(`load theme from "tiny.css"`, () => {
6+
const path = require.resolve("../../../../tests/stubs/css/tiny.css");
7+
const theme = loadThemeWorker(path);
8+
expect(theme.prefix).toBe("tw");
9+
expect(theme.keyframes.size).toBe(
10+
["spin", "ping", "pulse", "bounce"].length * 2
11+
);
12+
console.log("Theme values:", theme.values);
13+
expect(theme.values.size).toBe(3);
14+
});

src/worker.mjs

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/stubs/css/tiny.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@import "tailwindcss" prefix(tw);
2+
3+
/* Minimalist theme used for testing */
4+
@theme {
5+
--*: initial;
6+
--color-tiny: #123456;
7+
--spacing: 1px;
8+
--font-body: Inter, sans-serif;
9+
}

0 commit comments

Comments
 (0)