@cssdoc/dtcg 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 +24 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.mjs +52 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Danny Wahl
|
|
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,24 @@
|
|
|
1
|
+
# @cssdoc/dtcg
|
|
2
|
+
|
|
3
|
+
Export the declared custom properties as [W3C Design Tokens](https://tr.designtokens.org/) (DTCG). Each
|
|
4
|
+
`@property` becomes a token with `$value` (its `initial-value`), `$type` (mapped from its `syntax`), and
|
|
5
|
+
`$description`, grouped by the record that declares it — so properties that double as design tokens
|
|
6
|
+
interchange with the token ecosystem.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { createIndex } from "@cssdoc/index";
|
|
12
|
+
import { writeDtcg } from "@cssdoc/dtcg";
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
|
|
15
|
+
const index = createIndex(readFileSync("dist/components.css", "utf8"));
|
|
16
|
+
writeDtcg({ index, outFile: "tokens.json" });
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Syntax → `$type` mapping: `<color>` → `color`, `<length>` → `dimension`, `<time>` → `duration`,
|
|
20
|
+
`<number>`/`<integer>`/`<percentage>` → `number`. Unmapped syntaxes omit `$type`.
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CssDocIndex } from "@cssdoc/index";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/** A single DTCG token. */
|
|
5
|
+
interface DtcgToken {
|
|
6
|
+
$value: string;
|
|
7
|
+
$type?: string;
|
|
8
|
+
$description?: string;
|
|
9
|
+
}
|
|
10
|
+
/** A DTCG token document: a group per record, each holding its custom-property tokens. */
|
|
11
|
+
type DtcgDocument = Record<string, Record<string, DtcgToken>>;
|
|
12
|
+
/** Build a DTCG token document from the index's declared custom properties. */
|
|
13
|
+
declare function toDtcg(index: CssDocIndex): DtcgDocument;
|
|
14
|
+
/** Write the DTCG document to `outFile` (defaults to `tokens.json`). */
|
|
15
|
+
declare function writeDtcg(options: {
|
|
16
|
+
index: CssDocIndex;
|
|
17
|
+
outFile?: string;
|
|
18
|
+
}): {
|
|
19
|
+
outFile: string;
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
export { DtcgDocument, DtcgToken, toDtcg, writeDtcg };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* `@cssdoc/dtcg` — export the declared custom properties as W3C Design Tokens (DTCG). Each `@property`
|
|
6
|
+
* becomes a token with `$value` (its `initial-value`), `$type` (mapped from its `syntax`), and
|
|
7
|
+
* `$description`, grouped by the record that declares it — so properties that double as design tokens
|
|
8
|
+
* interchange with the token ecosystem.
|
|
9
|
+
*
|
|
10
|
+
* @module @cssdoc/dtcg
|
|
11
|
+
*/
|
|
12
|
+
/** Map a CSS `@property` syntax descriptor to a DTCG `$type`, when there's a clear correspondence. */
|
|
13
|
+
const SYNTAX_TO_TYPE = {
|
|
14
|
+
"<color>": "color",
|
|
15
|
+
"<length>": "dimension",
|
|
16
|
+
"<length-percentage>": "dimension",
|
|
17
|
+
"<percentage>": "number",
|
|
18
|
+
"<number>": "number",
|
|
19
|
+
"<integer>": "number",
|
|
20
|
+
"<time>": "duration",
|
|
21
|
+
"<angle>": "number"
|
|
22
|
+
};
|
|
23
|
+
function dtcgType(syntax) {
|
|
24
|
+
return syntax ? SYNTAX_TO_TYPE[syntax.trim()] : void 0;
|
|
25
|
+
}
|
|
26
|
+
/** Build a DTCG token document from the index's declared custom properties. */
|
|
27
|
+
function toDtcg(index) {
|
|
28
|
+
const root = {};
|
|
29
|
+
for (const entry of index.entries) {
|
|
30
|
+
if (!entry.cssPropertiesDeclared.length) continue;
|
|
31
|
+
const group = {};
|
|
32
|
+
for (const property of entry.cssPropertiesDeclared) {
|
|
33
|
+
const type = dtcgType(property.syntax);
|
|
34
|
+
group[property.name.replace(/^--/u, "")] = {
|
|
35
|
+
$value: property.defaultValue ?? "",
|
|
36
|
+
...type ? { $type: type } : {},
|
|
37
|
+
...property.description ? { $description: property.description } : {}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
root[entry.name] = group;
|
|
41
|
+
}
|
|
42
|
+
return root;
|
|
43
|
+
}
|
|
44
|
+
/** Write the DTCG document to `outFile` (defaults to `tokens.json`). */
|
|
45
|
+
function writeDtcg(options) {
|
|
46
|
+
const outFile = options.outFile ?? "tokens.json";
|
|
47
|
+
mkdirSync(dirname(outFile), { recursive: true });
|
|
48
|
+
writeFileSync(outFile, `${JSON.stringify(toDtcg(options.index), null, 2)}\n`);
|
|
49
|
+
return { outFile };
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { toDtcg, writeDtcg };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cssdoc/dtcg",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Export cssdoc custom properties as W3C Design Tokens (DTCG): $value, $type, $description.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"css",
|
|
7
|
+
"cssdoc",
|
|
8
|
+
"design-tokens",
|
|
9
|
+
"dtcg"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://cssdoc.dev",
|
|
12
|
+
"bugs": "https://github.com/thedannywahl/cssdoc/issues",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/thedannywahl/cssdoc.git",
|
|
17
|
+
"directory": "generators/dtcg"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./dist/index.mjs",
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@cssdoc/core": "0.1.0",
|
|
32
|
+
"@cssdoc/index": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^24.13.3",
|
|
36
|
+
"publint": "^0.3.21",
|
|
37
|
+
"typescript": "^6.0.3",
|
|
38
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.4",
|
|
39
|
+
"vite-plus": "0.2.4"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "vp pack",
|
|
43
|
+
"dev": "vp pack --watch",
|
|
44
|
+
"test": "vp test",
|
|
45
|
+
"check": "vp check",
|
|
46
|
+
"publint": "publint"
|
|
47
|
+
}
|
|
48
|
+
}
|