@tbela99/css-parser 1.2.0 → 1.3.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +29 -11
  3. package/dist/index-umd-web.js +2041 -1967
  4. package/dist/index.cjs +2129 -1964
  5. package/dist/index.d.ts +1936 -77
  6. package/dist/lib/ast/expand.js +4 -65
  7. package/dist/lib/ast/features/calc.js +3 -27
  8. package/dist/lib/ast/features/inlinecssvariables.js +2 -21
  9. package/dist/lib/ast/features/prefix.js +2 -1
  10. package/dist/lib/ast/features/transform.js +4 -3
  11. package/dist/lib/ast/features/type.js +9 -0
  12. package/dist/lib/ast/math/expression.js +26 -149
  13. package/dist/lib/ast/math/math.js +9 -9
  14. package/dist/lib/ast/minify.js +82 -171
  15. package/dist/lib/ast/transform/compute.js +4 -37
  16. package/dist/lib/ast/transform/matrix.js +33 -34
  17. package/dist/lib/ast/transform/minify.js +32 -51
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +31 -39
  24. package/dist/lib/ast/types.js +459 -5
  25. package/dist/lib/ast/walk.js +18 -0
  26. package/dist/lib/fs/resolve.js +11 -3
  27. package/dist/lib/parser/declaration/map.js +1 -0
  28. package/dist/lib/parser/declaration/set.js +2 -2
  29. package/dist/lib/parser/parse.js +139 -32
  30. package/dist/lib/parser/tokenize.js +41 -93
  31. package/dist/lib/parser/utils/type.js +1 -1
  32. package/dist/lib/renderer/render.js +61 -30
  33. package/dist/lib/renderer/sourcemap/sourcemap.js +34 -0
  34. package/dist/lib/syntax/color/cmyk.js +2 -2
  35. package/dist/lib/syntax/color/color-mix.js +11 -12
  36. package/dist/lib/syntax/color/color.js +14 -7
  37. package/dist/lib/syntax/color/hsl.js +4 -4
  38. package/dist/lib/syntax/color/hwb.js +27 -8
  39. package/dist/lib/syntax/color/lab.js +4 -4
  40. package/dist/lib/syntax/color/lch.js +4 -4
  41. package/dist/lib/syntax/color/oklab.js +4 -4
  42. package/dist/lib/syntax/color/oklch.js +4 -4
  43. package/dist/lib/syntax/color/relativecolor.js +1 -1
  44. package/dist/lib/syntax/color/rgb.js +4 -4
  45. package/dist/lib/syntax/color/utils/components.js +15 -3
  46. package/dist/lib/syntax/color/utils/distance.js +15 -1
  47. package/dist/lib/syntax/syntax.js +18 -17
  48. package/dist/lib/syntax/utils.js +1 -1
  49. package/dist/lib/validation/at-rules/document.js +1 -1
  50. package/dist/lib/validation/at-rules/import.js +4 -4
  51. package/dist/lib/validation/at-rules/keyframes.js +0 -11
  52. package/dist/lib/validation/at-rules/supports.js +6 -6
  53. package/dist/lib/validation/config.js +0 -4
  54. package/dist/lib/validation/config.json.js +33 -30
  55. package/dist/lib/validation/parser/parse.js +0 -8
  56. package/dist/lib/validation/selector.js +0 -9
  57. package/dist/lib/validation/syntax.js +17 -135
  58. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -11
  59. package/dist/lib/validation/syntaxes/family-name.js +0 -32
  60. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -11
  61. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -26
  62. package/dist/lib/validation/syntaxes/url.js +0 -33
  63. package/dist/lib/validation/utils/list.js +0 -8
  64. package/dist/node.js +229 -0
  65. package/dist/web.js +158 -0
  66. package/package.json +14 -11
  67. package/dist/node/index.js +0 -57
  68. package/dist/node/load.js +0 -20
  69. package/dist/web/index.js +0 -66
  70. package/dist/web/load.js +0 -31
