@tbela99/css-parser 1.4.1 → 1.4.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.
Files changed (216) hide show
  1. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
  2. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
  3. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
  4. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
  5. package/.repl_history +4 -0
  6. package/CHANGELOG.md +168 -4
  7. package/README.md +82 -0
  8. package/badges/coverage.svg +20 -0
  9. package/deno.lock +2861 -0
  10. package/dist/config.json.js +33 -1
  11. package/dist/index-umd-web.js +24393 -18479
  12. package/dist/index.cjs +27430 -21519
  13. package/dist/index.d.ts +1403 -933
  14. package/dist/lib/ast/clone.d.ts +10 -0
  15. package/dist/lib/ast/clone.js +45 -0
  16. package/dist/lib/ast/expand.d.ts +14 -0
  17. package/dist/lib/ast/expand.js +89 -64
  18. package/dist/lib/ast/features/calc.d.ts +10 -0
  19. package/dist/lib/ast/features/calc.js +62 -24
  20. package/dist/lib/ast/features/if.d.ts +10 -0
  21. package/dist/lib/ast/features/if.js +215 -0
  22. package/dist/lib/ast/features/index.d.ts +6 -0
  23. package/dist/lib/ast/features/index.js +1 -0
  24. package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
  25. package/dist/lib/ast/features/inlinecssvariables.js +32 -27
  26. package/dist/lib/ast/features/prefix.d.ts +8 -0
  27. package/dist/lib/ast/features/prefix.js +68 -43
  28. package/dist/lib/ast/features/shorthand.d.ts +12 -0
  29. package/dist/lib/ast/features/shorthand.js +6 -9
  30. package/dist/lib/ast/features/transform.d.ts +10 -0
  31. package/dist/lib/ast/features/transform.js +9 -13
  32. package/dist/lib/ast/features/type.d.ts +15 -0
  33. package/dist/lib/ast/find.d.ts +165 -0
  34. package/dist/lib/ast/find.js +175 -0
  35. package/dist/lib/ast/math/expression.d.ts +18 -0
  36. package/dist/lib/ast/math/expression.js +140 -98
  37. package/dist/lib/ast/math/math.d.ts +6 -0
  38. package/dist/lib/ast/math/math.js +30 -41
  39. package/dist/lib/ast/minify.d.ts +19 -0
  40. package/dist/lib/ast/minify.js +543 -215
  41. package/dist/lib/ast/transform/compute.d.ts +8 -0
  42. package/dist/lib/ast/transform/compute.js +82 -69
  43. package/dist/lib/ast/transform/matrix.d.ts +22 -0
  44. package/dist/lib/ast/transform/matrix.js +12 -26
  45. package/dist/lib/ast/transform/minify.d.ts +5 -0
  46. package/dist/lib/ast/transform/minify.js +20 -20
  47. package/dist/lib/ast/transform/perspective.d.ts +3 -0
  48. package/dist/lib/ast/transform/perspective.js +1 -1
  49. package/dist/lib/ast/transform/rotate.d.ts +12 -0
  50. package/dist/lib/ast/transform/rotate.js +1 -1
  51. package/dist/lib/ast/transform/scale.d.ts +6 -0
  52. package/dist/lib/ast/transform/scale.js +1 -1
  53. package/dist/lib/ast/transform/skew.d.ts +4 -0
  54. package/dist/lib/ast/transform/skew.js +1 -1
  55. package/dist/lib/ast/transform/translate.d.ts +6 -0
  56. package/dist/lib/ast/transform/translate.js +1 -1
  57. package/dist/lib/ast/transform/utils.d.ts +9 -0
  58. package/dist/lib/ast/types.d.ts +903 -0
  59. package/dist/lib/ast/types.js +277 -23
  60. package/dist/lib/ast/walk.d.ts +162 -0
  61. package/dist/lib/ast/walk.js +116 -60
  62. package/dist/lib/fs/resolve.d.ts +20 -0
  63. package/dist/lib/fs/resolve.js +37 -45
  64. package/dist/lib/parser/declaration/list.d.ts +16 -0
  65. package/dist/lib/parser/declaration/list.js +26 -24
  66. package/dist/lib/parser/declaration/map.d.ts +15 -0
  67. package/dist/lib/parser/declaration/map.js +140 -95
  68. package/dist/lib/parser/declaration/set.d.ts +9 -0
  69. package/dist/lib/parser/declaration/set.js +30 -25
  70. package/dist/lib/parser/node.d.ts +7 -0
  71. package/dist/lib/parser/parse.d.ts +107 -0
  72. package/dist/lib/parser/parse.js +1454 -1445
  73. package/dist/lib/parser/tokenize.d.ts +57 -0
  74. package/dist/lib/parser/tokenize.js +557 -404
  75. package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
  76. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  77. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
  78. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  79. package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
  80. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  81. package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
  82. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  83. package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
  84. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  85. package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
  86. package/dist/lib/parser/utils/at-rule-page.js +28 -0
  87. package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
  88. package/dist/lib/parser/utils/at-rule-support.js +366 -0
  89. package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
  90. package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
  91. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  92. package/dist/lib/parser/utils/at-rule.d.ts +13 -0
  93. package/dist/lib/parser/utils/at-rule.js +37 -0
  94. package/dist/lib/parser/utils/cache.d.ts +6 -0
  95. package/dist/lib/parser/utils/cache.js +19 -0
  96. package/dist/lib/parser/utils/config.d.ts +2 -0
  97. package/dist/lib/parser/utils/config.js +1 -0
  98. package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
  99. package/dist/lib/parser/utils/declaration.d.ts +18 -0
  100. package/dist/lib/parser/utils/declaration.js +569 -91
  101. package/dist/lib/parser/utils/eq.d.ts +1 -0
  102. package/dist/lib/parser/utils/hash.d.ts +21 -0
  103. package/dist/lib/parser/utils/hash.js +1 -1
  104. package/dist/lib/parser/utils/selector.d.ts +5 -0
  105. package/dist/lib/parser/utils/selector.js +476 -0
  106. package/dist/lib/parser/utils/text.d.ts +3 -0
  107. package/dist/lib/parser/utils/text.js +17 -1
  108. package/dist/lib/parser/utils/token.d.ts +14 -0
  109. package/dist/lib/parser/utils/token.js +102 -0
  110. package/dist/lib/parser/utils/type.d.ts +2 -0
  111. package/dist/lib/parser/utils/type.js +29 -18
  112. package/dist/lib/renderer/render.d.ts +28 -0
  113. package/dist/lib/renderer/render.js +421 -262
  114. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
  115. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
  116. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  117. package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
  118. package/dist/lib/syntax/color/a98rgb.js +8 -12
  119. package/dist/lib/syntax/color/cmyk.d.ts +10 -0
  120. package/dist/lib/syntax/color/cmyk.js +23 -21
  121. package/dist/lib/syntax/color/color-mix.d.ts +2 -0
  122. package/dist/lib/syntax/color/color-mix.js +88 -77
  123. package/dist/lib/syntax/color/color.d.ts +42 -0
  124. package/dist/lib/syntax/color/color.js +65 -68
  125. package/dist/lib/syntax/color/hex.d.ts +16 -0
  126. package/dist/lib/syntax/color/hex.js +27 -31
  127. package/dist/lib/syntax/color/hsl.d.ts +20 -0
  128. package/dist/lib/syntax/color/hsl.js +5 -12
  129. package/dist/lib/syntax/color/hsv.d.ts +2 -0
  130. package/dist/lib/syntax/color/hwb.d.ts +21 -0
  131. package/dist/lib/syntax/color/hwb.js +8 -21
  132. package/dist/lib/syntax/color/lab.d.ts +25 -0
  133. package/dist/lib/syntax/color/lab.js +20 -21
  134. package/dist/lib/syntax/color/lch.d.ts +23 -0
  135. package/dist/lib/syntax/color/lch.js +13 -15
  136. package/dist/lib/syntax/color/oklab.d.ts +22 -0
  137. package/dist/lib/syntax/color/oklab.js +20 -39
  138. package/dist/lib/syntax/color/oklch.d.ts +20 -0
  139. package/dist/lib/syntax/color/oklch.js +14 -16
  140. package/dist/lib/syntax/color/p3.d.ts +6 -0
  141. package/dist/lib/syntax/color/p3.js +0 -8
  142. package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
  143. package/dist/lib/syntax/color/rec2020.d.ts +2 -0
  144. package/dist/lib/syntax/color/rec2020.js +9 -13
  145. package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
  146. package/dist/lib/syntax/color/relativecolor.js +68 -41
  147. package/dist/lib/syntax/color/rgb.d.ts +20 -0
  148. package/dist/lib/syntax/color/rgb.js +14 -18
  149. package/dist/lib/syntax/color/srgb.d.ts +23 -0
  150. package/dist/lib/syntax/color/srgb.js +27 -26
  151. package/dist/lib/syntax/color/utils/components.d.ts +2 -0
  152. package/dist/lib/syntax/color/utils/components.js +30 -14
  153. package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
  154. package/dist/lib/syntax/color/utils/distance.js +1 -8
  155. package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
  156. package/dist/lib/syntax/color/xyz.d.ts +5 -0
  157. package/dist/lib/syntax/color/xyz.js +8 -20
  158. package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
  159. package/dist/lib/syntax/color/xyzd50.js +6 -20
  160. package/dist/lib/syntax/constants.d.ts +67 -0
  161. package/dist/lib/syntax/constants.js +436 -0
  162. package/dist/lib/syntax/syntax.d.ts +38 -0
  163. package/dist/lib/syntax/syntax.js +533 -568
  164. package/dist/lib/validation/config.d.ts +14 -0
  165. package/dist/lib/validation/config.js +72 -33
  166. package/dist/lib/validation/config.json.js +1159 -74
  167. package/dist/lib/validation/json.d.ts +2 -0
  168. package/dist/lib/validation/match.d.ts +38 -0
  169. package/dist/lib/validation/match.js +2985 -0
  170. package/dist/lib/validation/parser/parse.d.ts +8 -0
  171. package/dist/lib/validation/parser/parse.js +684 -935
  172. package/dist/lib/validation/parser/typedef.d.ts +95 -0
  173. package/dist/lib/validation/parser/typedef.js +100 -0
  174. package/dist/lib/validation/utils/list.d.ts +4 -0
  175. package/dist/lib/validation/utils/list.js +4 -11
  176. package/dist/lib/validation/utils/whitespace.d.ts +2 -0
  177. package/dist/lib/validation/utils/whitespace.js +2 -8
  178. package/dist/node.d.ts +207 -0
  179. package/dist/node.js +53 -47
  180. package/dist/web.d.ts +169 -0
  181. package/dist/web.js +50 -41
  182. package/package.json +18 -13
  183. package/playground/index.html +1328 -0
  184. package/playground/sw.js +55 -0
  185. package/playground/tree.js +176 -0
  186. package/dist/lib/syntax/color/utils/constants.js +0 -214
  187. package/dist/lib/syntax/utils.js +0 -70
  188. package/dist/lib/validation/at-rules/container.js +0 -342
  189. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  190. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  191. package/dist/lib/validation/at-rules/document.js +0 -89
  192. package/dist/lib/validation/at-rules/else.js +0 -5
  193. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  194. package/dist/lib/validation/at-rules/import.js +0 -150
  195. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  196. package/dist/lib/validation/at-rules/layer.js +0 -41
  197. package/dist/lib/validation/at-rules/media.js +0 -255
  198. package/dist/lib/validation/at-rules/namespace.js +0 -81
  199. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  200. package/dist/lib/validation/at-rules/page.js +0 -100
  201. package/dist/lib/validation/at-rules/supports.js +0 -295
  202. package/dist/lib/validation/at-rules/when.js +0 -185
  203. package/dist/lib/validation/atrule.js +0 -184
  204. package/dist/lib/validation/selector.js +0 -36
  205. package/dist/lib/validation/syntax.js +0 -1073
  206. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  207. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  208. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  209. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  210. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  211. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  212. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  213. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  214. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  215. package/dist/lib/validation/syntaxes/selector.js +0 -5
  216. package/dist/lib/validation/syntaxes/url.js +0 -40
