@tbela99/css-parser 1.4.8 → 1.4.9
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 +28 -3
- package/dist/index-umd-web.js +12897 -13217
- package/dist/index.cjs +10394 -10716
- package/dist/index.d.ts +816 -89
- package/dist/lib/ast/features/calc.js +3 -1
- package/dist/lib/ast/features/if.js +2 -96
- package/dist/lib/ast/features/prefix.js +7 -2
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/find.js +6 -6
- package/dist/lib/ast/math/expression.js +30 -12
- package/dist/lib/ast/minify.js +34 -5
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/utils.js +29 -21
- package/dist/lib/ast/types.js +8 -0
- package/dist/lib/fs/resolve.js +1 -16
- package/dist/lib/parser/declaration/list.js +6 -1
- package/dist/lib/parser/{linemap.js → linesmap.js} +25 -9
- package/dist/lib/parser/parse.js +1264 -151
- package/dist/lib/parser/source.js +48 -17
- package/dist/lib/parser/tokenize.js +112 -133
- package/dist/lib/parser/utils/at-rule-container.js +12 -44
- package/dist/lib/parser/utils/at-rule-generic.js +12 -24
- package/dist/lib/parser/utils/at-rule-import.js +4 -172
- package/dist/lib/parser/utils/at-rule-media.js +14 -251
- package/dist/lib/parser/utils/at-rule-support.js +8 -258
- package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
- package/dist/lib/parser/utils/declaration.js +5 -95
- package/dist/lib/parser/utils/hash.js +54 -1
- package/dist/lib/parser/utils/selector.js +1 -1
- package/dist/lib/parser/utils/token.js +1 -0
- package/dist/lib/renderer/render.js +76 -43
- package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
- package/dist/lib/syntax/color/cmyk.js +3 -3
- package/dist/lib/syntax/color/color-mix.js +7 -7
- package/dist/lib/syntax/color/color.js +7 -7
- package/dist/lib/syntax/color/hsl.js +6 -6
- package/dist/lib/syntax/color/hwb.js +6 -6
- package/dist/lib/syntax/color/lab.js +5 -5
- package/dist/lib/syntax/color/lch.js +6 -6
- package/dist/lib/syntax/color/oklab.js +5 -5
- package/dist/lib/syntax/color/oklch.js +5 -5
- package/dist/lib/syntax/color/relative-color.js +8 -1
- package/dist/lib/syntax/color/utils/distance.js +2 -2
- package/dist/lib/syntax/constants.js +2 -1
- package/dist/lib/syntax/syntax.js +11 -8
- package/dist/lib/validation/config.json.js +27 -6
- package/dist/lib/validation/match.js +42 -845
- package/dist/node.js +134 -9
- package/dist/web.js +133 -6
- package/llms.txt +73 -0
- package/package.json +13 -16
- package/dist/lib/parser/arena.js +0 -206
package/dist/node.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import process from 'node:process';
|
|
2
1
|
import { deprecate } from 'node:util';
|
|
3
2
|
import { Readable } from 'node:stream';
|
|
4
3
|
import { createReadStream } from 'node:fs';
|
|
5
4
|
import { lstat, readFile } from 'node:fs/promises';
|
|
6
|
-
import { doParse } from './lib/parser/parse.js';
|
|
5
|
+
import { doParse, doParseSync } from './lib/parser/parse.js';
|
|
7
6
|
export { parseDeclarations, parseString } from './lib/parser/parse.js';
|
|
8
7
|
import { doRender } from './lib/renderer/render.js';
|
|
9
8
|
export { renderValue as renderToken } from './lib/renderer/render.js';
|
|
@@ -12,6 +11,9 @@ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, Valid
|
|
|
12
11
|
import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
|
|
13
12
|
import { dirname, resolve, matchUrl } from './lib/fs/resolve.js';
|
|
14
13
|
import { ResponseType } from './types.js';
|
|
14
|
+
import { resolve as resolve$1 } from 'node:path';
|
|
15
|
+
import { SourceFile } from './lib/parser/source.js';
|
|
16
|
+
import { cwd } from 'node:process';
|
|
15
17
|
export { minify } from './lib/ast/minify.js';
|
|
16
18
|
export { expand } from './lib/ast/expand.js';
|
|
17
19
|
export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
|
|
@@ -103,7 +105,7 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
103
105
|
* ```
|
|
104
106
|
*/
|
|
105
107
|
function render(data, options = {}, mapping) {
|
|
106
|
-
return doRender(data, Object.assign(options, { resolve, dirname, cwd: options.cwd ??
|
|
108
|
+
return doRender(data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? resolve$1() }), mapping);
|
|
107
109
|
}
|
|
108
110
|
/**
|
|
109
111
|
* Parse css file
|
|
@@ -133,9 +135,125 @@ function render(data, options = {}, mapping) {
|
|
|
133
135
|
const parseFile = deprecate(async (file, options = {}, asStream = false) => parse({ file, asStream, ...options }), "parseFile is deprecated, use parse instead as parse({file, asStream, ...options})");
|
|
134
136
|
/**
|
|
135
137
|
* Parse css
|
|
136
|
-
* @param
|
|
138
|
+
* @param args
|
|
139
|
+
*
|
|
140
|
+
* Parsing a string
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
*
|
|
144
|
+
* import {parse} from '@tbela99/css-parser';
|
|
145
|
+
*
|
|
146
|
+
* // css string
|
|
147
|
+
* let result = await parse(css, {nestingRules: true});
|
|
148
|
+
* console.log(result.ast);
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
152
|
+
function parseSync(...args) {
|
|
153
|
+
let options;
|
|
154
|
+
let stream;
|
|
155
|
+
if (typeof args[0] === "string") {
|
|
156
|
+
stream = args[0];
|
|
157
|
+
options = args[1];
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const { input, ...opt } = args[0];
|
|
161
|
+
options = opt;
|
|
162
|
+
stream = input;
|
|
163
|
+
}
|
|
164
|
+
options ??= {};
|
|
165
|
+
options.src ??= "";
|
|
166
|
+
options.sourcesMap ??= new Map;
|
|
167
|
+
Object.assign(options, {
|
|
168
|
+
resolve,
|
|
169
|
+
dirname,
|
|
170
|
+
cwd: options.cwd ?? cwd(),
|
|
171
|
+
});
|
|
172
|
+
options.src = resolve(options.src, options.cwd).relative;
|
|
173
|
+
if (options.source == null) {
|
|
174
|
+
const source = new SourceFile(typeof stream == "string" ? stream : "", [], options.src);
|
|
175
|
+
options.sourcesMap.set(source.id, source);
|
|
176
|
+
options.source = source;
|
|
177
|
+
}
|
|
178
|
+
options.parseInfo = {
|
|
179
|
+
stream,
|
|
180
|
+
buffer: "",
|
|
181
|
+
src: options.src ?? "",
|
|
182
|
+
offset: 0,
|
|
183
|
+
time: 0,
|
|
184
|
+
source: options.source,
|
|
185
|
+
position: 0,
|
|
186
|
+
currentPosition: -1,
|
|
187
|
+
};
|
|
188
|
+
const result = doParseSync(tokenize(options.parseInfo), options);
|
|
189
|
+
const { revMapping, ...res } = result;
|
|
190
|
+
return res;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Transform css
|
|
194
|
+
* @param css
|
|
137
195
|
* @param options
|
|
138
196
|
*
|
|
197
|
+
* ```ts
|
|
198
|
+
*
|
|
199
|
+
* import {transform} from '@tbela99/css-parser';
|
|
200
|
+
*
|
|
201
|
+
* // css string
|
|
202
|
+
* const result = await transform(css);
|
|
203
|
+
* console.log(result.code);
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
*/
|
|
207
|
+
function transformSync(...args) {
|
|
208
|
+
let options;
|
|
209
|
+
let stream;
|
|
210
|
+
if (typeof args[0] === "string") {
|
|
211
|
+
stream = args[0];
|
|
212
|
+
options = args[1];
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const { input, ...opt } = args[0];
|
|
216
|
+
// @ts-ignore
|
|
217
|
+
options = opt;
|
|
218
|
+
stream = input;
|
|
219
|
+
}
|
|
220
|
+
options ??= {};
|
|
221
|
+
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
222
|
+
const startTime = performance.now();
|
|
223
|
+
const parseResult = parseSync(stream, options);
|
|
224
|
+
let mapping = null;
|
|
225
|
+
let importMapping = null;
|
|
226
|
+
if (typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) {
|
|
227
|
+
mapping = parseResult.mapping;
|
|
228
|
+
importMapping = parseResult.importMapping;
|
|
229
|
+
}
|
|
230
|
+
else if (typeof options.module == "object" &&
|
|
231
|
+
typeof options.module.scoped == "number" &&
|
|
232
|
+
options.module.scoped & ModuleScopeEnumOptions.ICSS) {
|
|
233
|
+
mapping = parseResult.mapping;
|
|
234
|
+
importMapping = parseResult.importMapping;
|
|
235
|
+
}
|
|
236
|
+
// ast already expanded by parse
|
|
237
|
+
const rendered = render(parseResult.ast, {
|
|
238
|
+
...options,
|
|
239
|
+
expandNestingRules: false,
|
|
240
|
+
}, mapping != null ? { mapping, importMapping } : null);
|
|
241
|
+
return {
|
|
242
|
+
...parseResult,
|
|
243
|
+
...rendered,
|
|
244
|
+
errors: parseResult.errors.concat(rendered.errors),
|
|
245
|
+
stats: {
|
|
246
|
+
bytesOut: rendered.code.length,
|
|
247
|
+
...parseResult.stats,
|
|
248
|
+
render: rendered.stats.total,
|
|
249
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Parse css
|
|
255
|
+
* @param args
|
|
256
|
+
*
|
|
139
257
|
* @throws Error file not found
|
|
140
258
|
*
|
|
141
259
|
* Parsing a string
|
|
@@ -196,21 +314,28 @@ async function parse(...args) {
|
|
|
196
314
|
}
|
|
197
315
|
options ??= {};
|
|
198
316
|
options.src ??= "";
|
|
317
|
+
options.sourcesMap ??= new Map;
|
|
199
318
|
Object.assign(options, {
|
|
200
319
|
load,
|
|
201
320
|
resolve,
|
|
202
321
|
dirname,
|
|
203
|
-
cwd: options.cwd ??
|
|
322
|
+
cwd: options.cwd ?? cwd(),
|
|
204
323
|
});
|
|
205
324
|
options.src = resolve(options.src, options.cwd).relative;
|
|
325
|
+
if (options.source == null) {
|
|
326
|
+
const source = new SourceFile(typeof stream == "string" ? stream : "", [], options.src);
|
|
327
|
+
options.sourcesMap.set(source.id, source);
|
|
328
|
+
options.source = source;
|
|
329
|
+
}
|
|
206
330
|
options.parseInfo = {
|
|
207
331
|
stream,
|
|
208
332
|
buffer: "",
|
|
209
333
|
src: options.src ?? "",
|
|
210
334
|
offset: 0,
|
|
211
335
|
time: 0,
|
|
212
|
-
|
|
213
|
-
|
|
336
|
+
source: options.source,
|
|
337
|
+
position: 0,
|
|
338
|
+
currentPosition: -1,
|
|
214
339
|
};
|
|
215
340
|
return doParse(stream instanceof ReadableStream ? tokenizeStream(stream, options.parseInfo) : tokenize(options.parseInfo), options).then((result) => {
|
|
216
341
|
const { revMapping, ...res } = result;
|
|
@@ -251,7 +376,7 @@ const transformFile = deprecate(async (file, options = {}, asStream = false) =>
|
|
|
251
376
|
* @param css
|
|
252
377
|
* @param options
|
|
253
378
|
*
|
|
254
|
-
*
|
|
379
|
+
* Parsing a string
|
|
255
380
|
*
|
|
256
381
|
* ```ts
|
|
257
382
|
*
|
|
@@ -342,4 +467,4 @@ async function transform(...args) {
|
|
|
342
467
|
});
|
|
343
468
|
}
|
|
344
469
|
|
|
345
|
-
export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, render, resolve, transform, transformFile };
|
|
470
|
+
export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, parseSync, render, resolve, transform, transformFile, transformSync };
|
package/dist/web.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { doParse } from './lib/parser/parse.js';
|
|
1
|
+
import { doParse, doParseSync } from './lib/parser/parse.js';
|
|
2
2
|
export { parseDeclarations, parseString } from './lib/parser/parse.js';
|
|
3
3
|
import { doRender } from './lib/renderer/render.js';
|
|
4
4
|
export { renderValue as renderToken } from './lib/renderer/render.js';
|
|
@@ -7,6 +7,7 @@ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, Valid
|
|
|
7
7
|
import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
|
|
8
8
|
import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
|
|
9
9
|
import { ResponseType } from './types.js';
|
|
10
|
+
import { SourceFile } from './lib/parser/source.js';
|
|
10
11
|
export { minify } from './lib/ast/minify.js';
|
|
11
12
|
export { expand } from './lib/ast/expand.js';
|
|
12
13
|
export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
|
|
@@ -124,6 +125,125 @@ async function parseFile(file, options = {}, asStream = false) {
|
|
|
124
125
|
console.warn("DeprecationWarning: parseFile is deprecated, use parse instead as parse({file, asStream, ...options})");
|
|
125
126
|
return parse({ file, asStream, ...options });
|
|
126
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Parse css
|
|
130
|
+
* @param args
|
|
131
|
+
*
|
|
132
|
+
* Parsing a string
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
*
|
|
136
|
+
* import {parse} from '@tbela99/css-parser';
|
|
137
|
+
*
|
|
138
|
+
* // css string
|
|
139
|
+
* let result = await parse(css, {nestingRules: true});
|
|
140
|
+
* console.log(result.ast);
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
*/
|
|
144
|
+
function parseSync(...args) {
|
|
145
|
+
let options;
|
|
146
|
+
let stream;
|
|
147
|
+
if (typeof args[0] === "string") {
|
|
148
|
+
stream = args[0];
|
|
149
|
+
options = args[1];
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const { input, ...opt } = args[0];
|
|
153
|
+
options = opt;
|
|
154
|
+
stream = input;
|
|
155
|
+
}
|
|
156
|
+
options ??= {};
|
|
157
|
+
options.src ??= "";
|
|
158
|
+
options.sourcesMap ??= new Map;
|
|
159
|
+
Object.assign(options, {
|
|
160
|
+
resolve,
|
|
161
|
+
dirname,
|
|
162
|
+
cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
|
|
163
|
+
? self.location.pathname
|
|
164
|
+
: dirname(self.location.pathname),
|
|
165
|
+
});
|
|
166
|
+
options.src = resolve(options.src, options.cwd).relative;
|
|
167
|
+
if (options.source == null) {
|
|
168
|
+
const source = new SourceFile(typeof stream == "string" ? stream : "", [], options.src);
|
|
169
|
+
options.sourcesMap.set(source.id, source);
|
|
170
|
+
options.source = source;
|
|
171
|
+
}
|
|
172
|
+
options.parseInfo = {
|
|
173
|
+
stream,
|
|
174
|
+
buffer: "",
|
|
175
|
+
src: options.src ?? "",
|
|
176
|
+
offset: 0,
|
|
177
|
+
time: 0,
|
|
178
|
+
source: options.source,
|
|
179
|
+
position: 0,
|
|
180
|
+
currentPosition: -1,
|
|
181
|
+
};
|
|
182
|
+
const result = doParseSync(tokenize(options.parseInfo), options);
|
|
183
|
+
const { revMapping, ...res } = result;
|
|
184
|
+
return res;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Transform css
|
|
188
|
+
* @param css
|
|
189
|
+
* @param options
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
*
|
|
193
|
+
* import {transform} from '@tbela99/css-parser';
|
|
194
|
+
*
|
|
195
|
+
* // css string
|
|
196
|
+
* const result = await transform(css);
|
|
197
|
+
* console.log(result.code);
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
*/
|
|
201
|
+
function transformSync(...args) {
|
|
202
|
+
let options;
|
|
203
|
+
let stream;
|
|
204
|
+
if (typeof args[0] === "string") {
|
|
205
|
+
stream = args[0];
|
|
206
|
+
options = args[1];
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
const { input, ...opt } = args[0];
|
|
210
|
+
// @ts-ignore
|
|
211
|
+
options = opt;
|
|
212
|
+
stream = input;
|
|
213
|
+
}
|
|
214
|
+
options ??= {};
|
|
215
|
+
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
216
|
+
const startTime = performance.now();
|
|
217
|
+
const parseResult = parseSync(stream, options);
|
|
218
|
+
let mapping = null;
|
|
219
|
+
let importMapping = null;
|
|
220
|
+
if (typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) {
|
|
221
|
+
mapping = parseResult.mapping;
|
|
222
|
+
importMapping = parseResult.importMapping;
|
|
223
|
+
}
|
|
224
|
+
else if (typeof options.module == "object" &&
|
|
225
|
+
typeof options.module.scoped == "number" &&
|
|
226
|
+
options.module.scoped & ModuleScopeEnumOptions.ICSS) {
|
|
227
|
+
mapping = parseResult.mapping;
|
|
228
|
+
importMapping = parseResult.importMapping;
|
|
229
|
+
}
|
|
230
|
+
// ast already expanded by parse
|
|
231
|
+
const rendered = render(parseResult.ast, {
|
|
232
|
+
...options,
|
|
233
|
+
expandNestingRules: false,
|
|
234
|
+
}, mapping != null ? { mapping, importMapping } : null);
|
|
235
|
+
return {
|
|
236
|
+
...parseResult,
|
|
237
|
+
...rendered,
|
|
238
|
+
errors: parseResult.errors.concat(rendered.errors),
|
|
239
|
+
stats: {
|
|
240
|
+
bytesOut: rendered.code.length,
|
|
241
|
+
...parseResult.stats,
|
|
242
|
+
render: rendered.stats.total,
|
|
243
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
127
247
|
/**
|
|
128
248
|
* Parse css
|
|
129
249
|
* @param stream
|
|
@@ -172,6 +292,7 @@ async function parse(...args) {
|
|
|
172
292
|
}
|
|
173
293
|
options ??= {};
|
|
174
294
|
options.src ??= "";
|
|
295
|
+
options.sourcesMap ??= new Map;
|
|
175
296
|
Object.assign(options, {
|
|
176
297
|
load,
|
|
177
298
|
resolve,
|
|
@@ -180,16 +301,22 @@ async function parse(...args) {
|
|
|
180
301
|
? self.location.pathname
|
|
181
302
|
: dirname(self.location.pathname),
|
|
182
303
|
});
|
|
304
|
+
options.src = resolve(options.src, options.cwd).relative;
|
|
305
|
+
if (options.source == null) {
|
|
306
|
+
const source = new SourceFile(typeof stream === "string" ? stream : "", [], options.src);
|
|
307
|
+
options.sourcesMap.set(source.id, source);
|
|
308
|
+
options.source = source;
|
|
309
|
+
}
|
|
183
310
|
options.parseInfo = {
|
|
184
311
|
stream,
|
|
185
312
|
buffer: "",
|
|
186
313
|
offset: 0,
|
|
187
314
|
time: 0,
|
|
188
|
-
|
|
189
|
-
position:
|
|
190
|
-
currentPosition:
|
|
315
|
+
source: options.source,
|
|
316
|
+
position: 0,
|
|
317
|
+
currentPosition: -1,
|
|
191
318
|
};
|
|
192
|
-
return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize(options.parseInfo), options).then((result) => {
|
|
319
|
+
return doParse(stream instanceof ReadableStream ? tokenizeStream(stream, options.parseInfo) : tokenize(options.parseInfo), options).then((result) => {
|
|
193
320
|
const { revMapping, ...res } = result;
|
|
194
321
|
return res;
|
|
195
322
|
});
|
|
@@ -298,4 +425,4 @@ async function transform(...args) {
|
|
|
298
425
|
});
|
|
299
426
|
}
|
|
300
427
|
|
|
301
|
-
export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, render, resolve, transform, transformFile };
|
|
428
|
+
export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, parseSync, render, resolve, transform, transformFile, transformSync };
|
package/llms.txt
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# css-parser
|
|
2
|
+
|
|
3
|
+
## Project overview
|
|
4
|
+
- This repository contains css-parser, a dependency-free CSS parser, transformer, minifier, and validator for Node.js and browsers.
|
|
5
|
+
- The library follows the CSS Syntax Module Level 3 specification and uses MDN syntax data for validation.
|
|
6
|
+
- It is designed for fault-tolerant parsing, AST manipulation, CSS Modules, minification, color processing, and syntax lowering.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
- npm: npm install @tbela99/css-parser
|
|
10
|
+
- jsr: deno add @tbela99/css-parser
|
|
11
|
+
- Main entry points:
|
|
12
|
+
- @tbela99/css-parser or @tbela99/css-parser/node
|
|
13
|
+
- @tbela99/css-parser/web
|
|
14
|
+
|
|
15
|
+
## Core APIs
|
|
16
|
+
- parse(css, options): async api to parse CSS and returns an AST, errors, and stats.
|
|
17
|
+
- parseSync(css, options): sync api to parse CSS and returns an AST, errors, and stats.
|
|
18
|
+
- render(ast, options): renders an AST back to CSS text.
|
|
19
|
+
- transform(css, options): async api to parse CSS and generate output in one step.
|
|
20
|
+
- transformSync(css, options): sync api to parse CSS and generate output in one step.
|
|
21
|
+
- walk(ast, filter): traverses the AST for custom transformations.
|
|
22
|
+
|
|
23
|
+
## Common capabilities
|
|
24
|
+
- Parse CSS into an AST and inspect or modify it.
|
|
25
|
+
- Validate declarations against CSS syntax definitions.
|
|
26
|
+
- Support CSS Modules with scoped class generation.
|
|
27
|
+
- Minify CSS safely with options such as inlineCssVariables, computeCalcExpression, removeDuplicateDeclarations, and beautify.
|
|
28
|
+
- Transform ASTs through visitors and custom traversal logic.
|
|
29
|
+
- Lower modern CSS syntax such as nested CSS and if() to broadly compatible output.
|
|
30
|
+
- Generate source maps and handle advanced color functions and conversions.
|
|
31
|
+
|
|
32
|
+
## Typical usage
|
|
33
|
+
```ts
|
|
34
|
+
import {transform, ColorType} from '@tbela99/css-parser';
|
|
35
|
+
|
|
36
|
+
const css = `.foo { color: red; }`;
|
|
37
|
+
const result = await transform(css, {
|
|
38
|
+
beautify: true,
|
|
39
|
+
convertColor: ColorType.SRGB,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(result.code);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Important behavior notes
|
|
46
|
+
- parse() and transform() are lenient by default and preserve unknown constructs unless configured otherwise.
|
|
47
|
+
- Comments are removed by default; preserve them with removeComments: false or preserveLicense: true.
|
|
48
|
+
- Validation errors are available through the parse/transform result and through node.state and node.errors.
|
|
49
|
+
- The library prioritizes compact output while preserving semantics.
|
|
50
|
+
|
|
51
|
+
## Useful concepts
|
|
52
|
+
- parse() returns AST, errors, and stats.
|
|
53
|
+
- render() turns an AST into CSS text.
|
|
54
|
+
- transform() performs parsing and rendering together.
|
|
55
|
+
- AST node types include StyleSheet, Rule, AtRule, Declaration, Comment, and Keyframes variants.
|
|
56
|
+
|
|
57
|
+
## Documentation files
|
|
58
|
+
- files/getting-started.md
|
|
59
|
+
- files/usage.md
|
|
60
|
+
- files/validation.md
|
|
61
|
+
- files/css-module.md
|
|
62
|
+
- files/minification.md
|
|
63
|
+
- files/transform.md
|
|
64
|
+
- files/syntax-lowering.md
|
|
65
|
+
- files/ast.md
|
|
66
|
+
- files/utilities.md
|
|
67
|
+
|
|
68
|
+
## Repository layout
|
|
69
|
+
- src/: parser implementation and core library code
|
|
70
|
+
- test/: regression and behavior tests
|
|
71
|
+
- files/: documentation content used by the docs site
|
|
72
|
+
- benchmark/: performance and minification benchmarks
|
|
73
|
+
- scripts/: helper scripts and patches
|
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": "1.4.
|
|
4
|
+
"version": "1.4.9",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
@@ -60,35 +60,32 @@
|
|
|
60
60
|
},
|
|
61
61
|
"homepage": "https://github.com/tbela99/css-parser#readme",
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@esm-bundle/chai": "^4.3.4-fix.0",
|
|
64
63
|
"@rollup/plugin-commonjs": "^29.0.3",
|
|
65
64
|
"@rollup/plugin-json": "^6.1.0",
|
|
66
65
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
67
66
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
68
67
|
"@types/chai": "^5.2.3",
|
|
69
68
|
"@types/mocha": "^10.0.10",
|
|
70
|
-
"@types/node": "^26.
|
|
71
|
-
"@types/web": "^0.0.
|
|
69
|
+
"@types/node": "^26.2.0",
|
|
70
|
+
"@types/web": "^0.0.354",
|
|
72
71
|
"@web/test-runner": "^1.0.0",
|
|
73
72
|
"@web/test-runner-playwright": "^1.0.0",
|
|
73
|
+
"chai": "^6.2.2",
|
|
74
74
|
"esno": "^4.8.0",
|
|
75
75
|
"markdown-it-task-lists": "^2.1.1",
|
|
76
|
-
"mocha": "^11.
|
|
77
|
-
"playwright": "^1.
|
|
78
|
-
"rollup": "^4.62.
|
|
79
|
-
"rollup-plugin-dts": "^6.
|
|
76
|
+
"mocha": "^11.8.0",
|
|
77
|
+
"playwright": "^1.62.1",
|
|
78
|
+
"rollup": "^4.62.4",
|
|
79
|
+
"rollup-plugin-dts": "^6.5.1",
|
|
80
80
|
"superc8": "^12.7.0",
|
|
81
81
|
"tslib": "^2.8.1",
|
|
82
82
|
"typedoc": "^0.28.20",
|
|
83
83
|
"typedoc-plugin-coverage": "^4.0.3",
|
|
84
|
+
"typedoc-plugin-llms-txt": "^0.1.2",
|
|
84
85
|
"typedoc-plugin-redirect": "^1.3.0",
|
|
85
|
-
"typedoc-unhoax-theme": "^0.5.3"
|
|
86
|
+
"typedoc-unhoax-theme": "^0.5.3",
|
|
87
|
+
"@types/chai-as-promised": "^8.0.2",
|
|
88
|
+
"chai-as-promised": "^8.0.2"
|
|
86
89
|
},
|
|
87
|
-
"sideEffects": false
|
|
88
|
-
"allowScripts": {
|
|
89
|
-
"esbuild@0.28.0": true,
|
|
90
|
-
"esbuild@0.28.1": true,
|
|
91
|
-
"fsevents@2.3.2": true,
|
|
92
|
-
"fsevents@2.3.3": true
|
|
93
|
-
}
|
|
90
|
+
"sideEffects": false
|
|
94
91
|
}
|