@tbela99/css-parser 2.0.1 → 2.2.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/CHANGELOG.md +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { Readable } from 'node:stream';
|
|
@@ -11,16 +11,20 @@ import { lstat, readFile } from 'node:fs/promises';
|
|
|
11
11
|
import { doParse, doParseSync } from './lib/parser/parse.js';
|
|
12
12
|
export { parseDeclarations, parseString } from './lib/parser/parse.js';
|
|
13
13
|
import { doRender } from './lib/printer/render.js';
|
|
14
|
-
export { renderValue
|
|
14
|
+
export { renderValue } from './lib/printer/render.js';
|
|
15
|
+
import { TransformResult, RenderResult } from './lib/printer/result.js';
|
|
16
|
+
import { useNativeBuffer } from './lib/printer/writer.js';
|
|
17
|
+
import { Buffer } from 'node:buffer';
|
|
15
18
|
import { ModuleScopeEnumOptions } from './lib/ast/types.js';
|
|
16
19
|
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
|
|
17
|
-
import { Tokenizer } from './lib/parser/tokenize.js';
|
|
20
|
+
import { createParseInfo, Tokenizer } from './lib/parser/tokenize.js';
|
|
21
|
+
import { isCssInput } from './lib/parser/input.js';
|
|
18
22
|
import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
|
|
19
23
|
import { ResponseType } from './types.js';
|
|
20
24
|
import { resolve as resolve$1 } from 'node:path';
|
|
21
25
|
import { SourceFile } from './lib/parser/source.js';
|
|
22
26
|
import { cwd } from 'node:process';
|
|
23
|
-
import {
|
|
27
|
+
import { validateSyncArguments, parseResult } from './utils/sync.js';
|
|
24
28
|
export { minify } from './lib/ast/minify.js';
|
|
25
29
|
export { convertColor } from './lib/syntax/color/color.js';
|
|
26
30
|
export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
|
|
@@ -29,8 +33,10 @@ export { ARENA, ARENAID, AstArenaNode } from './lib/ast/arena-node.js';
|
|
|
29
33
|
export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
|
|
30
34
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
31
35
|
export { parseSyntax } from './lib/validation/parser/parse.js';
|
|
32
|
-
export { version } from '
|
|
36
|
+
export { version } from './version.js';
|
|
33
37
|
|
|
38
|
+
// the rendered css is encoded with the node.js Buffer: faster than TextEncoder.encodeInto()
|
|
39
|
+
useNativeBuffer(Buffer);
|
|
34
40
|
/**
|
|
35
41
|
* Load file or url
|
|
36
42
|
* @param url
|
|
@@ -44,8 +50,8 @@ export { version } from '../package.json.js';
|
|
|
44
50
|
* ```
|
|
45
51
|
*/
|
|
46
52
|
async function load(url, currentDirectory = ".", responseType = false) {
|
|
47
|
-
const resolved = typeof url
|
|
48
|
-
if (typeof responseType
|
|
53
|
+
const resolved = typeof url === "string" ? resolve(url, currentDirectory) : url;
|
|
54
|
+
if (typeof responseType === "boolean") {
|
|
49
55
|
responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
|
|
50
56
|
}
|
|
51
57
|
if (matchUrl.test(resolved.absolute)) {
|
|
@@ -53,13 +59,13 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
53
59
|
if (!response.ok) {
|
|
54
60
|
throw new Error(`${response.status} ${response.statusText} ${response.url}`);
|
|
55
61
|
}
|
|
56
|
-
if (responseType
|
|
62
|
+
if (responseType === ResponseType.ArrayBuffer) {
|
|
57
63
|
return response.arrayBuffer();
|
|
58
64
|
}
|
|
59
|
-
if (responseType
|
|
65
|
+
if (responseType === ResponseType.JSON) {
|
|
60
66
|
return response.json();
|
|
61
67
|
}
|
|
62
|
-
return responseType
|
|
68
|
+
return responseType === ResponseType.ReadableStream
|
|
63
69
|
? response.body
|
|
64
70
|
: response.text();
|
|
65
71
|
});
|
|
@@ -67,11 +73,14 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
67
73
|
try {
|
|
68
74
|
const stats = await lstat(resolved.absolute);
|
|
69
75
|
if (stats.isFile()) {
|
|
70
|
-
if (responseType
|
|
71
|
-
return readFile(resolved.absolute, "utf-8").then((buffer) => responseType
|
|
76
|
+
if (responseType === ResponseType.Text || responseType === ResponseType.JSON) {
|
|
77
|
+
return readFile(resolved.absolute, "utf-8").then((buffer) => responseType === ResponseType.JSON ? JSON.parse(buffer) : buffer);
|
|
72
78
|
}
|
|
73
|
-
if (responseType
|
|
74
|
-
|
|
79
|
+
if (responseType === ResponseType.ArrayBuffer) {
|
|
80
|
+
// the buffer of the file, without copy. a buffer that is a part of a larger buffer is copied
|
|
81
|
+
return readFile(resolved.absolute).then((buffer) => buffer.byteOffset === 0 && buffer.byteLength === buffer.buffer.byteLength
|
|
82
|
+
? buffer.buffer
|
|
83
|
+
: buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
|
75
84
|
}
|
|
76
85
|
return Readable.toWeb(createReadStream(resolved.absolute, {
|
|
77
86
|
encoding: "utf-8",
|
|
@@ -84,6 +93,7 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
84
93
|
}
|
|
85
94
|
/**
|
|
86
95
|
* Render the ast tree
|
|
96
|
+
* @param arena
|
|
87
97
|
* @param data
|
|
88
98
|
* @param options
|
|
89
99
|
* @param mapping
|
|
@@ -112,7 +122,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
112
122
|
* ```
|
|
113
123
|
*/
|
|
114
124
|
function render(arena, data, options = {}, mapping) {
|
|
115
|
-
return doRender(arena, data,
|
|
125
|
+
return doRender(arena, data,
|
|
126
|
+
// the options of the caller are not modified
|
|
127
|
+
{ ...options, resolve, dirname, cwd: options.cwd ?? resolve$1() }, mapping);
|
|
116
128
|
}
|
|
117
129
|
/**
|
|
118
130
|
* Parse CSS
|
|
@@ -133,7 +145,7 @@ function render(arena, data, options = {}, mapping) {
|
|
|
133
145
|
function parseSync(...args) {
|
|
134
146
|
let options;
|
|
135
147
|
let stream;
|
|
136
|
-
if (
|
|
148
|
+
if (isCssInput(args[0])) {
|
|
137
149
|
stream = args[0];
|
|
138
150
|
options = args[1];
|
|
139
151
|
}
|
|
@@ -145,7 +157,15 @@ function parseSync(...args) {
|
|
|
145
157
|
if (options != null) {
|
|
146
158
|
validateSyncArguments(options);
|
|
147
159
|
}
|
|
148
|
-
options
|
|
160
|
+
// the options of the caller are not modified: they may be reused
|
|
161
|
+
return parseSyncInput(stream, { ...options });
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* parse a css input. the options belong to the call: they are modified (source, parse info, ...)
|
|
165
|
+
* @param stream
|
|
166
|
+
* @param options
|
|
167
|
+
*/
|
|
168
|
+
function parseSyncInput(stream, options) {
|
|
149
169
|
options.src ??= "";
|
|
150
170
|
options.sourcesMap ??= new Map();
|
|
151
171
|
Object.assign(options, {
|
|
@@ -155,20 +175,11 @@ function parseSync(...args) {
|
|
|
155
175
|
});
|
|
156
176
|
options.src = resolve(options.src, options.cwd).relative;
|
|
157
177
|
if (options.source == null) {
|
|
158
|
-
const source = new SourceFile(typeof stream
|
|
178
|
+
const source = new SourceFile(typeof stream === "string" ? stream : "", [], options.src);
|
|
159
179
|
options.sourcesMap.set(source.id, source);
|
|
160
180
|
options.source = source;
|
|
161
181
|
}
|
|
162
|
-
options.parseInfo =
|
|
163
|
-
stream,
|
|
164
|
-
buffer: "",
|
|
165
|
-
src: options.src ?? "",
|
|
166
|
-
offset: 0,
|
|
167
|
-
time: 0,
|
|
168
|
-
source: options.source,
|
|
169
|
-
position: 0,
|
|
170
|
-
currentPosition: 0,
|
|
171
|
-
};
|
|
182
|
+
options.parseInfo = createParseInfo(stream, options.source);
|
|
172
183
|
const result = doParseSync(new Tokenizer(options.parseInfo), options);
|
|
173
184
|
return options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
174
185
|
? result
|
|
@@ -191,7 +202,7 @@ function parseSync(...args) {
|
|
|
191
202
|
function transformSync(...args) {
|
|
192
203
|
let options;
|
|
193
204
|
let stream;
|
|
194
|
-
if (
|
|
205
|
+
if (isCssInput(args[0])) {
|
|
195
206
|
stream = args[0];
|
|
196
207
|
options = args[1];
|
|
197
208
|
}
|
|
@@ -201,7 +212,11 @@ function transformSync(...args) {
|
|
|
201
212
|
options = opt;
|
|
202
213
|
stream = input;
|
|
203
214
|
}
|
|
204
|
-
options
|
|
215
|
+
if (options != null) {
|
|
216
|
+
validateSyncArguments(options);
|
|
217
|
+
}
|
|
218
|
+
// the options of the caller are not modified: they may be reused
|
|
219
|
+
options = { ...options };
|
|
205
220
|
if (options.minify == null) {
|
|
206
221
|
options.minify = true;
|
|
207
222
|
}
|
|
@@ -212,12 +227,12 @@ function transformSync(...args) {
|
|
|
212
227
|
options.removeCharset = true;
|
|
213
228
|
}
|
|
214
229
|
const startTime = performance.now();
|
|
215
|
-
const parseResult =
|
|
230
|
+
const parseResult = parseSyncInput(stream, options);
|
|
216
231
|
let mapping = null;
|
|
217
232
|
let importMapping = null;
|
|
218
|
-
if ((typeof options.module
|
|
219
|
-
(typeof options.module
|
|
220
|
-
typeof options.module.scoped
|
|
233
|
+
if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
|
|
234
|
+
(typeof options.module === "object" &&
|
|
235
|
+
typeof options.module.scoped === "number" &&
|
|
221
236
|
options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
|
|
222
237
|
mapping = parseResult.mapping;
|
|
223
238
|
importMapping = parseResult.importMapping;
|
|
@@ -227,17 +242,12 @@ function transformSync(...args) {
|
|
|
227
242
|
...options,
|
|
228
243
|
expandNestingRules: false,
|
|
229
244
|
}, mapping != null ? { mapping, importMapping } : null);
|
|
230
|
-
return {
|
|
231
|
-
|
|
232
|
-
...
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
...parseResult.stats,
|
|
237
|
-
render: rendered.stats.total,
|
|
238
|
-
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
239
|
-
},
|
|
240
|
-
};
|
|
245
|
+
return new TransformResult(parseResult, rendered, {
|
|
246
|
+
bytesOut: RenderResult.codeLength(rendered),
|
|
247
|
+
...parseResult.stats,
|
|
248
|
+
render: rendered.stats.total,
|
|
249
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
250
|
+
});
|
|
241
251
|
}
|
|
242
252
|
/**
|
|
243
253
|
* Parse CSS
|
|
@@ -286,7 +296,7 @@ function transformSync(...args) {
|
|
|
286
296
|
async function parse(...args) {
|
|
287
297
|
let options;
|
|
288
298
|
let stream;
|
|
289
|
-
if (
|
|
299
|
+
if (isCssInput(args[0]) || args[0] instanceof ReadableStream) {
|
|
290
300
|
stream = args[0];
|
|
291
301
|
options = args[1];
|
|
292
302
|
}
|
|
@@ -295,37 +305,40 @@ async function parse(...args) {
|
|
|
295
305
|
const { file, input, ...opt } = args[0];
|
|
296
306
|
options = opt;
|
|
297
307
|
if (file != null) {
|
|
298
|
-
return Promise.resolve((options.load
|
|
308
|
+
return Promise.resolve((options.load != null
|
|
309
|
+
? options.load(file, "", options.asStream ?? false)
|
|
310
|
+
: // the default loader loads the file as an ArrayBuffer: the preferred input of the tokenizer
|
|
311
|
+
load(file, "", options.asStream ? ResponseType.ReadableStream : ResponseType.ArrayBuffer))).then((stream) => parse(stream, { src: file, ...options }));
|
|
299
312
|
}
|
|
300
313
|
else {
|
|
301
314
|
stream = input;
|
|
302
315
|
}
|
|
303
316
|
}
|
|
304
|
-
options
|
|
317
|
+
// the options of the caller are not modified: they may be reused
|
|
318
|
+
return parseInput(stream, { ...options });
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* parse a css input. the options belong to the call: they are modified (source, parse info, ...)
|
|
322
|
+
* @param stream
|
|
323
|
+
* @param options
|
|
324
|
+
*/
|
|
325
|
+
async function parseInput(stream, options) {
|
|
305
326
|
options.src ??= "";
|
|
306
327
|
options.sourcesMap ??= new Map();
|
|
307
328
|
Object.assign(options, {
|
|
308
|
-
load
|
|
329
|
+
// a custom loader is used to load the imported files
|
|
330
|
+
load: options.load ?? load,
|
|
309
331
|
resolve,
|
|
310
332
|
dirname,
|
|
311
333
|
cwd: options.cwd ?? cwd(),
|
|
312
334
|
});
|
|
313
335
|
options.src = resolve(options.src, options.cwd).relative;
|
|
314
336
|
if (options.source == null) {
|
|
315
|
-
const source = new SourceFile(typeof stream
|
|
337
|
+
const source = new SourceFile(typeof stream === "string" ? stream : "", [], options.src);
|
|
316
338
|
options.sourcesMap.set(source.id, source);
|
|
317
339
|
options.source = source;
|
|
318
340
|
}
|
|
319
|
-
options.parseInfo =
|
|
320
|
-
stream,
|
|
321
|
-
buffer: "",
|
|
322
|
-
src: options.src ?? "",
|
|
323
|
-
offset: 0,
|
|
324
|
-
time: 0,
|
|
325
|
-
source: options.source,
|
|
326
|
-
position: 0,
|
|
327
|
-
currentPosition: 0,
|
|
328
|
-
};
|
|
341
|
+
options.parseInfo = createParseInfo(stream, options.source);
|
|
329
342
|
return doParse(stream instanceof ReadableStream
|
|
330
343
|
? new Tokenizer(options.parseInfo, stream).tokenizeStream()
|
|
331
344
|
: new Tokenizer(options.parseInfo), options).then((result) => options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
@@ -377,7 +390,7 @@ async function parse(...args) {
|
|
|
377
390
|
async function transform(...args) {
|
|
378
391
|
let options;
|
|
379
392
|
let stream;
|
|
380
|
-
if (
|
|
393
|
+
if (isCssInput(args[0]) || args[0] instanceof ReadableStream) {
|
|
381
394
|
stream = args[0];
|
|
382
395
|
options = args[1];
|
|
383
396
|
}
|
|
@@ -386,13 +399,17 @@ async function transform(...args) {
|
|
|
386
399
|
const { file, input, ...opt } = args[0];
|
|
387
400
|
options = opt;
|
|
388
401
|
if (file != null) {
|
|
389
|
-
return Promise.resolve((options.load
|
|
402
|
+
return Promise.resolve((options.load != null
|
|
403
|
+
? options.load(file, "", options.asStream ?? false)
|
|
404
|
+
: // the default loader loads the file as an ArrayBuffer: the preferred input of the tokenizer
|
|
405
|
+
load(file, "", options.asStream ? ResponseType.ReadableStream : ResponseType.ArrayBuffer))).then((stream) => transform(stream, { src: file, ...options }));
|
|
390
406
|
}
|
|
391
407
|
else {
|
|
392
408
|
stream = input;
|
|
393
409
|
}
|
|
394
410
|
}
|
|
395
|
-
options
|
|
411
|
+
// the options of the caller are not modified: they may be reused
|
|
412
|
+
options = { ...options };
|
|
396
413
|
if (options.minify == null) {
|
|
397
414
|
options.minify = true;
|
|
398
415
|
}
|
|
@@ -403,12 +420,12 @@ async function transform(...args) {
|
|
|
403
420
|
options.removeCharset = true;
|
|
404
421
|
}
|
|
405
422
|
const startTime = performance.now();
|
|
406
|
-
return
|
|
423
|
+
return parseInput(stream, options).then((parseResult) => {
|
|
407
424
|
let mapping = null;
|
|
408
425
|
let importMapping = null;
|
|
409
|
-
if ((typeof options.module
|
|
410
|
-
(typeof options.module
|
|
411
|
-
typeof options.module.scoped
|
|
426
|
+
if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
|
|
427
|
+
(typeof options.module === "object" &&
|
|
428
|
+
typeof options.module.scoped === "number" &&
|
|
412
429
|
options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
|
|
413
430
|
mapping = parseResult.mapping;
|
|
414
431
|
importMapping = parseResult.importMapping;
|
|
@@ -418,18 +435,13 @@ async function transform(...args) {
|
|
|
418
435
|
...options,
|
|
419
436
|
expandNestingRules: false,
|
|
420
437
|
}, mapping != null ? { mapping, importMapping } : null);
|
|
421
|
-
return {
|
|
422
|
-
|
|
423
|
-
...
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
...parseResult.stats,
|
|
428
|
-
render: rendered.stats.total,
|
|
429
|
-
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
430
|
-
},
|
|
431
|
-
};
|
|
438
|
+
return new TransformResult(parseResult, rendered, {
|
|
439
|
+
bytesOut: RenderResult.codeLength(rendered),
|
|
440
|
+
...parseResult.stats,
|
|
441
|
+
render: rendered.stats.total,
|
|
442
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
443
|
+
});
|
|
432
444
|
});
|
|
433
445
|
}
|
|
434
446
|
|
|
435
|
-
export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
|
|
447
|
+
export { ModuleScopeEnumOptions, RenderResult, ResponseType, TransformResult, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../lib/ast/types.js';
|
|
9
|
-
import { AstArena } from '../lib/ast/arena.js';
|
|
10
9
|
import { dirname } from '../lib/fs/resolve.js';
|
|
11
10
|
import { ResponseType } from '../types.js';
|
|
12
11
|
|
|
@@ -26,10 +25,10 @@ function parseResult(result, options) {
|
|
|
26
25
|
// extract inline source map from the input CSS
|
|
27
26
|
const arena = result.arena;
|
|
28
27
|
const token = arena.lastChild[result.ast];
|
|
29
|
-
if (token
|
|
30
|
-
arena.dataTypes[token]
|
|
28
|
+
if (token !== 0 &&
|
|
29
|
+
arena.dataTypes[token] === EnumToken.CommentTokenType &&
|
|
31
30
|
arena.startsWith(token, "val", "/*# sourceMappingURL=")) {
|
|
32
|
-
let data = arena.
|
|
31
|
+
let data = arena.resolveString(arena.stringValue[token]).slice(21, -2).trim();
|
|
33
32
|
if (data.endsWith(".map")) {
|
|
34
33
|
if (options.load == null) {
|
|
35
34
|
data = "";
|
|
@@ -69,12 +68,18 @@ function validateSyncArguments(options, prefix = "options.") {
|
|
|
69
68
|
let i;
|
|
70
69
|
for (i = 0; i < args.length; i++) {
|
|
71
70
|
const [key, value] = args[i];
|
|
72
|
-
if (typeof value
|
|
71
|
+
if (typeof value === "function") {
|
|
73
72
|
if (value instanceof Promise || Object.getPrototypeOf(value).constructor.name == "AsyncFunction") {
|
|
74
73
|
throw new Error(`[${prefix + key}]: Async functions are not supported in sync mode. Use parse() or transform() instead.`);
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
|
-
else if (value != null &&
|
|
76
|
+
else if (value != null &&
|
|
77
|
+
typeof value === "object" &&
|
|
78
|
+
// the plain objects (visitors, ...) and the arrays: the class instances (arenas, source files, maps, typed
|
|
79
|
+
// arrays, ...) are not traversed
|
|
80
|
+
(Array.isArray(value) ||
|
|
81
|
+
Object.getPrototypeOf(value) === Object.prototype ||
|
|
82
|
+
Object.getPrototypeOf(value) === null)) {
|
|
78
83
|
validateSyncArguments(value, prefix + key + ".");
|
|
79
84
|
}
|
|
80
85
|
}
|
package/dist/version.js
ADDED