package/dist/web.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ import type { AstNode, ParseResult, ParserOptions, RenderOptions, RenderResult, TransformOptions, TransformResult } from "./@types/index.d.ts";
2
+ import { dirname, resolve } from "./lib/fs/resolve.ts";
3
+ import { ResponseType } from "./types.ts";
4
+ export type * from "./@types/index.d.ts";
5
+ export type * from "./@types/ast.d.ts";
6
+ export type * from "./@types/token.d.ts";
7
+ export type * from "./@types/parse.d.ts";
8
+ export type * from "./@types/validation.d.ts";
9
+ export type * from "./@types/walker.d.ts";
10
+ export type { AstNode, ParseResult, ParserOptions, RenderOptions, RenderResult, TransformOptions, TransformResult, } from "./@types/index.d.ts";
11
+ export { minify } from "./lib/ast/minify.ts";
12
+ export { expand } from "./lib/ast/expand.ts";
13
+ export { walk, walkValues, WalkerEvent, WalkerOptionEnum } from "./lib/ast/walk.ts";
14
+ export { parseString } from "./lib/parser/parse.ts";
15
+ export { renderToken } from "./lib/renderer/render.ts";
16
+ export { convertColor } from "./lib/syntax/color/color.ts";
17
+ export { isOkLabClose, okLabDistance } from "./lib/syntax/color/utils/distance.ts";
18
+ export { parseDeclarations } from "./lib/parser/parse.ts";
19
+ export { find, findLast, findByValue, findAll } from "./lib/ast/find.ts";
20
+ export { cloneNode } from "./lib/ast/clone.ts";
21
+ export { EnumToken, ColorType, ValidationLevel, ModuleScopeEnumOptions, ModuleCaseTransformEnum, } from "./lib/ast/types.ts";
22
+ export { SourceMap } from "./lib/renderer/sourcemap/sourcemap.ts";
23
+ export type { ValidationToken } from "./lib/validation/parser/types.d.ts";
24
+ export { FeatureWalkMode } from "./lib/ast/features/type.ts";
25
+ export { dirname, resolve, ResponseType };
26
+ /**
27
+ * load file or url
28
+ * @param url
29
+ * @param currentDirectory
30
+ * @param responseType
31
+ * @throws Error file not found
32
+ *
33
+ * ```ts
34
+ * import {load, ResponseType} from '@tbela99/css-parser';
35
+ * const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
36
+ * ```
37
+ */
38
+ export declare function load(url: string, currentDirectory?: string, responseType?: boolean | ResponseType): Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
39
+ /**
40
+ * render the ast tree
41
+ * @param data
42
+ * @param options
43
+ * @param mapping
44
+ *
45
+ * Example:
46
+ *
47
+ * ```ts
48
+ *
49
+ * import {render, ColorType} from '@tbela99/css-parser';
50
+ *
51
+ * const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
52
+ * const parseResult = await parse(css);
53
+ *
54
+ * let renderResult = render(parseResult.ast);
55
+ * console.log(result.code);
56
+ *
57
+ * // body{color:red}
58
+ *
59
+ *
60
+ * renderResult = render(parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
61
+ * console.log(renderResult.code);
62
+ *
63
+ * // body {
64
+ * // color: color(srgb 1 0 0)
65
+ * // }
66
+ * ```
67
+ */
68
+ export declare function render(data: AstNode, options?: RenderOptions, mapping?: {
69
+ mapping: Record<string, string>;
70
+ importMapping: Record<string, Record<string, string>> | null;
71
+ } | null): RenderResult;
72
+ /**
73
+ * parse css file
74
+ * @param file url or path
75
+ * @param options
76
+ * @param asStream load file as stream
77
+ *
78
+ * @throws Error file not found
79
+ *
80
+ * Example:
81
+ *
82
+ * ```ts
83
+ *
84
+ * import {parseFile} from '@tbela99/css-parser/web';
85
+ *
86
+ * // remote file
87
+ * let result = await parseFile('https://docs.deno.com/styles.css');
88
+ * console.log(result.ast);
89
+ *
90
+ * // local file
91
+ * result = await parseFile('./css/styles.css');
92
+ * console.log(result.ast);
93
+ * ```
94
+ */
95
+ export declare function parseFile(file: string, options?: ParserOptions, asStream?: boolean): Promise<ParseResult>;
96
+ /**
97
+ * parse css
98
+ * @param stream
99
+ * @param options
100
+ *
101
+ * Example:
102
+ *
103
+ * ```ts
104
+ *
105
+ * import {parse} from '@tbela99/css-parser/web';
106
+ *
107
+ * // css string
108
+ * const result = await parse(css);
109
+ * console.log(result.ast);
110
+ * ```
111
+ *
112
+ * Example using fetch and readable stream
113
+ *
114
+ * ```ts
115
+ *
116
+ * import {parse} from '@tbela99/css-parser/web';
117
+ *
118
+ * const response = await fetch('https://docs.deno.com/styles.css');
119
+ * const result = await parse(response.body, {beautify: true});
120
+ *
121
+ * console.log(result.ast);
122
+ * ```
123
+ */
124
+ export declare function parse(stream: string | ReadableStream<Uint8Array>, options?: ParserOptions): Promise<ParseResult>;
125
+ /**
126
+ * transform css file
127
+ * @param file url or path
128
+ * @param options
129
+ * @param asStream load file as stream
130
+ *
131
+ * Example:
132
+ *
133
+ * ```ts
134
+ *
135
+ * import {transformFile} from '@tbela99/css-parser/web';
136
+ *
137
+ * // remote file
138
+ * let result = await transformFile('https://docs.deno.com/styles.css');
139
+ * console.log(result.code);
140
+ *
141
+ * // local file
142
+ * result = await transformFile('./css/styles.css');
143
+ * console.log(result.code);
144
+ * ```
145
+ */
146
+ export declare function transformFile(file: string, options?: TransformOptions, asStream?: boolean): Promise<TransformResult>;
147
+ /**
148
+ * transform css
149
+ * @param css
150
+ * @param options
151
+ *
152
+ * Example:
153
+ *
154
+ * ```ts
155
+ *
156
+ * import {transform} from '@tbela99/css-parser/web';
157
+ *
158
+ * // css string
159
+ * let result = await transform(css);
160
+ * console.log(result.code);
161
+ *
162
+ * // using readable stream
163
+ * const response = await fetch('https://docs.deno.com/styles.css');
164
+ * result = await transform(response.body, {beautify: true});
165
+ *
166
+ * console.log(result.code);
167
+ * ```
168
+ */
169
+ export declare function transform(css: string | ReadableStream<Uint8Array>, options?: TransformOptions): Promise<TransformResult>;
package/dist/web.js CHANGED
@@ -1,57 +1,57 @@
1
+ import { doParse } from './lib/parser/parse.js';
2
+ export { parseDeclarations, parseString } from './lib/parser/parse.js';
3
+ import { doRender } from './lib/renderer/render.js';
4
+ export { renderToken } from './lib/renderer/render.js';
1
5
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
2
6
  export { ColorType, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
7
+ import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
8
+ import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
9
+ import { ResponseType } from './types.js';
3
10
  export { minify } from './lib/ast/minify.js';
4
- export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
5
11
  export { expand } from './lib/ast/expand.js';
6
- import { doRender } from './lib/renderer/render.js';
7
- export { renderToken } from './lib/renderer/render.js';
8
- export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
9
- import { doParse } from './lib/parser/parse.js';
10
- export { parseDeclarations, parseString, parseTokens } from './lib/parser/parse.js';
11
- import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
12
- import './lib/parser/utils/config.js';
12
+ export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
13
13
  export { convertColor } from './lib/syntax/color/color.js';
14
- import './lib/syntax/color/utils/constants.js';
15
14
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
16
- import './lib/validation/config.js';
17
- import './lib/validation/parser/parse.js';
18
- import './lib/validation/syntaxes/complex-selector.js';
19
- import './lib/validation/syntax.js';
20
- import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
21
- import { ResponseType } from './types.js';
15
+ export { find, findAll, findByValue, findLast } from './lib/ast/find.js';
16
+ export { cloneNode } from './lib/ast/clone.js';
17
+ export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
22
18
  export { FeatureWalkMode } from './lib/ast/features/type.js';
23
19
 
24
20
  /**
25
- * default file or url loader
21
+ * load file or url
26
22
  * @param url
27
- * @param currentFile
28
- *
23
+ * @param currentDirectory
29
24
  * @param responseType
30
- * @private
25
+ * @throws Error file not found
26
+ *
27
+ * ```ts
28
+ * import {load, ResponseType} from '@tbela99/css-parser';
29
+ * const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
30
+ * ```
31
31
  */
32
- async function load(url, currentFile = '.', responseType = false) {
33
- if (typeof responseType == 'boolean') {
32
+ async function load(url, currentDirectory = ".", responseType = false) {
33
+ if (typeof responseType == "boolean") {
34
34
  responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
35
35
  }
36
36
  let t;
37
37
  if (matchUrl.test(url)) {
38
38
  t = new URL(url);
39
39
  }
40
- else if (currentFile != null && matchUrl.test(currentFile)) {
41
- t = new URL(url, currentFile);
40
+ else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
41
+ t = new URL(url, currentDirectory);
42
42
  }
43
43
  else {
44
- const path = resolve(url, currentFile).absolute;
44
+ const path = resolve(url, currentDirectory).absolute;
45
45
  t = new URL(path, self.origin);
46
46
  }
47
- return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {
47
+ return fetch(t, t.origin != self.origin ? { mode: "cors" } : {}).then(async (response) => {
48
48
  if (!response.ok) {
49
49
  throw new Error(`${response.status} ${response.statusText} ${response.url}`);
50
50
  }
51
51
  if (responseType == ResponseType.ArrayBuffer) {
52
52
  return response.arrayBuffer();
53
53
  }
54
- return responseType == ResponseType.ReadableStream ? response.body : await response.text();
54
+ return responseType == ResponseType.ReadableStream ? response.body : response.text();
55
55
  });
56
56
  }
57
57
  /**
@@ -87,7 +87,9 @@ function render(data, options = {}, mapping) {
87
87
  return doRender(data, Object.assign(options, {
88
88
  resolve,
89
89
  dirname,
90
- cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
90
+ cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
91
+ ? self.location.pathname
92
+ : dirname(self.location.pathname),
91
93
  }), mapping);
92
94
  }
93
95
  /**
@@ -114,7 +116,7 @@ function render(data, options = {}, mapping) {
114
116
  * ```
115
117
  */
116
118
  async function parseFile(file, options = {}, asStream = false) {
117
- return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => parse(stream, { src: file, ...options }));
119
+ return Promise.resolve((options.load ?? load)(file, ".", asStream)).then((stream) => parse(stream, { src: file, ...options }));
118
120
  }
119
121
  /**
120
122
  * parse css
@@ -145,18 +147,23 @@ async function parseFile(file, options = {}, asStream = false) {
145
147
  * ```
146
148
  */
147
149
  async function parse(stream, options = {}) {
148
- return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
150
+ options.parseInfo = {
149
151
  stream,
150
- buffer: '',
152
+ buffer: "",
151
153
  offset: 0,
152
- position: { ind: 0, lin: 1, col: 1 },
153
- currentPosition: { ind: -1, lin: 1, col: 0 }
154
- }), Object.assign(options, {
154
+ time: 0,
155
+ src: options.src ?? "",
156
+ position: { ind: 0, lin: 1, col: 0 },
157
+ currentPosition: { ind: -1, lin: 1, col: 0 },
158
+ };
159
+ return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize(options.parseInfo), Object.assign(options, {
155
160
  load,
156
161
  resolve,
157
162
  dirname,
158
- cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
159
- })).then(result => {
163
+ cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
164
+ ? self.location.pathname
165
+ : dirname(self.location.pathname),
166
+ })).then((result) => {
160
167
  const { revMapping, ...res } = result;
161
168
  return res;
162
169
  });
@@ -183,7 +190,7 @@ async function parse(stream, options = {}) {
183
190
  * ```
184
191
  */
185
192
  async function transformFile(file, options = {}, asStream = false) {
186
- return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => transform(stream, { src: file, ...options }));
193
+ return Promise.resolve((options.load ?? load)(file, ".", asStream)).then((stream) => transform(stream, { src: file, ...options }));
187
194
  }
188
195
  /**
189
196
  * transform css
@@ -213,18 +220,20 @@ async function transform(css, options = {}) {
213
220
  return parse(css, options).then((parseResult) => {
214
221
  let mapping = null;
215
222
  let importMapping = null;
216
- if (typeof options.module == 'number' && (options.module & ModuleScopeEnumOptions.ICSS)) {
223
+ if (typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) {
217
224
  mapping = parseResult.mapping;
218
225
  importMapping = parseResult.importMapping;
219
226
  }
220
- else if (typeof options.module == 'object' && typeof options.module.scoped == 'number' && (options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
227
+ else if (typeof options.module == "object" &&
228
+ typeof options.module.scoped == "number" &&
229
+ options.module.scoped & ModuleScopeEnumOptions.ICSS) {
221
230
  mapping = parseResult.mapping;
222
231
  importMapping = parseResult.importMapping;
223
232
  }
224
233
  // ast already expanded by parse
225
234
  const rendered = render(parseResult.ast, {
226
235
  ...options,
227
- expandNestingRules: false
236
+ expandNestingRules: false,
228
237
  }, mapping != null ? { mapping, importMapping } : null);
229
238
  return {
230
239
  ...parseResult,
@@ -234,8 +243,8 @@ async function transform(css, options = {}) {
234
243
  bytesOut: rendered.code.length,
235
244
  ...parseResult.stats,
236
245
  render: rendered.stats.total,
237
- total: `${(performance.now() - startTime).toFixed(2)}ms`
238
- }
246
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
247
+ },
239
248
  };
240
249
  });
241
250
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser, minifier and validator for node and the browser",
4
- "version": "v1.4.1",
4
+ "version": "1.4.3",
5
5
  "exports": {
6
6
  ".": "./dist/node.js",
7
7
  "./node": "./dist/node.js",
@@ -16,6 +16,8 @@
16
16
  "scripts": {
17
17
  "doc": "typedoc --tsconfig typedoc-tsconfig.jsonc",
18
18
  "build": "rollup -c;./build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
19
+ "watch": "rollup -c -w",
20
+ "test:watch": "web-test-runner \"./test/**/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --watch",
19
21
  "test": "web-test-runner \"./test/**/web.spec.js\" --timeout=10000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
20
22
  "test:web": "web-test-runner \"./test/**/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
21
23
  "test:node": "mocha -p --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
@@ -32,6 +34,8 @@
32
34
  "keywords": [
33
35
  "parser",
34
36
  "css",
37
+ "modules",
38
+ "css-modules",
35
39
  "css-parser",
36
40
  "node",
37
41
  "ast",
@@ -48,31 +52,32 @@
48
52
  "streaming-parser"
49
53
  ],
50
54
  "author": "Thierry Bela",
51
- "license": "MIT OR LGPL-3.0",
55
+ "license": "MIT",
52
56
  "bugs": {
53
57
  "url": "https://github.com/tbela99/css-parser/issues"
54
58
  },
55
59
  "homepage": "https://github.com/tbela99/css-parser#readme",
56
60
  "devDependencies": {
57
61
  "@esm-bundle/chai": "^4.3.4-fix.0",
58
- "@rollup/plugin-commonjs": "^28.0.8",
62
+ "@rollup/plugin-commonjs": "^29.0.3",
59
63
  "@rollup/plugin-json": "^6.1.0",
60
64
  "@rollup/plugin-node-resolve": "^16.0.3",
61
65
  "@rollup/plugin-typescript": "^12.3.0",
62
66
  "@types/chai": "^5.2.3",
63
67
  "@types/mocha": "^10.0.10",
64
- "@types/node": "^24.9.1",
65
- "@types/web": "^0.0.274",
68
+ "@types/node": "^25.9.3",
69
+ "@types/web": "^0.0.350",
66
70
  "@web/test-runner": "^0.20.2",
67
71
  "@web/test-runner-playwright": "^0.11.1",
68
- "c8": "^10.1.3",
72
+ "c8": "^11.0.0",
69
73
  "esno": "^4.8.0",
70
- "mocha": "^11.7.4",
71
- "playwright": "^1.56.1",
72
- "rollup": "^4.52.5",
73
- "rollup-plugin-dts": "^6.2.3",
74
+ "mocha": "^11.7.6",
75
+ "playwright": "^1.60.0",
76
+ "rollup": "^4.61.1",
77
+ "rollup-plugin-dts": "^6.4.1",
74
78
  "tslib": "^2.8.1",
75
- "typedoc": "^0.28.14",
76
- "typedoc-material-theme": "^1.4.0"
77
- }
79
+ "typedoc": "^0.28.19",
80
+ "typedoc-material-theme": "^1.4.1"
81
+ },
82
+ "sideEffects": false
78
83
  }