vite-plugin-css-vars-to-scss-tokens 1.0.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 +97 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -0
- package/dist/index.mjs +82 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yalla Nkardaz
|
|
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,97 @@
|
|
|
1
|
+
# vite-plugin-css-vars-to-scss-tokens
|
|
2
|
+
|
|
3
|
+
A small Vite plugin that generates SCSS token declarations from CSS custom properties.
|
|
4
|
+
|
|
5
|
+
The plugin reads a source variables file (default `src/styles/_variables.scss`) and writes a generated SCSS tokens file (default `src/styles/_tokens.scss`). This allows multiple projects to reuse CSS variables as SCSS variables via `var(--name)` declarations.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Automatically generates SCSS token declarations from CSS custom properties
|
|
10
|
+
- Avoids unnecessary writes to prevent extra rebuilds
|
|
11
|
+
- Watches the source file in Vite dev server mode
|
|
12
|
+
- Supports custom source and output file paths
|
|
13
|
+
- Written in TypeScript and published as a reusable npm package
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install --save-dev vite-plugin-css-vars-to-scss-tokens
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
or with Yarn:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add --dev vite-plugin-css-vars-to-scss-tokens
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
Add the plugin to your Vite configuration:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { defineConfig } from 'vite';
|
|
33
|
+
import scssTokensPlugin from 'vite-plugin-css-vars-to-scss-tokens';
|
|
34
|
+
|
|
35
|
+
export default defineConfig({
|
|
36
|
+
plugins: [
|
|
37
|
+
scssTokensPlugin(),
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Custom options
|
|
43
|
+
|
|
44
|
+
The plugin accepts an optional options object:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
scssTokensPlugin({
|
|
48
|
+
variablesFile: 'src/styles/_variables.scss',
|
|
49
|
+
tokensFile: 'src/styles/_tokens.scss',
|
|
50
|
+
logger: (message) => console.log(message),
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Options:
|
|
55
|
+
|
|
56
|
+
- `variablesFile?: string` — path to the source CSS/SCSS variables file relative to the project root
|
|
57
|
+
- `tokensFile?: string` — path to the generated SCSS tokens file relative to the project root
|
|
58
|
+
- `logger?: (message: string) => void` — optional custom logger callback
|
|
59
|
+
|
|
60
|
+
## Default behavior
|
|
61
|
+
|
|
62
|
+
By default, the plugin:
|
|
63
|
+
|
|
64
|
+
- reads `src/styles/_variables.scss`
|
|
65
|
+
- generates `src/styles/_tokens.scss`
|
|
66
|
+
- parses CSS variables defined as `--name: value;`
|
|
67
|
+
- creates SCSS declarations like `$name: var(--name);`
|
|
68
|
+
- logs updates to the console or the provided logger
|
|
69
|
+
|
|
70
|
+
## Example source file
|
|
71
|
+
|
|
72
|
+
```scss
|
|
73
|
+
:root {
|
|
74
|
+
--color-primary: #1e90ff;
|
|
75
|
+
--font-size-base: 16px;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Generates:
|
|
80
|
+
|
|
81
|
+
```scss
|
|
82
|
+
// AUTO-GENERATED — do not edit manually.
|
|
83
|
+
// Source: src/styles/_variables.scss
|
|
84
|
+
|
|
85
|
+
$color-primary: var(--color-primary);
|
|
86
|
+
$font-size-base: var(--font-size-base);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Notes
|
|
90
|
+
|
|
91
|
+
- The plugin is designed for Vite projects and uses the Vite plugin API.
|
|
92
|
+
- It works in both build and dev server modes.
|
|
93
|
+
- Generated files are only written when the content changes.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export interface ScssTokensPluginOptions {
|
|
3
|
+
/** Path to the source CSS/SCSS variables file relative to the project root. */
|
|
4
|
+
variablesFile?: string;
|
|
5
|
+
/** Path to the generated tokens file relative to the project root. */
|
|
6
|
+
tokensFile?: string;
|
|
7
|
+
/** Optional custom logger callback. */
|
|
8
|
+
logger?: (message: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export default function scssTokensPlugin(options?: ScssTokensPluginOptions): Plugin;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAkB,MAAM,MAAM,CAAC;AAInD,MAAM,WAAW,uBAAuB;IACtC,+EAA+E;IAC/E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC;AAwFD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG,MAAM,CA8BtF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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 index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
default: () => scssTokensPlugin
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_node_fs = __toESM(require("fs"));
|
|
37
|
+
var import_node_path = __toESM(require("path"));
|
|
38
|
+
var DEFAULT_VARIABLES_FILE = "src/styles/_variables.scss";
|
|
39
|
+
var DEFAULT_TOKENS_FILE = "src/styles/_tokens.scss";
|
|
40
|
+
function parseVariableNames(content) {
|
|
41
|
+
const regex = /^\s*--([\w-]+)\s*:/gm;
|
|
42
|
+
const names = [];
|
|
43
|
+
let match;
|
|
44
|
+
while ((match = regex.exec(content)) !== null) {
|
|
45
|
+
names.push(match[1]);
|
|
46
|
+
}
|
|
47
|
+
return names;
|
|
48
|
+
}
|
|
49
|
+
function generateTokens(names, sourceFile) {
|
|
50
|
+
const lines = names.map((name) => `$${name}: var(--${name});`);
|
|
51
|
+
return [
|
|
52
|
+
"// AUTO-GENERATED \u2014 do not edit manually.",
|
|
53
|
+
`// Source: ${sourceFile}`,
|
|
54
|
+
"",
|
|
55
|
+
...lines,
|
|
56
|
+
""
|
|
57
|
+
].join("\n");
|
|
58
|
+
}
|
|
59
|
+
function resolveFilePath(root, filePath) {
|
|
60
|
+
return import_node_path.default.resolve(root, filePath);
|
|
61
|
+
}
|
|
62
|
+
function ensureDirectory(filePath) {
|
|
63
|
+
import_node_fs.default.mkdirSync(import_node_path.default.dirname(filePath), { recursive: true });
|
|
64
|
+
}
|
|
65
|
+
function syncTokens(root, options, log) {
|
|
66
|
+
const variablesFile = options.variablesFile ?? DEFAULT_VARIABLES_FILE;
|
|
67
|
+
const tokensFile = options.tokensFile ?? DEFAULT_TOKENS_FILE;
|
|
68
|
+
const varsPath = resolveFilePath(root, variablesFile);
|
|
69
|
+
const tokensPath = resolveFilePath(root, tokensFile);
|
|
70
|
+
if (!import_node_fs.default.existsSync(varsPath)) {
|
|
71
|
+
log(`[vite-plugin-css-vars-to-scss-tokens] Source not found: ${variablesFile}`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const source = import_node_fs.default.readFileSync(varsPath, "utf-8");
|
|
75
|
+
const names = parseVariableNames(source);
|
|
76
|
+
if (names.length === 0) {
|
|
77
|
+
log("[vite-plugin-css-vars-to-scss-tokens] No CSS variables found \u2014 tokens file not updated.");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const tokens = generateTokens(names, variablesFile);
|
|
81
|
+
const existing = import_node_fs.default.existsSync(tokensPath) ? import_node_fs.default.readFileSync(tokensPath, "utf-8") : "";
|
|
82
|
+
if (existing === tokens) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
ensureDirectory(tokensPath);
|
|
86
|
+
import_node_fs.default.writeFileSync(tokensPath, tokens, "utf-8");
|
|
87
|
+
log(`[vite-plugin-css-vars-to-scss-tokens] Updated ${tokensFile} (${names.length} variables)`);
|
|
88
|
+
}
|
|
89
|
+
function scssTokensPlugin(options = {}) {
|
|
90
|
+
let root = process.cwd();
|
|
91
|
+
const log = options.logger ?? ((message) => console.log(message));
|
|
92
|
+
return {
|
|
93
|
+
name: "vite-plugin-css-vars-to-scss-tokens",
|
|
94
|
+
/**
|
|
95
|
+
* Save resolved Vite root for correct file path resolution.
|
|
96
|
+
*/
|
|
97
|
+
configResolved(config) {
|
|
98
|
+
root = config.root;
|
|
99
|
+
},
|
|
100
|
+
buildStart() {
|
|
101
|
+
syncTokens(root, options, log);
|
|
102
|
+
},
|
|
103
|
+
configureServer(server) {
|
|
104
|
+
const variablesFile = options.variablesFile ?? DEFAULT_VARIABLES_FILE;
|
|
105
|
+
const varsAbs = resolveFilePath(root, variablesFile);
|
|
106
|
+
server.watcher.add(varsAbs);
|
|
107
|
+
server.watcher.on("change", (file) => {
|
|
108
|
+
if (import_node_path.default.resolve(file) !== varsAbs) return;
|
|
109
|
+
syncTokens(root, options, (msg) => server.config.logger.info(msg, { timestamp: true }));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
var DEFAULT_VARIABLES_FILE = "src/styles/_variables.scss";
|
|
5
|
+
var DEFAULT_TOKENS_FILE = "src/styles/_tokens.scss";
|
|
6
|
+
function parseVariableNames(content) {
|
|
7
|
+
const regex = /^\s*--([\w-]+)\s*:/gm;
|
|
8
|
+
const names = [];
|
|
9
|
+
let match;
|
|
10
|
+
while ((match = regex.exec(content)) !== null) {
|
|
11
|
+
names.push(match[1]);
|
|
12
|
+
}
|
|
13
|
+
return names;
|
|
14
|
+
}
|
|
15
|
+
function generateTokens(names, sourceFile) {
|
|
16
|
+
const lines = names.map((name) => `$${name}: var(--${name});`);
|
|
17
|
+
return [
|
|
18
|
+
"// AUTO-GENERATED \u2014 do not edit manually.",
|
|
19
|
+
`// Source: ${sourceFile}`,
|
|
20
|
+
"",
|
|
21
|
+
...lines,
|
|
22
|
+
""
|
|
23
|
+
].join("\n");
|
|
24
|
+
}
|
|
25
|
+
function resolveFilePath(root, filePath) {
|
|
26
|
+
return path.resolve(root, filePath);
|
|
27
|
+
}
|
|
28
|
+
function ensureDirectory(filePath) {
|
|
29
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
function syncTokens(root, options, log) {
|
|
32
|
+
const variablesFile = options.variablesFile ?? DEFAULT_VARIABLES_FILE;
|
|
33
|
+
const tokensFile = options.tokensFile ?? DEFAULT_TOKENS_FILE;
|
|
34
|
+
const varsPath = resolveFilePath(root, variablesFile);
|
|
35
|
+
const tokensPath = resolveFilePath(root, tokensFile);
|
|
36
|
+
if (!fs.existsSync(varsPath)) {
|
|
37
|
+
log(`[vite-plugin-css-vars-to-scss-tokens] Source not found: ${variablesFile}`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const source = fs.readFileSync(varsPath, "utf-8");
|
|
41
|
+
const names = parseVariableNames(source);
|
|
42
|
+
if (names.length === 0) {
|
|
43
|
+
log("[vite-plugin-css-vars-to-scss-tokens] No CSS variables found \u2014 tokens file not updated.");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const tokens = generateTokens(names, variablesFile);
|
|
47
|
+
const existing = fs.existsSync(tokensPath) ? fs.readFileSync(tokensPath, "utf-8") : "";
|
|
48
|
+
if (existing === tokens) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
ensureDirectory(tokensPath);
|
|
52
|
+
fs.writeFileSync(tokensPath, tokens, "utf-8");
|
|
53
|
+
log(`[vite-plugin-css-vars-to-scss-tokens] Updated ${tokensFile} (${names.length} variables)`);
|
|
54
|
+
}
|
|
55
|
+
function scssTokensPlugin(options = {}) {
|
|
56
|
+
let root = process.cwd();
|
|
57
|
+
const log = options.logger ?? ((message) => console.log(message));
|
|
58
|
+
return {
|
|
59
|
+
name: "vite-plugin-css-vars-to-scss-tokens",
|
|
60
|
+
/**
|
|
61
|
+
* Save resolved Vite root for correct file path resolution.
|
|
62
|
+
*/
|
|
63
|
+
configResolved(config) {
|
|
64
|
+
root = config.root;
|
|
65
|
+
},
|
|
66
|
+
buildStart() {
|
|
67
|
+
syncTokens(root, options, log);
|
|
68
|
+
},
|
|
69
|
+
configureServer(server) {
|
|
70
|
+
const variablesFile = options.variablesFile ?? DEFAULT_VARIABLES_FILE;
|
|
71
|
+
const varsAbs = resolveFilePath(root, variablesFile);
|
|
72
|
+
server.watcher.add(varsAbs);
|
|
73
|
+
server.watcher.on("change", (file) => {
|
|
74
|
+
if (path.resolve(file) !== varsAbs) return;
|
|
75
|
+
syncTokens(root, options, (msg) => server.config.logger.info(msg, { timestamp: true }));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
scssTokensPlugin as default
|
|
82
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-css-vars-to-scss-tokens",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vite plugin that generates SCSS token variables from CSS custom properties",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Yalla Nkardaz",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/DemerNkardaz/vite-plugin-css-vars-to-scss-tokens"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/DemerNkardaz/vite-plugin-css-vars-to-scss-tokens#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/DemerNkardaz/vite-plugin-css-vars-to-scss-tokens/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "commonjs",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"module": "./dist/index.mjs",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.mjs",
|
|
23
|
+
"require": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"vite",
|
|
31
|
+
"plugin",
|
|
32
|
+
"scss",
|
|
33
|
+
"css",
|
|
34
|
+
"tokens",
|
|
35
|
+
"css-variables"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup && tsc --project tsconfig.build.json",
|
|
39
|
+
"dev": "tsup --watch",
|
|
40
|
+
"prepublishOnly": "npm run build"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"vite": ">=4.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^25.9.1",
|
|
47
|
+
"tsup": "^8.5.1",
|
|
48
|
+
"typescript": "^6.0.3",
|
|
49
|
+
"vite": "^8.0.14"
|
|
50
|
+
}
|
|
51
|
+
}
|