@tbela99/rust-css-parser-wasm 0.0.0-stage → 2.3.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.
@@ -0,0 +1,31 @@
1
+ // Enums of the typescript API (data only). Generated by scripts/gen-enums.mjs.
2
+
3
+ export const ResponseType = Object.freeze({
4
+ "Text": 0,
5
+ "ReadableStream": 1,
6
+ "ArrayBuffer": 2,
7
+ "JSON": 3,
8
+ "0": "Text",
9
+ "1": "ReadableStream",
10
+ "2": "ArrayBuffer",
11
+ "3": "JSON",
12
+ });
13
+
14
+ export const StringFlag = Object.freeze({
15
+ "Color": 0,
16
+ "SimpleSelector": 1,
17
+ "VendorPrefix": 2,
18
+ "0": "Color",
19
+ "1": "SimpleSelector",
20
+ "2": "VendorPrefix",
21
+ });
22
+
23
+ export const StringSource = Object.freeze({
24
+ "Pool": -1,
25
+ "Canonical": -2,
26
+ "Value": -3,
27
+ "-1": "Pool",
28
+ "-2": "Canonical",
29
+ "-3": "Value",
30
+ });
31
+
@@ -0,0 +1,16 @@
1
+ // exports shared by node.js and web.js
2
+ export { RenderResult, TransformResult } from "./printer/result.js";
3
+ export { minify } from "./ast/minify.js";
4
+ export { parseDeclarations, parseString } from "./parser/parse.js";
5
+ export { renderValue } from "./printer/render.js";
6
+ export { convertColor } from "./syntax/color/color.js";
7
+ export { isOkLabClose, okLabDistance } from "./syntax/color/utils/distance.js";
8
+ export { Arena, AstArena, TokenField, arenaColumns } from "./ast/arena.js";
9
+ export { ARENA, ARENAID, AstArenaNode } from "./ast/arena-node.js";
10
+ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ModuleScopeEnumOptions, WalkerEvent, WalkerOptionEnum } from "./ast/types.js";
11
+ export { SourceMap } from "./printer/sourcemap.js";
12
+ export { FeatureWalkMode } from "./ast/features/type.js";
13
+ export { parseSyntax } from "./validation/parser/parse.js";
14
+ export { ResponseType } from "./enums-extra.js";
15
+ export { dirname, resolve } from "./fs/resolve.js";
16
+ export { version } from "./version.js";
@@ -0,0 +1,11 @@
1
+ import { rt } from "../core.js";
2
+
3
+ export const matchUrl = /^(https?:)?\/\//;
4
+
5
+ export function dirname(path) {
6
+ return rt.call("dirname", path);
7
+ }
8
+
9
+ export function resolve(url, currentDirectory, cwd) {
10
+ return rt.call("resolve", url, currentDirectory, cwd);
11
+ }
package/dist/node.js ADDED
@@ -0,0 +1,60 @@
1
+ // node entry point (port of src/node.ts)
2
+ import { Readable } from "node:stream";
3
+ import { createReadStream } from "node:fs";
4
+ import { lstat, readFile } from "node:fs/promises";
5
+ import { cwd } from "node:process";
6
+ import { createApi } from "./api.js";
7
+ import { matchUrl, resolve } from "./fs/resolve.js";
8
+ import { ResponseType } from "./enums-extra.js";
9
+
10
+ export * from "./exports.js";
11
+
12
+ export async function load(url, currentDirectory = ".", responseType = false) {
13
+ const resolved = typeof url === "string" ? resolve(url, currentDirectory) : url;
14
+
15
+ if (typeof responseType === "boolean") {
16
+ responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
17
+ }
18
+
19
+ if (matchUrl.test(resolved.absolute)) {
20
+ return fetch(resolved.absolute).then(async (response) => {
21
+ if (!response.ok) {
22
+ throw new Error(`${response.status} ${response.statusText} ${response.url}`);
23
+ }
24
+
25
+ if (responseType === ResponseType.ArrayBuffer) {
26
+ return response.arrayBuffer();
27
+ }
28
+
29
+ if (responseType === ResponseType.JSON) {
30
+ return response.json();
31
+ }
32
+
33
+ return responseType === ResponseType.ReadableStream ? response.body : response.text();
34
+ });
35
+ }
36
+
37
+ try {
38
+ const stats = await lstat(resolved.absolute);
39
+
40
+ if (stats.isFile()) {
41
+ if (responseType === ResponseType.Text || responseType === ResponseType.JSON) {
42
+ return readFile(resolved.absolute, "utf-8").then((buffer) => (responseType === ResponseType.JSON ? JSON.parse(buffer) : buffer));
43
+ }
44
+
45
+ if (responseType === ResponseType.ArrayBuffer) {
46
+ return readFile(resolved.absolute).then((buffer) =>
47
+ buffer.byteOffset === 0 && buffer.byteLength === buffer.buffer.byteLength
48
+ ? buffer.buffer
49
+ : buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
50
+ );
51
+ }
52
+
53
+ return Readable.toWeb(createReadStream(resolved.absolute, { encoding: "utf-8", highWaterMark: 64 * 1024 }));
54
+ }
55
+ } catch (error) {}
56
+
57
+ throw new Error(`File not found: '${resolved.absolute || url}'`);
58
+ }
59
+
60
+ export const { render, parse, parseSync, transform, transformSync } = createApi({ load, cwd: () => cwd() });
@@ -0,0 +1,25 @@
1
+ // PropertyList of src/parser/declaration/list.ts: the declarations of a rule, the duplicates removed and the
2
+ // shorthands computed. the declarations are processed by rust when the list is iterated.
3
+
4
+ import { runtimeOf } from "../../core.js";
5
+ import "../../ast/arena.js";
6
+
7
+ export class PropertyList {
8
+ #arena;
9
+ #options;
10
+ #declarations = [];
11
+
12
+ constructor(arena, options = {}) {
13
+ this.#arena = arena;
14
+ this.#options = options;
15
+ }
16
+
17
+ addDeclaration(declaration) {
18
+ this.#declarations.push(declaration);
19
+ return this;
20
+ }
21
+
22
+ [Symbol.iterator]() {
23
+ return runtimeOf(this.#arena).call("propertyList", this.#arena, this.#options, this.#declarations)[Symbol.iterator]();
24
+ }
25
+ }
@@ -0,0 +1,10 @@
1
+ import { runtimeOf } from "../core.js";
2
+ import "../ast/arena.js";
3
+
4
+ export function parseDeclarations(declaration, arena) {
5
+ return runtimeOf(arena).call("parseDeclarations", declaration, arena);
6
+ }
7
+
8
+ export function parseString(arena, src, options = { parseColor: true }, errors) {
9
+ return runtimeOf(arena).call("parseString", arena, src, options);
10
+ }
@@ -0,0 +1,34 @@
1
+ import { runtimeOf } from "../core.js";
2
+ import "../ast/arena.js";
3
+ import { RenderResult } from "./result.js";
4
+ import { SourceMap } from "./sourcemap.js";
5
+
6
+ /**
7
+ * render statistics of a render: the identical declarations and constructs are rendered once
8
+ */
9
+ export class RenderMemo {
10
+ declarations = { size: 0, hits: 0 };
11
+ constructs = { size: 0, hits: 0 };
12
+ }
13
+
14
+ /**
15
+ * a render result from the rust result
16
+ */
17
+ export function renderResult(result) {
18
+ return new RenderResult(result.write, result.errors, result.stats, result.map == null ? undefined : new SourceMap(result.map), result.codeLength);
19
+ }
20
+
21
+ export function doRender(arena, data, options = {}, mapping, memo) {
22
+ const result = runtimeOf(arena).call("render", arena, data, options, mapping ?? null, memo != null);
23
+
24
+ if (memo != null) {
25
+ memo.declarations = result.memo.declarations;
26
+ memo.constructs = result.memo.constructs;
27
+ }
28
+
29
+ return renderResult(result);
30
+ }
31
+
32
+ export function renderValue(arena, token, options = {}) {
33
+ return runtimeOf(arena).call("renderValue", arena, token, options);
34
+ }
@@ -0,0 +1,67 @@
1
+ // Port of src/printer/result.ts: the code is decoded from the utf-8 output when it is read.
2
+
3
+ const decoder = new TextDecoder();
4
+
5
+ export class RenderResult {
6
+ write;
7
+ errors;
8
+ stats;
9
+ map;
10
+ #code = null;
11
+ #codeLength;
12
+
13
+ constructor(write, errors, stats, map, codeLength = -1) {
14
+ this.write = write;
15
+ this.errors = errors;
16
+ this.stats = stats;
17
+ this.map = map;
18
+ this.#codeLength = codeLength;
19
+ }
20
+
21
+ static codeLength(result) {
22
+ return result.#codeLength >= 0 ? result.#codeLength : result.code.length;
23
+ }
24
+
25
+ get code() {
26
+ if (this.#code === null) {
27
+ this.#code = decoder.decode(this.write);
28
+ }
29
+
30
+ return this.#code;
31
+ }
32
+
33
+ toJSON() {
34
+ const { write, ...json } = this;
35
+ json.code = this.code;
36
+ return json;
37
+ }
38
+ }
39
+
40
+ export class TransformResult extends RenderResult {
41
+ ast;
42
+ arena;
43
+
44
+ constructor(parseResult, renderResult, stats) {
45
+ super(renderResult.write, parseResult.errors.concat(renderResult.errors), stats, renderResult.map, RenderResult.codeLength(renderResult));
46
+ this.ast = parseResult.ast;
47
+ this.arena = parseResult.arena;
48
+
49
+ if (parseResult.mapping !== undefined) {
50
+ this.mapping = parseResult.mapping;
51
+ }
52
+
53
+ if (parseResult.cssModuleVariables !== undefined) {
54
+ this.cssModuleVariables = parseResult.cssModuleVariables;
55
+ }
56
+
57
+ if (parseResult.importMapping !== undefined) {
58
+ this.importMapping = parseResult.importMapping;
59
+ }
60
+
61
+ if (parseResult.revMapping !== undefined) {
62
+ this.revMapping = parseResult.revMapping;
63
+ }
64
+ }
65
+ }
66
+
67
+ RenderResult.shape = new RenderResult(new Uint8Array(0), [], { total: "0ms" });
@@ -0,0 +1,30 @@
1
+ // Source map of a render result (src/printer/sourcemap.ts). The mappings are encoded by rust; find()
2
+ // decodes them in rust.
3
+
4
+ import { rt } from "../core.js";
5
+
6
+ export class SourceMap {
7
+ #json;
8
+
9
+ constructor(sourcemaps) {
10
+ if (sourcemaps == null) {
11
+ this.#json = { version: 3, sources: [], names: [], mappings: "" };
12
+ } else {
13
+ this.#json = typeof sourcemaps === "string" ? JSON.parse(sourcemaps) : { ...sourcemaps };
14
+ }
15
+ }
16
+
17
+ computePositions() {}
18
+
19
+ find(line, column) {
20
+ return rt.call("sourceMapFind", JSON.stringify(this.#json), line, column);
21
+ }
22
+
23
+ toUrl() {
24
+ return rt.call("sourceMapUrl", JSON.stringify(this.#json));
25
+ }
26
+
27
+ toJSON() {
28
+ return this.#json;
29
+ }
30
+ }