package/dist/node.js ADDED
@@ -0,0 +1,229 @@
1
+ import process from 'node:process';
2
+ export { ColorType, EnumToken, ValidationLevel } from './lib/ast/types.js';
3
+ export { minify } from './lib/ast/minify.js';
4
+ export { WalkerOptionEnum, WalkerValueEvent, walk, walkValues } from './lib/ast/walk.js';
5
+ 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';
13
+ export { convertColor } from './lib/syntax/color/color.js';
14
+ import './lib/syntax/color/utils/constants.js';
15
+ export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
16
+ import './lib/validation/config.js';
17
+ import './lib/validation/parser/types.js';
18
+ import './lib/validation/parser/parse.js';
19
+ import './lib/validation/syntaxes/complex-selector.js';
20
+ import './lib/validation/syntax.js';
21
+ import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
22
+ import { Readable } from 'node:stream';
23
+ import { createReadStream } from 'node:fs';
24
+ export { FeatureWalkMode } from './lib/ast/features/type.js';
25
+
26
+ /**
27
+ * node module entry point
28
+ * @module node
29
+ */
30
+ /**
31
+ * load file or url as stream
32
+ * @param url
33
+ * @param currentFile
34
+ *
35
+ * @private
36
+ */
37
+ async function getStream(url, currentFile = '.') {
38
+ const resolved = resolve(url, currentFile);
39
+ // @ts-ignore
40
+ return matchUrl.test(resolved.absolute) ? fetch(resolved.absolute).then((response) => {
41
+ if (!response.ok) {
42
+ throw new Error(`${response.status} ${response.statusText} ${response.url}`);
43
+ }
44
+ return response.body;
45
+ }) : Readable.toWeb(createReadStream(resolved.absolute));
46
+ }
47
+ /**
48
+ * render ast tree
49
+ * @param data
50
+ * @param options
51
+ *
52
+ * Example:
53
+ *
54
+ * ```ts
55
+ *
56
+ * import {render, ColorType} from '@tbela99/css-parser';
57
+ *
58
+ * // remote file
59
+ * let result = render(ast);
60
+ * console.log(result.code);
61
+ *
62
+ * // local file
63
+ * result = await parseFile(ast, {beatify: true, convertColor: ColorType.SRGB});
64
+ * console.log(result.code);
65
+ * ```
66
+ */
67
+ function render(data, options = {}) {
68
+ return doRender(data, Object.assign(options, { getStream, resolve, dirname, cwd: options.cwd ?? process.cwd() }));
69
+ }
70
+ /**
71
+ * parse css file
72
+ * @param file url or path
73
+ * @param options
74
+ *
75
+ * Example:
76
+ *
77
+ * ```ts
78
+ *
79
+ * import {parseFile} from '@tbela99/css-parser';
80
+ *
81
+ * // remote file
82
+ * let result = await parseFile('https://docs.deno.com/styles.css');
83
+ * console.log(result.ast);
84
+ *
85
+ * // local file
86
+ * result = await parseFile('./css/styles.css');
87
+ * console.log(result.ast);
88
+ * ```
89
+ */
90
+ async function parseFile(file, options = {}) {
91
+ return getStream(file).then(stream => parse(stream, { src: file, ...options }));
92
+ }
93
+ /**
94
+ * parse css
95
+ * @param stream
96
+ * @param opt
97
+ *
98
+ * Example:
99
+ *
100
+ * ```ts
101
+ *
102
+ * import {transform} from '@tbela99/css-parser';
103
+ *
104
+ * // css string
105
+ * let result = await transform(css);
106
+ * console.log(result.code);
107
+ * ```
108
+ *
109
+ * Example using stream
110
+ *
111
+ * ```ts
112
+ *
113
+ * import {parse} from '@tbela99/css-parser';
114
+ * import {Readable} from "node:stream";
115
+ *
116
+ * // usage: node index.ts < styles.css or cat styles.css | node index.ts
117
+ *
118
+ * const readableStream = Readable.toWeb(process.stdin);
119
+ * const result = await parse(readableStream, {beautify: true});
120
+ *
121
+ * console.log(result.ast);
122
+ * ```
123
+ *
124
+ * Example using fetch
125
+ *
126
+ * ```ts
127
+ *
128
+ * import {parse} from '@tbela99/css-parser';
129
+ *
130
+ * const response = await fetch('https://docs.deno.com/styles.css');
131
+ * result = await parse(response.body, {beautify: true});
132
+ *
133
+ * console.log(result.ast);
134
+ * ```
135
+ */
136
+ async function parse(stream, opt = {}) {
137
+ return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
138
+ stream,
139
+ buffer: '',
140
+ position: { ind: 0, lin: 1, col: 1 },
141
+ currentPosition: { ind: -1, lin: 1, col: 0 }
142
+ }), Object.assign(opt, { getStream, resolve, dirname, cwd: opt.cwd ?? process.cwd() }));
143
+ }
144
+ /**
145
+ * transform css file
146
+ * @param file url or path
147
+ * @param options
148
+ *
149
+ * Example:
150
+ *
151
+ * ```ts
152
+ *
153
+ * import {transformFile} from '@tbela99/css-parser';
154
+ *
155
+ * // remote file
156
+ * let result = await transformFile('https://docs.deno.com/styles.css');
157
+ * console.log(result.code);
158
+ *
159
+ * // local file
160
+ * result = await transformFile('./css/styles.css');
161
+ * console.log(result.code);
162
+ * ```
163
+ */
164
+ async function transformFile(file, options = {}) {
165
+ return getStream(file).then(stream => transform(stream, { src: file, ...options }));
166
+ }
167
+ /**
168
+ * transform css
169
+ * @param css
170
+ * @param options
171
+ *
172
+ * Example:
173
+ *
174
+ * ```ts
175
+ *
176
+ * import {transform} from '@tbela99/css-parser';
177
+ *
178
+ * // css string
179
+ * let result = await transform(css);
180
+ * console.log(result.code);
181
+ * ```
182
+ *
183
+ * Example using stream
184
+ *
185
+ * ```ts
186
+ *
187
+ * import {transform} from '@tbela99/css-parser';
188
+ * import {Readable} from "node:stream";
189
+ *
190
+ * // usage: node index.ts < styles.css or cat styles.css | node index.ts
191
+ *
192
+ * const readableStream = Readable.toWeb(process.stdin);
193
+ * const result = await transform(readableStream, {beautify: true});
194
+ *
195
+ * console.log(result.code);
196
+ * ```
197
+ *
198
+ * Example using fetch
199
+ *
200
+ * ```ts
201
+ *
202
+ * import {transform} from '@tbela99/css-parser';
203
+ *
204
+ * const response = await fetch('https://docs.deno.com/styles.css');
205
+ * result = await transform(response.body, {beautify: true});
206
+ *
207
+ * console.log(result.code);
208
+ * ```
209
+ */
210
+ async function transform(css, options = {}) {
211
+ options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
212
+ const startTime = performance.now();
213
+ return parse(css, options).then((parseResult) => {
214
+ const rendered = render(parseResult.ast, options);
215
+ return {
216
+ ...parseResult,
217
+ ...rendered,
218
+ errors: parseResult.errors.concat(rendered.errors),
219
+ stats: {
220
+ bytesOut: rendered.code.length,
221
+ ...parseResult.stats,
222
+ render: rendered.stats.total,
223
+ total: `${(performance.now() - startTime).toFixed(2)}ms`
224
+ }
225
+ };
226
+ });
227
+ }
228
+
229
+ export { dirname, getStream, parse, parseFile, render, resolve, transform, transformFile };
package/dist/web.js ADDED
@@ -0,0 +1,158 @@
1
+ export { ColorType, EnumToken, ValidationLevel } from './lib/ast/types.js';
2
+ export { minify } from './lib/ast/minify.js';
3
+ export { WalkerOptionEnum, WalkerValueEvent, walk, walkValues } from './lib/ast/walk.js';
4
+ export { expand } from './lib/ast/expand.js';
5
+ import { doRender } from './lib/renderer/render.js';
6
+ export { renderToken } from './lib/renderer/render.js';
7
+ export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
8
+ import { doParse } from './lib/parser/parse.js';
9
+ export { parseDeclarations, parseString, parseTokens } from './lib/parser/parse.js';
10
+ import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
11
+ import './lib/parser/utils/config.js';
12
+ export { convertColor } from './lib/syntax/color/color.js';
13
+ import './lib/syntax/color/utils/constants.js';
14
+ export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
15
+ import './lib/validation/config.js';
16
+ import './lib/validation/parser/types.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
+ export { FeatureWalkMode } from './lib/ast/features/type.js';
22
+
23
+ /**
24
+ * web module entry point
25
+ * @module web
26
+ */
27
+ /**
28
+ * load file or url as stream
29
+ * @param url
30
+ * @param currentFile
31
+ *
32
+ * @private
33
+ */
34
+ async function getStream(url, currentFile = '.') {
35
+ let t;
36
+ if (matchUrl.test(url)) {
37
+ t = new URL(url);
38
+ }
39
+ else if (currentFile != null && matchUrl.test(currentFile)) {
40
+ t = new URL(url, currentFile);
41
+ }
42
+ else {
43
+ const path = resolve(url, currentFile).absolute;
44
+ // @ts-ignore
45
+ t = new URL(path, self.origin);
46
+ }
47
+ // @ts-ignore
48
+ return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then((response) => {
49
+ if (!response.ok) {
50
+ throw new Error(`${response.status} ${response.statusText} ${response.url}`);
51
+ }
52
+ return response.body;
53
+ });
54
+ }
55
+ /**
56
+ * render ast node
57
+ * @param data
58
+ * @param options
59
+ */
60
+ function render(data, options = {}) {
61
+ return doRender(data, Object.assign(options, {
62
+ getStream,
63
+ resolve,
64
+ dirname,
65
+ cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
66
+ }));
67
+ }
68
+ /**
69
+ * parse css file
70
+ * @param file url or path
71
+ * @param options
72
+ */
73
+ async function parseFile(file, options = {}) {
74
+ return getStream(file).then(stream => parse(stream, { src: file, ...options }));
75
+ }
76
+ /**
77
+ * parse css
78
+ * @param stream
79
+ * @param opt
80
+ */
81
+ async function parse(stream, opt = {}) {
82
+ return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
83
+ stream,
84
+ buffer: '',
85
+ position: { ind: 0, lin: 1, col: 1 },
86
+ currentPosition: { ind: -1, lin: 1, col: 0 }
87
+ }), Object.assign(opt, {
88
+ getStream,
89
+ resolve,
90
+ dirname,
91
+ cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
92
+ }));
93
+ }
94
+ /**
95
+ * transform css file
96
+ * @param file url or path
97
+ * @param options
98
+ *
99
+ * Example:
100
+ *
101
+ * ```ts
102
+ *
103
+ * import {transformFile} from '@tbela99/css-parser/web';
104
+ *
105
+ * // remote file
106
+ * let result = await transformFile('https://docs.deno.com/styles.css');
107
+ * console.log(result.code);
108
+ *
109
+ * // local file
110
+ * result = await transformFile('./css/styles.css');
111
+ * console.log(result.code);
112
+ * ```
113
+ */
114
+ async function transformFile(file, options = {}) {
115
+ return getStream(file).then(stream => transform(stream, { src: file, ...options }));
116
+ }
117
+ /**
118
+ * transform css
119
+ * @param css
120
+ * @param options
121
+ *
122
+ * Example:
123
+ *
124
+ * ```ts
125
+ *
126
+ * import {transform} from '@tbela99/css-parser/web';
127
+ *
128
+ * // css string
129
+ * let result = await transform(css);
130
+ * console.log(result.code);
131
+ *
132
+ * // using readable stream
133
+ * const response = await fetch('https://docs.deno.com/styles.css');
134
+ * result = await transform(response.body, {beautify: true});
135
+ *
136
+ * console.log(result.code);
137
+ * ```
138
+ */
139
+ async function transform(css, options = {}) {
140
+ options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
141
+ const startTime = performance.now();
142
+ return parse(css, options).then((parseResult) => {
143
+ const rendered = render(parseResult.ast, options);
144
+ return {
145
+ ...parseResult,
146
+ ...rendered,
147
+ errors: parseResult.errors.concat(rendered.errors),
148
+ stats: {
149
+ bytesOut: rendered.code.length,
150
+ ...parseResult.stats,
151
+ render: rendered.stats.total,
152
+ total: `${(performance.now() - startTime).toFixed(2)}ms`
153
+ }
154
+ };
155
+ });
156
+ }
157
+
158
+ export { dirname, getStream, parse, parseFile, render, resolve, transform, transformFile };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser for node and the browser",
4
- "version": "v1.2.0",
4
+ "version": "v1.3.1",
5
5
  "exports": {
6
- ".": "./dist/node/index.js",
7
- "./node": "./dist/node/index.js",
6
+ ".": "./dist/node.js",
7
+ "./node": "./dist/node.js",
8
8
  "./umd": "./dist/index-umd-web.js",
9
- "./web": "./dist/web/index.js",
9
+ "./web": "./dist/web.js",
10
10
  "./cjs": "./dist/index.cjs"
11
11
  },
