@projectwallace/css-layer-tree 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Project Wallace
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,68 @@
1
+ # css-layer-tree
2
+
3
+ Lay out the composition of your CSS `@layer` architecture. See which layers are used, where they are defined and how they are nested.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm install @projectwallace/css-layer-tree
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { get_tree } from '@projectwallace/css-layer-tree'
15
+
16
+ let css = `
17
+ @import url("test.css") layer;
18
+ @import url("test.css") LAYER(test);
19
+ @layer anotherTest {
20
+ @layer moreTest {
21
+ @layer deepTest {}
22
+ }
23
+ };
24
+ /* anonymous @layer */
25
+ @layer {}
26
+ `
27
+
28
+ let tree = get_tree(css)
29
+ ```
30
+
31
+ This example would result in this `tree`:
32
+
33
+ ```js
34
+ ;[
35
+ {
36
+ name: '__anonymous-1__',
37
+ locations: [{ line: 2, column: 3, start: 3, end: 33 }],
38
+ children: [],
39
+ },
40
+ {
41
+ name: 'test',
42
+ locations: [{ line: 3, column: 3, start: 36, end: 72 }],
43
+ children: [],
44
+ },
45
+ {
46
+ name: 'anotherTest',
47
+ locations: [{ line: 4, column: 3, start: 75, end: 148 }],
48
+ children: [
49
+ {
50
+ name: 'moreTest',
51
+ locations: [{ line: 5, column: 4, start: 99, end: 144 }],
52
+ children: [
53
+ {
54
+ name: 'deepTest',
55
+ locations: [{ line: 6, column: 5, start: 121, end: 139 }],
56
+ children: [],
57
+ },
58
+ ],
59
+ },
60
+ ],
61
+ },
62
+ ]
63
+ ```
64
+
65
+ ## Related projects
66
+
67
+ - [Online CSS Layers visualizer](https://www.projectwallace.com/css-layers-visualizer) - See this library in action online!
68
+ - [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com)
@@ -0,0 +1,119 @@
1
+ import * as p from "css-tree";
2
+ class f {
3
+ /** @param {string} name */
4
+ constructor(e) {
5
+ this.name = e, this.children = /* @__PURE__ */ new Map(), this.locations = [];
6
+ }
7
+ /**
8
+ *
9
+ * @param {string[]} path
10
+ * @param {string} name
11
+ * @param {T} location
12
+ */
13
+ add_child(e, l, c) {
14
+ let n = this;
15
+ if (e.forEach((s) => {
16
+ n = n.children.get(s);
17
+ }), n.children.has(l))
18
+ n.children.get(l).locations.push(c);
19
+ else {
20
+ const s = new f(l);
21
+ s.locations.push(c), n.children.set(l, s);
22
+ }
23
+ }
24
+ /**
25
+ * @typedef PlainObject
26
+ * @property {string} name
27
+ * @property {T[]} locations
28
+ * @property {PlainObject[]} children
29
+ */
30
+ /**
31
+ * Convert the tree to a plain object for easy testing
32
+ * @returns {PlainObject}
33
+ */
34
+ to_plain_object() {
35
+ return {
36
+ name: this.name,
37
+ locations: this.locations,
38
+ children: Array.from(this.children.values()).map((e) => e.to_plain_object())
39
+ };
40
+ }
41
+ }
42
+ function d(u) {
43
+ let e = u.loc;
44
+ if (e)
45
+ return {
46
+ line: e.start.line,
47
+ column: e.start.column,
48
+ start: e.start.offset,
49
+ end: e.end.offset
50
+ };
51
+ }
52
+ function _(u) {
53
+ return u.name.toLowerCase() === "layer";
54
+ }
55
+ function h(u) {
56
+ let e = [], l = new f("root"), c = 0;
57
+ function n() {
58
+ return c++, `__anonymous-${c}__`;
59
+ }
60
+ function s(t) {
61
+ return p.generate(t).split(".").map((a) => a.trim());
62
+ }
63
+ return p.walk(u, {
64
+ visit: "Atrule",
65
+ enter(t) {
66
+ if (_(t)) {
67
+ let a = d(t);
68
+ if (t.prelude === null) {
69
+ let r = n();
70
+ l.add_child(e, r, a), e.push(r);
71
+ return;
72
+ }
73
+ if (t.prelude.type === "AtrulePrelude")
74
+ if (t.block === null) {
75
+ let r = p.findAll(t.prelude, (o) => o.type === "Layer").map((o) => o.name);
76
+ for (let o of r)
77
+ l.add_child(e, o, a);
78
+ } else
79
+ for (let r of s(t.prelude))
80
+ l.add_child(e, r, a), e.push(r);
81
+ } else if (t.name.toLowerCase() === "import" && t.prelude !== null && t.prelude.type === "AtrulePrelude") {
82
+ let a = d(t), r = t.prelude, o = p.find(r, (i) => i.type === "Layer");
83
+ if (o) {
84
+ for (let i of s(o))
85
+ l.add_child(e, i, a), e.push(i);
86
+ return this.skip;
87
+ }
88
+ if (p.find(r, (i) => i.type === "Function" && i.name.toLowerCase() === "layer"))
89
+ return l.add_child([], n(), a), this.skip;
90
+ if (p.find(r, (i) => i.type === "Identifier" && i.name.toLowerCase() === "layer"))
91
+ return l.add_child([], n(), a), this.skip;
92
+ }
93
+ },
94
+ leave(t) {
95
+ if (_(t))
96
+ if (t.prelude !== null && t.prelude.type === "AtrulePrelude") {
97
+ let a = s(t.prelude);
98
+ for (let r = 0; r < a.length; r++)
99
+ e.pop();
100
+ } else
101
+ e.pop();
102
+ else t.name.toLowerCase() === "import" && (e.length = 0);
103
+ }
104
+ }), l.to_plain_object().children;
105
+ }
106
+ function w(u) {
107
+ let e = p.parse(u, {
108
+ positions: !0,
109
+ parseAtrulePrelude: !0,
110
+ parseValue: !1,
111
+ parseRulePrelude: !1,
112
+ parseCustomProperty: !1
113
+ });
114
+ return h(e);
115
+ }
116
+ export {
117
+ w as get_tree,
118
+ h as get_tree_from_ast
119
+ };
@@ -0,0 +1 @@
1
+ (function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("css-tree")):typeof define=="function"&&define.amd?define(["exports","css-tree"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o.cssLayerTree={},o.csstree))})(this,function(o,d){"use strict";function g(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const r in n)if(r!=="default"){const u=Object.getOwnPropertyDescriptor(n,r);Object.defineProperty(e,r,u.get?u:{enumerable:!0,get:()=>n[r]})}}return e.default=n,Object.freeze(e)}const p=g(d);class _{constructor(e){this.name=e,this.children=new Map,this.locations=[]}add_child(e,r,u){let s=this;if(e.forEach(c=>{s=s.children.get(c)}),s.children.has(r))s.children.get(r).locations.push(u);else{const c=new _(r);c.locations.push(u),s.children.set(r,c)}}to_plain_object(){return{name:this.name,locations:this.locations,children:Array.from(this.children.values()).map(e=>e.to_plain_object())}}}function y(n){let e=n.loc;if(e)return{line:e.start.line,column:e.start.column,start:e.start.offset,end:e.end.offset}}function h(n){return n.name.toLowerCase()==="layer"}function m(n){let e=[],r=new _("root"),u=0;function s(){return u++,`__anonymous-${u}__`}function c(t){return p.generate(t).split(".").map(i=>i.trim())}return p.walk(n,{visit:"Atrule",enter(t){if(h(t)){let i=y(t);if(t.prelude===null){let l=s();r.add_child(e,l,i),e.push(l);return}if(t.prelude.type==="AtrulePrelude")if(t.block===null){let l=p.findAll(t.prelude,f=>f.type==="Layer").map(f=>f.name);for(let f of l)r.add_child(e,f,i)}else for(let l of c(t.prelude))r.add_child(e,l,i),e.push(l)}else if(t.name.toLowerCase()==="import"&&t.prelude!==null&&t.prelude.type==="AtrulePrelude"){let i=y(t),l=t.prelude,f=p.find(l,a=>a.type==="Layer");if(f){for(let a of c(f))r.add_child(e,a,i),e.push(a);return this.skip}if(p.find(l,a=>a.type==="Function"&&a.name.toLowerCase()==="layer"))return r.add_child([],s(),i),this.skip;if(p.find(l,a=>a.type==="Identifier"&&a.name.toLowerCase()==="layer"))return r.add_child([],s(),i),this.skip}},leave(t){if(h(t))if(t.prelude!==null&&t.prelude.type==="AtrulePrelude"){let i=c(t.prelude);for(let l=0;l<i.length;l++)e.pop()}else e.pop();else t.name.toLowerCase()==="import"&&(e.length=0)}}),r.to_plain_object().children}function w(n){let e=p.parse(n,{positions:!0,parseAtrulePrelude:!0,parseValue:!1,parseRulePrelude:!1,parseCustomProperty:!1});return m(e)}o.get_tree=w,o.get_tree_from_ast=m,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { type CssNode } from "css-tree";
2
+
3
+ export type Location = {
4
+ line: number;
5
+ column: number;
6
+ start: number;
7
+ end: number;
8
+ }
9
+
10
+ export type TreeNode = {
11
+ name: string;
12
+ children: TreeNode[];
13
+ locations: Location[];
14
+ }
15
+
16
+ export function get_tree_from_ast(ast: CssNode): TreeNode['children'];
17
+ export function get_tree(css: string): TreeNode['children'];
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@projectwallace/css-layer-tree",
3
+ "version": "0.0.1",
4
+ "description": "Discover the composition of your CSS @layers",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/projectwallace/css-layers.git"
8
+ },
9
+ "homepage": "https://github.com/projectwallace/css-layer-tree",
10
+ "issues": "https://github.com/projectwallace/css-layer-tree/issues",
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "source": "src/index.js",
14
+ "exports": {
15
+ "types": "./index.d.ts",
16
+ "require": "./dist/css-layer-tree.umd.cjs",
17
+ "default": "./dist/css-layer-tree.js"
18
+ },
19
+ "types": "./index.d.ts",
20
+ "main": "./dist/css-layer-tree.umd.cjs",
21
+ "module": "./dist/css-layer-tree.js",
22
+ "unpkg": "./dist/css-layer-tree.umd.cjs",
23
+ "scripts": {
24
+ "build": "vite build",
25
+ "test": "c8 --reporter=lcov uvu",
26
+ "check": "tsc",
27
+ "prettier": "prettier --check src/**/*.js test/**/*.js"
28
+ },
29
+ "devDependencies": {
30
+ "@codecov/vite-plugin": "^1.2.1",
31
+ "@types/css-tree": "^2.3.8",
32
+ "c8": "^10.1.2",
33
+ "prettier": "^3.3.3",
34
+ "typescript": "5.4.2",
35
+ "uvu": "^0.5.6",
36
+ "vite": "^5.4.10"
37
+ },
38
+ "dependencies": {
39
+ "css-tree": "^3.0.0"
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "index.d.ts"
44
+ ],
45
+ "keywords": [
46
+ "css",
47
+ "stylesheet",
48
+ "layer",
49
+ "layers",
50
+ "composition",
51
+ "architecture",
52
+ "tree"
53
+ ],
54
+ "prettier": {
55
+ "semi": false,
56
+ "useTabs": true,
57
+ "printWidth": 140,
58
+ "singleQuote": true
59
+ }
60
+ }