@tbela99/css-parser 0.0.1 → 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
 
@@ -12,11 +12,15 @@ $ npm install @tbela99/css-parser
12
12
 
13
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)
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)
17
17
  - automatically generate nested css rules
18
+ - generate sourcemap
18
19
  - compute css shorthands. see the list below
19
- - expand nested css
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
26
  ## Transform
@@ -25,9 +29,9 @@ Parse and render css in a single pass.
25
29
 
26
30
  ### Usage
27
31
 
28
- ```javascript
32
+ ```typescript
29
33
 
30
- transform(css, transformOptions = {})
34
+ transform(css, transformOptions: TransformOptions = {}): TransformResult
31
35
  ```
32
36
 
33
37
  ### Example
@@ -36,7 +40,7 @@ transform(css, transformOptions = {})
36
40
 
37
41
  import {transform} from '@tbela99/css-parser';
38
42
 
39
- 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'});
40
44
  ```
41
45
 
42
46
  ### TransformOptions
@@ -45,23 +49,35 @@ Include ParseOptions and RenderOptions
45
49
 
46
50
  #### ParseOptions
47
51
 
48
- - src: string, optional. css file location to be used with sourcemap.
49
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.
50
55
  - nestingRules: boolean, optional. automatically generated nested rules.
51
- - removeEmpty: boolean, remove empty nodes from the ast.
52
- - 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.
53
61
  - cwd: string, optional. the current working directory. when specified url() are resolved using this value
54
- - resolveImport: boolean, optional. replace @import node by the content of the referenced stylesheet.
55
- - 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.
56
67
 
57
68
  #### RenderOptions
69
+
58
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.
59
75
  - indent: string, optional. css indention string. uses space character by default.
60
- - newLine: string, new line character.
76
+ - newLine: string, optional. new line character.
61
77
  - removeComments: boolean, remove comments in generated css.
62
- - preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
63
78
  - colorConvert: boolean, convert colors to hex.
64
-
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.
65
81
 
66
82
  ## Parsing
67
83
 
@@ -84,7 +100,7 @@ const {ast, errors, stats} = await parse(css);
84
100
  ### Usage
85
101
 
86
102
  ```javascript
87
- render(ast, RenderOptions = {});
103
+ doRender(ast, RenderOptions = {});
88
104
  ```
89
105
 
90
106
  ### Example
@@ -163,34 +179,27 @@ Single JavaScript file
163
179
 
164
180
  CSS
165
181
 
166
- ```css
182
+ ```javascript
183
+ const {parse, render} = require("@tbela99/css-parser/cjs");
167
184
 
185
+ const css = `
168
186
  table.colortable td {
169
- text-align:center;
187
+ text-align:center;
170
188
  }
171
189
  table.colortable td.c {
172
- text-transform:uppercase;
190
+ text-transform:uppercase;
173
191
  }
174
192
  table.colortable td:first-child, table.colortable td:first-child+td {
175
- border:1px solid black;
193
+ border:1px solid black;
176
194
  }
177
195
  table.colortable th {
178
- text-align:center;
179
- background:black;
180
- color:white;
196
+ text-align:center;
197
+ background:black;
198
+ color:white;
181
199
  }
182
- ```
200
+ `;
183
201
 
184
- Javascript
185
- ```javascript
186
- import {parse, render} from '@tbela99/css-parser';
187
-
188
-
189
- const options = {minify: true, nestingRules: true};
190
-
191
- const {code} = await parse(css, options).then(result => render(result.ast, {minify: false}));
192
- //
193
- console.debug(code);
202
+ const result = await parse(css, {nestingRules:true}).then(result => render(result.ast, {minify:false}).code);
194
203
  ```
195
204
 
196
205
  Result
@@ -270,6 +279,69 @@ table.colortable th {
270
279
  }
271
280
  ```
272
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
+
273
345
  ## AST
274
346
 
275
347
  ### Comment
@@ -300,8 +372,14 @@ table.colortable th {
300
372
  - typ: string 'Stylesheet'
301
373
  - chi: array of children
302
374
 
375
+ ## Sourcemap
376
+
377
+ - [x] sourcemap generation
378
+
303
379
  ## Minification
304
380
 
381
+ - [x] reduce calc()
382
+ - [x] inline css variables
305
383
  - [x] merge identical rules
306
384
  - [x] merge adjacent rules
307
385
  - [x] minify colors
@@ -311,12 +389,11 @@ table.colortable th {
311
389
  - [x] conditionally unwrap :is()
312
390
  - [x] automatic css nesting
313
391
  - [x] automatically wrap selectors using :is()
314
- - [x] multi-level shorthand properties (border - [border-width, border-color, border-style, etc.]) https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties
315
392
  - [x] avoid reparsing (declarations, selectors, at-rule)
316
393
  - [x] node and browser versions
317
394
  - [x] decode and replace utf-8 escape sequence
318
395
 
319
- ## Computed shorthands
396
+ ## Computed shorthands properties
320
397
  - [x] background
321
398
  - [x] border
322
399
  - [x] border-bottom
@@ -338,3 +415,7 @@ table.colortable th {
338
415
  ## Performance
339
416
 
340
417
  - [x] flatten @import
418
+
419
+ ---
420
+
421
+ Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license