@tbela99/css-parser 0.0.1-rc7 → 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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # css-parser
4
4
 
5
- CSS parser for node and the browser
5
+ CSS parser and minifier for node and the browser
6
6
 
7
7
  ## Installation
8
8
 
@@ -10,28 +10,28 @@ CSS parser for node and the browser
10
10
  $ npm install @tbela99/css-parser
11
11
  ```
12
12
 
13
- ### Features
13
+ ## Features
14
14
 
15
- - fault tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
16
- - efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
17
- - replace @import at-rules with actual css content of the imported rule
15
+ - fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
16
+ - efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html), see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
18
17
  - automatically generate nested css rules
19
- - expand nested css
18
+ - generate sourcemap
19
+ - compute css shorthands. see the list below
20
+ - compute calc() expression
21
+ - nested css expansion
22
+ - remove duplicate properties
23
+ - flatten @import rules
20
24
  - works the same way in node and web browser
21
25
 
22
- ### Performance
23
-
24
- - flatten @import
25
-
26
26
  ## Transform
27
27
 
28
28
  Parse and render css in a single pass.
29
29
 
30
30
  ### Usage
31
31
 
32
- ```javascript
32
+ ```typescript
33
33
 
34
- transform(css, transformOptions = {})
34
+ transform(css, transformOptions: TransformOptions = {}): TransformResult
35
35
  ```
36
36
 
37
37
  ### Example
@@ -40,7 +40,7 @@ transform(css, transformOptions = {})
40
40
 
41
41
  import {transform} from '@tbela99/css-parser';
42
42
 
43
- const {ast, code, errors, stats} = await transform(css, {minify: true, resolveImport: true, cwd: 'files/css'});
43
+ const {ast, code, map, errors, stats} = await transform(css, {minify: true, resolveImport: true, cwd: 'files/css'});
44
44
  ```
45
45
 
46
46
  ### TransformOptions
@@ -49,23 +49,35 @@ Include ParseOptions and RenderOptions
49
49
 
50
50
  #### ParseOptions
51
51
 
52
- - src: string, optional. css file location to be used with sourcemap.
53
52
  - minify: boolean, optional. default to _true_. optimize ast.
53
+ - src: string, optional. original css file location to be used with sourcemap.
54
+ - sourcemap: boolean, optional. preserve node location data.
54
55
  - nestingRules: boolean, optional. automatically generated nested rules.
55
- - removeEmpty: boolean, remove empty nodes from the ast.
56
- - location: boolean, optional. includes node location in the ast, required for sourcemap generation.
56
+ - expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules to false.
57
+ - removeCharset: boolean, optional. remove @charset.
58
+ - removeEmpty: boolean, optional. remove empty rule lists from the ast.
59
+ - resolveUrls: boolean, optional. resolve css 'url()' according to the parameters 'src' and 'cwd'
60
+ - resolveImport: boolean, optional. replace @import rule by the content of its referenced stylesheet.
57
61
  - cwd: string, optional. the current working directory. when specified url() are resolved using this value
58
- - resolveImport: boolean, optional. replace @import node by the content of the referenced stylesheet.
59
- - resolveUrls: boolean, optional. resolve css url() according to the parameters 'src' and 'cwd'
62
+ - removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
63
+ - computeShorthand: boolean, optional. compute shorthand properties.
64
+ - inlineCssVariables: boolean, optional. replace css variables with their current value.
65
+ - computeCalcExpression: boolean, optional. evaluate calc() expression
66
+ - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} rule.
60
67
 
61
68
  #### RenderOptions
69
+
62
70
  - minify: boolean, optional. default to _true_. minify css output.
71
+ - expandNestingRules: boolean, optional. expand nesting rules.
72
+ - sourcemap: boolean, optional. generate sourcemap
73
+ - preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
74
+ - sourcemap: boolean, optional. generate sourcemap.
63
75
  - indent: string, optional. css indention string. uses space character by default.
64
- - newLine: string, new line character.
76
+ - newLine: string, optional. new line character.
65
77
  - removeComments: boolean, remove comments in generated css.
66
- - preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
67
78
  - colorConvert: boolean, convert colors to hex.
68
-
79
+ - output: string, optional. file where to store css. url() are resolved according to the specified value. no file is created though.
80
+ - cwd: string, optional. value used as current working directory. when output is not provided, urls are resolved according to this value.
69
81
 
70
82
  ## Parsing
71
83
 
@@ -88,7 +100,7 @@ const {ast, errors, stats} = await parse(css);
88
100
  ### Usage
89
101
 
90
102
  ```javascript
91
- render(ast, RenderOptions = {});
103
+ doRender(ast, RenderOptions = {});
92
104
  ```
93
105
 
94
106
  ### Example
@@ -131,7 +143,7 @@ import as a CommonJS module
131
143
 
132
144
  ```javascript
133
145
 
134
- import {transform} from '@tbela99/css-parser/cjs';
146
+ const {transform} = require('@tbela99/css-parser/cjs');
135
147
 
