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/dist/webpack.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 files = await (0, import_fast_glob.default)(normalized, { absolute: true });
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 toCamelCase(cssVarName) {
79
- return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
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 generateJs(varNames) {
82
- const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
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) => ` ${toCamelCase(name)}: 'var(${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
  }));
@@ -1,5 +1,6 @@
1
1
  import { Options } from './unplugin.cjs';
2
2
  import 'unplugin';
3
+ import './generator-Ba8XeISD.cjs';
3
4
 
4
5
  declare const _default: (options: Options) => WebpackPluginInstance;
5
6
 
package/dist/webpack.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Options } from './unplugin.js';
2
2
  import 'unplugin';
3
+ import './generator-Ba8XeISD.js';
3
4
 
4
5
  declare const _default: (options: Options) => WebpackPluginInstance;
5
6
 
package/dist/webpack.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unplugin_default
3
- } from "./chunk-3LGOJDGV.js";
4
- import "./chunk-SUEAXDIO.js";
3
+ } from "./chunk-B57NHMQD.js";
4
+ import "./chunk-4VL6W5GO.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.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Generate TypeScript typed constants from CSS custom properties",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",