@fullstackunicorn/style 1.0.14 → 1.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/index.dep.js +1 -0
- package/index.js +0 -0
- package/modules/engine/geometry.js +181 -0
- package/modules/engine/parse.js +5 -2
- package/modules/run.js +1 -1
- package/package.json +51 -53
- package/package.md +14 -6
package/index.dep.js
CHANGED
package/index.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { path, readFile, realpath } from '#@/index.dep.js'
|
|
2
|
+
import { style } from '#@/modules/engine/style.js'
|
|
3
|
+
|
|
4
|
+
class Geometry {
|
|
5
|
+
|
|
6
|
+
constructor(basePath) {
|
|
7
|
+
this.filename = path.resolve(basePath, 'configure/geometry.js')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
invalid = message => {
|
|
11
|
+
throw new Error(`Invalid Geometry: ${message} (${this.filename})`)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
safe = name => {
|
|
15
|
+
if (['__proto__', 'prototype', 'constructor'].includes(name)) this.invalid(`unsafe property ${name}`)
|
|
16
|
+
return name
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
read = async () => {
|
|
20
|
+
if (!this.pending) this.pending = this.load()
|
|
21
|
+
return this.pending
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
load = async () => {
|
|
25
|
+
let source
|
|
26
|
+
try {
|
|
27
|
+
this.filename = await realpath(this.filename)
|
|
28
|
+
source = await readFile(this.filename, 'utf8')
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new Error(`Cannot read Geometry (${this.filename})`, { cause: error })
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const tokens = []
|
|
34
|
+
let offset = 0
|
|
35
|
+
let newline = false
|
|
36
|
+
while (offset < source.length) {
|
|
37
|
+
const remaining = source.slice(offset)
|
|
38
|
+
const skipped = remaining.match(/^(?:\s+|\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)/)
|
|
39
|
+
if (skipped) {
|
|
40
|
+
newline ||= skipped[0].includes('\n')
|
|
41
|
+
offset += skipped[0].length
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
const match = remaining.match(/^(?:"(?:[^"\\\r\n]|\\["\\nrt])*"|'(?:[^'\\\r\n]|\\['\\nrt])*'|(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|[A-Za-z_$][\w$]*|[{}[\].,:;=+-])/)
|
|
45
|
+
if (!match) this.invalid(`unsupported syntax at line ${source.slice(0, offset).split('\n').length}`)
|
|
46
|
+
tokens.push({ value: match[0], newline })
|
|
47
|
+
offset += match[0].length
|
|
48
|
+
newline = false
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const variables = Object.create(null)
|
|
52
|
+
let position = 0
|
|
53
|
+
let exported = false
|
|
54
|
+
const current = () => tokens[position]?.value
|
|
55
|
+
const take = expected => {
|
|
56
|
+
const value = current()
|
|
57
|
+
if (expected && value !== expected) this.invalid(`expected ${expected}, received ${value ?? 'end of file'}`)
|
|
58
|
+
position++
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
const name = () => {
|
|
62
|
+
const value = take()
|
|
63
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(value ?? '')) this.invalid('expected a property name')
|
|
64
|
+
return this.safe(value)
|
|
65
|
+
}
|
|
66
|
+
const member = (object, key) => {
|
|
67
|
+
if (!object || typeof object !== 'object' || !Object.hasOwn(object, key)) this.invalid(`unknown property ${key}`)
|
|
68
|
+
return object[key]
|
|
69
|
+
}
|
|
70
|
+
const string = token => token.slice(1, -1).replace(/\\(["'\\nrt])/g, (_, value) => ({ n: '\n', r: '\r', t: '\t' }[value] ?? value))
|
|
71
|
+
const value = () => {
|
|
72
|
+
const token = current()
|
|
73
|
+
if (token === '{') {
|
|
74
|
+
take('{')
|
|
75
|
+
const object = Object.create(null)
|
|
76
|
+
while (current() !== '}') {
|
|
77
|
+
const key = name()
|
|
78
|
+
if (Object.hasOwn(object, key)) this.invalid(`duplicate property ${key}`)
|
|
79
|
+
object[key] = current() === ':' ? (take(':'), value()) : member(variables, key)
|
|
80
|
+
if (current() !== ',') break
|
|
81
|
+
take(',')
|
|
82
|
+
}
|
|
83
|
+
take('}')
|
|
84
|
+
return object
|
|
85
|
+
}
|
|
86
|
+
if (token === '[') {
|
|
87
|
+
take('[')
|
|
88
|
+
const array = []
|
|
89
|
+
while (current() !== ']') {
|
|
90
|
+
array.push(value())
|
|
91
|
+
if (current() !== ',') break
|
|
92
|
+
take(',')
|
|
93
|
+
}
|
|
94
|
+
take(']')
|
|
95
|
+
return array
|
|
96
|
+
}
|
|
97
|
+
if (token?.startsWith('"') || token?.startsWith("'")) return string(take())
|
|
98
|
+
if (token === 'true' || token === 'false' || token === 'null') return JSON.parse(take())
|
|
99
|
+
if (/^(?:\d|[+-]$)/.test(token ?? '')) {
|
|
100
|
+
const sign = token === '-' || token === '+' ? take() : ''
|
|
101
|
+
const number = Number(sign + take())
|
|
102
|
+
if (!Number.isFinite(number)) this.invalid('expected a finite number')
|
|
103
|
+
return number
|
|
104
|
+
}
|
|
105
|
+
let reference = member(variables, name())
|
|
106
|
+
while (current() === '.') {
|
|
107
|
+
take('.')
|
|
108
|
+
reference = member(reference, name())
|
|
109
|
+
}
|
|
110
|
+
return reference
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
while (position < tokens.length) {
|
|
114
|
+
if (current() === 'const') {
|
|
115
|
+
take('const')
|
|
116
|
+
const key = name()
|
|
117
|
+
if (Object.hasOwn(variables, key)) this.invalid(`duplicate declaration ${key}`)
|
|
118
|
+
take('=')
|
|
119
|
+
variables[key] = value()
|
|
120
|
+
} else if (current() === 'export') {
|
|
121
|
+
if (exported) this.invalid('duplicate export')
|
|
122
|
+
take('export')
|
|
123
|
+
take('{')
|
|
124
|
+
take('geometry')
|
|
125
|
+
if (current() === ',') take(',')
|
|
126
|
+
take('}')
|
|
127
|
+
exported = true
|
|
128
|
+
} else {
|
|
129
|
+
let object = member(variables, name())
|
|
130
|
+
take('.')
|
|
131
|
+
let key = name()
|
|
132
|
+
while (current() === '.') {
|
|
133
|
+
object = member(object, key)
|
|
134
|
+
take('.')
|
|
135
|
+
key = name()
|
|
136
|
+
}
|
|
137
|
+
if (!object || typeof object !== 'object' || Array.isArray(object)) this.invalid(`cannot assign ${key}`)
|
|
138
|
+
take('=')
|
|
139
|
+
object[key] = value()
|
|
140
|
+
}
|
|
141
|
+
if (current() === ';') take(';')
|
|
142
|
+
else if (position < tokens.length && !tokens[position].newline) this.invalid(`unexpected ${current()}`)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const geometry = variables.geometry
|
|
146
|
+
if (!exported || !geometry || typeof geometry !== 'object' || Array.isArray(geometry)) this.invalid('expected named export { geometry } with an object value')
|
|
147
|
+
return geometry
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
classes = async (source, filename) => {
|
|
151
|
+
const classes = []
|
|
152
|
+
const expression = /\bs\s*\[/g
|
|
153
|
+
let match
|
|
154
|
+
while ((match = expression.exec(source)) !== null) {
|
|
155
|
+
const literal = source.slice(expression.lastIndex).match(/^\s*(['"`])([^\\]*?)\1\s*\]/)
|
|
156
|
+
if (!literal) this.invalid(`unsupported computed style in ${filename}`)
|
|
157
|
+
expression.lastIndex += literal[0].length
|
|
158
|
+
let item = literal[2]
|
|
159
|
+
const dynamic = literal[1] === '`' && item.includes('${')
|
|
160
|
+
if (literal[1] === '`') {
|
|
161
|
+
const geometry = dynamic ? await this.read() : null
|
|
162
|
+
item = item.replace(/\$\{([^}]+)\}/g, (_, lookup) => {
|
|
163
|
+
if (!/^geometry(?:\.[A-Za-z_$][\w$]*)+$/.test(lookup.trim())) this.invalid(`unsupported computed style ${lookup} in ${filename}`)
|
|
164
|
+
let resolved = geometry
|
|
165
|
+
for (const key of lookup.trim().split('.').slice(1)) {
|
|
166
|
+
this.safe(key)
|
|
167
|
+
resolved = resolved && typeof resolved === 'object' && Object.hasOwn(resolved, key) ? resolved[key] : undefined
|
|
168
|
+
}
|
|
169
|
+
if ((typeof resolved !== 'number' || !Number.isFinite(resolved)) && typeof resolved !== 'string') this.invalid(`${lookup} is not a Style value in ${filename}`)
|
|
170
|
+
return String(resolved)
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
const parsed = style.parseItem(item)
|
|
174
|
+
if (!/^\w+$/.test(item) || (dynamic && parsed.value?.valueKind === 'keyword') || style.getContents([parsed], {}, false, false).missing.length) this.invalid(`unknown computed style ${item} in ${filename}`)
|
|
175
|
+
classes.push(item)
|
|
176
|
+
}
|
|
177
|
+
return classes
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const geometry = basePath => new Geometry(basePath)
|
package/modules/engine/parse.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { fsa, glob } from '#@/index.dep.js'
|
|
2
|
+
import { geometry } from '#@/modules/engine/geometry.js'
|
|
2
3
|
|
|
3
4
|
class Parse {
|
|
4
5
|
|
|
5
|
-
project = async (sourcePaths = [process.env.BASE_PATH]) => {
|
|
6
|
+
project = async (sourcePaths = [process.env.BASE_PATH], basePath = sourcePaths?.[0]) => {
|
|
6
7
|
|
|
7
8
|
if (!Array.isArray(sourcePaths) || !sourcePaths.length || sourcePaths.some(value => typeof value !== 'string' || !value.trim())) {
|
|
8
9
|
throw new Error('SOURCE_PATHS must be a non-empty array of paths')
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
const projectGeometry = geometry(basePath)
|
|
13
|
+
|
|
11
14
|
const files = [...new Set((await Promise.all(sourcePaths.map(sourcePath => glob(`${sourcePath}/**/*.js`, {
|
|
12
15
|
ignore: [`${sourcePath}/node_modules/**`, `${sourcePath}/@*/**`]
|
|
13
16
|
})))).flat())]
|
|
@@ -26,7 +29,7 @@ class Parse {
|
|
|
26
29
|
const c_regex = /\bc\.(\w+)/g
|
|
27
30
|
const f_regex = /\bf\.(\w+)/g
|
|
28
31
|
|
|
29
|
-
const s_matches = []
|
|
32
|
+
const s_matches = isStyle ? await projectGeometry.classes(data, file) : []
|
|
30
33
|
const c_matches = []
|
|
31
34
|
const f_matches = []
|
|
32
35
|
|
package/modules/run.js
CHANGED
|
@@ -35,7 +35,7 @@ export const run = async (setup) => {
|
|
|
35
35
|
'line' // border-color
|
|
36
36
|
]
|
|
37
37
|
|
|
38
|
-
const parsed = await parse.project(setup.SOURCE_PATHS || [setup.BASE_PATH])
|
|
38
|
+
const parsed = await parse.project(setup.SOURCE_PATHS || [setup.BASE_PATH], setup.BASE_PATH)
|
|
39
39
|
|
|
40
40
|
const fontList = parsed.f
|
|
41
41
|
const styleList = style.getList(parsed.s)
|
package/package.json
CHANGED
|
@@ -1,54 +1,52 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
}
|
|
2
|
+
"version": "1.1.0",
|
|
3
|
+
"name": "@fullstackunicorn/style",
|
|
4
|
+
"author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
|
|
5
|
+
"description": "A development utility for generating responsive web and native styles (CSS, SCSS, JS, RN) from a declarative style.config.js file.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"packageManager": "yarn@4.18.1",
|
|
9
|
+
"readme": "package.md",
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.dep.js",
|
|
14
|
+
"modules/",
|
|
15
|
+
"package.md"
|
|
16
|
+
],
|
|
17
|
+
"bin": "./index.cmd.js",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://gitlab.com/fullstackunicorn/style.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"style-generator",
|
|
24
|
+
"scss",
|
|
25
|
+
"react-native",
|
|
26
|
+
"css",
|
|
27
|
+
"design-system",
|
|
28
|
+
"utilities",
|
|
29
|
+
"responsive-design",
|
|
30
|
+
"fullstackunicorn"
|
|
31
|
+
],
|
|
32
|
+
"sideEffects": [
|
|
33
|
+
"index.cmd.js"
|
|
34
|
+
],
|
|
35
|
+
"imports": {
|
|
36
|
+
"#@": "./",
|
|
37
|
+
"#@/*": "./*"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node --test tests/*.test.js"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@fullstackunicorn/filesystem": "^1.0.4",
|
|
44
|
+
"@fullstackunicorn/initialize": "^1.0.7",
|
|
45
|
+
"glob": "^13.0.0",
|
|
46
|
+
"sass": "^1.96.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@fullstackunicorn/cli": "^1.0.3",
|
|
50
|
+
"@fullstackunicorn/reduce": "^1.0.19"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/package.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
`SOURCE_PATHS` optionally adds explicit source roots to the normal project scan. This is used when shared components live in an installed package; nested dependencies remain excluded.
|
|
4
4
|
|
|
5
|
+
Version 1.1.0 also resolves computed utility keys such as
|
|
6
|
+
``s[`paddingX${geometry.page.padding}`]`` from the project's
|
|
7
|
+
`configure/geometry.js`. `BASE_PATH` identifies that project even when
|
|
8
|
+
`SOURCE_PATHS` includes installed shared components. The static leaf declares
|
|
9
|
+
objects and literal values with `const` and property assignments, then exports
|
|
10
|
+
`{ geometry }`; imports, calls and environment-dependent expressions are rejected.
|
|
11
|
+
It is parsed once per project scan, without executing application code. Existing
|
|
12
|
+
literal utility classes continue to work without a Geometry file.
|
|
13
|
+
|
|
14
|
+
Computed keys must resolve to an existing Style utility exactly. Missing paths,
|
|
15
|
+
unsupported expressions and unknown keys fail the build. Decimal and negative
|
|
16
|
+
utility tokens retain their existing spelling, such as `2d5` and `4n`; the parser
|
|
17
|
+
does not silently rewrite a numeric interpolation into a different runtime key.
|
|
18
|
+
|
|
5
19
|
A cross‑platform styling engine for Web and React Native.
|
|
6
20
|
It generates unified utility classes, directional rules, responsive ratios, color‑mode styles, and typography maps — all from a single configuration file.
|
|
7
21
|
|
|
@@ -13,12 +27,6 @@ Designed for use inside FullstackUnicorn projects, but fully usable as a standal
|
|
|
13
27
|
yarn add @fullstackunicorn/style
|
|
14
28
|
```
|
|
15
29
|
|
|
16
|
-
or
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm install @fullstackunicorn/style
|
|
20
|
-
```
|
|
21
|
-
|
|
22
30
|
## 🧩 Usage
|
|
23
31
|
|
|
24
32
|
Create a `style.config.js` file in the root folder of the project:
|