css-typed-vars 0.1.0 → 0.3.0
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/README.md +30 -2
- package/dist/chunk-3XKCOLZL.js +26 -0
- package/dist/{chunk-SUEAXDIO.js → chunk-4VL6W5GO.js} +34 -10
- package/dist/{chunk-3LGOJDGV.js → chunk-B57NHMQD.js} +7 -7
- package/dist/cli.cjs +79 -18
- package/dist/cli.js +13 -13
- package/dist/esbuild.cjs +35 -14
- package/dist/esbuild.d.cts +1 -0
- package/dist/esbuild.d.ts +1 -0
- package/dist/esbuild.js +2 -2
- package/dist/generator-Ba8XeISD.d.cts +6 -0
- package/dist/generator-Ba8XeISD.d.ts +6 -0
- package/dist/index.cjs +63 -10
- package/dist/index.d.cts +8 -4
- package/dist/index.d.ts +8 -4
- package/dist/index.js +8 -11
- package/dist/rollup.cjs +35 -14
- package/dist/rollup.d.cts +1 -0
- package/dist/rollup.d.ts +1 -0
- package/dist/rollup.js +2 -2
- package/dist/unplugin.cjs +35 -14
- package/dist/unplugin.d.cts +4 -0
- package/dist/unplugin.d.ts +4 -0
- package/dist/unplugin.js +2 -2
- package/dist/vite.cjs +35 -14
- package/dist/vite.d.cts +1 -0
- package/dist/vite.d.ts +1 -0
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +35 -14
- package/dist/webpack.d.cts +1 -0
- package/dist/webpack.d.ts +1 -0
- package/dist/webpack.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,10 +108,19 @@ npx css-typed-vars --input "src/styles/**/*.css" --output src/cssVars.ts
|
|
|
108
108
|
npx css-typed-vars --input "src/styles/**/*.{css,scss}" --output src/cssVars.ts --watch
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
+
```sh
|
|
112
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --exclude "**/vendor/**"
|
|
113
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prefix theme --naming snake
|
|
114
|
+
npx css-typed-vars --input "src/**/*.css" --output src/cssVars.js # generates .js + .d.ts
|
|
115
|
+
```
|
|
116
|
+
|
|
111
117
|
| Flag | Description |
|
|
112
118
|
|------|-------------|
|
|
113
119
|
| `--input` | Glob pattern for CSS/SCSS/Less files |
|
|
114
|
-
| `--output` |
|
|
120
|
+
| `--output` | Output file. `.ts` → TypeScript, `.js` → JavaScript + `.d.ts` alongside |
|
|
121
|
+
| `--exclude` | Glob pattern for files to exclude (single value; use config file for multiple) |
|
|
122
|
+
| `--prefix` | Prefix for generated keys: `--prefix theme` → `themeColorPrimary` |
|
|
123
|
+
| `--naming` | Key naming: `camelCase` (default), `snake`, `kebab` |
|
|
115
124
|
| `--watch` | Watch for file changes and regenerate |
|
|
116
125
|
|
|
117
126
|
CLI flags override values from the config file.
|
|
@@ -124,6 +133,19 @@ The CLI looks for a config file in the current working directory (in order):
|
|
|
124
133
|
2. `css-typed-vars.config.mjs`
|
|
125
134
|
3. `css-typed-vars.config.json`
|
|
126
135
|
|
|
136
|
+
All options are supported in the config file:
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
// css-typed-vars.config.js
|
|
140
|
+
export default {
|
|
141
|
+
input: 'src/styles/**/*.{css,scss}',
|
|
142
|
+
output: 'src/cssVars.ts',
|
|
143
|
+
exclude: ['**/vendor/**', '**/node_modules/**'],
|
|
144
|
+
prefix: 'theme',
|
|
145
|
+
naming: 'snake', // 'camelCase' | 'snake' | 'kebab'
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
127
149
|
---
|
|
128
150
|
|
|
129
151
|
## Plugin
|
|
@@ -215,6 +237,9 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
|
|
|
215
237
|
| Option | Type | Default | Description |
|
|
216
238
|
|--------|------|---------|-------------|
|
|
217
239
|
| `input` | `string \| string[]` | — | Glob pattern for CSS/SCSS/Less files |
|
|
240
|
+
| `exclude` | `string \| string[]` | — | Glob pattern(s) for files to exclude |
|
|
241
|
+
| `prefix` | `string` | — | Prefix for generated keys: `'theme'` → `themeColorPrimary` |
|
|
242
|
+
| `naming` | `'camelCase' \| 'snake' \| 'kebab'` | `'camelCase'` | Key naming convention |
|
|
218
243
|
| `dts` | `string \| false` | inside `node_modules` | Path to write type declarations. `false` to skip |
|
|
219
244
|
|
|
220
245
|
---
|
|
@@ -267,7 +292,10 @@ import { generate } from 'css-typed-vars';
|
|
|
267
292
|
|
|
268
293
|
await generate({
|
|
269
294
|
input: 'src/styles/**/*.{css,scss}',
|
|
270
|
-
output: 'src/cssVars.ts',
|
|
295
|
+
output: 'src/cssVars.ts', // or 'src/cssVars.js' → generates .js + .d.ts
|
|
296
|
+
exclude: ['**/vendor/**'],
|
|
297
|
+
prefix: 'theme',
|
|
298
|
+
naming: 'snake', // 'camelCase' | 'snake' | 'kebab'
|
|
271
299
|
});
|
|
272
300
|
```
|
|
273
301
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateCode,
|
|
3
|
+
generateDeclaration,
|
|
4
|
+
generateJs,
|
|
5
|
+
scanVarNames
|
|
6
|
+
} from "./chunk-4VL6W5GO.js";
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { writeFile, mkdir } from "fs/promises";
|
|
10
|
+
import { dirname, resolve } from "path";
|
|
11
|
+
async function generate(options) {
|
|
12
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
13
|
+
const outPath = resolve(options.output);
|
|
14
|
+
await mkdir(dirname(outPath), { recursive: true });
|
|
15
|
+
if (options.output.endsWith(".js")) {
|
|
16
|
+
await writeFile(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
17
|
+
const dtsPath = outPath.replace(/\.js$/, ".d.ts");
|
|
18
|
+
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
19
|
+
} else {
|
|
20
|
+
await writeFile(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
generate
|
|
26
|
+
};
|
|
@@ -21,11 +21,12 @@ function parseVarNames(css) {
|
|
|
21
21
|
// src/scanner.ts
|
|
22
22
|
import { readFile } from "fs/promises";
|
|
23
23
|
import fg from "fast-glob";
|
|
24
|
-
async function scanVarNames(patterns) {
|
|
24
|
+
async function scanVarNames(patterns, exclude) {
|
|
25
25
|
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
26
26
|
(p) => p.replace(/\\/g, "/")
|
|
27
27
|
);
|
|
28
|
-
const
|
|
28
|
+
const normalizedExclude = exclude ? (Array.isArray(exclude) ? exclude : [exclude]).map((p) => p.replace(/\\/g, "/")) : [];
|
|
29
|
+
const files = await fg(normalized, { absolute: true, ignore: normalizedExclude });
|
|
29
30
|
const all = /* @__PURE__ */ new Set();
|
|
30
31
|
await Promise.all(
|
|
31
32
|
files.map(async (file) => {
|
|
@@ -39,11 +40,27 @@ async function scanVarNames(patterns) {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
// src/generator.ts
|
|
42
|
-
function
|
|
43
|
-
|
|
43
|
+
function toKey(cssVarName, naming = "camelCase") {
|
|
44
|
+
const stripped = cssVarName.replace(/^--/, "");
|
|
45
|
+
if (naming === "snake") return stripped.replace(/-/g, "_");
|
|
46
|
+
if (naming === "kebab") return stripped;
|
|
47
|
+
return stripped.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
44
48
|
}
|
|
45
|
-
function
|
|
46
|
-
|
|
49
|
+
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
50
|
+
if (!prefix) return key;
|
|
51
|
+
if (naming === "snake") return `${prefix}_${key}`;
|
|
52
|
+
if (naming === "kebab") return `${prefix}-${key}`;
|
|
53
|
+
return prefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
54
|
+
}
|
|
55
|
+
function formatKey(key, naming = "camelCase") {
|
|
56
|
+
if (naming === "kebab") return `'${key}'`;
|
|
57
|
+
return key;
|
|
58
|
+
}
|
|
59
|
+
function generateCode(varNames, prefix, naming) {
|
|
60
|
+
const entries = varNames.map((name) => {
|
|
61
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
62
|
+
return ` ${key}: 'var(${name})',`;
|
|
63
|
+
});
|
|
47
64
|
return [
|
|
48
65
|
"// generated \u2014 do not edit",
|
|
49
66
|
"export const cssVars = {",
|
|
@@ -54,17 +71,24 @@ function generateCode(varNames) {
|
|
|
54
71
|
""
|
|
55
72
|
].join("\n");
|
|
56
73
|
}
|
|
57
|
-
function generateJs(varNames) {
|
|
58
|
-
const entries = varNames.map((name) =>
|
|
74
|
+
function generateJs(varNames, prefix, naming) {
|
|
75
|
+
const entries = varNames.map((name) => {
|
|
76
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
77
|
+
return ` ${key}: 'var(${name})',`;
|
|
78
|
+
});
|
|
59
79
|
return [
|
|
80
|
+
"// generated \u2014 do not edit",
|
|
60
81
|
"export const cssVars = {",
|
|
61
82
|
...entries,
|
|
62
83
|
"};",
|
|
63
84
|
""
|
|
64
85
|
].join("\n");
|
|
65
86
|
}
|
|
66
|
-
function generateDeclaration(varNames) {
|
|
67
|
-
const entries = varNames.map((name) =>
|
|
87
|
+
function generateDeclaration(varNames, prefix, naming) {
|
|
88
|
+
const entries = varNames.map((name) => {
|
|
89
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
90
|
+
return ` ${key}: 'var(${name})';`;
|
|
91
|
+
});
|
|
68
92
|
return [
|
|
69
93
|
"export declare const cssVars: {",
|
|
70
94
|
...entries,
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
generateDeclaration,
|
|
4
4
|
generateJs,
|
|
5
5
|
scanVarNames
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-4VL6W5GO.js";
|
|
7
7
|
|
|
8
8
|
// src/unplugin.ts
|
|
9
9
|
import { createUnplugin } from "unplugin";
|
|
@@ -22,10 +22,10 @@ var unplugin_default = createUnplugin((options) => ({
|
|
|
22
22
|
configureServer(server) {
|
|
23
23
|
server.watcher.on("change", async (file) => {
|
|
24
24
|
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
25
|
-
const names = await scanVarNames(options.input);
|
|
25
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
26
26
|
const dtsPath = getDtsPath(options);
|
|
27
27
|
if (dtsPath) {
|
|
28
|
-
await writeFile(dtsPath, generateDeclaration(names), "utf8");
|
|
28
|
+
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
29
29
|
}
|
|
30
30
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
31
31
|
if (mod) {
|
|
@@ -38,16 +38,16 @@ var unplugin_default = createUnplugin((options) => ({
|
|
|
38
38
|
async buildStart() {
|
|
39
39
|
const dtsPath = getDtsPath(options);
|
|
40
40
|
if (!dtsPath) return;
|
|
41
|
-
const names = await scanVarNames(options.input);
|
|
42
|
-
await writeFile(dtsPath, generateDeclaration(names), "utf8");
|
|
41
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
42
|
+
await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
43
43
|
},
|
|
44
44
|
resolveId(id) {
|
|
45
45
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
46
46
|
},
|
|
47
47
|
async load(id) {
|
|
48
48
|
if (id === RESOLVED_ID) {
|
|
49
|
-
const names = await scanVarNames(options.input);
|
|
50
|
-
return generateJs(names);
|
|
49
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
50
|
+
return generateJs(names, options.prefix, options.naming);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}));
|
package/dist/cli.cjs
CHANGED
|
@@ -24,11 +24,15 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
));
|
|
25
25
|
|
|
26
26
|
// src/cli.ts
|
|
27
|
-
var
|
|
28
|
-
var
|
|
27
|
+
var import_promises3 = require("fs/promises");
|
|
28
|
+
var import_node_path2 = require("path");
|
|
29
29
|
var import_node_url = require("url");
|
|
30
30
|
var import_chokidar = require("chokidar");
|
|
31
31
|
|
|
32
|
+
// src/index.ts
|
|
33
|
+
var import_promises2 = require("fs/promises");
|
|
34
|
+
var import_node_path = require("path");
|
|
35
|
+
|
|
32
36
|
// src/scanner.ts
|
|
33
37
|
var import_promises = require("fs/promises");
|
|
34
38
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
@@ -47,11 +51,12 @@ function parseVarNames(css) {
|
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
// src/scanner.ts
|
|
50
|
-
async function scanVarNames(patterns) {
|
|
54
|
+
async function scanVarNames(patterns, exclude) {
|
|
51
55
|
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
52
56
|
(p) => p.replace(/\\/g, "/")
|
|
53
57
|
);
|
|
54
|
-
const
|
|
58
|
+
const normalizedExclude = exclude ? (Array.isArray(exclude) ? exclude : [exclude]).map((p) => p.replace(/\\/g, "/")) : [];
|
|
59
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
55
60
|
const all = /* @__PURE__ */ new Set();
|
|
56
61
|
await Promise.all(
|
|
57
62
|
files.map(async (file) => {
|
|
@@ -65,11 +70,27 @@ async function scanVarNames(patterns) {
|
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
// src/generator.ts
|
|
68
|
-
function
|
|
69
|
-
|
|
73
|
+
function toKey(cssVarName, naming = "camelCase") {
|
|
74
|
+
const stripped = cssVarName.replace(/^--/, "");
|
|
75
|
+
if (naming === "snake") return stripped.replace(/-/g, "_");
|
|
76
|
+
if (naming === "kebab") return stripped;
|
|
77
|
+
return stripped.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
78
|
+
}
|
|
79
|
+
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
80
|
+
if (!prefix) return key;
|
|
81
|
+
if (naming === "snake") return `${prefix}_${key}`;
|
|
82
|
+
if (naming === "kebab") return `${prefix}-${key}`;
|
|
83
|
+
return prefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
70
84
|
}
|
|
71
|
-
function
|
|
72
|
-
|
|
85
|
+
function formatKey(key, naming = "camelCase") {
|
|
86
|
+
if (naming === "kebab") return `'${key}'`;
|
|
87
|
+
return key;
|
|
88
|
+
}
|
|
89
|
+
function generateCode(varNames, prefix, naming) {
|
|
90
|
+
const entries = varNames.map((name) => {
|
|
91
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
92
|
+
return ` ${key}: 'var(${name})',`;
|
|
93
|
+
});
|
|
73
94
|
return [
|
|
74
95
|
"// generated \u2014 do not edit",
|
|
75
96
|
"export const cssVars = {",
|
|
@@ -80,6 +101,46 @@ function generateCode(varNames) {
|
|
|
80
101
|
""
|
|
81
102
|
].join("\n");
|
|
82
103
|
}
|
|
104
|
+
function generateJs(varNames, prefix, naming) {
|
|
105
|
+
const entries = varNames.map((name) => {
|
|
106
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
107
|
+
return ` ${key}: 'var(${name})',`;
|
|
108
|
+
});
|
|
109
|
+
return [
|
|
110
|
+
"// generated \u2014 do not edit",
|
|
111
|
+
"export const cssVars = {",
|
|
112
|
+
...entries,
|
|
113
|
+
"};",
|
|
114
|
+
""
|
|
115
|
+
].join("\n");
|
|
116
|
+
}
|
|
117
|
+
function generateDeclaration(varNames, prefix, naming) {
|
|
118
|
+
const entries = varNames.map((name) => {
|
|
119
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
120
|
+
return ` ${key}: 'var(${name})';`;
|
|
121
|
+
});
|
|
122
|
+
return [
|
|
123
|
+
"export declare const cssVars: {",
|
|
124
|
+
...entries,
|
|
125
|
+
"};",
|
|
126
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
127
|
+
""
|
|
128
|
+
].join("\n");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/index.ts
|
|
132
|
+
async function generate(options) {
|
|
133
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
134
|
+
const outPath = (0, import_node_path.resolve)(options.output);
|
|
135
|
+
await (0, import_promises2.mkdir)((0, import_node_path.dirname)(outPath), { recursive: true });
|
|
136
|
+
if (options.output.endsWith(".js")) {
|
|
137
|
+
await (0, import_promises2.writeFile)(outPath, generateJs(names, options.prefix, options.naming), "utf8");
|
|
138
|
+
const dtsPath = outPath.replace(/\.js$/, ".d.ts");
|
|
139
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
140
|
+
} else {
|
|
141
|
+
await (0, import_promises2.writeFile)(outPath, generateCode(names, options.prefix, options.naming), "utf8");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
83
144
|
|
|
84
145
|
// src/cli.ts
|
|
85
146
|
var args = process.argv.slice(2);
|
|
@@ -95,10 +156,10 @@ async function loadConfig() {
|
|
|
95
156
|
"css-typed-vars.config.json"
|
|
96
157
|
];
|
|
97
158
|
for (const file of candidates) {
|
|
98
|
-
const path = (0,
|
|
159
|
+
const path = (0, import_node_path2.resolve)(process.cwd(), file);
|
|
99
160
|
try {
|
|
100
161
|
if (file.endsWith(".json")) {
|
|
101
|
-
const content = await (0,
|
|
162
|
+
const content = await (0, import_promises3.readFile)(path, "utf8");
|
|
102
163
|
return JSON.parse(content);
|
|
103
164
|
} else {
|
|
104
165
|
const mod = await import((0, import_node_url.pathToFileURL)(path).href);
|
|
@@ -109,28 +170,28 @@ async function loadConfig() {
|
|
|
109
170
|
}
|
|
110
171
|
return {};
|
|
111
172
|
}
|
|
112
|
-
async function run(input, output) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
await (0, import_promises2.mkdir)((0, import_node_path.dirname)((0, import_node_path.resolve)(output)), { recursive: true });
|
|
116
|
-
await (0, import_promises2.writeFile)(output, code, "utf8");
|
|
117
|
-
console.log(`Generated ${names.length} variables \u2192 ${output}`);
|
|
173
|
+
async function run(input, output, exclude, prefix, naming) {
|
|
174
|
+
await generate({ input, output, exclude, prefix, naming });
|
|
175
|
+
console.log(`Generated \u2192 ${output}`);
|
|
118
176
|
}
|
|
119
177
|
async function main() {
|
|
120
178
|
const config = await loadConfig();
|
|
121
179
|
const input = getArg("--input") ?? config.input;
|
|
122
180
|
const output = getArg("--output") ?? config.output;
|
|
181
|
+
const exclude = getArg("--exclude") ?? config.exclude;
|
|
182
|
+
const prefix = getArg("--prefix") ?? config.prefix;
|
|
183
|
+
const naming = getArg("--naming") ?? config.naming;
|
|
123
184
|
if (!input || !output) {
|
|
124
185
|
console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
|
|
125
186
|
console.error("Or add a css-typed-vars.config.js file with input and output fields.");
|
|
126
187
|
process.exit(1);
|
|
127
188
|
}
|
|
128
|
-
await run(input, output);
|
|
189
|
+
await run(input, output, exclude, prefix, naming);
|
|
129
190
|
if (watchMode) {
|
|
130
191
|
const patterns = Array.isArray(input) ? input : [input];
|
|
131
192
|
(0, import_chokidar.watch)(patterns).on("change", (file) => {
|
|
132
193
|
console.log(`Changed: ${file}`);
|
|
133
|
-
run(input, output).catch(console.error);
|
|
194
|
+
run(input, output, exclude, prefix, naming).catch(console.error);
|
|
134
195
|
});
|
|
135
196
|
console.log("Watching for changes...");
|
|
136
197
|
}
|
package/dist/cli.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
generate
|
|
4
|
+
} from "./chunk-3XKCOLZL.js";
|
|
5
|
+
import "./chunk-4VL6W5GO.js";
|
|
6
6
|
|
|
7
7
|
// src/cli.ts
|
|
8
|
-
import { readFile
|
|
9
|
-
import { resolve
|
|
8
|
+
import { readFile } from "fs/promises";
|
|
9
|
+
import { resolve } from "path";
|
|
10
10
|
import { pathToFileURL } from "url";
|
|
11
11
|
import { watch } from "chokidar";
|
|
12
12
|
var args = process.argv.slice(2);
|
|
@@ -36,28 +36,28 @@ async function loadConfig() {
|
|
|
36
36
|
}
|
|
37
37
|
return {};
|
|
38
38
|
}
|
|
39
|
-
async function run(input, output) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
await mkdir(dirname(resolve(output)), { recursive: true });
|
|
43
|
-
await writeFile(output, code, "utf8");
|
|
44
|
-
console.log(`Generated ${names.length} variables \u2192 ${output}`);
|
|
39
|
+
async function run(input, output, exclude, prefix, naming) {
|
|
40
|
+
await generate({ input, output, exclude, prefix, naming });
|
|
41
|
+
console.log(`Generated \u2192 ${output}`);
|
|
45
42
|
}
|
|
46
43
|
async function main() {
|
|
47
44
|
const config = await loadConfig();
|
|
48
45
|
const input = getArg("--input") ?? config.input;
|
|
49
46
|
const output = getArg("--output") ?? config.output;
|
|
47
|
+
const exclude = getArg("--exclude") ?? config.exclude;
|
|
48
|
+
const prefix = getArg("--prefix") ?? config.prefix;
|
|
49
|
+
const naming = getArg("--naming") ?? config.naming;
|
|
50
50
|
if (!input || !output) {
|
|
51
51
|
console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
|
|
52
52
|
console.error("Or add a css-typed-vars.config.js file with input and output fields.");
|
|
53
53
|
process.exit(1);
|
|
54
54
|
}
|
|
55
|
-
await run(input, output);
|
|
55
|
+
await run(input, output, exclude, prefix, naming);
|
|
56
56
|
if (watchMode) {
|
|
57
57
|
const patterns = Array.isArray(input) ? input : [input];
|
|
58
58
|
watch(patterns).on("change", (file) => {
|
|
59
59
|
console.log(`Changed: ${file}`);
|
|
60
|
-
run(input, output).catch(console.error);
|
|
60
|
+
run(input, output, exclude, prefix, naming).catch(console.error);
|
|
61
61
|
});
|
|
62
62
|
console.log("Watching for changes...");
|
|
63
63
|
}
|
package/dist/esbuild.cjs
CHANGED
|
@@ -57,11 +57,12 @@ function parseVarNames(css) {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
// src/scanner.ts
|
|
60
|
-
async function scanVarNames(patterns) {
|
|
60
|
+
async function scanVarNames(patterns, exclude) {
|
|
61
61
|
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
62
62
|
(p) => p.replace(/\\/g, "/")
|
|
63
63
|
);
|
|
64
|
-
const
|
|
64
|
+
const normalizedExclude = exclude ? (Array.isArray(exclude) ? exclude : [exclude]).map((p) => p.replace(/\\/g, "/")) : [];
|
|
65
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true, ignore: normalizedExclude });
|
|
65
66
|
const all = /* @__PURE__ */ new Set();
|
|
66
67
|
await Promise.all(
|
|
67
68
|
files.map(async (file) => {
|
|
@@ -75,20 +76,40 @@ async function scanVarNames(patterns) {
|
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
// src/generator.ts
|
|
78
|
-
function
|
|
79
|
-
|
|
79
|
+
function toKey(cssVarName, naming = "camelCase") {
|
|
80
|
+
const stripped = cssVarName.replace(/^--/, "");
|
|
81
|
+
if (naming === "snake") return stripped.replace(/-/g, "_");
|
|
82
|
+
if (naming === "kebab") return stripped;
|
|
83
|
+
return stripped.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
80
84
|
}
|
|
81
|
-
function
|
|
82
|
-
|
|
85
|
+
function applyPrefix(key, prefix, naming = "camelCase") {
|
|
86
|
+
if (!prefix) return key;
|
|
87
|
+
if (naming === "snake") return `${prefix}_${key}`;
|
|
88
|
+
if (naming === "kebab") return `${prefix}-${key}`;
|
|
89
|
+
return prefix + key.charAt(0).toUpperCase() + key.slice(1);
|
|
90
|
+
}
|
|
91
|
+
function formatKey(key, naming = "camelCase") {
|
|
92
|
+
if (naming === "kebab") return `'${key}'`;
|
|
93
|
+
return key;
|
|
94
|
+
}
|
|
95
|
+
function generateJs(varNames, prefix, naming) {
|
|
96
|
+
const entries = varNames.map((name) => {
|
|
97
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
98
|
+
return ` ${key}: 'var(${name})',`;
|
|
99
|
+
});
|
|
83
100
|
return [
|
|
101
|
+
"// generated \u2014 do not edit",
|
|
84
102
|
"export const cssVars = {",
|
|
85
103
|
...entries,
|
|
86
104
|
"};",
|
|
87
105
|
""
|
|
88
106
|
].join("\n");
|
|
89
107
|
}
|
|
90
|
-
function generateDeclaration(varNames) {
|
|
91
|
-
const entries = varNames.map((name) =>
|
|
108
|
+
function generateDeclaration(varNames, prefix, naming) {
|
|
109
|
+
const entries = varNames.map((name) => {
|
|
110
|
+
const key = formatKey(applyPrefix(toKey(name, naming), prefix, naming), naming);
|
|
111
|
+
return ` ${key}: 'var(${name})';`;
|
|
112
|
+
});
|
|
92
113
|
return [
|
|
93
114
|
"export declare const cssVars: {",
|
|
94
115
|
...entries,
|
|
@@ -112,10 +133,10 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
|
|
|
112
133
|
configureServer(server) {
|
|
113
134
|
server.watcher.on("change", async (file) => {
|
|
114
135
|
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
115
|
-
const names = await scanVarNames(options.input);
|
|
136
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
116
137
|
const dtsPath = getDtsPath(options);
|
|
117
138
|
if (dtsPath) {
|
|
118
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
139
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
119
140
|
}
|
|
120
141
|
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
121
142
|
if (mod) {
|
|
@@ -128,16 +149,16 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
|
|
|
128
149
|
async buildStart() {
|
|
129
150
|
const dtsPath = getDtsPath(options);
|
|
130
151
|
if (!dtsPath) return;
|
|
131
|
-
const names = await scanVarNames(options.input);
|
|
132
|
-
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
152
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
153
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
|
|
133
154
|
},
|
|
134
155
|
resolveId(id) {
|
|
135
156
|
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
136
157
|
},
|
|
137
158
|
async load(id) {
|
|
138
159
|
if (id === RESOLVED_ID) {
|
|
139
|
-
const names = await scanVarNames(options.input);
|
|
140
|
-
return generateJs(names);
|
|
160
|
+
const names = await scanVarNames(options.input, options.exclude);
|
|
161
|
+
return generateJs(names, options.prefix, options.naming);
|
|
141
162
|
}
|
|
142
163
|
}
|
|
143
164
|
}));
|
package/dist/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
+
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
+
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
+
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
+
|
|
6
|
+
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type NamingConvention = 'camelCase' | 'snake' | 'kebab';
|
|
2
|
+
declare function generateCode(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
3
|
+
declare function generateJs(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
4
|
+
declare function generateDeclaration(varNames: string[], prefix?: string, naming?: NamingConvention): string;
|
|
5
|
+
|
|
6
|
+
export { type NamingConvention as N, generateDeclaration as a, generateJs as b, generateCode as g };
|