wispcss 0.0.0-stage → 1.0.4
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/CHANGELOG.md +74 -0
- package/LICENSE +49 -0
- package/README.md +385 -2
- package/README.zh-CN.md +385 -0
- package/dist/wispcss.css +45823 -0
- package/dist/wispcss.min.css +2 -0
- package/examples/add-h2-ids.cjs +284 -0
- package/examples/demo-actions.html +188 -0
- package/examples/demo-alarm.html +219 -0
- package/examples/demo-animation.html +399 -0
- package/examples/demo-aura.html +195 -0
- package/examples/demo-avatar.html +279 -0
- package/examples/demo-background.html +284 -0
- package/examples/demo-badge.html +398 -0
- package/examples/demo-blob.html +307 -0
- package/examples/demo-blockquote.html +190 -0
- package/examples/demo-bookmark.html +243 -0
- package/examples/demo-breadcrumb.html +179 -0
- package/examples/demo-btns.html +349 -0
- package/examples/demo-bubble.html +215 -0
- package/examples/demo-buoy.html +187 -0
- package/examples/demo-button-icon.html +231 -0
- package/examples/demo-button-link.html +177 -0
- package/examples/demo-button.html +361 -0
- package/examples/demo-callout.html +601 -0
- package/examples/demo-check.html +323 -0
- package/examples/demo-code.html +212 -0
- package/examples/demo-color.html +545 -0
- package/examples/demo-comment.html +510 -0
- package/examples/demo-deform.html +229 -0
- package/examples/demo-dialog.html +932 -0
- package/examples/demo-divider.html +208 -0
- package/examples/demo-drawer.html +623 -0
- package/examples/demo-dropmenu.html +382 -0
- package/examples/demo-droppanel.html +354 -0
- package/examples/demo-ellipsis.html +145 -0
- package/examples/demo-emphasis.html +146 -0
- package/examples/demo-empty.html +112 -0
- package/examples/demo-entry.html +219 -0
- package/examples/demo-field.html +359 -0
- package/examples/demo-file.html +206 -0
- package/examples/demo-flag.html +225 -0
- package/examples/demo-form.html +201 -0
- package/examples/demo-gilt.html +170 -0
- package/examples/demo-heading.html +93 -0
- package/examples/demo-icon.html +164 -0
- package/examples/demo-input.html +300 -0
- package/examples/demo-kbd.html +202 -0
- package/examples/demo-lamp.html +248 -0
- package/examples/demo-link.html +142 -0
- package/examples/demo-normalize.html +366 -0
- package/examples/demo-otp.html +277 -0
- package/examples/demo-pagination.html +562 -0
- package/examples/demo-position.html +205 -0
- package/examples/demo-poster.html +384 -0
- package/examples/demo-pre - /345/211/257/346/234/254.html" +169 -0
- package/examples/demo-pre.html +205 -0
- package/examples/demo-preset.html +344 -0
- package/examples/demo-progress.html +303 -0
- package/examples/demo-range.html +211 -0
- package/examples/demo-rank.html +270 -0
- package/examples/demo-rating.html +251 -0
- package/examples/demo-reset.html +105 -0
- package/examples/demo-segment.html +436 -0
- package/examples/demo-select.html +332 -0
- package/examples/demo-shadow.html +152 -0
- package/examples/demo-shell.html +377 -0
- package/examples/demo-shimmer.html +217 -0
- package/examples/demo-size.html +149 -0
- package/examples/demo-skeleton.html +143 -0
- package/examples/demo-sketch.html +213 -0
- package/examples/demo-spacer.html +150 -0
- package/examples/demo-spinner.html +220 -0
- package/examples/demo-stats.html +165 -0
- package/examples/demo-status.html +212 -0
- package/examples/demo-step.html +240 -0
- package/examples/demo-styles.css +14 -0
- package/examples/demo-summary.html +128 -0
- package/examples/demo-swatch.html +253 -0
- package/examples/demo-switch.html +223 -0
- package/examples/demo-table.html +339 -0
- package/examples/demo-tag.html +318 -0
- package/examples/demo-text.html +91 -0
- package/examples/demo-theme.html +167 -0
- package/examples/demo-tools.html +152 -0
- package/examples/demo-transition.html +169 -0
- package/examples/demo-tree.html +929 -0
- package/examples/demo-underline.html +76 -0
- package/examples/demo-utils.js +131 -0
- package/examples/demo-validate.html +120 -0
- package/examples/index.html +149 -0
- package/package.json +73 -3
- package/script-gen-color.js +148 -0
- package/script-mini.js +78 -0
- package/script-prefix.js +398 -0
- package/skill.md +575 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Last modified: 2026/07/31 17:04:55
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
|
|
11
|
+
// 获取 __dirname 的 ES Module 等价物
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = path.dirname(__filename);
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// 配置颜色列表
|
|
17
|
+
const colors = [
|
|
18
|
+
'primary',
|
|
19
|
+
'secondary',
|
|
20
|
+
'tertiary',
|
|
21
|
+
'success',
|
|
22
|
+
'info',
|
|
23
|
+
'highlight',
|
|
24
|
+
'error',
|
|
25
|
+
'warning',
|
|
26
|
+
'caution',
|
|
27
|
+
'title',
|
|
28
|
+
'ink',
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
// 变体配置
|
|
32
|
+
//highlight颜色比较亮,需要使用-body色
|
|
33
|
+
const getLightColors = function (color) {
|
|
34
|
+
let getColors = (color) => `
|
|
35
|
+
--color-${color}: oklch(from var(--color-${color}-base) l calc(c * var(--color-c-factor,1)) h);
|
|
36
|
+
--color-${color}-outline: oklch(from var(--color-${color}) l c h);
|
|
37
|
+
--color-${color}-hover: oklch(from var(--color-${color}) calc(l + (1 - l) * 0.25) c h);
|
|
38
|
+
--color-${color}-active: oklch(from var(--color-${color}) calc(l * 0.9) c h);
|
|
39
|
+
--color-${color}-contrast: oklch(from var(--color-${color}) clamp(var(--light-min), (var(--light-threshold-contrast) - l) * 100, var(--light-max)) 0 0);
|
|
40
|
+
--color-${color}-body: oklch(from var(--color-${color}) min(var(--light-threshold-body),l) c h);
|
|
41
|
+
--color-${color}-surface: oklch(from var(--color-${color}) var(--light-threshold-surface) min(var(--chorme-surface),c ) h);
|
|
42
|
+
--color-${color}-fg: oklch(from var(--color-${color}) min(var(--light-threshold-fg),l) c h);
|
|
43
|
+
--color-${color}-bg: oklch(from var(--color-${color}) var(--light-threshold-bg) min(var(--chorme-bg),c) h);
|
|
44
|
+
--color-${color}-bg-hover: oklch(from var(--color-${color}) var(--light-threshold-bg-hover) min(var(--chorme-bg-hover),c) h);
|
|
45
|
+
--color-${color}-bg-active:oklch(from var(--color-${color}) var(--light-threshold-bg-active) min(var(--chorme-bg-active),c) h);
|
|
46
|
+
--color-${color}-bd: oklch(from var(--color-${color}) var(--light-threshold-bd) min(var(--chorme-bd),c ) h);
|
|
47
|
+
--color-${color}-bd-hover: oklch(from var(--color-${color}) var(--light-threshold-bd-hover) min(var(--chorme-bd-hover),c ) h);
|
|
48
|
+
--color-${color}-bd-active: oklch(from var(--color-${color}) var(--light-threshold-bd-active) min(var(--chorme-bd-active),c ) h);
|
|
49
|
+
`;
|
|
50
|
+
let getAltColors = (color) => `
|
|
51
|
+
--color-${color}-base: oklch(from var(--color-title) calc(l * ${color === 'description' ? 2.5 : color === 'caption' ? 3.5 : color === 'placeholder' ? 4.5 : 1}) c h);
|
|
52
|
+
--color-${color}: var(--color-${color}-base);
|
|
53
|
+
--color-${color}-outline: var(--color-title);
|
|
54
|
+
--color-${color}-hover: oklch(from var(--color-title) calc(l + (1 - l) * 0.25) c h);
|
|
55
|
+
--color-${color}-active: oklch(from var(--color-title) calc(l * 0.9) c h);
|
|
56
|
+
--color-${color}-contrast: var(--color-title-contrast);
|
|
57
|
+
--color-${color}-body: var(--color-${color}-base);
|
|
58
|
+
--color-${color}-fg: var(--color-${color}-base);
|
|
59
|
+
--color-${color}-surface: var(--color-title-surface);
|
|
60
|
+
--color-${color}-bg: var(--color-title-bg);
|
|
61
|
+
--color-${color}-bg-hover: var(--color-title-bg-hover);
|
|
62
|
+
--color-${color}-bg-active: var(--color-title-bg-active);
|
|
63
|
+
--color-${color}-bd: var(--color-title-bd);
|
|
64
|
+
--color-${color}-bd-hover: var(--color-title-bd-hover);
|
|
65
|
+
--color-${color}-bd-active: var(--color-title-bd-active);
|
|
66
|
+
`;
|
|
67
|
+
let result = getColors(color);
|
|
68
|
+
if (color === 'title') {
|
|
69
|
+
result += getAltColors('description');
|
|
70
|
+
result += getAltColors('caption');
|
|
71
|
+
result += getAltColors('placeholder');
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
const getDarkColors = function (color) {
|
|
76
|
+
//-bg,-bg-hover,-bg-active的c值上限约定0.12,0.14,0.16,是经验判断的数值
|
|
77
|
+
let getColors = (color) => `
|
|
78
|
+
${color === 'title' ? `--color-${color}-base: oklch(from var(--color-ink-base) var(--light-threshold-body) c h);
|
|
79
|
+
--color-${color}: oklch(from var(--color-${color}-base) l calc(c * var(--color-c-factor,1)) h); ` : ''}
|
|
80
|
+
--color-${color}-hover: oklch(from var(--color-${color}) calc(l + (1 - l) * 0.25) c h);
|
|
81
|
+
--color-${color}-active: oklch(from var(--color-${color}) calc(l * 0.9) c h);
|
|
82
|
+
--color-${color}-body: oklch(from var(--color-${color}) min(var(--light-threshold-body),l) c h);
|
|
83
|
+
--color-${color}-surface:transparent;
|
|
84
|
+
--color-${color}-fg: oklch(from var(--color-${color}) min(var(--light-threshold-fg),l) c h);
|
|
85
|
+
--color-${color}-bg: oklch(from var(--color-${color}) var(--light-threshold-bg) c h / var(--opacity-bg));
|
|
86
|
+
--color-${color}-bg-hover: oklch(from var(--color-${color}) var(--light-threshold-bg-hover) c h / var(--opacity-bg-hover));
|
|
87
|
+
--color-${color}-bg-active:oklch(from var(--color-${color}) var(--light-threshold-bg-active) c h / var(--opacity-bg-active));
|
|
88
|
+
--color-${color}-bd: oklch(from var(--color-${color}) var(--light-threshold-bd) min(c ,0.12) h);
|
|
89
|
+
--color-${color}-bd-hover: oklch(from var(--color-${color}) var(--light-threshold-bd-hover) min(c ,0.14) h);
|
|
90
|
+
--color-${color}-bd-active: oklch(from var(--color-${color}) var(--light-threshold-bd-active) min(c ,0.16) h);
|
|
91
|
+
`;
|
|
92
|
+
let getAltColors = (color) => `
|
|
93
|
+
--color-${color}-base: oklch(from var(--color-title) calc(l * ${color === 'description' ? 0.85 : color === 'caption' ? 0.65 : color === 'placeholder' ? 0.5 : 1}) c h);
|
|
94
|
+
--color-${color}: var(--color-${color}-base);
|
|
95
|
+
--color-${color}-hover: oklch(from var(--color-${color}) calc(l + (1 - l) * 0.25) c h);
|
|
96
|
+
--color-${color}-active: oklch(from var(--color-${color}) calc(l * 0.9) c h);
|
|
97
|
+
--color-${color}-body: var(--color-${color});
|
|
98
|
+
--color-${color}-fg: var(--color-${color});
|
|
99
|
+
`;
|
|
100
|
+
let result = getColors(color);
|
|
101
|
+
if (color === 'title') {
|
|
102
|
+
result += getAltColors('description');
|
|
103
|
+
result += getAltColors('caption');
|
|
104
|
+
result += getAltColors('placeholder');
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
// 生成所有样式
|
|
109
|
+
function generate() {
|
|
110
|
+
//@theme{}
|
|
111
|
+
let output = '@theme {\n';
|
|
112
|
+
//获得light colors
|
|
113
|
+
let lightColors = ``;
|
|
114
|
+
colors.forEach(color => {
|
|
115
|
+
lightColors += getLightColors(color) + '\n\n';
|
|
116
|
+
});
|
|
117
|
+
//获得dark colors
|
|
118
|
+
let darkColors = ``;
|
|
119
|
+
colors.forEach(color => {
|
|
120
|
+
darkColors += getDarkColors(color) + '\n\n';
|
|
121
|
+
});
|
|
122
|
+
output += lightColors;
|
|
123
|
+
output += '}\n';
|
|
124
|
+
//.light{}
|
|
125
|
+
output += '@utility light{\n&:where(:not(:root)) {\n';
|
|
126
|
+
output += lightColors;
|
|
127
|
+
output += '}\n}\n';
|
|
128
|
+
//暗黑
|
|
129
|
+
output += `
|
|
130
|
+
@media (prefers-color-scheme: dark) {
|
|
131
|
+
:root,
|
|
132
|
+
:host {
|
|
133
|
+
`;
|
|
134
|
+
output += darkColors;
|
|
135
|
+
output += '}\n}\n\n';
|
|
136
|
+
output += '@utility dark{\n';
|
|
137
|
+
output += darkColors;
|
|
138
|
+
output += '}\n';
|
|
139
|
+
return output;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 写入文件
|
|
143
|
+
const outputPath = path.join(__dirname, 'src/color-base.css');
|
|
144
|
+
fs.writeFileSync(outputPath, generate());
|
|
145
|
+
console.log(`✅ 已生样式文件: ${outputPath}`);
|
|
146
|
+
|
|
147
|
+
// 导出供其他模块使用(可选)
|
|
148
|
+
export { colors, generate };
|
package/script-mini.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Last modified: 2026/11/02 11:05:00
|
|
3
|
+
*
|
|
4
|
+
* CSS 压缩脚本 - 将 dist 目录下的每个 CSS 文件压缩为同名 .min.css 文件
|
|
5
|
+
* dist/wispcss.css -> dist/wispcss.min.css
|
|
6
|
+
* *.min.css 不会被再次压缩,因此脚本可重复执行。
|
|
7
|
+
*
|
|
8
|
+
* 使用 lightningcss(Tailwind v4 内部使用的同一个压缩器)。
|
|
9
|
+
* 说明:clean-css(cleancss) 无法解析 Tailwind v4 产物的现代语法,
|
|
10
|
+
* 在 tokenizer 阶段会直接抛异常,因此不再使用。
|
|
11
|
+
*
|
|
12
|
+
* 使用方式:node script-mini.js 或 npm run mini
|
|
13
|
+
*/
|
|
14
|
+
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'fs';
|
|
15
|
+
import path from 'path';
|
|
16
|
+
import { fileURLToPath } from 'url';
|
|
17
|
+
import { transform } from 'lightningcss';
|
|
18
|
+
|
|
19
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
const DIST_DIR = path.join(__dirname, 'dist');
|
|
21
|
+
|
|
22
|
+
const formatSize = (bytes) => `${(bytes / 1024).toFixed(1)} KB`;
|
|
23
|
+
|
|
24
|
+
const minifyCssFile = (input, output, filename) => {
|
|
25
|
+
const code = readFileSync(input);
|
|
26
|
+
const result = transform({ filename, code, minify: true });
|
|
27
|
+
writeFileSync(output, result.code);
|
|
28
|
+
return { before: code.length, after: result.code.length, warnings: result.warnings || [] };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const minifyCss = () => {
|
|
32
|
+
if (!existsSync(DIST_DIR)) {
|
|
33
|
+
console.error(`dist directory not found: ${DIST_DIR}`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 只处理 .css,跳过压缩产物 *.min.css
|
|
38
|
+
const files = readdirSync(DIST_DIR, { withFileTypes: true })
|
|
39
|
+
.filter((entry) => entry.isFile())
|
|
40
|
+
.map((entry) => entry.name)
|
|
41
|
+
.filter((name) => name.toLowerCase().endsWith('.css') && !name.toLowerCase().endsWith('.min.css'))
|
|
42
|
+
.sort();
|
|
43
|
+
|
|
44
|
+
if (!files.length) {
|
|
45
|
+
console.log('No CSS files found in dist, nothing to minify.');
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
console.log(`Minifying ${files.length} CSS file(s) in dist...`);
|
|
50
|
+
|
|
51
|
+
let failed = 0;
|
|
52
|
+
|
|
53
|
+
for (const name of files) {
|
|
54
|
+
const input = path.join(DIST_DIR, name);
|
|
55
|
+
const output = path.join(DIST_DIR, name.replace(/\.css$/i, '.min.css'));
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const { before, after, warnings } = minifyCssFile(input, output, name);
|
|
59
|
+
const ratio = before ? ((1 - after / before) * 100).toFixed(1) : '0.0';
|
|
60
|
+
console.log(
|
|
61
|
+
` ✓ ${name} -> ${path.basename(output)} ${formatSize(before)} -> ${formatSize(after)} (-${ratio}%)`
|
|
62
|
+
);
|
|
63
|
+
if (warnings.length) console.warn(` ${warnings.length} warning(s) reported by lightningcss`);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
failed += 1;
|
|
66
|
+
console.error(` ✗ ${name}: ${error.message}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (failed) {
|
|
71
|
+
console.error(`Minification finished with ${failed} error(s).`);
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
console.log('Minification complete.');
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
minifyCss();
|
package/script-prefix.js
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prefix Generator
|
|
3
|
+
*
|
|
4
|
+
* Usage: npm run prefix
|
|
5
|
+
*
|
|
6
|
+
* What it does (CSS files only):
|
|
7
|
+
* 1. Copy the whole `src` directory to `src-prefix` (an existing copy is removed first).
|
|
8
|
+
* 2. In `src-prefix/index.css`, rewrite `@import "tailwindcss";`
|
|
9
|
+
* to `@import "tailwindcss" prefix(tw);`.
|
|
10
|
+
* 3. Prefix every class name of each `@apply` statement in all CSS files under `src-prefix`.
|
|
11
|
+
* 4. Prefix static class selectors in rule headers, including `:where()`, `:not()`, `:has()` and `:is()`:
|
|
12
|
+
* @apply transition-all-normal inline-flex-center background-cover;
|
|
13
|
+
* -> @apply tw:transition-all-normal tw:inline-flex-center tw:background-cover;
|
|
14
|
+
* &:where(.check-subtle, .check-ghost) -> &:where(.tw\:check-subtle, .tw\:check-ghost)
|
|
15
|
+
*
|
|
16
|
+
* Tailwind v4 requires prefixed class names inside @apply as well, otherwise compilation fails:
|
|
17
|
+
* Cannot apply unprefixed utility class `xxx`. Did you mean `tw:xxx`?
|
|
18
|
+
*
|
|
19
|
+
* Note: `@apply` occurrences inside comments or strings are left untouched.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import fs from 'fs';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import readline from 'readline/promises';
|
|
25
|
+
import { fileURLToPath } from 'url';
|
|
26
|
+
|
|
27
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
const SRC_DIR = path.join(__dirname, 'src');
|
|
29
|
+
const DEST_DIR = path.join(__dirname, 'src-prefix');
|
|
30
|
+
const ENTRY_FILE = 'index.css';
|
|
31
|
+
const DEFAULT_PREFIX = 'tw';
|
|
32
|
+
const APPLY = '@apply';
|
|
33
|
+
|
|
34
|
+
/* ============================================
|
|
35
|
+
@apply transformation
|
|
36
|
+
============================================ */
|
|
37
|
+
const isWordChar = (char) => char !== undefined && /[\w-]/.test(char);
|
|
38
|
+
|
|
39
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Find the position of `@apply` on a line, ignoring comments and strings.
|
|
43
|
+
* The shared `state` keeps the block-comment state across lines.
|
|
44
|
+
* Returns -1 when the line has no `@apply` left to transform.
|
|
45
|
+
*/
|
|
46
|
+
const findApplyIndex = (line, state) => {
|
|
47
|
+
let quote = '';
|
|
48
|
+
let i = 0;
|
|
49
|
+
|
|
50
|
+
while (i < line.length) {
|
|
51
|
+
const char = line[i];
|
|
52
|
+
|
|
53
|
+
if (state.inBlock) {
|
|
54
|
+
const end = line.indexOf('*/', i);
|
|
55
|
+
if (end === -1) return -1; // block comment continues on the next line
|
|
56
|
+
state.inBlock = false;
|
|
57
|
+
i = end + 2;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (quote) {
|
|
62
|
+
if (char === '\\') {
|
|
63
|
+
i += 2;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === quote) quote = '';
|
|
67
|
+
i += 1;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (char === '"' || char === "'") {
|
|
72
|
+
quote = char;
|
|
73
|
+
i += 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (char === '/' && line[i + 1] === '*') {
|
|
78
|
+
state.inBlock = true;
|
|
79
|
+
i += 2;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (
|
|
84
|
+
line.startsWith(APPLY, i) &&
|
|
85
|
+
!isWordChar(line[i - 1]) &&
|
|
86
|
+
!isWordChar(line[i + APPLY.length])
|
|
87
|
+
) {
|
|
88
|
+
return i;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
i += 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return -1;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Prefix a single class name while keeping trailing markers such as `!` and `,`.
|
|
99
|
+
* CSS variable syntax and already prefixed names are returned unchanged.
|
|
100
|
+
*/
|
|
101
|
+
const prefixToken = (token, prefix) => {
|
|
102
|
+
const trailing = /[!,]+$/.exec(token);
|
|
103
|
+
const suffix = trailing ? trailing[0] : '';
|
|
104
|
+
const core = suffix ? token.slice(0, -suffix.length) : token;
|
|
105
|
+
|
|
106
|
+
if (!core) return token;
|
|
107
|
+
if (core.startsWith('(') || core.startsWith('--')) return token;
|
|
108
|
+
if (new RegExp(`^${escapeRegExp(prefix)}:`).test(core)) return token;
|
|
109
|
+
|
|
110
|
+
return `${prefix}:${core}${suffix}`;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/** Prefix a whole class-name list, normalising whitespace to single spaces */
|
|
114
|
+
const prefixClassList = (value, prefix) => {
|
|
115
|
+
const tokens = value.trim().split(/\s+/).filter(Boolean);
|
|
116
|
+
if (!tokens.length) return '';
|
|
117
|
+
return tokens.map((token) => prefixToken(token, prefix)).join(' ');
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Prefix static class selectors in CSS rule headers.
|
|
122
|
+
* The returned selector uses a CSS-escaped colon: `.tw\\:class-name`.
|
|
123
|
+
*/
|
|
124
|
+
const prefixSelectorClasses = (selector, prefix) => {
|
|
125
|
+
let result = '';
|
|
126
|
+
let quote = '';
|
|
127
|
+
let attributeDepth = 0;
|
|
128
|
+
let i = 0;
|
|
129
|
+
|
|
130
|
+
while (i < selector.length) {
|
|
131
|
+
const char = selector[i];
|
|
132
|
+
|
|
133
|
+
if (quote) {
|
|
134
|
+
result += char;
|
|
135
|
+
if (char === '\\' && i + 1 < selector.length) {
|
|
136
|
+
result += selector[i + 1];
|
|
137
|
+
i += 2;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (char === quote) quote = '';
|
|
141
|
+
i += 1;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (char === '"' || char === "'") {
|
|
146
|
+
quote = char;
|
|
147
|
+
result += char;
|
|
148
|
+
i += 1;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (char === '/' && selector[i + 1] === '*') {
|
|
153
|
+
const end = selector.indexOf('*/', i + 2);
|
|
154
|
+
if (end === -1) {
|
|
155
|
+
result += selector.slice(i);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
result += selector.slice(i, end + 2);
|
|
159
|
+
i = end + 2;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (char === '[') attributeDepth += 1;
|
|
164
|
+
if (char === ']') attributeDepth -= 1;
|
|
165
|
+
|
|
166
|
+
if (char === '\\') {
|
|
167
|
+
result += selector.slice(i, i + 2);
|
|
168
|
+
i += 2;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (char !== '.' || attributeDepth > 0) {
|
|
173
|
+
result += char;
|
|
174
|
+
i += 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const match = /^\.((?:--|-?(?:[A-Za-z_\u0080-\uFFFF]|\\(?:[0-9a-fA-F]{1,6}\s?|[^\r\n])))(?:[A-Za-z0-9_\u0080-\uFFFF-]|\\(?:[0-9a-fA-F]{1,6}\s?|[^\r\n]))*)/.exec(selector.slice(i));
|
|
179
|
+
if (!match) {
|
|
180
|
+
result += char;
|
|
181
|
+
i += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const className = match[1];
|
|
186
|
+
const hasPrefix = className.startsWith(`${prefix}\\:`);
|
|
187
|
+
|
|
188
|
+
if (hasPrefix) {
|
|
189
|
+
result += match[0];
|
|
190
|
+
} else {
|
|
191
|
+
result += `.${prefix}\\:${className}`;
|
|
192
|
+
}
|
|
193
|
+
i += match[0].length;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return result;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/** Transform only rule headers, leaving declarations, comments and strings untouched */
|
|
200
|
+
const transformSelectorHeaders = (content, prefix) => {
|
|
201
|
+
let result = '';
|
|
202
|
+
let segmentStart = 0;
|
|
203
|
+
let quote = '';
|
|
204
|
+
let inComment = false;
|
|
205
|
+
let parentheses = 0;
|
|
206
|
+
let brackets = 0;
|
|
207
|
+
|
|
208
|
+
for (let i = 0; i < content.length; i += 1) {
|
|
209
|
+
const char = content[i];
|
|
210
|
+
const next = content[i + 1];
|
|
211
|
+
|
|
212
|
+
if (inComment) {
|
|
213
|
+
if (char === '*' && next === '/') {
|
|
214
|
+
inComment = false;
|
|
215
|
+
i += 1;
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (quote) {
|
|
221
|
+
if (char === '\\') i += 1;
|
|
222
|
+
else if (char === quote) quote = '';
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (char === '/' && next === '*') {
|
|
227
|
+
inComment = true;
|
|
228
|
+
i += 1;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (char === '"' || char === "'") {
|
|
233
|
+
quote = char;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (char === '\\') {
|
|
238
|
+
i += 1;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (char === '(') parentheses += 1;
|
|
242
|
+
if (char === ')') parentheses -= 1;
|
|
243
|
+
if (char === '[') brackets += 1;
|
|
244
|
+
if (char === ']') brackets -= 1;
|
|
245
|
+
if (parentheses > 0 || brackets > 0) continue;
|
|
246
|
+
|
|
247
|
+
if (char === '{') {
|
|
248
|
+
const header = content.slice(segmentStart, i);
|
|
249
|
+
const code = header.replace(/\/\*[\s\S]*?\*\//g, '').trimStart();
|
|
250
|
+
// Rule headers and selector-bearing at-rules contain selectors, not declaration values.
|
|
251
|
+
const isSelector = !code.startsWith('@') && !code.startsWith('--');
|
|
252
|
+
const hasSelector = /^@(custom-variant|scope)\b/.test(code);
|
|
253
|
+
result += isSelector || hasSelector ? prefixSelectorClasses(header, prefix) : header;
|
|
254
|
+
result += char;
|
|
255
|
+
segmentStart = i + 1;
|
|
256
|
+
} else if (char === '}' || char === ';') {
|
|
257
|
+
const statement = content.slice(segmentStart, i + 1);
|
|
258
|
+
const code = statement.replace(/\/\*[\s\S]*?\*\//g, '').trimStart();
|
|
259
|
+
result += char === ';' && /^@custom-variant\b/.test(code)
|
|
260
|
+
? prefixSelectorClasses(statement, prefix)
|
|
261
|
+
: statement;
|
|
262
|
+
segmentStart = i + 1;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return result + content.slice(segmentStart);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/** Rewrite every `@apply` statement of a single line (comments and strings excluded) */
|
|
270
|
+
const transformLine = (line, prefix, state) => {
|
|
271
|
+
let result = '';
|
|
272
|
+
let rest = line;
|
|
273
|
+
|
|
274
|
+
for (;;) {
|
|
275
|
+
const at = findApplyIndex(rest, state);
|
|
276
|
+
if (at === -1) {
|
|
277
|
+
result += rest;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const start = at + APPLY.length;
|
|
282
|
+
let end = rest.indexOf(';', start);
|
|
283
|
+
if (end === -1) end = rest.length;
|
|
284
|
+
|
|
285
|
+
let value = rest.slice(start, end);
|
|
286
|
+
const suffix = rest.slice(end);
|
|
287
|
+
|
|
288
|
+
// Keep the inline comment that follows the `@apply` statement
|
|
289
|
+
let comment = '';
|
|
290
|
+
const commentAt = value.search(/\/\*/);
|
|
291
|
+
if (commentAt !== -1) {
|
|
292
|
+
comment = value.slice(commentAt);
|
|
293
|
+
value = value.slice(0, commentAt);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const prefixed = prefixClassList(value, prefix);
|
|
297
|
+
if (!prefixed) {
|
|
298
|
+
// Empty statement or comment only: skip this `@apply` and keep scanning
|
|
299
|
+
result += rest.slice(0, start);
|
|
300
|
+
rest = rest.slice(start);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
result += `${rest.slice(0, start)} ${prefixed}${comment ? ` ${comment}` : ''}`;
|
|
305
|
+
rest = suffix;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return result;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/** Rewrite the tailwindcss import of the entry file */
|
|
312
|
+
const transformEntryImport = (content, prefix) => {
|
|
313
|
+
const pattern = /@import\s+(['"])tailwindcss\1\s*;/g;
|
|
314
|
+
return content.replace(pattern, (full, quote) =>
|
|
315
|
+
full.includes('prefix(') ? full : `@import ${quote}tailwindcss${quote} prefix(${prefix});`
|
|
316
|
+
);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/* ============================================
|
|
320
|
+
Main
|
|
321
|
+
============================================ */
|
|
322
|
+
/** Collect every CSS file below a directory, recursively */
|
|
323
|
+
const walkCssFiles = (dir) => {
|
|
324
|
+
const files = [];
|
|
325
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
326
|
+
const fullPath = path.join(dir, entry.name);
|
|
327
|
+
if (entry.isDirectory()) files.push(...walkCssFiles(fullPath));
|
|
328
|
+
else if (entry.isFile() && entry.name.toLowerCase().endsWith('.css')) files.push(fullPath);
|
|
329
|
+
}
|
|
330
|
+
return files;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const askPrefix = async () => {
|
|
334
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
335
|
+
try {
|
|
336
|
+
const answer = await rl.question(
|
|
337
|
+
`Enter Tailwind prefix (press Enter to use the default "${DEFAULT_PREFIX}"): `
|
|
338
|
+
);
|
|
339
|
+
const prefix = answer.trim().replace(/[::\s]+$/, '');
|
|
340
|
+
return prefix || DEFAULT_PREFIX;
|
|
341
|
+
} finally {
|
|
342
|
+
rl.close();
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const main = async () => {
|
|
347
|
+
const prefix = await askPrefix();
|
|
348
|
+
|
|
349
|
+
fs.rmSync(DEST_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
350
|
+
fs.cpSync(SRC_DIR, DEST_DIR, { recursive: true });
|
|
351
|
+
|
|
352
|
+
console.log('');
|
|
353
|
+
console.log(`Source : ${path.basename(SRC_DIR)}`);
|
|
354
|
+
console.log(`Destination : ${path.basename(DEST_DIR)}`);
|
|
355
|
+
console.log(`Prefix : ${prefix}:`);
|
|
356
|
+
|
|
357
|
+
const cssFiles = walkCssFiles(DEST_DIR);
|
|
358
|
+
|
|
359
|
+
let changedFiles = 0;
|
|
360
|
+
let changedLines = 0;
|
|
361
|
+
|
|
362
|
+
for (const file of cssFiles) {
|
|
363
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
364
|
+
const isEntry = path.basename(file) === ENTRY_FILE;
|
|
365
|
+
const state = { inBlock: false };
|
|
366
|
+
|
|
367
|
+
let lines = 0;
|
|
368
|
+
const nextLines = content.split('\n').map((line) => {
|
|
369
|
+
const next = line.includes(APPLY) ? transformLine(line, prefix, state) : null;
|
|
370
|
+
if (next === null) {
|
|
371
|
+
// Lines without `@apply` still need scanning to track the block-comment state
|
|
372
|
+
findApplyIndex(line, state);
|
|
373
|
+
return line;
|
|
374
|
+
}
|
|
375
|
+
if (next !== line) lines += 1;
|
|
376
|
+
return next;
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
let nextContent = transformSelectorHeaders(nextLines.join('\n'), prefix);
|
|
380
|
+
if (isEntry) nextContent = transformEntryImport(nextContent, prefix);
|
|
381
|
+
|
|
382
|
+
if (nextContent !== content) {
|
|
383
|
+
fs.writeFileSync(file, nextContent, 'utf8');
|
|
384
|
+
changedFiles += 1;
|
|
385
|
+
changedLines += lines;
|
|
386
|
+
console.log(` ✓ ${path.relative(DEST_DIR, file)}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
console.log('');
|
|
391
|
+
console.log(`CSS files : ${cssFiles.length}`);
|
|
392
|
+
console.log(`Files rewritten : ${changedFiles}`);
|
|
393
|
+
console.log(`@apply lines fixed : ${changedLines}`);
|
|
394
|
+
console.log('Done.');
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
main();
|
|
398
|
+
|