12
12
  "type": "module",
@@ -15,10 +15,11 @@
15
15
  "test": "test"
16
16
  },
17
17
  "scripts": {
18
+ "doc": "typedoc --tsconfig typedoc-tsconfig.jsonc",
18
19
  "build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
19
- "test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
20
- "test:web": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
21
- "test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
20
+ "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\"",
21
+ "test:web": "web-test-runner \"test/**/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
22
+ "test:node": "mocha -p --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
22
23
  "test:cov": "c8 -x 'test/specs/**/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
23
24
  "test:web-cov": "web-test-runner -x 'test/specs/**/*.js' \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
24
25
  "profile": "node --enable-source-maps --inspect-brk test/inspect.js",
@@ -65,9 +66,11 @@
65
66
  "c8": "^10.1.3",
66
67
  "esno": "^4.8.0",
67
68
  "mocha": "^11.7.1",
68
- "playwright": "^1.54.1",
69
- "rollup": "^4.45.1",
69
+ "playwright": "^1.55.0",
70
+ "rollup": "^4.48.0",
70
71
  "rollup-plugin-dts": "^6.2.1",
71
- "tslib": "^2.8.1"
72
+ "tslib": "^2.8.1",
73
+ "typedoc": "^0.28.10",
74
+ "typedoc-material-theme": "^1.4.0"
72
75
  }
