@weerachai06/tw-scanner 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/validator.js +22 -17
- package/package.json +1 -1
package/dist/validator.js
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import { compile } from '@tailwindcss/node';
|
|
4
|
+
// ─── Compile with fallback for unknown @apply utilities ──────────────────────
|
|
5
|
+
// Tailwind v4 throws when @apply references a utility it doesn't know about.
|
|
6
|
+
// This can happen with custom utilities defined via PostCSS plugins or external
|
|
7
|
+
// font/design systems. We recover by injecting empty @utility stubs and retrying.
|
|
8
|
+
async function compileWithFallback(css, base, stubs = []) {
|
|
9
|
+
const injected = stubs.map((u) => `@utility ${u} { --tw-stub: 0; }`).join('\n');
|
|
10
|
+
const input = injected ? injected + '\n' + css : css;
|
|
11
|
+
try {
|
|
12
|
+
return await compile(input, { base, onDependency: () => { } });
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
const msg = err.message ?? '';
|
|
16
|
+
const match = msg.match(/Cannot apply unknown utility class [`']?([^\s`']+)[`']?/);
|
|
17
|
+
if (match && stubs.length < 50) {
|
|
18
|
+
const unknown = match[1];
|
|
19
|
+
console.warn(`⚠ Unknown utility in @apply: "${unknown}" — stub injected for scanning. Add "@utility ${unknown} {}" to your CSS to silence this.`);
|
|
20
|
+
return compileWithFallback(css, base, [...stubs, unknown]);
|
|
21
|
+
}
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
4
25
|
// ─── Cache: compile result per CSS file ──────────────────────────────────────
|
|
5
26
|
const compileCache = new Map();
|
|
6
27
|
export async function loadTailwindContext(cssFile) {
|
|
@@ -11,23 +32,7 @@ export async function loadTailwindContext(cssFile) {
|
|
|
11
32
|
throw new Error(`Tailwind CSS file not found: ${abs}`);
|
|
12
33
|
}
|
|
13
34
|
const css = fs.readFileSync(abs, 'utf8');
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
result = await compile(css, {
|
|
17
|
-
base: path.dirname(abs),
|
|
18
|
-
onDependency: () => { },
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
catch (err) {
|
|
22
|
-
const msg = err.message ?? '';
|
|
23
|
-
const match = msg.match(/Cannot apply unknown utility class [`']?([^\s`']+)[`']?/);
|
|
24
|
-
if (match) {
|
|
25
|
-
throw new Error(`Tailwind could not compile "${path.relative(process.cwd(), abs)}" — ` +
|
|
26
|
-
`unknown utility used in @apply: "${match[1]}".\n` +
|
|
27
|
-
` Fix: add "@utility ${match[1]} {}" to your CSS entry file to register it as a custom utility.`);
|
|
28
|
-
}
|
|
29
|
-
throw err;
|
|
30
|
-
}
|
|
35
|
+
const result = await compileWithFallback(css, path.dirname(abs));
|
|
31
36
|
compileCache.set(abs, result);
|
|
32
37
|
return result;
|
|
33
38
|
}
|