@tbela99/css-parser 1.3.4 → 1.4.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/dist/web.js CHANGED
@@ -1,4 +1,5 @@
1
- export { ColorType, EnumToken, ValidationLevel } from './lib/ast/types.js';
1
+ import { ModuleScopeEnumOptions } from './lib/ast/types.js';
2
+ export { ColorType, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
2
3
  export { minify } from './lib/ast/minify.js';
3
4
  export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
4
5
  export { expand } from './lib/ast/expand.js';
@@ -17,6 +18,7 @@ import './lib/validation/parser/parse.js';
17
18
  import './lib/validation/syntaxes/complex-selector.js';
18
19
  import './lib/validation/syntax.js';
19
20
  import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
21
+ import { ResponseType } from './types.js';
20
22
  export { FeatureWalkMode } from './lib/ast/features/type.js';
21
23
 
22
24
  /**
@@ -24,10 +26,13 @@ export { FeatureWalkMode } from './lib/ast/features/type.js';
24
26
  * @param url
25
27
  * @param currentFile
26
28
  *
27
- * @param asStream
29
+ * @param responseType
28
30
  * @private
29
31
  */
30
- async function load(url, currentFile = '.', asStream = false) {
32
+ async function load(url, currentFile = '.', responseType = false) {
33
+ if (typeof responseType == 'boolean') {
34
+ responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
35
+ }
31
36
  let t;
32
37
  if (matchUrl.test(url)) {
33
38
  t = new URL(url);
@@ -43,13 +48,17 @@ async function load(url, currentFile = '.', asStream = false) {
43
48
  if (!response.ok) {
44
49
  throw new Error(`${response.status} ${response.statusText} ${response.url}`);
45
50
  }
46
- return asStream ? response.body : await response.text();
51
+ if (responseType == ResponseType.ArrayBuffer) {
52
+ return response.arrayBuffer();
53
+ }
54
+ return responseType == ResponseType.ReadableStream ? response.body : await response.text();
47
55
  });
48
56
  }
49
57
  /**
50
58
  * render the ast tree
51
59
  * @param data
52
60
  * @param options
61
+ * @param mapping
53
62
  *
54
63
  * Example:
55
64
  *
@@ -74,12 +83,12 @@ async function load(url, currentFile = '.', asStream = false) {
74
83
  * // }
75
84
  * ```
76
85
  */
77
- function render(data, options = {}) {
86
+ function render(data, options = {}, mapping) {
78
87
  return doRender(data, Object.assign(options, {
79
88
  resolve,
80
89
  dirname,
81
90
  cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
82
- }));
91
+ }), mapping);
83
92
  }
84
93
  /**
85
94
  * parse css file
@@ -139,6 +148,7 @@ async function parse(stream, options = {}) {
139
148
  return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
140
149
  stream,
141
150
  buffer: '',
151
+ offset: 0,
142
152
  position: { ind: 0, lin: 1, col: 1 },
143
153
  currentPosition: { ind: -1, lin: 1, col: 0 }
144
154
  }), Object.assign(options, {
@@ -146,7 +156,10 @@ async function parse(stream, options = {}) {
146
156
  resolve,
147
157
  dirname,
148
158
  cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
149
- }));
159
+ })).then(result => {
160
+ const { revMapping, ...res } = result;
161
+ return res;
162
+ });
150
163
  }
151
164
  /**
152
165
  * transform css file
@@ -198,8 +211,21 @@ async function transform(css, options = {}) {
198
211
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
199
212
  const startTime = performance.now();
200
213
  return parse(css, options).then((parseResult) => {
214
+ let mapping = null;
215
+ let importMapping = null;
216
+ if (typeof options.module == 'number' && (options.module & ModuleScopeEnumOptions.ICSS)) {
217
+ mapping = parseResult.mapping;
218
+ importMapping = parseResult.importMapping;
219
+ }
220
+ else if (typeof options.module == 'object' && typeof options.module.scoped == 'number' && (options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
221
+ mapping = parseResult.mapping;
222
+ importMapping = parseResult.importMapping;
223
+ }
201
224
  // ast already expanded by parse
202
- const rendered = render(parseResult.ast, { ...options, expandNestingRules: false });
225
+ const rendered = render(parseResult.ast, {
226
+ ...options,
227
+ expandNestingRules: false
228
+ }, mapping != null ? { mapping, importMapping } : null);
203
229
  return {
204
230
  ...parseResult,
205
231
  ...rendered,
@@ -214,4 +240,4 @@ async function transform(css, options = {}) {
214
240
  });
215
241
  }
216
242
 
217
- export { dirname, load, parse, parseFile, render, resolve, transform, transformFile };
243
+ export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, render, resolve, transform, transformFile };
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.3.4",
4
+ "version": "v1.4.0",
5
5
  "exports": {
6
6
  ".": "./dist/node.js",
7
7
  "./node": "./dist/node.js",
@@ -15,15 +15,15 @@
15
15
  },
16
16
  "scripts": {
17
17
  "doc": "typedoc --tsconfig typedoc-tsconfig.jsonc",
18
- "build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
19
- "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
- "test:web": "web-test-runner \"test/**/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
18
+ "build": "rollup -c;./build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
19
+ "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
+ "test:web": "web-test-runner \"./test/**/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
21
21
  "test:node": "mocha -p --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
22
22
  "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
23
  "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
24
  "profile": "node --enable-source-maps --inspect-brk test/inspect.js",
25
25
  "syntax-update": "esno tools/validation.ts",
26
- "debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
26
+ "debug": "web-test-runner \"./test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
@@ -55,24 +55,24 @@
55
55
  "homepage": "https://github.com/tbela99/css-parser#readme",
56
56
  "devDependencies": {
57
57
  "@esm-bundle/chai": "^4.3.4-fix.0",
58
- "@rollup/plugin-commonjs": "^28.0.6",
58
+ "@rollup/plugin-commonjs": "^28.0.8",
59
59
  "@rollup/plugin-json": "^6.1.0",
60
- "@rollup/plugin-node-resolve": "^16.0.1",
61
- "@rollup/plugin-typescript": "^12.1.4",
62
- "@types/chai": "^5.2.2",
60
+ "@rollup/plugin-node-resolve": "^16.0.3",
61
+ "@rollup/plugin-typescript": "^12.3.0",
62
+ "@types/chai": "^5.2.3",
63
63
  "@types/mocha": "^10.0.10",
64
- "@types/node": "^24.0.10",
65
- "@types/web": "^0.0.245",
64
+ "@types/node": "^24.9.1",
65
+ "@types/web": "^0.0.274",
66
66
  "@web/test-runner": "^0.20.2",
67
67
  "@web/test-runner-playwright": "^0.11.1",
68
68
  "c8": "^10.1.3",
69
69
  "esno": "^4.8.0",
70
- "mocha": "^11.7.1",
71
- "playwright": "^1.55.0",
72
- "rollup": "^4.48.0",
73
- "rollup-plugin-dts": "^6.2.1",
70
+ "mocha": "^11.7.4",
71
+ "playwright": "^1.56.1",
72
+ "rollup": "^4.52.5",
73
+ "rollup-plugin-dts": "^6.2.3",
74
74
  "tslib": "^2.8.1",
75
- "typedoc": "^0.28.13",
75
+ "typedoc": "^0.28.14",
76
76
  "typedoc-material-theme": "^1.4.0"
77
77
  }
78
78
  }