css-typed-vars 0.3.1 → 0.4.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 CHANGED
@@ -1,3 +1,5 @@
1
+ <img src="https://repository-images.githubusercontent.com/1204912196/ef5412c5-0846-4b94-bf5d-811202144eb9" alt="css-typed-vars" />
2
+
1
3
  <h1>css-typed-vars</h1>
2
4
  <p>Generate TypeScript typed constants from CSS custom properties</p>
3
5
 
@@ -112,6 +114,7 @@ npx css-typed-vars --input "src/styles/**/*.{css,scss}" --output src/cssVars.ts
112
114
  npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --exclude "**/vendor/**"
113
115
  npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --prefix theme --naming snake
114
116
  npx css-typed-vars --input "src/**/*.css" --output src/cssVars.js # generates .js + .d.ts
117
+ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.ts --selector ".dark"
115
118
  ```
116
119
 
117
120
  | Flag | Description |
@@ -121,6 +124,7 @@ npx css-typed-vars --input "src/**/*.css" --output src/cssVars.js # generates .
121
124
  | `--exclude` | Glob pattern for files to exclude (single value; use config file for multiple) |
122
125
  | `--prefix` | Prefix for generated keys: `--prefix theme` → `themeColorPrimary` |
123
126
  | `--naming` | Key naming: `camelCase` (default), `snake`, `kebab` |
127
+ | `--selector` | Extra CSS selector to scan for variables (single value; use config file for multiple) |
124
128
  | `--watch` | Watch for file changes and regenerate |
125
129
  | `--version`, `-v` | Print the version number and exit |
126
130
 
@@ -144,6 +148,7 @@ export default {
144
148
  exclude: ['**/vendor/**', '**/node_modules/**'],
145
149
  prefix: 'theme',
146
150
  naming: 'snake', // 'camelCase' | 'snake' | 'kebab'
151
+ selectors: ['.dark', '[data-theme="dark"]'],
147
152
  };
148
153
  ```
149
154
 
@@ -241,6 +246,7 @@ Turbopack does not yet have a public plugin API for virtual modules. Use the CLI
241
246
  | `exclude` | `string \| string[]` | — | Glob pattern(s) for files to exclude |
242
247
  | `prefix` | `string` | — | Prefix for generated keys: `'theme'` → `themeColorPrimary` |
243
248
  | `naming` | `'camelCase' \| 'snake' \| 'kebab'` | `'camelCase'` | Key naming convention |
249
+ | `selectors` | `string[]` | — | Extra CSS selectors to scan, e.g. `['.dark', '[data-theme="dark"]']` |
244
250
  | `dts` | `string \| false` | inside `node_modules` | Path to write type declarations. `false` to skip |
245
251
 
246
252
  ---
@@ -297,6 +303,7 @@ await generate({
297
303
  exclude: ['**/vendor/**'],
298
304
  prefix: 'theme',
299
305
  naming: 'snake', // 'camelCase' | 'snake' | 'kebab'
306
+ selectors: ['.dark', '[data-theme="dark"]'],
300
307
  });
