nitrowind 0.0.0-stage → 0.1.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/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/css.d.ts +6 -0
- package/dist/color.d.ts +24 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +158 -0
- package/dist/color.js.map +1 -0
- package/dist/core.d.ts +121 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +1026 -0
- package/dist/core.js.map +1 -0
- package/dist/css/compile.d.ts +18 -0
- package/dist/css/compile.d.ts.map +1 -0
- package/dist/css/compile.js +1018 -0
- package/dist/css/compile.js.map +1 -0
- package/dist/css/declarations.d.ts +18 -0
- package/dist/css/declarations.d.ts.map +1 -0
- package/dist/css/declarations.js +1173 -0
- package/dist/css/declarations.js.map +1 -0
- package/dist/css/index.d.ts +7 -0
- package/dist/css/index.d.ts.map +1 -0
- package/dist/css/index.js +7 -0
- package/dist/css/index.js.map +1 -0
- package/dist/css/parser.d.ts +9 -0
- package/dist/css/parser.d.ts.map +1 -0
- package/dist/css/parser.js +188 -0
- package/dist/css/parser.js.map +1 -0
- package/dist/css/tokenizer.d.ts +14 -0
- package/dist/css/tokenizer.d.ts.map +1 -0
- package/dist/css/tokenizer.js +200 -0
- package/dist/css/tokenizer.js.map +1 -0
- package/dist/css/types.d.ts +128 -0
- package/dist/css/types.d.ts.map +1 -0
- package/dist/css/types.js +19 -0
- package/dist/css/types.js.map +1 -0
- package/dist/css/values.d.ts +37 -0
- package/dist/css/values.d.ts.map +1 -0
- package/dist/css/values.js +343 -0
- package/dist/css/values.js.map +1 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +99 -0
- package/dist/environment.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +140 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +17 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +12 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-namespace.d.ts +29 -0
- package/dist/jsx-namespace.d.ts.map +1 -0
- package/dist/jsx-namespace.js +2 -0
- package/dist/jsx-namespace.js.map +1 -0
- package/dist/jsx-runtime.d.ts +22 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +21 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/math.d.ts +50 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +442 -0
- package/dist/math.js.map +1 -0
- package/dist/plugin.d.ts +45 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +172 -0
- package/dist/plugin.js.map +1 -0
- package/dist/props.d.ts +29 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +163 -0
- package/dist/props.js.map +1 -0
- package/dist/scope.d.ts +99 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +228 -0
- package/dist/scope.js.map +1 -0
- package/dist/store.d.ts +18 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +244 -0
- package/dist/store.js.map +1 -0
- package/dist/tokens/index.d.ts +12 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +237 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/tailwind.generated.d.ts +3 -0
- package/dist/tokens/tailwind.generated.d.ts.map +1 -0
- package/dist/tokens/tailwind.generated.js +610 -0
- package/dist/tokens/tailwind.generated.js.map +1 -0
- package/dist/tokens/types.d.ts +95 -0
- package/dist/tokens/types.d.ts.map +1 -0
- package/dist/tokens/types.js +2 -0
- package/dist/tokens/types.js.map +1 -0
- package/dist/transform.d.ts +22 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +79 -0
- package/dist/transform.js.map +1 -0
- package/dist/transition.d.ts +7 -0
- package/dist/transition.d.ts.map +1 -0
- package/dist/transition.js +355 -0
- package/dist/transition.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +109 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +2000 -0
- package/dist/utilities.js.map +1 -0
- package/dist/variables.d.ts +65 -0
- package/dist/variables.d.ts.map +1 -0
- package/dist/variables.js +408 -0
- package/dist/variables.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/withNitroWind.d.ts +11 -0
- package/dist/withNitroWind.d.ts.map +1 -0
- package/dist/withNitroWind.js +27 -0
- package/dist/withNitroWind.js.map +1 -0
- package/package.json +89 -4
- package/src/color.ts +164 -0
- package/src/core.ts +1248 -0
- package/src/css/compile.ts +1198 -0
- package/src/css/declarations.ts +1141 -0
- package/src/css/index.ts +22 -0
- package/src/css/parser.ts +196 -0
- package/src/css/tokenizer.ts +227 -0
- package/src/css/types.ts +178 -0
- package/src/css/values.ts +330 -0
- package/src/environment.ts +112 -0
- package/src/hooks.ts +190 -0
- package/src/index.ts +61 -0
- package/src/jsx-dev-runtime.ts +43 -0
- package/src/jsx-namespace.ts +23 -0
- package/src/jsx-runtime.ts +39 -0
- package/src/math.ts +488 -0
- package/src/plugin.ts +230 -0
- package/src/props.ts +173 -0
- package/src/scope.ts +331 -0
- package/src/store.ts +288 -0
- package/src/tokens/index.ts +252 -0
- package/src/tokens/tailwind.generated.ts +613 -0
- package/src/tokens/types.ts +99 -0
- package/src/transform.ts +115 -0
- package/src/transition.ts +414 -0
- package/src/types.ts +248 -0
- package/src/utilities.ts +1992 -0
- package/src/variables.ts +509 -0
- package/src/version.ts +2 -0
- package/src/withNitroWind.ts +36 -0
package/src/css/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { applyCSSTokenResets, detectCustomClassCycles, parseCSS } from './compile'
|
|
2
|
+
export {
|
|
3
|
+
applyRawDeclaration,
|
|
4
|
+
classifyRawDeclaration,
|
|
5
|
+
isSupportedRawProperty,
|
|
6
|
+
validateRawDeclaration,
|
|
7
|
+
} from './declarations'
|
|
8
|
+
export { parseStylesheet } from './parser'
|
|
9
|
+
export { type CSSToken, type CSSTokenKind, tokenizeCSS } from './tokenizer'
|
|
10
|
+
export * from './types'
|
|
11
|
+
export {
|
|
12
|
+
parseCSSBoxShadows,
|
|
13
|
+
parseCSSBreakpoint,
|
|
14
|
+
parseCSSColor,
|
|
15
|
+
parseCSSDropShadows,
|
|
16
|
+
parseCSSFontWeight,
|
|
17
|
+
parseCSSLength,
|
|
18
|
+
parseCSSLengthOrPercent,
|
|
19
|
+
parseCSSLineHeight,
|
|
20
|
+
parseCSSRatio,
|
|
21
|
+
splitTopLevelCommas,
|
|
22
|
+
} from './values'
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { type CSSToken, tokenizeCSS } from './tokenizer'
|
|
2
|
+
import {
|
|
3
|
+
type CSSAtRuleNode,
|
|
4
|
+
type CSSDeclarationNode,
|
|
5
|
+
type CSSLocation,
|
|
6
|
+
type CSSNode,
|
|
7
|
+
CSSParseError,
|
|
8
|
+
type CSSRuleNode,
|
|
9
|
+
type CSSStylesheetNode,
|
|
10
|
+
} from './types'
|
|
11
|
+
|
|
12
|
+
const IMPORTANT_RE = /\s*!important\s*$/i
|
|
13
|
+
|
|
14
|
+
function joinTokens(tokens: readonly CSSToken[], start: number, end: number): string {
|
|
15
|
+
let out = ''
|
|
16
|
+
for (let i = start; i < end; i++) {
|
|
17
|
+
out += tokens[i]!.value
|
|
18
|
+
}
|
|
19
|
+
return out.trim()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Recursive-descent CSS parser producing a location-annotated AST of at-rules,
|
|
24
|
+
* rules, and declarations. Zero dependencies, Hermes-compatible.
|
|
25
|
+
*/
|
|
26
|
+
export function parseStylesheet(css: string, options?: { readonly filename?: string }): CSSStylesheetNode {
|
|
27
|
+
const file = options?.filename
|
|
28
|
+
const tokens = tokenizeCSS(css, file)
|
|
29
|
+
const len = tokens.length
|
|
30
|
+
let pos = 0
|
|
31
|
+
|
|
32
|
+
const skipTrivia = (): void => {
|
|
33
|
+
while (pos < len && (tokens[pos]!.kind === 'whitespace' || tokens[pos]!.kind === 'semicolon')) {
|
|
34
|
+
pos++
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const parseBlock = (ownerLocation: CSSLocation, expectClose: boolean): CSSNode[] => {
|
|
39
|
+
const nodes: CSSNode[] = []
|
|
40
|
+
while (pos < len) {
|
|
41
|
+
skipTrivia()
|
|
42
|
+
if (pos >= len) break
|
|
43
|
+
const token = tokens[pos]!
|
|
44
|
+
if (token.kind === 'rbrace') {
|
|
45
|
+
if (!expectClose) {
|
|
46
|
+
throw new CSSParseError('Unexpected closing brace "}"', token.location)
|
|
47
|
+
}
|
|
48
|
+
pos++
|
|
49
|
+
return nodes
|
|
50
|
+
}
|
|
51
|
+
if (token.kind === 'at-word') {
|
|
52
|
+
nodes.push(parseAtRule())
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
const node = parseRuleOrDeclaration()
|
|
56
|
+
if (node !== null) nodes.push(node)
|
|
57
|
+
}
|
|
58
|
+
if (expectClose) {
|
|
59
|
+
throw new CSSParseError('Unclosed block "{"', ownerLocation)
|
|
60
|
+
}
|
|
61
|
+
return nodes
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const parseAtRule = (): CSSAtRuleNode => {
|
|
65
|
+
const atToken = tokens[pos++]!
|
|
66
|
+
const preludeStart = pos
|
|
67
|
+
let parenDepth = 0
|
|
68
|
+
let bracketDepth = 0
|
|
69
|
+
|
|
70
|
+
while (pos < len) {
|
|
71
|
+
const token = tokens[pos]!
|
|
72
|
+
if (token.kind === 'lparen') parenDepth++
|
|
73
|
+
else if (token.kind === 'rparen') parenDepth = Math.max(0, parenDepth - 1)
|
|
74
|
+
else if (token.kind === 'lbracket') bracketDepth++
|
|
75
|
+
else if (token.kind === 'rbracket') bracketDepth = Math.max(0, bracketDepth - 1)
|
|
76
|
+
else if (parenDepth === 0 && bracketDepth === 0) {
|
|
77
|
+
if (token.kind === 'semicolon') {
|
|
78
|
+
const prelude = joinTokens(tokens, preludeStart, pos)
|
|
79
|
+
pos++
|
|
80
|
+
return {
|
|
81
|
+
kind: 'at-rule',
|
|
82
|
+
name: atToken.value.toLowerCase(),
|
|
83
|
+
prelude,
|
|
84
|
+
location: atToken.location,
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (token.kind === 'lbrace') {
|
|
88
|
+
const prelude = joinTokens(tokens, preludeStart, pos)
|
|
89
|
+
pos++
|
|
90
|
+
const children = parseBlock(atToken.location, true)
|
|
91
|
+
return {
|
|
92
|
+
kind: 'at-rule',
|
|
93
|
+
name: atToken.value.toLowerCase(),
|
|
94
|
+
prelude,
|
|
95
|
+
nodes: children,
|
|
96
|
+
location: atToken.location,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (token.kind === 'rbrace') {
|
|
100
|
+
const prelude = joinTokens(tokens, preludeStart, pos)
|
|
101
|
+
return {
|
|
102
|
+
kind: 'at-rule',
|
|
103
|
+
name: atToken.value.toLowerCase(),
|
|
104
|
+
prelude,
|
|
105
|
+
location: atToken.location,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
pos++
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const prelude = joinTokens(tokens, preludeStart, pos)
|
|
113
|
+
return {
|
|
114
|
+
kind: 'at-rule',
|
|
115
|
+
name: atToken.value.toLowerCase(),
|
|
116
|
+
prelude,
|
|
117
|
+
location: atToken.location,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const parseRuleOrDeclaration = (): CSSRuleNode | CSSDeclarationNode | null => {
|
|
122
|
+
const startPos = pos
|
|
123
|
+
const startLoc = tokens[startPos]!.location
|
|
124
|
+
let parenDepth = 0
|
|
125
|
+
let bracketDepth = 0
|
|
126
|
+
let firstColon = -1
|
|
127
|
+
let delimiter: 'lbrace' | 'semicolon' | 'rbrace' | 'eof' = 'eof'
|
|
128
|
+
|
|
129
|
+
while (pos < len) {
|
|
130
|
+
const token = tokens[pos]!
|
|
131
|
+
if (token.kind === 'lparen') parenDepth++
|
|
132
|
+
else if (token.kind === 'rparen') parenDepth = Math.max(0, parenDepth - 1)
|
|
133
|
+
else if (token.kind === 'lbracket') bracketDepth++
|
|
134
|
+
else if (token.kind === 'rbracket') bracketDepth = Math.max(0, bracketDepth - 1)
|
|
135
|
+
else if (parenDepth === 0 && bracketDepth === 0) {
|
|
136
|
+
if (token.kind === 'colon' && firstColon === -1) {
|
|
137
|
+
firstColon = pos
|
|
138
|
+
} else if (token.kind === 'lbrace') {
|
|
139
|
+
delimiter = 'lbrace'
|
|
140
|
+
break
|
|
141
|
+
} else if (token.kind === 'semicolon') {
|
|
142
|
+
delimiter = 'semicolon'
|
|
143
|
+
break
|
|
144
|
+
} else if (token.kind === 'rbrace') {
|
|
145
|
+
delimiter = 'rbrace'
|
|
146
|
+
break
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
pos++
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (delimiter === 'lbrace') {
|
|
153
|
+
const selector = joinTokens(tokens, startPos, pos)
|
|
154
|
+
pos++ // consume '{'
|
|
155
|
+
const children = parseBlock(startLoc, true)
|
|
156
|
+
if (!selector) {
|
|
157
|
+
throw new CSSParseError('Expected selector before "{"', startLoc)
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
kind: 'rule',
|
|
161
|
+
selector,
|
|
162
|
+
nodes: children,
|
|
163
|
+
location: startLoc,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const endPos = pos
|
|
168
|
+
if (delimiter === 'semicolon') pos++
|
|
169
|
+
|
|
170
|
+
if (firstColon === -1) {
|
|
171
|
+
const raw = joinTokens(tokens, startPos, endPos)
|
|
172
|
+
if (!raw) return null
|
|
173
|
+
throw new CSSParseError(`Expected ":" in declaration "${raw}"`, startLoc)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const property = joinTokens(tokens, startPos, firstColon)
|
|
177
|
+
if (!property) {
|
|
178
|
+
throw new CSSParseError('Expected property name before ":"', startLoc)
|
|
179
|
+
}
|
|
180
|
+
const rawValue = joinTokens(tokens, firstColon + 1, endPos)
|
|
181
|
+
const important = IMPORTANT_RE.test(rawValue)
|
|
182
|
+
const value = important ? rawValue.replace(IMPORTANT_RE, '').trim() : rawValue
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
kind: 'declaration',
|
|
186
|
+
property,
|
|
187
|
+
value,
|
|
188
|
+
important,
|
|
189
|
+
location: startLoc,
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const rootLoc: CSSLocation = file !== undefined ? { line: 1, column: 1, file } : { line: 1, column: 1 }
|
|
194
|
+
const nodes = parseBlock(rootLoc, false)
|
|
195
|
+
return { kind: 'stylesheet', nodes, location: rootLoc }
|
|
196
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { type CSSLocation, CSSParseError } from './types'
|
|
2
|
+
|
|
3
|
+
export type CSSTokenKind =
|
|
4
|
+
| 'whitespace'
|
|
5
|
+
| 'at-word'
|
|
6
|
+
| 'string'
|
|
7
|
+
| 'lbrace'
|
|
8
|
+
| 'rbrace'
|
|
9
|
+
| 'lparen'
|
|
10
|
+
| 'rparen'
|
|
11
|
+
| 'lbracket'
|
|
12
|
+
| 'rbracket'
|
|
13
|
+
| 'semicolon'
|
|
14
|
+
| 'colon'
|
|
15
|
+
| 'text'
|
|
16
|
+
|
|
17
|
+
export interface CSSToken {
|
|
18
|
+
readonly kind: CSSTokenKind
|
|
19
|
+
readonly value: string
|
|
20
|
+
readonly location: CSSLocation
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function makeLocation(line: number, column: number, file?: string): CSSLocation {
|
|
24
|
+
return file !== undefined ? { line, column, file } : { line, column }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Zero-dependency CSS tokenizer with 1-based line and column tracking.
|
|
29
|
+
* Strips block comments (`/* ... *\/`) into whitespace separators while preserving
|
|
30
|
+
* strings, escapes, and punctuation needed by the recursive-descent parser.
|
|
31
|
+
*/
|
|
32
|
+
export function tokenizeCSS(css: string, file?: string): CSSToken[] {
|
|
33
|
+
const tokens: CSSToken[] = []
|
|
34
|
+
const len = css.length
|
|
35
|
+
let i = 0
|
|
36
|
+
let line = 1
|
|
37
|
+
let column = 1
|
|
38
|
+
|
|
39
|
+
const advance = (): string => {
|
|
40
|
+
const ch = css[i++]!
|
|
41
|
+
if (ch === '\r') {
|
|
42
|
+
if (i < len && css[i] === '\n') i++
|
|
43
|
+
line++
|
|
44
|
+
column = 1
|
|
45
|
+
return '\n'
|
|
46
|
+
}
|
|
47
|
+
if (ch === '\n') {
|
|
48
|
+
line++
|
|
49
|
+
column = 1
|
|
50
|
+
return '\n'
|
|
51
|
+
}
|
|
52
|
+
column++
|
|
53
|
+
return ch
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const pushWhitespace = (location: CSSLocation): void => {
|
|
57
|
+
const prev = tokens[tokens.length - 1]
|
|
58
|
+
if (prev !== undefined && prev.kind === 'whitespace') return
|
|
59
|
+
tokens.push({ kind: 'whitespace', value: ' ', location })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
while (i < len) {
|
|
63
|
+
const ch = css[i]!
|
|
64
|
+
const loc = makeLocation(line, column, file)
|
|
65
|
+
|
|
66
|
+
// Whitespace
|
|
67
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f') {
|
|
68
|
+
while (i < len) {
|
|
69
|
+
const c = css[i]!
|
|
70
|
+
if (c === ' ' || c === '\t' || c === '\n' || c === '\r' || c === '\f') {
|
|
71
|
+
advance()
|
|
72
|
+
} else {
|
|
73
|
+
break
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
pushWhitespace(loc)
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Block comment /* ... */
|
|
81
|
+
if (ch === '/' && i + 1 < len && css[i + 1] === '*') {
|
|
82
|
+
advance()
|
|
83
|
+
advance()
|
|
84
|
+
let closed = false
|
|
85
|
+
while (i < len) {
|
|
86
|
+
if (css[i] === '*' && i + 1 < len && css[i + 1] === '/') {
|
|
87
|
+
advance()
|
|
88
|
+
advance()
|
|
89
|
+
closed = true
|
|
90
|
+
break
|
|
91
|
+
}
|
|
92
|
+
advance()
|
|
93
|
+
}
|
|
94
|
+
if (!closed) {
|
|
95
|
+
throw new CSSParseError('Unterminated CSS comment', loc)
|
|
96
|
+
}
|
|
97
|
+
pushWhitespace(loc)
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Quoted string '...' or "..."
|
|
102
|
+
if (ch === '"' || ch === "'") {
|
|
103
|
+
const quote = advance()
|
|
104
|
+
let raw = quote
|
|
105
|
+
let closed = false
|
|
106
|
+
while (i < len) {
|
|
107
|
+
const c = css[i]!
|
|
108
|
+
if (c === '\\') {
|
|
109
|
+
raw += advance()
|
|
110
|
+
if (i < len) raw += advance()
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
if (c === quote) {
|
|
114
|
+
raw += advance()
|
|
115
|
+
closed = true
|
|
116
|
+
break
|
|
117
|
+
}
|
|
118
|
+
if (c === '\n' || c === '\r') {
|
|
119
|
+
break
|
|
120
|
+
}
|
|
121
|
+
raw += advance()
|
|
122
|
+
}
|
|
123
|
+
if (!closed) {
|
|
124
|
+
throw new CSSParseError('Unterminated CSS string', loc)
|
|
125
|
+
}
|
|
126
|
+
tokens.push({ kind: 'string', value: raw, location: loc })
|
|
127
|
+
continue
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Single-character structural punctuation
|
|
131
|
+
if (ch === '{') {
|
|
132
|
+
advance()
|
|
133
|
+
tokens.push({ kind: 'lbrace', value: '{', location: loc })
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
if (ch === '}') {
|
|
137
|
+
advance()
|
|
138
|
+
tokens.push({ kind: 'rbrace', value: '}', location: loc })
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (ch === '(') {
|
|
142
|
+
advance()
|
|
143
|
+
tokens.push({ kind: 'lparen', value: '(', location: loc })
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
if (ch === ')') {
|
|
147
|
+
advance()
|
|
148
|
+
tokens.push({ kind: 'rparen', value: ')', location: loc })
|
|
149
|
+
continue
|
|
150
|
+
}
|
|
151
|
+
if (ch === '[') {
|
|
152
|
+
advance()
|
|
153
|
+
tokens.push({ kind: 'lbracket', value: '[', location: loc })
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
if (ch === ']') {
|
|
157
|
+
advance()
|
|
158
|
+
tokens.push({ kind: 'rbracket', value: ']', location: loc })
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
if (ch === ';') {
|
|
162
|
+
advance()
|
|
163
|
+
tokens.push({ kind: 'semicolon', value: ';', location: loc })
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
if (ch === ':') {
|
|
167
|
+
advance()
|
|
168
|
+
tokens.push({ kind: 'colon', value: ':', location: loc })
|
|
169
|
+
continue
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// At-keyword `@name`
|
|
173
|
+
if (ch === '@') {
|
|
174
|
+
advance()
|
|
175
|
+
let name = ''
|
|
176
|
+
while (i < len) {
|
|
177
|
+
const c = css[i]!
|
|
178
|
+
if (/[A-Za-z0-9_-]/.test(c)) {
|
|
179
|
+
name += advance()
|
|
180
|
+
} else {
|
|
181
|
+
break
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (name.length === 0) {
|
|
185
|
+
throw new CSSParseError('Expected at-rule name after "@"', loc)
|
|
186
|
+
}
|
|
187
|
+
tokens.push({ kind: 'at-word', value: name, location: loc })
|
|
188
|
+
continue
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Any other contiguous text chunk (identifiers, numbers, units, operators, selectors, escapes)
|
|
192
|
+
let text = ''
|
|
193
|
+
while (i < len) {
|
|
194
|
+
const c = css[i]!
|
|
195
|
+
if (
|
|
196
|
+
c === ' ' ||
|
|
197
|
+
c === '\t' ||
|
|
198
|
+
c === '\n' ||
|
|
199
|
+
c === '\r' ||
|
|
200
|
+
c === '\f' ||
|
|
201
|
+
c === '{' ||
|
|
202
|
+
c === '}' ||
|
|
203
|
+
c === '(' ||
|
|
204
|
+
c === ')' ||
|
|
205
|
+
c === '[' ||
|
|
206
|
+
c === ']' ||
|
|
207
|
+
c === ';' ||
|
|
208
|
+
c === ':' ||
|
|
209
|
+
c === '"' ||
|
|
210
|
+
c === "'" ||
|
|
211
|
+
c === '@' ||
|
|
212
|
+
(c === '/' && i + 1 < len && css[i + 1] === '*')
|
|
213
|
+
) {
|
|
214
|
+
break
|
|
215
|
+
}
|
|
216
|
+
if (c === '\\') {
|
|
217
|
+
text += advance()
|
|
218
|
+
if (i < len) text += advance()
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
text += advance()
|
|
222
|
+
}
|
|
223
|
+
tokens.push({ kind: 'text', value: text, location: loc })
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return tokens
|
|
227
|
+
}
|
package/src/css/types.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { TokensConfig } from '../tokens/types'
|
|
2
|
+
import type { ColorScheme, CSSVariables } from '../types'
|
|
3
|
+
|
|
4
|
+
/** 1-based source location inside a CSS string or file. */
|
|
5
|
+
export interface CSSLocation {
|
|
6
|
+
readonly line: number
|
|
7
|
+
readonly column: number
|
|
8
|
+
readonly file?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type CSSWarningCode =
|
|
12
|
+
| 'unresolved-import'
|
|
13
|
+
| 'unsupported-at-rule'
|
|
14
|
+
| 'unsupported-rule'
|
|
15
|
+
| 'unsupported-declaration'
|
|
16
|
+
| 'invalid-token-value'
|
|
17
|
+
| 'variable-parity'
|
|
18
|
+
| (string & {})
|
|
19
|
+
|
|
20
|
+
/** A non-fatal diagnostic emitted while parsing CSS. */
|
|
21
|
+
export interface CSSWarning {
|
|
22
|
+
readonly code: CSSWarningCode
|
|
23
|
+
readonly message: string
|
|
24
|
+
readonly line: number
|
|
25
|
+
readonly column: number
|
|
26
|
+
readonly file?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Formats a source location as `file:line:column` or `line:column`. */
|
|
30
|
+
export function formatLocation(location: CSSLocation): string {
|
|
31
|
+
const pos = `${location.line}:${location.column}`
|
|
32
|
+
return location.file ? `${location.file}:${pos}` : pos
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Error thrown for CSS syntax errors, circular `@import`s, invalid `color-scheme`, or warnings in `strict` mode. */
|
|
36
|
+
export class CSSParseError extends Error {
|
|
37
|
+
readonly line: number
|
|
38
|
+
readonly column: number
|
|
39
|
+
readonly file?: string
|
|
40
|
+
|
|
41
|
+
constructor(message: string, location: CSSLocation) {
|
|
42
|
+
super(`[nitrowind] ${message} (${formatLocation(location)})`)
|
|
43
|
+
this.name = 'CSSParseError'
|
|
44
|
+
this.line = location.line
|
|
45
|
+
this.column = location.column
|
|
46
|
+
this.file = location.file
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type ResolvedImport = string | { readonly css: string; readonly filename?: string }
|
|
51
|
+
|
|
52
|
+
/** Options for `parseCSS(css, options)`. */
|
|
53
|
+
export interface ParseCSSOptions {
|
|
54
|
+
/** Optional filename or path attached to diagnostics and passed as `from` to `resolveImport`. */
|
|
55
|
+
readonly filename?: string
|
|
56
|
+
/**
|
|
57
|
+
* Resolves relative `@import './...'` specifiers to CSS source text, or `{ css, filename }`
|
|
58
|
+
* for nested relative imports.
|
|
59
|
+
*/
|
|
60
|
+
readonly resolveImport?: (specifier: string, from?: string) => ResolvedImport
|
|
61
|
+
/** Called for every non-fatal diagnostic warning produced during parsing. */
|
|
62
|
+
readonly onWarning?: (warning: CSSWarning) => void
|
|
63
|
+
/** When `true`, warnings throw a `CSSParseError` instead of continuing. Defaults to `false`. */
|
|
64
|
+
readonly strict?: boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type CSSTokenNamespace =
|
|
68
|
+
| 'colors'
|
|
69
|
+
| 'fontSize'
|
|
70
|
+
| 'fontWeight'
|
|
71
|
+
| 'leading'
|
|
72
|
+
| 'radius'
|
|
73
|
+
| 'shadow'
|
|
74
|
+
| 'insetShadow'
|
|
75
|
+
| 'dropShadow'
|
|
76
|
+
| 'container'
|
|
77
|
+
| 'aspect'
|
|
78
|
+
| 'breakpoints'
|
|
79
|
+
| 'spacingScale'
|
|
80
|
+
|
|
81
|
+
/** Records `--<ns>-*: initial;` and `--<ns>-<key>: initial;` resets from `@theme` blocks. */
|
|
82
|
+
export interface CSSTokenResets {
|
|
83
|
+
/** Whole namespaces cleared via `--<ns>-*: initial;` or `--*: initial;`. */
|
|
84
|
+
readonly namespaces?: readonly CSSTokenNamespace[]
|
|
85
|
+
/** Individual keys cleared per namespace via `--<ns>-<key>: initial;`. */
|
|
86
|
+
readonly keys?: Readonly<Partial<Record<CSSTokenNamespace, readonly string[]>>>
|
|
87
|
+
/** Prefix wildcards cleared per namespace via `--<ns>-<prefix>-*: initial;`. */
|
|
88
|
+
readonly prefixes?: Readonly<Partial<Record<CSSTokenNamespace, readonly string[]>>>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A theme block parsed from `@variant <name> { ... }`. */
|
|
92
|
+
export interface CSSThemeEntry {
|
|
93
|
+
readonly colorScheme?: ColorScheme
|
|
94
|
+
readonly variables: Readonly<CSSVariables>
|
|
95
|
+
/** Source location of `@variant <name>`, used to report missing `color-scheme` errors. */
|
|
96
|
+
readonly location?: CSSLocation
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Precedence rank of a CSS custom class: `'component'` loses to utilities; `'utility'` uses normal last-wins order. */
|
|
100
|
+
export type CSSCustomClassRank = 'component' | 'utility'
|
|
101
|
+
|
|
102
|
+
/** A single source-order entry inside a CSS custom class rule. */
|
|
103
|
+
export type CSSCustomClassEntry =
|
|
104
|
+
| {
|
|
105
|
+
readonly kind: 'apply'
|
|
106
|
+
readonly classes: readonly string[]
|
|
107
|
+
}
|
|
108
|
+
| {
|
|
109
|
+
readonly kind: 'declaration'
|
|
110
|
+
readonly property: string
|
|
111
|
+
readonly value: string
|
|
112
|
+
}
|
|
113
|
+
| {
|
|
114
|
+
readonly kind: 'variant'
|
|
115
|
+
readonly variant: string
|
|
116
|
+
readonly entries: readonly CSSCustomClassEntry[]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Plain JSON-serializable definition of a CSS custom class (`.class`, `@layer components`, or `@utility`). */
|
|
120
|
+
export interface CSSCustomClassDefinition {
|
|
121
|
+
readonly rank: CSSCustomClassRank
|
|
122
|
+
readonly entries: readonly CSSCustomClassEntry[]
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Plain JSON-serializable NitroWind configuration compiled from CSS by `parseCSS`
|
|
127
|
+
* or at build time by `nitrowind/plugin`.
|
|
128
|
+
*/
|
|
129
|
+
export interface NitroWindCSSConfig {
|
|
130
|
+
/** Recognized preset imports (`'tailwindcss'`, `'nitrowind'`, `'uniwind'`) and resolved relative imports. */
|
|
131
|
+
readonly imports: readonly string[]
|
|
132
|
+
/** Design tokens extracted from `@theme`, `@theme inline`, and Tailwind-prefixed CSS variables. */
|
|
133
|
+
readonly tokens: TokensConfig
|
|
134
|
+
/** Token resets (`initial`) declared in `@theme` blocks, applied to defaults before CSS and JS tokens. */
|
|
135
|
+
readonly tokenResets?: CSSTokenResets
|
|
136
|
+
/** Shared CSS variables declared in `@theme` and `:root`. */
|
|
137
|
+
readonly variables: Readonly<CSSVariables>
|
|
138
|
+
/** Per-theme CSS variable layers declared in `@variant <theme>` blocks. */
|
|
139
|
+
readonly themes: Readonly<Record<string, CSSThemeEntry>>
|
|
140
|
+
/** Custom classes parsed from `.class`, `@layer components`, and `@utility` rules. */
|
|
141
|
+
readonly customClasses?: Readonly<Record<string, CSSCustomClassDefinition>>
|
|
142
|
+
/** Non-fatal warnings emitted during parsing. */
|
|
143
|
+
readonly warnings: readonly CSSWarning[]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
// AST nodes (shared with custom-class compilation and tooling)
|
|
148
|
+
|
|
149
|
+
export type CSSNode = CSSAtRuleNode | CSSRuleNode | CSSDeclarationNode
|
|
150
|
+
|
|
151
|
+
export interface CSSStylesheetNode {
|
|
152
|
+
readonly kind: 'stylesheet'
|
|
153
|
+
readonly nodes: readonly CSSNode[]
|
|
154
|
+
readonly location: CSSLocation
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface CSSAtRuleNode {
|
|
158
|
+
readonly kind: 'at-rule'
|
|
159
|
+
readonly name: string
|
|
160
|
+
readonly prelude: string
|
|
161
|
+
readonly nodes?: readonly CSSNode[]
|
|
162
|
+
readonly location: CSSLocation
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface CSSRuleNode {
|
|
166
|
+
readonly kind: 'rule'
|
|
167
|
+
readonly selector: string
|
|
168
|
+
readonly nodes: readonly CSSNode[]
|
|
169
|
+
readonly location: CSSLocation
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface CSSDeclarationNode {
|
|
173
|
+
readonly kind: 'declaration'
|
|
174
|
+
readonly property: string
|
|
175
|
+
readonly value: string
|
|
176
|
+
readonly important: boolean
|
|
177
|
+
readonly location: CSSLocation
|
|
178
|
+
}
|