73
- }
76
+ }
@@ -1,57 +0,0 @@
1
- import process from 'node:process';
2
- export { ColorType, EnumToken, ValidationLevel } from '../lib/ast/types.js';
3
- export { minify } from '../lib/ast/minify.js';
4
- export { walk, walkValues } from '../lib/ast/walk.js';
5
- export { expand } from '../lib/ast/expand.js';
6
- import { doRender } from '../lib/renderer/render.js';
7
- export { renderToken } from '../lib/renderer/render.js';
8
- import '../lib/syntax/color/utils/constants.js';
9
- export { convertColor } from '../lib/syntax/color/color.js';
10
- export { isOkLabClose, okLabDistance } from '../lib/syntax/color/utils/distance.js';
11
- import { doParse } from '../lib/parser/parse.js';
12
- export { parseString, parseTokens } from '../lib/parser/parse.js';
13
- import '../lib/parser/tokenize.js';
14
- import '../lib/parser/utils/config.js';
15
- import '../lib/validation/config.js';
16
- import '../lib/validation/parser/types.js';
17
- import '../lib/validation/parser/parse.js';
18
- import '../lib/validation/syntaxes/complex-selector.js';
19
- import '../lib/validation/syntax.js';
20
- import { dirname, resolve } from '../lib/fs/resolve.js';
21
- import { load } from './load.js';
22
-
23
- /**
24
- * render ast node
25
- */
26
- function render(data, options = {}) {
27
- return doRender(data, Object.assign(options, { load, resolve, dirname, cwd: options.cwd ?? process.cwd() }));
28
- }
29
- /**
30
- * parse css
31
- */
32
- async function parse(iterator, opt = {}) {
33
- return doParse(iterator, Object.assign(opt, { load, resolve, dirname, cwd: opt.cwd ?? process.cwd() }));
34
- }
35
- /**
36
- * parse and render css
37
- */
38
- async function transform(css, options = {}) {
39
- options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
40
- const startTime = performance.now();
41
- return parse(css, options).then((parseResult) => {
42
- const rendered = render(parseResult.ast, options);
43
- return {
44
- ...parseResult,
45
- ...rendered,
46
- errors: parseResult.errors.concat(rendered.errors),
47
- stats: {
48
- bytesOut: rendered.code.length,
49
- ...parseResult.stats,
50
- render: rendered.stats.total,
51
- total: `${(performance.now() - startTime).toFixed(2)}ms`
52
- }
53
- };
54
- });
55
- }
56
-
57
- export { dirname, load, parse, render, resolve, transform };
package/dist/node/load.js DELETED
@@ -1,20 +0,0 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { resolve, matchUrl } from '../lib/fs/resolve.js';
3
-
4
- function parseResponse(response) {
5
- if (!response.ok) {
6
- throw new Error(`${response.status} ${response.statusText} ${response.url}`);
7
- }
8
- return response.text();
9
- }
10
- /**
11
- * load file
12
- * @param url
13
- * @param currentFile
14
- */
15
- async function load(url, currentFile = '.') {
16
- const resolved = resolve(url, currentFile);
17
- return matchUrl.test(resolved.absolute) ? fetch(resolved.absolute).then(parseResponse) : readFile(resolved.absolute, { encoding: 'utf-8' });
18
- }
19
-
20
- export { load };
package/dist/web/index.js DELETED
@@ -1,66 +0,0 @@
1
- export { ColorType, EnumToken, ValidationLevel } from '../lib/ast/types.js';
2
- export { minify } from '../lib/ast/minify.js';
3
- export { walk, walkValues } from '../lib/ast/walk.js';
4
- export { expand } from '../lib/ast/expand.js';
5
- import { doRender } from '../lib/renderer/render.js';
6
- export { renderToken } from '../lib/renderer/render.js';
7
- import '../lib/syntax/color/utils/constants.js';
8
- export { convertColor } from '../lib/syntax/color/color.js';
9
- export { isOkLabClose, okLabDistance } from '../lib/syntax/color/utils/distance.js';
10
- import { doParse } from '../lib/parser/parse.js';
11
- export { parseString, parseTokens } from '../lib/parser/parse.js';
12
- import '../lib/parser/tokenize.js';
13
- import '../lib/parser/utils/config.js';
14
- import '../lib/validation/config.js';
15
- import '../lib/validation/parser/types.js';
16
- import '../lib/validation/parser/parse.js';
17
- import '../lib/validation/syntaxes/complex-selector.js';
18
- import '../lib/validation/syntax.js';
19
- import { dirname, resolve } from '../lib/fs/resolve.js';
20
- import { load } from './load.js';
21
-
22
- /**
23
- * render ast node
24
- */
25
- function render(data, options = {}) {
26
- return doRender(data, Object.assign(options, {
27
- load,
28
- resolve,
29
- dirname,
30
- cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
31
- }));
32
- }
33
- /**
34
- * parse css
35
- */
36
- async function parse(iterator, opt = {}) {
37
- return doParse(iterator, Object.assign(opt, {
38
- load,
39
- resolve,
40
- dirname,
41
- cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
42
- }));
43
- }
44
- /**
45
- * parse and render css
46
- */
47
- async function transform(css, options = {}) {
48
- options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
49
- const startTime = performance.now();
50
- return parse(css, options).then((parseResult) => {
51
- const rendered = render(parseResult.ast, options);
52
- return {
53
- ...parseResult,
54
- ...rendered,
55
- errors: parseResult.errors.concat(rendered.errors),
56
- stats: {
57
- bytesOut: rendered.code.length,
58
- ...parseResult.stats,
59
- render: rendered.stats.total,
60
- total: `${(performance.now() - startTime).toFixed(2)}ms`
61
- }
62
- };
63
- });
64
- }
65
-
66
- export { dirname, load, parse, render, resolve, transform };
package/dist/web/load.js DELETED
@@ -1,31 +0,0 @@
1
- import { matchUrl, resolve } from '../lib/fs/resolve.js';
2
-
3
- function parseResponse(response) {
4
- if (!response.ok) {
5
- throw new Error(`${response.status} ${response.statusText} ${response.url}`);
6
- }
7
- return response.text();
8
- }
9
- /**
10
- * load file
11
- * @param url
12
- * @param currentFile
13
- */
14
- async function load(url, currentFile) {
15
- let t;
16
- if (matchUrl.test(url)) {
17
- t = new URL(url);
18
- }
19
- else if (matchUrl.test(currentFile)) {
20
- t = new URL(url, currentFile);
21
- }
22
- else {
23
- const path = resolve(url, currentFile).absolute;
24
- // @ts-ignore
25
- t = new URL(path, self.origin);
26
- }
27
- // @ts-ignore
28
- return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(parseResponse);
29
- }
30
-
31
- export { load };