301
308
  ```
302
309
 
@@ -314,7 +321,7 @@ import { parseVarNames, generateCode, scanVarNames } from 'css-typed-vars';
314
321
  | SCSS | `.scss` |
315
322
  | Less | `.less` |
316
323
 
317
- Variables must be declared inside a `:root {}` block. Attribute selectors are supported (e.g. `:root[data-theme="dark"]`).
324
+ By default, variables are scanned from `:root {}` blocks (including attribute selectors like `:root[data-theme="dark"]`). Use the `selectors` option to also pick up variables from other selectors such as `.dark` or `[data-theme="dark"]`.
318
325
 
319
326
  ## Contributing
320
327
 
@@ -6,13 +6,17 @@ var getDirname = () => path.dirname(getFilename());
6
6
  var __dirname = /* @__PURE__ */ getDirname();
7
7
 
8
8
  // src/parser.ts
9
- function parseVarNames(css) {
9
+ function parseVarNames(css, selectors) {
10
10
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
11
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]);
12
+ const allSelectors = [":root", ...selectors ?? []];
13
+ for (const sel of allSelectors) {
14
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
15
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
16
+ for (const match of stripped.matchAll(regex)) {
17
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
18
+ names.add(prop[0]);
19
+ }
16
20
  }
17
21
  }
18
22
  return [...names];
@@ -21,7 +25,7 @@ function parseVarNames(css) {
21
25
  // src/scanner.ts
22
26
  import { readFile } from "fs/promises";
23
27
  import fg from "fast-glob";
24
- async function scanVarNames(patterns, exclude) {
28
+ async function scanVarNames(patterns, exclude, selectors) {
25
29
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
26
30
  (p) => p.replace(/\\/g, "/")
27
31
  );
@@ -31,7 +35,7 @@ async function scanVarNames(patterns, exclude) {
31
35
  await Promise.all(
32
36
  files.map(async (file) => {
33
37
  const css = await readFile(file, "utf8");
34
- for (const name of parseVarNames(css)) {
38
+ for (const name of parseVarNames(css, selectors)) {
35
39
  all.add(name);
36
40
  }
37
41
  })
@@ -3,7 +3,7 @@ import {
3
3
  generateDeclaration,
4
4
  generateJs,
5
5
  scanVarNames
6
- } from "./chunk-4VL6W5GO.js";
6
+ } from "./chunk-B6LVS5G7.js";
7
7
 
8
8
  // src/unplugin.ts
9
9
  import { createUnplugin } from "unplugin";
@@ -22,7 +22,7 @@ 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, options.exclude);
25
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
26
26
  const dtsPath = getDtsPath(options);
27
27
  if (dtsPath) {
28
28
  await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -38,7 +38,7 @@ 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, options.exclude);
41
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
42
42
  await writeFile(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
43
43
  },
44
44
  resolveId(id) {
@@ -46,7 +46,7 @@ var unplugin_default = createUnplugin((options) => ({
46
46
  },
47
47
  async load(id) {
48
48
  if (id === RESOLVED_ID) {
49
- const names = await scanVarNames(options.input, options.exclude);
49
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
50
50
  return generateJs(names, options.prefix, options.naming);
51
51
  }
52
52
  }
@@ -3,13 +3,13 @@ import {
3
3
  generateDeclaration,
4
4
  generateJs,
5
5
  scanVarNames
6
- } from "./chunk-4VL6W5GO.js";
6
+ } from "./chunk-B6LVS5G7.js";
7
7
 
8
8
  // src/index.ts
9
9
  import { writeFile, mkdir } from "fs/promises";
10
10
  import { dirname, resolve } from "path";
11
11
  async function generate(options) {
12
- const names = await scanVarNames(options.input, options.exclude);
12
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
13
13
  if (names.length === 0) {
14
14
  console.warn("css-typed-vars: no CSS custom properties found.");
15
15
  }
package/dist/cli.cjs CHANGED
@@ -42,20 +42,24 @@ var import_promises = require("fs/promises");
42
42
  var import_fast_glob = __toESM(require("fast-glob"), 1);
43
43
 
44
44
  // src/parser.ts
45
- function parseVarNames(css) {
45
+ function parseVarNames(css, selectors) {
46
46
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
47
47
  const names = /* @__PURE__ */ new Set();
48
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
49
- for (const match of stripped.matchAll(rootRegex)) {
50
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
51
- names.add(prop[0]);
48
+ const allSelectors = [":root", ...selectors ?? []];
49
+ for (const sel of allSelectors) {
50
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
51
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
52
+ for (const match of stripped.matchAll(regex)) {
53
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
54
+ names.add(prop[0]);
55
+ }
52
56
  }
53
57
  }
54
58
  return [...names];
55
59
  }
56
60
 
57
61
  // src/scanner.ts
58
- async function scanVarNames(patterns, exclude) {
62
+ async function scanVarNames(patterns, exclude, selectors) {
59
63
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
60
64
  (p) => p.replace(/\\/g, "/")
61
65
  );
@@ -65,7 +69,7 @@ async function scanVarNames(patterns, exclude) {
65
69
  await Promise.all(
66
70
  files.map(async (file) => {
67
71
  const css = await (0, import_promises.readFile)(file, "utf8");
68
- for (const name of parseVarNames(css)) {
72
+ for (const name of parseVarNames(css, selectors)) {
69
73
  all.add(name);
70
74
  }
71
75
  })
@@ -134,7 +138,7 @@ function generateDeclaration(varNames, prefix, naming) {
134
138
 
135
139
  // src/index.ts
136
140
  async function generate(options) {
137
- const names = await scanVarNames(options.input, options.exclude);
141
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
138
142
  if (names.length === 0) {
139
143
  console.warn("css-typed-vars: no CSS custom properties found.");
140
144
  }
@@ -177,8 +181,8 @@ async function loadConfig() {
177
181
  }
178
182
  return {};
179
183
  }
180
- async function run(input, output, exclude, prefix, naming) {
181
- await generate({ input, output, exclude, prefix, naming });
184
+ async function run(input, output, exclude, prefix, naming, selectors) {
185
+ await generate({ input, output, exclude, prefix, naming, selectors });
182
186
  console.log(`Generated \u2192 ${output}`);
183
187
  }
184
188
  async function main() {
@@ -193,17 +197,19 @@ async function main() {
193
197
  const exclude = getArg("--exclude") ?? config.exclude;
194
198
  const prefix = getArg("--prefix") ?? config.prefix;
195
199
  const naming = getArg("--naming") ?? config.naming;
200
+ const selectorArg = getArg("--selector");
201
+ const selectors = selectorArg ? [selectorArg] : config.selectors;
196
202
  if (!input || !output) {
197
203
  console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
198
204
  console.error("Or add a css-typed-vars.config.js file with input and output fields.");
199
205
  process.exit(1);
200
206
  }
201
- await run(input, output, exclude, prefix, naming);
207
+ await run(input, output, exclude, prefix, naming, selectors);
202
208
  if (watchMode) {
203
209
  const patterns = Array.isArray(input) ? input : [input];
204
210
  const makeHandler = (label) => (file) => {
205
211
  console.log(`${label}: ${file}`);
206
- run(input, output, exclude, prefix, naming).catch(console.error);
212
+ run(input, output, exclude, prefix, naming, selectors).catch(console.error);
207
213
  };
208
214
  (0, import_chokidar.watch)(patterns).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
209
215
  console.log("Watching for changes...");
package/dist/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  generate
4
- } from "./chunk-S2QNFCPF.js";
5
- import "./chunk-4VL6W5GO.js";
4
+ } from "./chunk-Z7K77FZI.js";
5
+ import "./chunk-B6LVS5G7.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import { readFile } from "fs/promises";
@@ -36,8 +36,8 @@ async function loadConfig() {
36
36
  }
37
37
  return {};
38
38
  }
39
- async function run(input, output, exclude, prefix, naming) {
40
- await generate({ input, output, exclude, prefix, naming });
39
+ async function run(input, output, exclude, prefix, naming, selectors) {
40
+ await generate({ input, output, exclude, prefix, naming, selectors });
41
41
  console.log(`Generated \u2192 ${output}`);
42
42
  }
43
43
  async function main() {
@@ -52,17 +52,19 @@ async function main() {
52
52
  const exclude = getArg("--exclude") ?? config.exclude;
53
53
  const prefix = getArg("--prefix") ?? config.prefix;
54
54
  const naming = getArg("--naming") ?? config.naming;
55
+ const selectorArg = getArg("--selector");
56
+ const selectors = selectorArg ? [selectorArg] : config.selectors;
55
57
  if (!input || !output) {
56
58
  console.error("Usage: css-typed-vars --input <glob> --output <file> [--watch]");
57
59
  console.error("Or add a css-typed-vars.config.js file with input and output fields.");
58
60
  process.exit(1);
59
61
  }
60
- await run(input, output, exclude, prefix, naming);
62
+ await run(input, output, exclude, prefix, naming, selectors);
61
63
  if (watchMode) {
62
64
  const patterns = Array.isArray(input) ? input : [input];
63
65
  const makeHandler = (label) => (file) => {
64
66
  console.log(`${label}: ${file}`);
65
- run(input, output, exclude, prefix, naming).catch(console.error);
67
+ run(input, output, exclude, prefix, naming, selectors).catch(console.error);
66
68
  };
67
69
  watch(patterns).on("change", makeHandler("Changed")).on("add", makeHandler("Added")).on("unlink", makeHandler("Removed"));
68
70
  console.log("Watching for changes...");
package/dist/esbuild.cjs CHANGED
@@ -44,20 +44,24 @@ var import_promises = require("fs/promises");
44
44
  var import_fast_glob = __toESM(require("fast-glob"), 1);
45
45
 
46
46
  // src/parser.ts
47
- function parseVarNames(css) {
47
+ function parseVarNames(css, selectors) {
48
48
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
49
49
  const names = /* @__PURE__ */ new Set();
50
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
51
- for (const match of stripped.matchAll(rootRegex)) {
52
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
53
- names.add(prop[0]);
50
+ const allSelectors = [":root", ...selectors ?? []];
51
+ for (const sel of allSelectors) {
52
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
53
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
54
+ for (const match of stripped.matchAll(regex)) {
55
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
56
+ names.add(prop[0]);
57
+ }
54
58
  }
55
59
  }
56
60
  return [...names];
57
61
  }
58
62
 
59
63
  // src/scanner.ts
60
- async function scanVarNames(patterns, exclude) {
64
+ async function scanVarNames(patterns, exclude, selectors) {
61
65
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
62
66
  (p) => p.replace(/\\/g, "/")
63
67
  );
@@ -67,7 +71,7 @@ async function scanVarNames(patterns, exclude) {
67
71
  await Promise.all(
68
72
  files.map(async (file) => {
69
73
  const css = await (0, import_promises.readFile)(file, "utf8");
70
- for (const name of parseVarNames(css)) {
74
+ for (const name of parseVarNames(css, selectors)) {
71
75
  all.add(name);
72
76
  }
73
77
  })
@@ -133,7 +137,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
133
137
  configureServer(server) {
134
138
  server.watcher.on("change", async (file) => {
135
139
  if (!/\.(css|scss|less)$/i.test(file)) return;
136
- const names = await scanVarNames(options.input, options.exclude);
140
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
137
141
  const dtsPath = getDtsPath(options);
138
142
  if (dtsPath) {
139
143
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -149,7 +153,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
149
153
  async buildStart() {
150
154
  const dtsPath = getDtsPath(options);
151
155
  if (!dtsPath) return;
152
- const names = await scanVarNames(options.input, options.exclude);
156
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
153
157
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
154
158
  },
155
159
  resolveId(id) {
@@ -157,7 +161,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
157
161
  },
158
162
  async load(id) {
159
163
  if (id === RESOLVED_ID) {
160
- const names = await scanVarNames(options.input, options.exclude);
164
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
161
165
  return generateJs(names, options.prefix, options.naming);
162
166
  }
163
167
  }
package/dist/esbuild.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-B57NHMQD.js";
4
- import "./chunk-4VL6W5GO.js";
3
+ } from "./chunk-PKKMLIWX.js";
4
+ import "./chunk-B6LVS5G7.js";
5
5
 
6
6
  // src/esbuild.ts
7
7
  var esbuild_default = unplugin_default.esbuild;
package/dist/index.cjs CHANGED
@@ -46,20 +46,24 @@ var import_promises = require("fs/promises");
46
46
  var import_fast_glob = __toESM(require("fast-glob"), 1);
47
47
 
48
48
  // src/parser.ts
49
- function parseVarNames(css) {
49
+ function parseVarNames(css, selectors) {
50
50
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
51
51
  const names = /* @__PURE__ */ new Set();
52
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
53
- for (const match of stripped.matchAll(rootRegex)) {
54
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
55
- names.add(prop[0]);
52
+ const allSelectors = [":root", ...selectors ?? []];
53
+ for (const sel of allSelectors) {
54
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
55
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
56
+ for (const match of stripped.matchAll(regex)) {
57
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
58
+ names.add(prop[0]);
59
+ }
56
60
  }
57
61
  }
58
62
  return [...names];
59
63
  }
60
64
 
61
65
  // src/scanner.ts
62
- async function scanVarNames(patterns, exclude) {
66
+ async function scanVarNames(patterns, exclude, selectors) {
63
67
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
64
68
  (p) => p.replace(/\\/g, "/")
65
69
  );
@@ -69,7 +73,7 @@ async function scanVarNames(patterns, exclude) {
69
73
  await Promise.all(
70
74
  files.map(async (file) => {
71
75
  const css = await (0, import_promises.readFile)(file, "utf8");
72
- for (const name of parseVarNames(css)) {
76
+ for (const name of parseVarNames(css, selectors)) {
73
77
  all.add(name);
74
78
  }
75
79
  })
@@ -138,7 +142,7 @@ function generateDeclaration(varNames, prefix, naming) {
138
142
 
139
143
  // src/index.ts
140
144
  async function generate(options) {
141
- const names = await scanVarNames(options.input, options.exclude);
145
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
142
146
  if (names.length === 0) {
143
147
  console.warn("css-typed-vars: no CSS custom properties found.");
144
148
  }
package/dist/index.d.cts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { N as NamingConvention } from './generator-Ba8XeISD.cjs';
2
2
  export { g as generateCode, a as generateDeclaration, b as generateJs } from './generator-Ba8XeISD.cjs';
3
3
 
4
- declare function parseVarNames(css: string): string[];
4
+ declare function parseVarNames(css: string, selectors?: string[]): string[];
5
5
 
6
- declare function scanVarNames(patterns: string | string[], exclude?: string | string[]): Promise<string[]>;
6
+ declare function scanVarNames(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<string[]>;
7
7
 
8
8
  interface GenerateOptions {
9
9
  input: string | string[];
@@ -11,6 +11,7 @@ interface GenerateOptions {
11
11
  exclude?: string | string[];
12
12
  prefix?: string;
13
13
  naming?: NamingConvention;
14
+ selectors?: string[];
14
15
  }
15
16
  declare function generate(options: GenerateOptions): Promise<void>;
16
17
 
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { N as NamingConvention } from './generator-Ba8XeISD.js';
2
2
  export { g as generateCode, a as generateDeclaration, b as generateJs } from './generator-Ba8XeISD.js';
3
3
 
4
- declare function parseVarNames(css: string): string[];
4
+ declare function parseVarNames(css: string, selectors?: string[]): string[];
5
5
 
6
- declare function scanVarNames(patterns: string | string[], exclude?: string | string[]): Promise<string[]>;
6
+ declare function scanVarNames(patterns: string | string[], exclude?: string | string[], selectors?: string[]): Promise<string[]>;
7
7
 
8
8
  interface GenerateOptions {
9
9
  input: string | string[];
@@ -11,6 +11,7 @@ interface GenerateOptions {
11
11
  exclude?: string | string[];
12
12
  prefix?: string;
13
13
  naming?: NamingConvention;
14
+ selectors?: string[];
14
15
  }
15
16
  declare function generate(options: GenerateOptions): Promise<void>;
16
17
 
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  generate
3
- } from "./chunk-S2QNFCPF.js";
3
+ } from "./chunk-Z7K77FZI.js";
4
4
  import {
5
5
  generateCode,
6
6
  generateDeclaration,
7
7
  generateJs,
8
8
  parseVarNames,
9
9
  scanVarNames
10
- } from "./chunk-4VL6W5GO.js";
10
+ } from "./chunk-B6LVS5G7.js";
11
11
  export {
12
12
  generate,
13
13
  generateCode,
package/dist/rollup.cjs CHANGED
@@ -44,20 +44,24 @@ var import_promises = require("fs/promises");
44
44
  var import_fast_glob = __toESM(require("fast-glob"), 1);
45
45
 
46
46
  // src/parser.ts
47
- function parseVarNames(css) {
47
+ function parseVarNames(css, selectors) {
48
48
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
49
49
  const names = /* @__PURE__ */ new Set();
50
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
51
- for (const match of stripped.matchAll(rootRegex)) {
52
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
53
- names.add(prop[0]);
50
+ const allSelectors = [":root", ...selectors ?? []];
51
+ for (const sel of allSelectors) {
52
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
53
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
54
+ for (const match of stripped.matchAll(regex)) {
55
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
56
+ names.add(prop[0]);
57
+ }
54
58
  }
55
59
  }
56
60
  return [...names];
57
61
  }
58
62
 
59
63
  // src/scanner.ts
60
- async function scanVarNames(patterns, exclude) {
64
+ async function scanVarNames(patterns, exclude, selectors) {
61
65
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
62
66
  (p) => p.replace(/\\/g, "/")
63
67
  );
@@ -67,7 +71,7 @@ async function scanVarNames(patterns, exclude) {
67
71
  await Promise.all(
68
72
  files.map(async (file) => {
69
73
  const css = await (0, import_promises.readFile)(file, "utf8");
70
- for (const name of parseVarNames(css)) {
74
+ for (const name of parseVarNames(css, selectors)) {
71
75
  all.add(name);
72
76
  }
73
77
  })
@@ -133,7 +137,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
133
137
  configureServer(server) {
134
138
  server.watcher.on("change", async (file) => {
135
139
  if (!/\.(css|scss|less)$/i.test(file)) return;
136
- const names = await scanVarNames(options.input, options.exclude);
140
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
137
141
  const dtsPath = getDtsPath(options);
138
142
  if (dtsPath) {
139
143
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -149,7 +153,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
149
153
  async buildStart() {
150
154
  const dtsPath = getDtsPath(options);
151
155
  if (!dtsPath) return;
152
- const names = await scanVarNames(options.input, options.exclude);
156
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
153
157
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
154
158
  },
155
159
  resolveId(id) {
@@ -157,7 +161,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
157
161
  },
158
162
  async load(id) {
159
163
  if (id === RESOLVED_ID) {
160
- const names = await scanVarNames(options.input, options.exclude);
164
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
161
165
  return generateJs(names, options.prefix, options.naming);
162
166
  }
163
167
  }
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-B57NHMQD.js";
4
- import "./chunk-4VL6W5GO.js";
3
+ } from "./chunk-PKKMLIWX.js";
4
+ import "./chunk-B6LVS5G7.js";
5
5
 
6
6
  // src/rollup.ts
7
7
  var rollup_default = unplugin_default.rollup;
package/dist/unplugin.cjs CHANGED
@@ -42,20 +42,24 @@ var import_promises = require("fs/promises");
42
42
  var import_fast_glob = __toESM(require("fast-glob"), 1);
43
43
 
44
44
  // src/parser.ts
45
- function parseVarNames(css) {
45
+ function parseVarNames(css, selectors) {
46
46
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
47
47
  const names = /* @__PURE__ */ new Set();
48
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
49
- for (const match of stripped.matchAll(rootRegex)) {
50
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
51
- names.add(prop[0]);
48
+ const allSelectors = [":root", ...selectors ?? []];
49
+ for (const sel of allSelectors) {
50
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
51
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
52
+ for (const match of stripped.matchAll(regex)) {
53
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
54
+ names.add(prop[0]);
55
+ }
52
56
  }
53
57
  }
54
58
  return [...names];
55
59
  }
56
60
 
57
61
  // src/scanner.ts
58
- async function scanVarNames(patterns, exclude) {
62
+ async function scanVarNames(patterns, exclude, selectors) {
59
63
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
60
64
  (p) => p.replace(/\\/g, "/")
61
65
  );
@@ -65,7 +69,7 @@ async function scanVarNames(patterns, exclude) {
65
69
  await Promise.all(
66
70
  files.map(async (file) => {
67
71
  const css = await (0, import_promises.readFile)(file, "utf8");
68
- for (const name of parseVarNames(css)) {
72
+ for (const name of parseVarNames(css, selectors)) {
69
73
  all.add(name);
70
74
  }
71
75
  })
@@ -131,7 +135,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
131
135
  configureServer(server) {
132
136
  server.watcher.on("change", async (file) => {
133
137
  if (!/\.(css|scss|less)$/i.test(file)) return;
134
- const names = await scanVarNames(options.input, options.exclude);
138
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
135
139
  const dtsPath = getDtsPath(options);
136
140
  if (dtsPath) {
137
141
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -147,7 +151,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
147
151
  async buildStart() {
148
152
  const dtsPath = getDtsPath(options);
149
153
  if (!dtsPath) return;
150
- const names = await scanVarNames(options.input, options.exclude);
154
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
151
155
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
152
156
  },
153
157
  resolveId(id) {
@@ -155,7 +159,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
155
159
  },
156
160
  async load(id) {
157
161
  if (id === RESOLVED_ID) {
158
- const names = await scanVarNames(options.input, options.exclude);
162
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
159
163
  return generateJs(names, options.prefix, options.naming);
160
164
  }
161
165
  }
@@ -13,6 +13,7 @@ interface Options {
13
13
  exclude?: string | string[];
14
14
  prefix?: string;
15
15
  naming?: NamingConvention;
16
+ selectors?: string[];
16
17
  }
17
18
  declare const _default: unplugin.UnpluginInstance<Options, boolean>;
18
19
 
@@ -13,6 +13,7 @@ interface Options {
13
13
  exclude?: string | string[];
14
14
  prefix?: string;
15
15
  naming?: NamingConvention;
16
+ selectors?: string[];
16
17
  }
17
18
  declare const _default: unplugin.UnpluginInstance<Options, boolean>;
18
19
 
package/dist/unplugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-B57NHMQD.js";
4
- import "./chunk-4VL6W5GO.js";
3
+ } from "./chunk-PKKMLIWX.js";
4
+ import "./chunk-B6LVS5G7.js";
5
5
  export {
6
6
  unplugin_default as default
7
7
  };
package/dist/vite.cjs CHANGED
@@ -44,20 +44,24 @@ var import_promises = require("fs/promises");
44
44
  var import_fast_glob = __toESM(require("fast-glob"), 1);
45
45
 
46
46
  // src/parser.ts
47
- function parseVarNames(css) {
47
+ function parseVarNames(css, selectors) {
48
48
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
49
49
  const names = /* @__PURE__ */ new Set();
50
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
51
- for (const match of stripped.matchAll(rootRegex)) {
52
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
53
- names.add(prop[0]);
50
+ const allSelectors = [":root", ...selectors ?? []];
51
+ for (const sel of allSelectors) {
52
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
53
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
54
+ for (const match of stripped.matchAll(regex)) {
55
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
56
+ names.add(prop[0]);
57
+ }
54
58
  }
55
59
  }
56
60
  return [...names];
57
61
  }
58
62
 
59
63
  // src/scanner.ts
60
- async function scanVarNames(patterns, exclude) {
64
+ async function scanVarNames(patterns, exclude, selectors) {
61
65
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
62
66
  (p) => p.replace(/\\/g, "/")
63
67
  );
@@ -67,7 +71,7 @@ async function scanVarNames(patterns, exclude) {
67
71
  await Promise.all(
68
72
  files.map(async (file) => {
69
73
  const css = await (0, import_promises.readFile)(file, "utf8");
70
- for (const name of parseVarNames(css)) {
74
+ for (const name of parseVarNames(css, selectors)) {
71
75
  all.add(name);
72
76
  }
73
77
  })
@@ -133,7 +137,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
133
137
  configureServer(server) {
134
138
  server.watcher.on("change", async (file) => {
135
139
  if (!/\.(css|scss|less)$/i.test(file)) return;
136
- const names = await scanVarNames(options.input, options.exclude);
140
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
137
141
  const dtsPath = getDtsPath(options);
138
142
  if (dtsPath) {
139
143
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -149,7 +153,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
149
153
  async buildStart() {
150
154
  const dtsPath = getDtsPath(options);
151
155
  if (!dtsPath) return;
152
- const names = await scanVarNames(options.input, options.exclude);
156
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
153
157
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
154
158
  },
155
159
  resolveId(id) {
@@ -157,7 +161,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
157
161
  },
158
162
  async load(id) {
159
163
  if (id === RESOLVED_ID) {
160
- const names = await scanVarNames(options.input, options.exclude);
164
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
161
165
  return generateJs(names, options.prefix, options.naming);
162
166
  }
163
167
  }
package/dist/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-B57NHMQD.js";
4
- import "./chunk-4VL6W5GO.js";
3
+ } from "./chunk-PKKMLIWX.js";
4
+ import "./chunk-B6LVS5G7.js";
5
5
 
6
6
  // src/vite.ts
7
7
  var vite_default = unplugin_default.vite;
package/dist/webpack.cjs CHANGED
@@ -44,20 +44,24 @@ var import_promises = require("fs/promises");
44
44
  var import_fast_glob = __toESM(require("fast-glob"), 1);
45
45
 
46
46
  // src/parser.ts
47
- function parseVarNames(css) {
47
+ function parseVarNames(css, selectors) {
48
48
  const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
49
49
  const names = /* @__PURE__ */ new Set();
50
- const rootRegex = /:root[^{]*\{([^}]*)}/g;
51
- for (const match of stripped.matchAll(rootRegex)) {
52
- for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
53
- names.add(prop[0]);
50
+ const allSelectors = [":root", ...selectors ?? []];
51
+ for (const sel of allSelectors) {
52
+ const escaped = sel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
53
+ const regex = new RegExp(`${escaped}[^{]*\\{([^}]*)\\}`, "g");
54
+ for (const match of stripped.matchAll(regex)) {
55
+ for (const prop of match[1].matchAll(/--[\w-]+(?=\s*:)/g)) {
56
+ names.add(prop[0]);
57
+ }
54
58
  }
55
59
  }
56
60
  return [...names];
57
61
  }
58
62
 
59
63
  // src/scanner.ts
60
- async function scanVarNames(patterns, exclude) {
64
+ async function scanVarNames(patterns, exclude, selectors) {
61
65
  const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
62
66
  (p) => p.replace(/\\/g, "/")
63
67
  );
@@ -67,7 +71,7 @@ async function scanVarNames(patterns, exclude) {
67
71
  await Promise.all(
68
72
  files.map(async (file) => {
69
73
  const css = await (0, import_promises.readFile)(file, "utf8");
70
- for (const name of parseVarNames(css)) {
74
+ for (const name of parseVarNames(css, selectors)) {
71
75
  all.add(name);
72
76
  }
73
77
  })
@@ -133,7 +137,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
133
137
  configureServer(server) {
134
138
  server.watcher.on("change", async (file) => {
135
139
  if (!/\.(css|scss|less)$/i.test(file)) return;
136
- const names = await scanVarNames(options.input, options.exclude);
140
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
137
141
  const dtsPath = getDtsPath(options);
138
142
  if (dtsPath) {
139
143
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
@@ -149,7 +153,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
149
153
  async buildStart() {
150
154
  const dtsPath = getDtsPath(options);
151
155
  if (!dtsPath) return;
152
- const names = await scanVarNames(options.input, options.exclude);
156
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
153
157
  await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names, options.prefix, options.naming), "utf8");
154
158
  },
155
159
  resolveId(id) {
@@ -157,7 +161,7 @@ var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
157
161
  },
158
162
  async load(id) {
159
163
  if (id === RESOLVED_ID) {
160
- const names = await scanVarNames(options.input, options.exclude);
164
+ const names = await scanVarNames(options.input, options.exclude, options.selectors);
161
165
  return generateJs(names, options.prefix, options.naming);
162
166
  }
163
167
  }
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-B57NHMQD.js";
4
- import "./chunk-4VL6W5GO.js";
3
+ } from "./chunk-PKKMLIWX.js";
4
+ import "./chunk-B6LVS5G7.js";
5
5
 
6
6
  // src/webpack.ts
7
7
  var webpack_default = unplugin_default.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-typed-vars",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",