css-typed-vars 0.1.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/LICENSE +21 -0
- package/README.md +303 -0
- package/dist/chunk-3LGOJDGV.js +57 -0
- package/dist/chunk-SUEAXDIO.js +84 -0
- package/dist/cli.cjs +138 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +65 -0
- package/dist/esbuild.cjs +146 -0
- package/dist/esbuild.d.cts +7 -0
- package/dist/esbuild.d.ts +7 -0
- package/dist/esbuild.js +10 -0
- package/dist/generated.d.ts +2 -0
- package/dist/index.cjs +107 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +21 -0
- package/dist/rollup.cjs +146 -0
- package/dist/rollup.d.cts +7 -0
- package/dist/rollup.d.ts +7 -0
- package/dist/rollup.js +10 -0
- package/dist/unplugin.cjs +141 -0
- package/dist/unplugin.d.cts +15 -0
- package/dist/unplugin.d.ts +15 -0
- package/dist/unplugin.js +7 -0
- package/dist/vars.cjs +2 -0
- package/dist/vars.js +1 -0
- package/dist/vite.cjs +146 -0
- package/dist/vite.d.cts +7 -0
- package/dist/vite.d.ts +7 -0
- package/dist/vite.js +10 -0
- package/dist/webpack.cjs +146 -0
- package/dist/webpack.d.cts +6 -0
- package/dist/webpack.d.ts +6 -0
- package/dist/webpack.js +10 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stanislav Kozachenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
<h1>css-typed-vars</h1>
|
|
2
|
+
<p>Generate TypeScript typed constants from CSS custom properties</p>
|
|
3
|
+
|
|
4
|
+
<p>
|
|
5
|
+
<a href="https://github.com/StanislavKozachenko/css-typed-vars/actions"><img src="https://github.com/StanislavKozachenko/css-typed-vars/actions/workflows/ci.yml/badge.svg" alt="CI" /></a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/css-typed-vars"><img src="https://img.shields.io/npm/v/css-typed-vars" alt="npm version" /></a>
|
|
7
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/github/license/StanislavKozachenko/css-typed-vars" alt="MIT License" /></a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Two approaches](#two-approaches)
|
|
14
|
+
- [CLI](#cli)
|
|
15
|
+
- [CLI flags](#cli-flags)
|
|
16
|
+
- [Config file](#config-file)
|
|
17
|
+
- [Plugin](#plugin)
|
|
18
|
+
- [Vite](#vite)
|
|
19
|
+
- [webpack](#webpack)
|
|
20
|
+
- [Rollup](#rollup)
|
|
21
|
+
- [esbuild](#esbuild)
|
|
22
|
+
- [Next.js (webpack)](#nextjs-webpack)
|
|
23
|
+
- [Turbopack](#turbopack)
|
|
24
|
+
- [Plugin options](#plugin-options)
|
|
25
|
+
- [Usage](#usage)
|
|
26
|
+
- [Programmatic API](#programmatic-api)
|
|
27
|
+
- [Supported formats](#supported-formats)
|
|
28
|
+
- [Contributing](#contributing)
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
`var(--color-primary)` is just a string. Rename the variable — the error shows up only in the browser.
|
|
33
|
+
|
|
34
|
+
`css-typed-vars` scans your CSS files and gives you typed constants. Use `cssVars.colorPrimary` instead — TypeScript catches missing variables at compile time.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
npm install -D css-typed-vars
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Two approaches
|
|
43
|
+
|
|
44
|
+
| | CLI | Plugin |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| How it works | Generates `cssVars.ts` in your project | Virtual module, no file generated |
|
|
47
|
+
| Import | `import { cssVars } from './cssVars'` | `import { cssVars } from 'css-typed-vars/vars'` |
|
|
48
|
+
| Watch | `--watch` flag | `vite dev`, browser reloads automatically |
|
|
49
|
+
| Works with | Everything | Vite, webpack, Rollup, esbuild (not Turbopack) |
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## CLI
|
|
54
|
+
|
|
55
|
+
Create a config file in your project root:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
// css-typed-vars.config.js
|
|
59
|
+
export default {
|
|
60
|
+
input: 'src/styles/**/*.{css,scss}',
|
|
61
|
+
output: 'src/cssVars.ts',
|
|
62
|
+
};
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Then run:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npx css-typed-vars # generate once
|
|
69
|
+
npx css-typed-vars --watch # watch for changes
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Add to your `package.json` scripts:
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"scripts": {
|
|
77
|
+
"generate:vars": "css-typed-vars",
|
|
78
|
+
"generate:vars:watch": "css-typed-vars --watch"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Given this CSS:
|
|
84
|
+
|
|
85
|
+
```css
|
|
86
|
+
:root {
|
|
87
|
+
--color-primary: #3b82f6;
|
|
88
|
+
--spacing-md: 8px;
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Generates `src/cssVars.ts`:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// generated — do not edit
|
|
96
|
+
export const cssVars = {
|
|
97
|
+
colorPrimary: 'var(--color-primary)',
|
|
98
|
+
spacingMd: 'var(--spacing-md)',
|
|
99
|
+
} as const;
|
|
100
|
+
|
|
101
|
+
export type CssVarName = keyof typeof cssVars;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### CLI flags
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
npx css-typed-vars --input "src/styles/**/*.css" --output src/cssVars.ts
|
|
108
|
+
npx css-typed-vars --input "src/styles/**/*.{css,scss}" --output src/cssVars.ts --watch
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
| Flag | Description |
|
|
112
|
+
|------|-------------|
|
|
113
|
+
| `--input` | Glob pattern for CSS/SCSS/Less files |
|
|
114
|
+
| `--output` | Path to the generated TypeScript file |
|
|
115
|
+
| `--watch` | Watch for file changes and regenerate |
|
|
116
|
+
|
|
117
|
+
CLI flags override values from the config file.
|
|
118
|
+
|
|
119
|
+
### Config file
|
|
120
|
+
|
|
121
|
+
The CLI looks for a config file in the current working directory (in order):
|
|
122
|
+
|
|
123
|
+
1. `css-typed-vars.config.js`
|
|
124
|
+
2. `css-typed-vars.config.mjs`
|
|
125
|
+
3. `css-typed-vars.config.json`
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Plugin
|
|
130
|
+
|
|
131
|
+
No file is generated — CSS variables are served as a virtual module directly by your bundler. Import from `css-typed-vars/vars` in your source code.
|
|
132
|
+
|
|
133
|
+
TypeScript types are written to `node_modules/css-typed-vars/dist/generated.d.ts` automatically on each build or dev server start.
|
|
134
|
+
|
|
135
|
+
### Vite
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
// vite.config.ts
|
|
139
|
+
import { defineConfig } from 'vite';
|
|
140
|
+
import cssTypedVars from 'css-typed-vars/vite';
|
|
141
|
+
|
|
142
|
+
export default defineConfig({
|
|
143
|
+
plugins: [
|
|
144
|
+
cssTypedVars({ input: 'src/styles/**/*.{css,scss}' }),
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
// src/index.ts
|
|
151
|
+
import { cssVars } from 'css-typed-vars/vars';
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### webpack
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
// webpack.config.js
|
|
158
|
+
import cssTypedVars from 'css-typed-vars/webpack';
|
|
159
|
+
|
|
160
|
+
export default {
|
|
161
|
+
plugins: [cssTypedVars({ input: 'src/styles/**/*.{css,scss}' })],
|
|
162
|
+
};
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Rollup
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
// rollup.config.js
|
|
169
|
+
import cssTypedVars from 'css-typed-vars/rollup';
|
|
170
|
+
|
|
171
|
+
export default {
|
|
172
|
+
plugins: [cssTypedVars({ input: 'src/styles/**/*.{css,scss}' })],
|
|
173
|
+
};
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### esbuild
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
import cssTypedVars from 'css-typed-vars/esbuild';
|
|
180
|
+
|
|
181
|
+
await esbuild.build({
|
|
182
|
+
plugins: [cssTypedVars({ input: 'src/styles/**/*.{css,scss}' })],
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Next.js (webpack)
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
// next.config.js
|
|
190
|
+
const cssTypedVars = require('css-typed-vars/webpack');
|
|
191
|
+
|
|
192
|
+
module.exports = {
|
|
193
|
+
webpack(config) {
|
|
194
|
+
config.plugins.push(cssTypedVars({ input: 'styles/**/*.{css,scss}' }));
|
|
195
|
+
return config;
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Turbopack
|
|
201
|
+
|
|
202
|
+
Turbopack does not yet have a public plugin API for virtual modules. Use the CLI instead:
|
|
203
|
+
|
|
204
|
+
```json
|
|
205
|
+
{
|
|
206
|
+
"scripts": {
|
|
207
|
+
"dev": "css-typed-vars && next dev --turbo",
|
|
208
|
+
"build": "css-typed-vars && next build"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Plugin options
|
|
214
|
+
|
|
215
|
+
| Option | Type | Default | Description |
|
|
216
|
+
|--------|------|---------|-------------|
|
|
217
|
+
| `input` | `string \| string[]` | — | Glob pattern for CSS/SCSS/Less files |
|
|
218
|
+
| `dts` | `string \| false` | inside `node_modules` | Path to write type declarations. `false` to skip |
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## Usage
|
|
223
|
+
|
|
224
|
+
**React:**
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import { cssVars } from './cssVars'; // CLI
|
|
228
|
+
// or
|
|
229
|
+
import { cssVars } from 'css-typed-vars/vars'; // plugin
|
|
230
|
+
|
|
231
|
+
<div style={{ color: cssVars.colorPrimary, padding: cssVars.spacingMd }} />
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**styled-components / emotion:**
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
const Button = styled.button`
|
|
238
|
+
color: ${cssVars.colorPrimary};
|
|
239
|
+
padding: ${cssVars.spacingMd};
|
|
240
|
+
`;
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Vue:**
|
|
244
|
+
|
|
245
|
+
```vue
|
|
246
|
+
<div :style="{ color: cssVars.colorPrimary }" />
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
**Svelte:**
|
|
250
|
+
|
|
251
|
+
```svelte
|
|
252
|
+
<div style:color={cssVars.colorPrimary} />
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Tailwind arbitrary values:**
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
<div className={`text-[${cssVars.colorPrimary}]`} />
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## Programmatic API
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { generate } from 'css-typed-vars';
|
|
267
|
+
|
|
268
|
+
await generate({
|
|
269
|
+
input: 'src/styles/**/*.{css,scss}',
|
|
270
|
+
output: 'src/cssVars.ts',
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Lower-level exports:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { parseVarNames, generateCode, scanVarNames } from 'css-typed-vars';
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Supported formats
|
|
281
|
+
|
|
282
|
+
| Format | Extension |
|
|
283
|
+
|--------|-----------|
|
|
284
|
+
| CSS | `.css` |
|
|
285
|
+
| SCSS | `.scss` |
|
|
286
|
+
| Less | `.less` |
|
|
287
|
+
|
|
288
|
+
Variables must be declared inside a `:root {}` block. Attribute selectors are supported (e.g. `:root[data-theme="dark"]`).
|
|
289
|
+
|
|
290
|
+
## Contributing
|
|
291
|
+
|
|
292
|
+
Contributions are welcome. Please open an issue before submitting a pull request.
|
|
293
|
+
|
|
294
|
+
```sh
|
|
295
|
+
git clone https://github.com/StanislavKozachenko/css-typed-vars.git
|
|
296
|
+
cd css-typed-vars
|
|
297
|
+
npm install
|
|
298
|
+
npm test
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
MIT
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__dirname,
|
|
3
|
+
generateDeclaration,
|
|
4
|
+
generateJs,
|
|
5
|
+
scanVarNames
|
|
6
|
+
} from "./chunk-SUEAXDIO.js";
|
|
7
|
+
|
|
8
|
+
// src/unplugin.ts
|
|
9
|
+
import { createUnplugin } from "unplugin";
|
|
10
|
+
import { writeFile } from "fs/promises";
|
|
11
|
+
import { join, resolve } from "path";
|
|
12
|
+
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
13
|
+
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
14
|
+
function getDtsPath(options) {
|
|
15
|
+
if (options.dts === false) return null;
|
|
16
|
+
return options.dts ? resolve(process.cwd(), options.dts) : join(__dirname, "generated.d.ts");
|
|
17
|
+
}
|
|
18
|
+
var unplugin_default = createUnplugin((options) => ({
|
|
19
|
+
name: "css-typed-vars",
|
|
20
|
+
vite: {
|
|
21
|
+
enforce: "pre",
|
|
22
|
+
configureServer(server) {
|
|
23
|
+
server.watcher.on("change", async (file) => {
|
|
24
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
25
|
+
const names = await scanVarNames(options.input);
|
|
26
|
+
const dtsPath = getDtsPath(options);
|
|
27
|
+
if (dtsPath) {
|
|
28
|
+
await writeFile(dtsPath, generateDeclaration(names), "utf8");
|
|
29
|
+
}
|
|
30
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
31
|
+
if (mod) {
|
|
32
|
+
server.moduleGraph.invalidateModule(mod);
|
|
33
|
+
server.ws.send({ type: "full-reload" });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
async buildStart() {
|
|
39
|
+
const dtsPath = getDtsPath(options);
|
|
40
|
+
if (!dtsPath) return;
|
|
41
|
+
const names = await scanVarNames(options.input);
|
|
42
|
+
await writeFile(dtsPath, generateDeclaration(names), "utf8");
|
|
43
|
+
},
|
|
44
|
+
resolveId(id) {
|
|
45
|
+
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
46
|
+
},
|
|
47
|
+
async load(id) {
|
|
48
|
+
if (id === RESOLVED_ID) {
|
|
49
|
+
const names = await scanVarNames(options.input);
|
|
50
|
+
return generateJs(names);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
unplugin_default
|
|
57
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// node_modules/tsup/assets/esm_shims.js
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
5
|
+
var getDirname = () => path.dirname(getFilename());
|
|
6
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
|
+
|
|
8
|
+
// src/parser.ts
|
|
9
|
+
function parseVarNames(css) {
|
|
10
|
+
const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
|
|
11
|
+
const names = /* @__PURE__ */ new Set();
|
|
12
|
+
const rootRegex = /:root[^{]*\{([^}]*)}/g;
|
|
13
|
+
for (const match of stripped.matchAll(rootRegex)) {
|
|
14
|
+
for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
15
|
+
names.add(prop[0]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return [...names];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/scanner.ts
|
|
22
|
+
import { readFile } from "fs/promises";
|
|
23
|
+
import fg from "fast-glob";
|
|
24
|
+
async function scanVarNames(patterns) {
|
|
25
|
+
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
26
|
+
(p) => p.replace(/\\/g, "/")
|
|
27
|
+
);
|
|
28
|
+
const files = await fg(normalized, { absolute: true });
|
|
29
|
+
const all = /* @__PURE__ */ new Set();
|
|
30
|
+
await Promise.all(
|
|
31
|
+
files.map(async (file) => {
|
|
32
|
+
const css = await readFile(file, "utf8");
|
|
33
|
+
for (const name of parseVarNames(css)) {
|
|
34
|
+
all.add(name);
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
return [...all];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/generator.ts
|
|
42
|
+
function toCamelCase(cssVarName) {
|
|
43
|
+
return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
44
|
+
}
|
|
45
|
+
function generateCode(varNames) {
|
|
46
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
47
|
+
return [
|
|
48
|
+
"// generated \u2014 do not edit",
|
|
49
|
+
"export const cssVars = {",
|
|
50
|
+
...entries,
|
|
51
|
+
"} as const;",
|
|
52
|
+
"",
|
|
53
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
54
|
+
""
|
|
55
|
+
].join("\n");
|
|
56
|
+
}
|
|
57
|
+
function generateJs(varNames) {
|
|
58
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
59
|
+
return [
|
|
60
|
+
"export const cssVars = {",
|
|
61
|
+
...entries,
|
|
62
|
+
"};",
|
|
63
|
+
""
|
|
64
|
+
].join("\n");
|
|
65
|
+
}
|
|
66
|
+
function generateDeclaration(varNames) {
|
|
67
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})';`);
|
|
68
|
+
return [
|
|
69
|
+
"export declare const cssVars: {",
|
|
70
|
+
...entries,
|
|
71
|
+
"};",
|
|
72
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
73
|
+
""
|
|
74
|
+
].join("\n");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export {
|
|
78
|
+
__dirname,
|
|
79
|
+
parseVarNames,
|
|
80
|
+
scanVarNames,
|
|
81
|
+
generateCode,
|
|
82
|
+
generateJs,
|
|
83
|
+
generateDeclaration
|
|
84
|
+
};
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli.ts
|
|
27
|
+
var import_promises2 = require("fs/promises");
|
|
28
|
+
var import_node_path = require("path");
|
|
29
|
+
var import_node_url = require("url");
|
|
30
|
+
var import_chokidar = require("chokidar");
|
|
31
|
+
|
|
32
|
+
// src/scanner.ts
|
|
33
|
+
var import_promises = require("fs/promises");
|
|
34
|
+
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
35
|
+
|
|
36
|
+
// src/parser.ts
|
|
37
|
+
function parseVarNames(css) {
|
|
38
|
+
const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
|
|
39
|
+
const names = /* @__PURE__ */ new Set();
|
|
40
|
+
const rootRegex = /:root[^{]*\{([^}]*)}/g;
|
|
41
|
+
for (const match of stripped.matchAll(rootRegex)) {
|
|
42
|
+
for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
|
|
43
|
+
names.add(prop[0]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [...names];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/scanner.ts
|
|
50
|
+
async function scanVarNames(patterns) {
|
|
51
|
+
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
52
|
+
(p) => p.replace(/\\/g, "/")
|
|
53
|
+
);
|
|
54
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true });
|
|
55
|
+
const all = /* @__PURE__ */ new Set();
|
|
56
|
+
await Promise.all(
|
|
57
|
+
files.map(async (file) => {
|
|
58
|
+
const css = await (0, import_promises.readFile)(file, "utf8");
|
|
59
|
+
for (const name of parseVarNames(css)) {
|
|
60
|
+
all.add(name);
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
return [...all];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/generator.ts
|
|
68
|
+
function toCamelCase(cssVarName) {
|
|
69
|
+
return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
70
|
+
}
|
|
71
|
+
function generateCode(varNames) {
|
|
72
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
73
|
+
return [
|
|
74
|
+
"// generated \u2014 do not edit",
|
|
75
|
+
"export const cssVars = {",
|
|
76
|
+
...entries,
|
|
77
|
+
"} as const;",
|
|
78
|
+
"",
|
|
79
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
80
|
+
""
|
|
81
|
+
].join("\n");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/cli.ts
|
|
85
|
+
var args = process.argv.slice(2);
|
|
86
|
+
var getArg = (flag) => {
|
|
87
|
+
const i = args.indexOf(flag);
|
|
88
|
+
return i !== -1 ? args[i + 1] : void 0;
|
|
89
|
+
};
|
|
90
|
+
var watchMode = args.includes("--watch");
|
|
91
|
+
async function loadConfig() {
|
|
92
|
+
const candidates = [
|
|
93
|
+
"css-typed-vars.config.js",
|
|
94
|
+
"css-typed-vars.config.mjs",
|
|
95
|
+
"css-typed-vars.config.json"
|
|
96
|
+
];
|
|
97
|
+
for (const file of candidates) {
|
|
98
|
+
const path = (0, import_node_path.resolve)(process.cwd(), file);
|
|
99
|
+
try {
|
|
100
|
+
if (file.endsWith(".json")) {
|
|
101
|
+
const content = await (0, import_promises2.readFile)(path, "utf8");
|
|
102
|
+
return JSON.parse(content);
|
|
103
|
+
} else {
|
|
104
|
+
const mod = await import((0, import_node_url.pathToFileURL)(path).href);
|
|
105
|
+
return mod.default;
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
async function run(input, output) {
|
|
113
|
+
const names = await scanVarNames(input);
|
|
114
|
+
const code = generateCode(names);
|
|
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}`);
|
|
118
|
+
}
|
|
119
|
+
async function main() {
|
|
120
|
+
const config = await loadConfig();
|
|
121
|
+
const input = getArg("--input") ?? config.input;
|
|
122
|
+
const output = getArg("--output") ?? config.output;
|
|
123
|
+
if (!input || !output) {
|
|
124
|
+
console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
|
|
125
|
+
console.error("Or add a css-typed-vars.config.js file with input and output fields.");
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
await run(input, output);
|
|
129
|
+
if (watchMode) {
|
|
130
|
+
const patterns = Array.isArray(input) ? input : [input];
|
|
131
|
+
(0, import_chokidar.watch)(patterns).on("change", (file) => {
|
|
132
|
+
console.log(`Changed: ${file}`);
|
|
133
|
+
run(input, output).catch(console.error);
|
|
134
|
+
});
|
|
135
|
+
console.log("Watching for changes...");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
main().catch(console.error);
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
generateCode,
|
|
4
|
+
scanVarNames
|
|
5
|
+
} from "./chunk-SUEAXDIO.js";
|
|
6
|
+
|
|
7
|
+
// src/cli.ts
|
|
8
|
+
import { readFile, writeFile, mkdir } from "fs/promises";
|
|
9
|
+
import { resolve, dirname } from "path";
|
|
10
|
+
import { pathToFileURL } from "url";
|
|
11
|
+
import { watch } from "chokidar";
|
|
12
|
+
var args = process.argv.slice(2);
|
|
13
|
+
var getArg = (flag) => {
|
|
14
|
+
const i = args.indexOf(flag);
|
|
15
|
+
return i !== -1 ? args[i + 1] : void 0;
|
|
16
|
+
};
|
|
17
|
+
var watchMode = args.includes("--watch");
|
|
18
|
+
async function loadConfig() {
|
|
19
|
+
const candidates = [
|
|
20
|
+
"css-typed-vars.config.js",
|
|
21
|
+
"css-typed-vars.config.mjs",
|
|
22
|
+
"css-typed-vars.config.json"
|
|
23
|
+
];
|
|
24
|
+
for (const file of candidates) {
|
|
25
|
+
const path = resolve(process.cwd(), file);
|
|
26
|
+
try {
|
|
27
|
+
if (file.endsWith(".json")) {
|
|
28
|
+
const content = await readFile(path, "utf8");
|
|
29
|
+
return JSON.parse(content);
|
|
30
|
+
} else {
|
|
31
|
+
const mod = await import(pathToFileURL(path).href);
|
|
32
|
+
return mod.default;
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
async function run(input, output) {
|
|
40
|
+
const names = await scanVarNames(input);
|
|
41
|
+
const code = generateCode(names);
|
|
42
|
+
await mkdir(dirname(resolve(output)), { recursive: true });
|
|
43
|
+
await writeFile(output, code, "utf8");
|
|
44
|
+
console.log(`Generated ${names.length} variables \u2192 ${output}`);
|
|
45
|
+
}
|
|
46
|
+
async function main() {
|
|
47
|
+
const config = await loadConfig();
|
|
48
|
+
const input = getArg("--input") ?? config.input;
|
|
49
|
+
const output = getArg("--output") ?? config.output;
|
|
50
|
+
if (!input || !output) {
|
|
51
|
+
console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
|
|
52
|
+
console.error("Or add a css-typed-vars.config.js file with input and output fields.");
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
await run(input, output);
|
|
56
|
+
if (watchMode) {
|
|
57
|
+
const patterns = Array.isArray(input) ? input : [input];
|
|
58
|
+
watch(patterns).on("change", (file) => {
|
|
59
|
+
console.log(`Changed: ${file}`);
|
|
60
|
+
run(input, output).catch(console.error);
|
|
61
|
+
});
|
|
62
|
+
console.log("Watching for changes...");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
main().catch(console.error);
|