svg-vectordrawable 0.1.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,65 @@
1
+ import { Config } from 'svgo';
2
+
3
+ /** A non-fatal issue encountered during conversion. */
4
+ interface Warning {
5
+ /** Stable machine-readable code, e.g. `unsupported-element`. */
6
+ code: string;
7
+ /** Human-readable explanation. */
8
+ message: string;
9
+ /** SVG element/attribute the warning relates to, when known. */
10
+ node?: string;
11
+ }
12
+ interface ConvertOptions {
13
+ /**
14
+ * Run svgo normalization first (inline styles, shapes→paths, bake transforms…).
15
+ * Strongly recommended; it is what makes conversion robust across SVG sources.
16
+ * @default true
17
+ */
18
+ optimize?: boolean;
19
+ /** Override the svgo config used for normalization (only when `optimize` is true). */
20
+ svgoConfig?: Config;
21
+ /** Decimal places kept for generated numbers (coordinates, radii). @default 3 */
22
+ floatPrecision?: number;
23
+ /** Concrete color substituted for `currentColor`. @default '#000000' */
24
+ currentColor?: string;
25
+ /**
26
+ * SVG paints unfilled shapes black by default. When true, paths without an
27
+ * explicit fill get `android:fillColor="#FF000000"`. @default true
28
+ */
29
+ fillBlackForUnfilled?: boolean;
30
+ /** Throw on the first unsupported construct instead of warning. @default false */
31
+ strict?: boolean;
32
+ /** Indentation width (spaces). @default 4 */
33
+ indent?: number;
34
+ /** Prepend an XML declaration (`<?xml version="1.0" encoding="utf-8"?>`). @default false */
35
+ xmlTag?: boolean;
36
+ /** Add `android:tint` to the `<vector>`. Android color literal (e.g. `#AARRGGBB`), passed verbatim. */
37
+ tint?: string;
38
+ /** Called for every warning as it happens (in addition to the returned list). */
39
+ onWarn?: (warning: Warning) => void;
40
+ }
41
+ interface ConvertResult {
42
+ /** The generated Android VectorDrawable XML. */
43
+ xml: string;
44
+ /** All non-fatal issues encountered. */
45
+ warnings: Warning[];
46
+ }
47
+ /** An RGBA color expressed as Android `#AARRGGBB`. */
48
+ type AndroidColor = `#${string}`;
49
+
50
+ declare function convert(svg: string, options?: ConvertOptions): ConvertResult;
51
+
52
+ /**
53
+ * Converts a single SVG file to a VectorDrawable XML file.
54
+ * Defaults the output path to the input with a `.xml` extension. (Node only.)
55
+ */
56
+ declare function convertFile(inputPath: string, outputPath?: string, options?: ConvertOptions): ConvertResult;
57
+ interface ConvertDirResult {
58
+ input: string;
59
+ output: string;
60
+ warnings: Warning[];
61
+ }
62
+ /** Converts every `*.svg` in a directory to `*.xml` in `outputDir`. (Node only, non-recursive.) */
63
+ declare function convertDir(inputDir: string, outputDir: string, options?: ConvertOptions): ConvertDirResult[];
64
+
65
+ export { type AndroidColor, type ConvertDirResult, type ConvertOptions, type ConvertResult, type Warning, convert, convertDir, convertFile };
@@ -0,0 +1,65 @@
1
+ import { Config } from 'svgo';
2
+
3
+ /** A non-fatal issue encountered during conversion. */
4
+ interface Warning {
5
+ /** Stable machine-readable code, e.g. `unsupported-element`. */
6
+ code: string;
7
+ /** Human-readable explanation. */
8
+ message: string;
9
+ /** SVG element/attribute the warning relates to, when known. */
10
+ node?: string;
11
+ }
12
+ interface ConvertOptions {
13
+ /**
14
+ * Run svgo normalization first (inline styles, shapes→paths, bake transforms…).
15
+ * Strongly recommended; it is what makes conversion robust across SVG sources.
16
+ * @default true
17
+ */
18
+ optimize?: boolean;
19
+ /** Override the svgo config used for normalization (only when `optimize` is true). */
20
+ svgoConfig?: Config;
21
+ /** Decimal places kept for generated numbers (coordinates, radii). @default 3 */
22
+ floatPrecision?: number;
23
+ /** Concrete color substituted for `currentColor`. @default '#000000' */
24
+ currentColor?: string;
25
+ /**
26
+ * SVG paints unfilled shapes black by default. When true, paths without an
27
+ * explicit fill get `android:fillColor="#FF000000"`. @default true
28
+ */
29
+ fillBlackForUnfilled?: boolean;
30
+ /** Throw on the first unsupported construct instead of warning. @default false */
31
+ strict?: boolean;
32
+ /** Indentation width (spaces). @default 4 */
33
+ indent?: number;
34
+ /** Prepend an XML declaration (`<?xml version="1.0" encoding="utf-8"?>`). @default false */
35
+ xmlTag?: boolean;
36
+ /** Add `android:tint` to the `<vector>`. Android color literal (e.g. `#AARRGGBB`), passed verbatim. */
37
+ tint?: string;
38
+ /** Called for every warning as it happens (in addition to the returned list). */
39
+ onWarn?: (warning: Warning) => void;
40
+ }
41
+ interface ConvertResult {
42
+ /** The generated Android VectorDrawable XML. */
43
+ xml: string;
44
+ /** All non-fatal issues encountered. */
45
+ warnings: Warning[];
46
+ }
47
+ /** An RGBA color expressed as Android `#AARRGGBB`. */
48
+ type AndroidColor = `#${string}`;
49
+
50
+ declare function convert(svg: string, options?: ConvertOptions): ConvertResult;
51
+
52
+ /**
53
+ * Converts a single SVG file to a VectorDrawable XML file.
54
+ * Defaults the output path to the input with a `.xml` extension. (Node only.)
55
+ */
56
+ declare function convertFile(inputPath: string, outputPath?: string, options?: ConvertOptions): ConvertResult;
57
+ interface ConvertDirResult {
58
+ input: string;
59
+ output: string;
60
+ warnings: Warning[];
61
+ }
62
+ /** Converts every `*.svg` in a directory to `*.xml` in `outputDir`. (Node only, non-recursive.) */
63
+ declare function convertDir(inputDir: string, outputDir: string, options?: ConvertOptions): ConvertDirResult[];
64
+
65
+ export { type AndroidColor, type ConvertDirResult, type ConvertOptions, type ConvertResult, type Warning, convert, convertDir, convertFile };
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ import {
2
+ convert
3
+ } from "./chunk-QG2B6V6V.js";
4
+
5
+ // src/file.ts
6
+ import { readFileSync, writeFileSync, readdirSync, statSync, mkdirSync } from "fs";
7
+ import { join, extname, basename, dirname } from "path";
8
+ function convertFile(inputPath, outputPath, options) {
9
+ const result = convert(readFileSync(inputPath, "utf8"), options);
10
+ const target = outputPath ?? join(dirname(inputPath), `${basename(inputPath, extname(inputPath))}.xml`);
11
+ mkdirSync(dirname(target), { recursive: true });
12
+ writeFileSync(target, result.xml);
13
+ return result;
14
+ }
15
+ function convertDir(inputDir, outputDir, options) {
16
+ mkdirSync(outputDir, { recursive: true });
17
+ const out = [];
18
+ for (const entry of readdirSync(inputDir)) {
19
+ const p = join(inputDir, entry);
20
+ if (statSync(p).isFile() && extname(entry).toLowerCase() === ".svg") {
21
+ const target = join(outputDir, `${basename(entry, ".svg")}.xml`);
22
+ const { warnings } = convertFile(p, target, options);
23
+ out.push({ input: p, output: target, warnings });
24
+ }
25
+ }
26
+ return out;
27
+ }
28
+ export {
29
+ convert,
30
+ convertDir,
31
+ convertFile
32
+ };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "svg-vectordrawable",
3
+ "version": "0.1.0",
4
+ "description": "Convert SVG to Android VectorDrawable XML. AST-based (via svgo), with gradients (incl. gradientTransform), clip-path, groups and full CSS color support. Fails loud on constructs a VectorDrawable cannot represent.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./browser": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/browser.js"
15
+ }
16
+ },
17
+ "bin": {
18
+ "svgvd": "./dist/cli.js"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "sideEffects": false,
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest",
28
+ "test:coverage": "vitest run --coverage",
29
+ "typecheck": "tsc --noEmit",
30
+ "lint": "eslint .",
31
+ "lint:fix": "eslint . --fix",
32
+ "format": "prettier --write .",
33
+ "format:check": "prettier --check .",
34
+ "validate:aapt2": "node scripts/aapt2-validate.mjs",
35
+ "prepublishOnly": "npm run build",
36
+ "release": "npm publish --provenance --access public"
37
+ },
38
+ "keywords": [
39
+ "svg",
40
+ "android",
41
+ "vectordrawable",
42
+ "vector-drawable",
43
+ "gradient",
44
+ "gradientTransform",
45
+ "figma",
46
+ "converter",
47
+ "svgo"
48
+ ],
49
+ "license": "MIT",
50
+ "engines": {
51
+ "node": ">=18"
52
+ },
53
+ "dependencies": {
54
+ "svgo": "^4.0.1"
55
+ },
56
+ "devDependencies": {
57
+ "@eslint/js": "^9.0.0",
58
+ "@types/node": "^20.14.0",
59
+ "@vitest/coverage-v8": "^2.1.0",
60
+ "eslint": "^9.0.0",
61
+ "globals": "^14.0.0",
62
+ "prettier": "^3.0.0",
63
+ "tsup": "^8.3.0",
64
+ "typescript": "^5.6.0",
65
+ "typescript-eslint": "^8.0.0",
66
+ "vitest": "^2.1.0"
67
+ }
68
+ }