136
148
  // ...
137
149
  ```
@@ -167,34 +179,27 @@ Single JavaScript file
167
179
 
168
180
  CSS
169
181
 
170
- ```css
182
+ ```javascript
183
+ const {parse, render} = require("@tbela99/css-parser/cjs");
171
184
 
185
+ const css = `
172
186
  table.colortable td {
173
- text-align:center;
187
+ text-align:center;
174
188
  }
175
189
  table.colortable td.c {
176
- text-transform:uppercase;
190
+ text-transform:uppercase;
177
191
  }
178
192
  table.colortable td:first-child, table.colortable td:first-child+td {
179
- border:1px solid black;
193
+ border:1px solid black;
180
194
  }
181
195
  table.colortable th {
182
- text-align:center;
183
- background:black;
184
- color:white;
196
+ text-align:center;
197
+ background:black;
198
+ color:white;
185
199
  }
186
- ```
187
-
188
- Javascript
189
- ```javascript
190
- import {parse, render} from '@tbela99/css-parser';
191
-
200
+ `;
192
201
 
193
- const options = {minify: true, nestingRules: true};
194
-
195
- const {code} = await parse(css, options).then(result => render(result.ast, {minify: false}));
196
- //
197
- console.debug(code);
202
+ const result = await parse(css, {nestingRules:true}).then(result => render(result.ast, {minify:false}).code);
198
203
  ```
199
204
 
200
205
  Result
@@ -274,6 +279,69 @@ table.colortable th {
274
279
  }
275
280
  ```
276
281
 
282
+ ### Example 3
283
+
284
+ ### Calc() resolution
285
+
286
+ ```javascript
287
+
288
+ import {parse, render} from '@tbela99/css-parser';
289
+
290
+ const css = `
291
+
292
+ .foo-bar {
293
+ width: calc(100px * 2);
294
+ height: calc(((75.37% - 63.5px) - 900px) + (2 * 100px));
295
+ max-width: calc(3.5rem + calc(var(--bs-border-width) * 2));
296
+ }
297
+ `;
298
+
299
+ const prettyPrint = await parse(css).then(result => render(result.ast, {minify: false}).code);
300
+
301
+ ```
302
+ result
303
+
304
+ ```css
305
+ .foo-bar {
306
+ width: 200px;
307
+ height: calc(75.37% - 763.5px);
308
+ max-width: calc(3.5rem + var(--bs-border-width)*2)
309
+ }
310
+ ```
311
+
312
+ ### Example 4
313
+
314
+ ### CSS variable inlining
315
+
316
+ ```javascript
317
+
318
+ import {parse, render} from '@tbela99/css-parser';
319
+
320
+ const css = `
321
+
322
+ :root {
323
+
324
+ --preferred-width: 20px;
325
+ }
326
+ .foo-bar {
327
+
328
+ width: calc(calc(var(--preferred-width) + 1px) / 3 + 5px);
329
+ height: calc(100% / 4);}
330
+ `
331
+
332
+ const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
333
+
334
+ ```
335
+ result
336
+
337
+ ```css
338
+ .foo-bar {
339
+ width: 12px;
340
+ height: 25%
341
+ }
342
+
343
+ ```
344
+
277
345
  ## AST
278
346
 
279
347
  ### Comment
@@ -303,3 +371,51 @@ table.colortable th {
303
371
 
304
372
  - typ: string 'Stylesheet'
305
373
  - chi: array of children
374
+
375
+ ## Sourcemap
376
+
377
+ - [x] sourcemap generation
378
+
379
+ ## Minification
380
+
381
+ - [x] reduce calc()
382
+ - [x] inline css variables
383
+ - [x] merge identical rules
384
+ - [x] merge adjacent rules
385
+ - [x] minify colors
386
+ - [x] minify numbers and Dimensions tokens
387
+ - [x] compute shorthand: see the list below
388
+ - [x] remove redundant declarations
389
+ - [x] conditionally unwrap :is()
390
+ - [x] automatic css nesting
391
+ - [x] automatically wrap selectors using :is()
392
+ - [x] avoid reparsing (declarations, selectors, at-rule)
393
+ - [x] node and browser versions
394
+ - [x] decode and replace utf-8 escape sequence
395
+
396
+ ## Computed shorthands properties
397
+ - [x] background
398
+ - [x] border
399
+ - [x] border-bottom
400
+ - [x] border-color
401
+ - [x] border-left
402
+ - [x] border-radius
403
+ - [x] border-right
404
+ - [x] border-style
405
+ - [x] border-top
406
+ - [x] border-width
407
+ - [x] font
408
+ - [x] inset
409
+ - [x] margin
410
+ - [x] outline
411
+ - [x] overflow
412
+ - [x] padding
413
+ - [x] text-decoration
414
+
415
+ ## Performance
416
+
417
+ - [x] flatten @import
418
+
419
+ ---
420
+
421
+ Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license