@svfig/build 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.
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/main.cjs +29034 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +28 -0
- package/dist/path.d.ts +24 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +54 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAkB,MAAM,aAAa,CAAC;AAI5D,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAcD,eAAO,MAAM,SAAS,GAAU,KAAK,QAAQ,EAAE,OAAM,aAAkB,KAAG,OAAO,CAAC,MAAM,CAGvF,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { generate } from '@svfig/core';
|
|
2
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
3
|
+
const DATA = /^data:image\//i;
|
|
4
|
+
const BLOB = /^blob:/i;
|
|
5
|
+
// Lazy-loaded so browser bundlers (vite) never statically pull in `./path`,
|
|
6
|
+
// which depends on node:fs/promises, node:path and node:buffer.
|
|
7
|
+
const RESOLVER_PATH = './path.js';
|
|
8
|
+
let resolverPromise = null;
|
|
9
|
+
const loadResolver = () => {
|
|
10
|
+
resolverPromise ??= import(/* @vite-ignore */ RESOLVER_PATH);
|
|
11
|
+
return resolverPromise;
|
|
12
|
+
};
|
|
13
|
+
export const renderSvg = async (doc, opts = {}) => {
|
|
14
|
+
const inlined = await inlineImages(doc, opts.baseDir);
|
|
15
|
+
return `${renderToStaticMarkup(await generate(inlined))}\n`;
|
|
16
|
+
};
|
|
17
|
+
const inlineImages = async (doc, baseDir) => {
|
|
18
|
+
let changed = false;
|
|
19
|
+
const elements = await Promise.all(doc.elements.map(async (el) => {
|
|
20
|
+
if (el.kind !== 'image' || !el.path)
|
|
21
|
+
return el;
|
|
22
|
+
if (DATA.test(el.path))
|
|
23
|
+
return el;
|
|
24
|
+
if (BLOB.test(el.path))
|
|
25
|
+
throw new Error(`Blob image paths cannot be resolved during build: ${el.path}`);
|
|
26
|
+
const { imageDataUri } = await loadResolver();
|
|
27
|
+
const path = await imageDataUri(el.path, { baseDir });
|
|
28
|
+
if (path === el.path)
|
|
29
|
+
return el;
|
|
30
|
+
changed = true;
|
|
31
|
+
return { ...el, path };
|
|
32
|
+
}));
|
|
33
|
+
return changed ? { ...doc, elements } : doc;
|
|
34
|
+
};
|