@tbela99/css-parser 1.3.1 → 1.3.3
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 +9 -0
- package/README.md +41 -5
- package/dist/index-umd-web.js +1272 -961
- package/dist/index.cjs +1194 -920
- package/dist/index.d.ts +921 -264
- package/dist/lib/ast/features/calc.js +5 -6
- package/dist/lib/ast/features/inlinecssvariables.js +5 -6
- package/dist/lib/ast/features/prefix.js +4 -14
- package/dist/lib/ast/features/shorthand.js +4 -6
- package/dist/lib/ast/features/transform.js +5 -6
- package/dist/lib/ast/features/type.js +4 -2
- package/dist/lib/ast/minify.js +55 -114
- package/dist/lib/ast/types.js +2 -2
- package/dist/lib/ast/walk.js +149 -59
- package/dist/lib/fs/resolve.js +6 -4
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/parse.js +297 -57
- package/dist/lib/parser/tokenize.js +1 -1
- package/dist/lib/renderer/render.js +5 -5
- package/dist/lib/syntax/color/cmyk.js +6 -3
- package/dist/lib/syntax/color/color-mix.js +2 -3
- package/dist/lib/syntax/color/color.js +28 -6
- package/dist/lib/syntax/color/hex.js +3 -0
- package/dist/lib/syntax/color/hsl.js +18 -7
- package/dist/lib/syntax/color/hwb.js +3 -3
- package/dist/lib/syntax/color/lab.js +4 -4
- package/dist/lib/syntax/color/lch.js +7 -4
- package/dist/lib/syntax/color/oklab.js +4 -4
- package/dist/lib/syntax/color/oklch.js +18 -6
- package/dist/lib/syntax/color/relativecolor.js +9 -56
- package/dist/lib/syntax/color/srgb.js +1 -1
- package/dist/lib/validation/config.json.js +4 -12
- package/dist/node.js +53 -31
- package/dist/web.js +76 -17
- package/package.json +2 -3
package/dist/web.js
CHANGED
|
@@ -21,17 +21,13 @@ import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
|
|
|
21
21
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @module web
|
|
26
|
-
*/
|
|
27
|
-
/**
|
|
28
|
-
* load file or url as stream
|
|
24
|
+
* default file or url loader
|
|
29
25
|
* @param url
|
|
30
26
|
* @param currentFile
|
|
31
27
|
*
|
|
32
28
|
* @private
|
|
33
29
|
*/
|
|
34
|
-
async function
|
|
30
|
+
async function load(url, currentFile = '.') {
|
|
35
31
|
let t;
|
|
36
32
|
if (matchUrl.test(url)) {
|
|
37
33
|
t = new URL(url);
|
|
@@ -53,13 +49,35 @@ async function getStream(url, currentFile = '.') {
|
|
|
53
49
|
});
|
|
54
50
|
}
|
|
55
51
|
/**
|
|
56
|
-
* render ast
|
|
52
|
+
* render the ast tree
|
|
57
53
|
* @param data
|
|
58
54
|
* @param options
|
|
55
|
+
*
|
|
56
|
+
* Example:
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
*
|
|
60
|
+
* import {render, ColorType} from '@tbela99/css-parser';
|
|
61
|
+
*
|
|
62
|
+
* const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
|
|
63
|
+
* const parseResult = await parse(css);
|
|
64
|
+
*
|
|
65
|
+
* let renderResult = render(parseResult.ast);
|
|
66
|
+
* console.log(result.code);
|
|
67
|
+
*
|
|
68
|
+
* // body{color:red}
|
|
69
|
+
*
|
|
70
|
+
*
|
|
71
|
+
* renderResult = render(parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
|
|
72
|
+
* console.log(renderResult.code);
|
|
73
|
+
*
|
|
74
|
+
* // body {
|
|
75
|
+
* // color: color(srgb 1 0 0)
|
|
76
|
+
* // }
|
|
77
|
+
* ```
|
|
59
78
|
*/
|
|
60
79
|
function render(data, options = {}) {
|
|
61
80
|
return doRender(data, Object.assign(options, {
|
|
62
|
-
getStream,
|
|
63
81
|
resolve,
|
|
64
82
|
dirname,
|
|
65
83
|
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
@@ -69,26 +87,66 @@ function render(data, options = {}) {
|
|
|
69
87
|
* parse css file
|
|
70
88
|
* @param file url or path
|
|
71
89
|
* @param options
|
|
90
|
+
*
|
|
91
|
+
* @throws Error file not found
|
|
92
|
+
*
|
|
93
|
+
* Example:
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
*
|
|
97
|
+
* import {parseFile} from '@tbela99/css-parser/web';
|
|
98
|
+
*
|
|
99
|
+
* // remote file
|
|
100
|
+
* let result = await parseFile('https://docs.deno.com/styles.css');
|
|
101
|
+
* console.log(result.ast);
|
|
102
|
+
*
|
|
103
|
+
* // local file
|
|
104
|
+
* result = await parseFile('./css/styles.css');
|
|
105
|
+
* console.log(result.ast);
|
|
106
|
+
* ```
|
|
72
107
|
*/
|
|
73
108
|
async function parseFile(file, options = {}) {
|
|
74
|
-
return
|
|
109
|
+
return load(file).then(stream => parse(stream, { src: file, ...options }));
|
|
75
110
|
}
|
|
76
111
|
/**
|
|
77
112
|
* parse css
|
|
78
113
|
* @param stream
|
|
79
|
-
* @param
|
|
114
|
+
* @param options
|
|
115
|
+
*
|
|
116
|
+
* Example:
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
*
|
|
120
|
+
* import {parse} from '@tbela99/css-parser/web';
|
|
121
|
+
*
|
|
122
|
+
* // css string
|
|
123
|
+
* const result = await parse(css);
|
|
124
|
+
* console.log(result.ast);
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* Example using fetch and readable stream
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
*
|
|
131
|
+
* import {parse} from '@tbela99/css-parser/web';
|
|
132
|
+
*
|
|
133
|
+
* const response = await fetch('https://docs.deno.com/styles.css');
|
|
134
|
+
* const result = await parse(response.body, {beautify: true});
|
|
135
|
+
*
|
|
136
|
+
* console.log(result.ast);
|
|
137
|
+
* ```
|
|
80
138
|
*/
|
|
81
|
-
async function parse(stream,
|
|
139
|
+
async function parse(stream, options = {}) {
|
|
82
140
|
return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
|
|
83
141
|
stream,
|
|
84
142
|
buffer: '',
|
|
85
143
|
position: { ind: 0, lin: 1, col: 1 },
|
|
86
144
|
currentPosition: { ind: -1, lin: 1, col: 0 }
|
|
87
|
-
}), Object.assign(
|
|
88
|
-
|
|
145
|
+
}), Object.assign(options, {
|
|
146
|
+
load,
|
|
89
147
|
resolve,
|
|
90
148
|
dirname,
|
|
91
|
-
cwd:
|
|
149
|
+
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
92
150
|
}));
|
|
93
151
|
}
|
|
94
152
|
/**
|
|
@@ -112,7 +170,7 @@ async function parse(stream, opt = {}) {
|
|
|
112
170
|
* ```
|
|
113
171
|
*/
|
|
114
172
|
async function transformFile(file, options = {}) {
|
|
115
|
-
return
|
|
173
|
+
return load(file).then(stream => transform(stream, { src: file, ...options }));
|
|
116
174
|
}
|
|
117
175
|
/**
|
|
118
176
|
* transform css
|
|
@@ -140,7 +198,8 @@ async function transform(css, options = {}) {
|
|
|
140
198
|
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
141
199
|
const startTime = performance.now();
|
|
142
200
|
return parse(css, options).then((parseResult) => {
|
|
143
|
-
|
|
201
|
+
// ast already expanded by parse
|
|
202
|
+
const rendered = render(parseResult.ast, { ...options, expandNestingRules: false });
|
|
144
203
|
return {
|
|
145
204
|
...parseResult,
|
|
146
205
|
...rendered,
|
|
@@ -155,4 +214,4 @@ async function transform(css, options = {}) {
|
|
|
155
214
|
});
|
|
156
215
|
}
|
|
157
216
|
|
|
158
|
-
export { dirname,
|
|
217
|
+
export { dirname, load, parse, parseFile, render, resolve, transform, transformFile };
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
|
-
"description": "CSS parser for node and the browser",
|
|
4
|
-
"version": "v1.3.
|
|
3
|
+
"description": "CSS parser, minifier and validator for node and the browser",
|
|
4
|
+
"version": "v1.3.3",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
8
|
-
"./umd": "./dist/index-umd-web.js",
|
|
9
8
|
"./web": "./dist/web.js",
|
|
10
9
|
"./cjs": "./dist/index.cjs"
|
|
11
10
|
},
|