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/esbuild.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/esbuild.ts
|
|
31
|
+
var esbuild_exports = {};
|
|
32
|
+
__export(esbuild_exports, {
|
|
33
|
+
default: () => esbuild_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(esbuild_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/esbuild.ts
|
|
146
|
+
var esbuild_default = unplugin_default.esbuild;
|
package/dist/esbuild.js
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
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/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
generate: () => generate,
|
|
34
|
+
generateCode: () => generateCode,
|
|
35
|
+
parseVarNames: () => parseVarNames,
|
|
36
|
+
scanVarNames: () => scanVarNames
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(src_exports);
|
|
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 generateCode(varNames) {
|
|
82
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
83
|
+
return [
|
|
84
|
+
"// generated \u2014 do not edit",
|
|
85
|
+
"export const cssVars = {",
|
|
86
|
+
...entries,
|
|
87
|
+
"} as const;",
|
|
88
|
+
"",
|
|
89
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
90
|
+
""
|
|
91
|
+
].join("\n");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/index.ts
|
|
95
|
+
async function generate(options) {
|
|
96
|
+
const names = await scanVarNames(options.input);
|
|
97
|
+
const code = generateCode(names);
|
|
98
|
+
await (0, import_promises2.mkdir)((0, import_node_path.dirname)((0, import_node_path.resolve)(options.output)), { recursive: true });
|
|
99
|
+
await (0, import_promises2.writeFile)(options.output, code, "utf8");
|
|
100
|
+
}
|
|
101
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
+
0 && (module.exports = {
|
|
103
|
+
generate,
|
|
104
|
+
generateCode,
|
|
105
|
+
parseVarNames,
|
|
106
|
+
scanVarNames
|
|
107
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare function parseVarNames(css: string): string[];
|
|
2
|
+
|
|
3
|
+
declare function generateCode(varNames: string[]): string;
|
|
4
|
+
|
|
5
|
+
declare function scanVarNames(patterns: string | string[]): Promise<string[]>;
|
|
6
|
+
|
|
7
|
+
interface GenerateOptions {
|
|
8
|
+
input: string | string[];
|
|
9
|
+
output: string;
|
|
10
|
+
}
|
|
11
|
+
declare function generate(options: GenerateOptions): Promise<void>;
|
|
12
|
+
|
|
13
|
+
export { type GenerateOptions, generate, generateCode, parseVarNames, scanVarNames };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare function parseVarNames(css: string): string[];
|
|
2
|
+
|
|
3
|
+
declare function generateCode(varNames: string[]): string;
|
|
4
|
+
|
|
5
|
+
declare function scanVarNames(patterns: string | string[]): Promise<string[]>;
|
|
6
|
+
|
|
7
|
+
interface GenerateOptions {
|
|
8
|
+
input: string | string[];
|
|
9
|
+
output: string;
|
|
10
|
+
}
|
|
11
|
+
declare function generate(options: GenerateOptions): Promise<void>;
|
|
12
|
+
|
|
13
|
+
export { type GenerateOptions, generate, generateCode, parseVarNames, scanVarNames };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateCode,
|
|
3
|
+
parseVarNames,
|
|
4
|
+
scanVarNames
|
|
5
|
+
} from "./chunk-SUEAXDIO.js";
|
|
6
|
+
|
|
7
|
+
// src/index.ts
|
|
8
|
+
import { writeFile, mkdir } from "fs/promises";
|
|
9
|
+
import { dirname, resolve } from "path";
|
|
10
|
+
async function generate(options) {
|
|
11
|
+
const names = await scanVarNames(options.input);
|
|
12
|
+
const code = generateCode(names);
|
|
13
|
+
await mkdir(dirname(resolve(options.output)), { recursive: true });
|
|
14
|
+
await writeFile(options.output, code, "utf8");
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
generate,
|
|
18
|
+
generateCode,
|
|
19
|
+
parseVarNames,
|
|
20
|
+
scanVarNames
|
|
21
|
+
};
|
package/dist/rollup.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/rollup.ts
|
|
31
|
+
var rollup_exports = {};
|
|
32
|
+
__export(rollup_exports, {
|
|
33
|
+
default: () => rollup_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(rollup_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/rollup.ts
|
|
146
|
+
var rollup_default = unplugin_default.rollup;
|
package/dist/rollup.d.ts
ADDED
package/dist/rollup.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
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/unplugin.ts
|
|
31
|
+
var unplugin_exports = {};
|
|
32
|
+
__export(unplugin_exports, {
|
|
33
|
+
default: () => unplugin_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(unplugin_exports);
|
|
36
|
+
var import_unplugin = require("unplugin");
|
|
37
|
+
var import_promises2 = require("fs/promises");
|
|
38
|
+
var import_node_path = require("path");
|
|
39
|
+
|
|
40
|
+
// src/scanner.ts
|
|
41
|
+
var import_promises = require("fs/promises");
|
|
42
|
+
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
43
|
+
|
|
44
|
+
// src/parser.ts
|
|
45
|
+
function parseVarNames(css) {
|
|
46
|
+
const stripped = css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
|
|
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]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return [...names];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/scanner.ts
|
|
58
|
+
async function scanVarNames(patterns) {
|
|
59
|
+
const normalized = (Array.isArray(patterns) ? patterns : [patterns]).map(
|
|
60
|
+
(p) => p.replace(/\\/g, "/")
|
|
61
|
+
);
|
|
62
|
+
const files = await (0, import_fast_glob.default)(normalized, { absolute: true });
|
|
63
|
+
const all = /* @__PURE__ */ new Set();
|
|
64
|
+
await Promise.all(
|
|
65
|
+
files.map(async (file) => {
|
|
66
|
+
const css = await (0, import_promises.readFile)(file, "utf8");
|
|
67
|
+
for (const name of parseVarNames(css)) {
|
|
68
|
+
all.add(name);
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
return [...all];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/generator.ts
|
|
76
|
+
function toCamelCase(cssVarName) {
|
|
77
|
+
return cssVarName.replace(/^--/, "").replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
78
|
+
}
|
|
79
|
+
function generateJs(varNames) {
|
|
80
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})',`);
|
|
81
|
+
return [
|
|
82
|
+
"export const cssVars = {",
|
|
83
|
+
...entries,
|
|
84
|
+
"};",
|
|
85
|
+
""
|
|
86
|
+
].join("\n");
|
|
87
|
+
}
|
|
88
|
+
function generateDeclaration(varNames) {
|
|
89
|
+
const entries = varNames.map((name) => ` ${toCamelCase(name)}: 'var(${name})';`);
|
|
90
|
+
return [
|
|
91
|
+
"export declare const cssVars: {",
|
|
92
|
+
...entries,
|
|
93
|
+
"};",
|
|
94
|
+
"export type CssVarName = keyof typeof cssVars;",
|
|
95
|
+
""
|
|
96
|
+
].join("\n");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/unplugin.ts
|
|
100
|
+
var VIRTUAL_ID = "css-typed-vars/vars";
|
|
101
|
+
var RESOLVED_ID = "\0css-typed-vars/vars";
|
|
102
|
+
function getDtsPath(options) {
|
|
103
|
+
if (options.dts === false) return null;
|
|
104
|
+
return options.dts ? (0, import_node_path.resolve)(process.cwd(), options.dts) : (0, import_node_path.join)(__dirname, "generated.d.ts");
|
|
105
|
+
}
|
|
106
|
+
var unplugin_default = (0, import_unplugin.createUnplugin)((options) => ({
|
|
107
|
+
name: "css-typed-vars",
|
|
108
|
+
vite: {
|
|
109
|
+
enforce: "pre",
|
|
110
|
+
configureServer(server) {
|
|
111
|
+
server.watcher.on("change", async (file) => {
|
|
112
|
+
if (!/\.(css|scss|less)$/i.test(file)) return;
|
|
113
|
+
const names = await scanVarNames(options.input);
|
|
114
|
+
const dtsPath = getDtsPath(options);
|
|
115
|
+
if (dtsPath) {
|
|
116
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
117
|
+
}
|
|
118
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
119
|
+
if (mod) {
|
|
120
|
+
server.moduleGraph.invalidateModule(mod);
|
|
121
|
+
server.ws.send({ type: "full-reload" });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
async buildStart() {
|
|
127
|
+
const dtsPath = getDtsPath(options);
|
|
128
|
+
if (!dtsPath) return;
|
|
129
|
+
const names = await scanVarNames(options.input);
|
|
130
|
+
await (0, import_promises2.writeFile)(dtsPath, generateDeclaration(names), "utf8");
|
|
131
|
+
},
|
|
132
|
+
resolveId(id) {
|
|
133
|
+
if (id === VIRTUAL_ID) return RESOLVED_ID;
|
|
134
|
+
},
|
|
135
|
+
async load(id) {
|
|
136
|
+
if (id === RESOLVED_ID) {
|
|
137
|
+
const names = await scanVarNames(options.input);
|
|
138
|
+
return generateJs(names);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
|
|
3
|
+
interface Options {
|
|
4
|
+
input: string | string[];
|
|
5
|
+
/**
|
|
6
|
+
* Where to write the TypeScript declaration file for `css-typed-vars/vars`.
|
|
7
|
+
* - `undefined` (default): write to the package's own dist directory inside node_modules
|
|
8
|
+
* - `string`: write to the specified path relative to project root
|
|
9
|
+
* - `false`: skip writing
|
|
10
|
+
*/
|
|
11
|
+
dts?: string | false;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: unplugin.UnpluginInstance<Options, boolean>;
|
|
14
|
+
|
|
15
|
+
export { type Options, _default as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
|
|
3
|
+
interface Options {
|
|
4
|
+
input: string | string[];
|
|
5
|
+
/**
|
|
6
|
+
* Where to write the TypeScript declaration file for `css-typed-vars/vars`.
|
|
7
|
+
* - `undefined` (default): write to the package's own dist directory inside node_modules
|
|
8
|
+
* - `string`: write to the specified path relative to project root
|
|
9
|
+
* - `false`: skip writing
|
|
10
|
+
*/
|
|
11
|
+
dts?: string | false;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: unplugin.UnpluginInstance<Options, boolean>;
|
|
14
|
+
|
|
15
|
+
export { type Options, _default as default };
|