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/dist/unplugin.js
ADDED
package/dist/vars.cjs
ADDED
package/dist/vars.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const cssVars = {};
|
package/dist/vite.cjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/vite.ts
|
|
31
|
+
var vite_exports = {};
|
|
32
|
+
__export(vite_exports, {
|
|
33
|
+
default: () => vite_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(vite_exports);
|
|
36
|
+
|
|
37
|
+
// src/unplugin.ts
|
|
38
|
+
var import_unplugin = require("unplugin");
|
|
39
|
+
var import_promises2 = require("fs/promises");
|
|
40
|
+
var import_node_path = require("path");
|
|
41
|
+
|
|
42
|
+
// src/scanner.ts
|
|
43
|
+
var import_promises = require("fs/promises");
|
|
44
|
+
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
|
+
|
|
46
|
+
// src/parser.ts
|
|
47
|
+
function parseVarNames(css) {
|
|
48
|
+
const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
|
|
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]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [...names];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/scanner.ts
|
|
60
|
+
async function scanVarNames(patterns) {
|
|
61
|
+
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
62
|
+
(p) => p.replace(/\\/g, "/")
|
|
63
|
+
);
|
|
64
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true });
|
|
65
|
+
const all = /* @__PURE__ */ new Set();
|
|
66
|
+
await Promise.all(
|
|
67
|
+
files.map(async (file) => {
|
|
68
|
+
const css = await (0, import_promises.readFile)(file, "utf8");
|
|
69
|
+
for (const name of parseVarNames(css)) {
|
|
70
|
+
all.add(name);
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
return [...all];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/generator.ts
|
|
78
|
+
function toCamelCase(cssVarName) {
|
|
79
|
+
return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
80
|
+
}
|
|
81
|
+
function generateJs(varNames) {
|
|
82
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
83
|
+
return [
|
|
84
|
+
"export const cssVars = {",
|
|
85
|
+
...entries,
|
|
86
|
+
"};",
|
|
87
|
+
""
|
|
88
|
+
].join("\n");
|
|
89
|
+
}
|
|
90
|
+
function generateDeclaration(varNames) {
|
|
91
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})';`);
|
|
92
|
+
return [
|
|
93
|
+
"export declare const cssVars: {",
|
|
94
|
+
...entries,
|
|
95
|
+
"};",
|
|
96
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
97
|
+
""
|
|
98
|
+
].join("\n");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/unplugin.ts
|
|
102
|
+
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
103
|
+
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
104
|
+
function getDtsPath(options) {
|
|
105
|
+
if (options.dts === false) return null;
|
|
106
|
+
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
107
|
+
}
|
|
108
|
+
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
|
|
109
|
+
name: "css-typed-vars",
|
|
110
|
+
vite: {
|
|
111
|
+
enforce: "pre",
|
|
112
|
+
configureServer(server) {
|
|
113
|
+
server.watcher.on("change", async (file) => {
|
|
114
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
115
|
+
const names = await scanVarNames(options.input);
|
|
116
|
+
const dtsPath = getDtsPath(options);
|
|
117
|
+
if (dtsPath) {
|
|
118
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
119
|
+
}
|
|
120
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
121
|
+
if (mod) {
|
|
122
|
+
server.moduleGraph.invalidateModule(mod);
|
|
123
|
+
server.ws.send({ type: "full-reload" });
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
async buildStart() {
|
|
129
|
+
const dtsPath = getDtsPath(options);
|
|
130
|
+
if (!dtsPath) return;
|
|
131
|
+
const names = await scanVarNames(options.input);
|
|
132
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
133
|
+
},
|
|
134
|
+
resolveId(id) {
|
|
135
|
+
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
136
|
+
},
|
|
137
|
+
async load(id) {
|
|
138
|
+
if (id === RESOLVED_ID) {
|
|
139
|
+
const names = await scanVarNames(options.input);
|
|
140
|
+
return generateJs(names);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}));
|
|
144
|
+
|
|
145
|
+
// src/vite.ts
|
|
146
|
+
var vite_default = unplugin_default.vite;
|
package/dist/vite.d.cts
ADDED
package/dist/vite.d.ts
ADDED
package/dist/vite.js
ADDED
package/dist/webpack.cjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/webpack.ts
|
|
31
|
+
var webpack_exports = {};
|
|
32
|
+
__export(webpack_exports, {
|
|
33
|
+
default: () => webpack_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(webpack_exports);
|
|
36
|
+
|
|
37
|
+
// src/unplugin.ts
|
|
38
|
+
var import_unplugin = require("unplugin");
|
|
39
|
+
var import_promises2 = require("fs/promises");
|
|
40
|
+
var import_node_path = require("path");
|
|
41
|
+
|
|
42
|
+
// src/scanner.ts
|
|
43
|
+
var import_promises = require("fs/promises");
|
|
44
|
+
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
45
|
+
|
|
46
|
+
// src/parser.ts
|
|
47
|
+
function parseVarNames(css) {
|
|
48
|
+
const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
|
|
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]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [...names];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/scanner.ts
|
|
60
|
+
async function scanVarNames(patterns) {
|
|
61
|
+
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
62
|
+
(p) => p.replace(/\\/g, "/")
|
|
63
|
+
);
|
|
64
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true });
|
|
65
|
+
const all = /* @__PURE__ */ new Set();
|
|
66
|
+
await Promise.all(
|
|
67
|
+
files.map(async (file) => {
|
|
68
|
+
const css = await (0, import_promises.readFile)(file, "utf8");
|
|
69
|
+
for (const name of parseVarNames(css)) {
|
|
70
|
+
all.add(name);
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
return [...all];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/generator.ts
|
|
78
|
+
function toCamelCase(cssVarName) {
|
|
79
|
+
return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
80
|
+
}
|
|
81
|
+
function generateJs(varNames) {
|
|
82
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
83
|
+
return [
|
|
84
|
+
"export const cssVars = {",
|
|
85
|
+
...entries,
|
|
86
|
+
"};",
|
|
87
|
+
""
|
|
88
|
+
].join("\n");
|
|
89
|
+
}
|
|
90
|
+
function generateDeclaration(varNames) {
|
|
91
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})';`);
|
|
92
|
+
return [
|
|
93
|
+
"export declare const cssVars: {",
|
|
94
|
+
...entries,
|
|
95
|
+
"};",
|
|
96
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
97
|
+
""
|
|
98
|
+
].join("\n");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/unplugin.ts
|
|
102
|
+
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
103
|
+
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
104
|
+
function getDtsPath(options) {
|
|
105
|
+
if (options.dts === false) return null;
|
|
106
|
+
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
107
|
+
}
|
|
108
|
+
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
|
|
109
|
+
name: "css-typed-vars",
|
|
110
|
+
vite: {
|
|
111
|
+
enforce: "pre",
|
|
112
|
+
configureServer(server) {
|
|
113
|
+
server.watcher.on("change", async (file) => {
|
|
114
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
115
|
+
const names = await scanVarNames(options.input);
|
|
116
|
+
const dtsPath = getDtsPath(options);
|
|
117
|
+
if (dtsPath) {
|
|
118
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
119
|
+
}
|
|
120
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
121
|
+
if (mod) {
|
|
122
|
+
server.moduleGraph.invalidateModule(mod);
|
|
123
|
+
server.ws.send({ type: "full-reload" });
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
async buildStart() {
|
|
129
|
+
const dtsPath = getDtsPath(options);
|
|
130
|
+
if (!dtsPath) return;
|
|
131
|
+
const names = await scanVarNames(options.input);
|
|
132
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
133
|
+
},
|
|
134
|
+
resolveId(id) {
|
|
135
|
+
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
136
|
+
},
|
|
137
|
+
async load(id) {
|
|
138
|
+
if (id === RESOLVED_ID) {
|
|
139
|
+
const names = await scanVarNames(options.input);
|
|
140
|
+
return generateJs(names);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}));
|
|
144
|
+
|
|
145
|
+
// src/webpack.ts
|
|
146
|
+
var webpack_default = unplugin_default.webpack;
|
package/dist/webpack.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "css-typed-vars",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generate TypeScript typed constants from CSS custom properties",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./vite": {
|
|
16
|
+
"types": "./dist/vite.d.ts",
|
|
17
|
+
"import": "./dist/vite.js",
|
|
18
|
+
"require": "./dist/vite.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./rollup": {
|
|
21
|
+
"types": "./dist/rollup.d.ts",
|
|
22
|
+
"import": "./dist/rollup.js",
|
|
23
|
+
"require": "./dist/rollup.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./webpack": {
|
|
26
|
+
"types": "./dist/webpack.d.ts",
|
|
27
|
+
"import": "./dist/webpack.js",
|
|
28
|
+
"require": "./dist/webpack.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./esbuild": {
|
|
31
|
+
"types": "./dist/esbuild.d.ts",
|
|
32
|
+
"import": "./dist/esbuild.js",
|
|
33
|
+
"require": "./dist/esbuild.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./vars": {
|
|
36
|
+
"types": "./dist/generated.d.ts",
|
|
37
|
+
"import": "./dist/vars.js",
|
|
38
|
+
"require": "./dist/vars.cjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"bin": {
|
|
42
|
+
"css-typed-vars": "./dist/cli.js"
|
|
43
|
+
},
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"homepage": "https://github.com/StanislavKozachenko/css-typed-vars#readme",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "https://github.com/StanislavKozachenko/css-typed-vars.git"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"css",
|
|
52
|
+
"scss",
|
|
53
|
+
"css-variables",
|
|
54
|
+
"custom-properties",
|
|
55
|
+
"typescript",
|
|
56
|
+
"codegen",
|
|
57
|
+
"typed"
|
|
58
|
+
],
|
|
59
|
+
"files": [
|
|
60
|
+
"dist"
|
|
61
|
+
],
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsup && node scripts/postbuild.mjs",
|
|
64
|
+
"test": "vitest run --passWithNoTests",
|
|
65
|
+
"typecheck": "tsc --noEmit"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"chokidar": "^3.6.0",
|
|
69
|
+
"fast-glob": "^3.3.2",
|
|
70
|
+
"unplugin": "^3.0.0"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@types/node": "^25.5.2",
|
|
74
|
+
"tsup": "^8.0.0",
|
|
75
|
+
"typescript": "^5.4.0",
|
|
76
|
+
"vitest": "^3.0.0"
|
|
77
|
+
}
|
|
78
|
+
}
|