@joplo-app/kit 0.2.13 → 0.2.15
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/asset.d.ts +4 -0
- package/dist/asset.js +48 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/dist/asset.d.ts
ADDED
package/dist/asset.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* asset('./photo.jpg'):把项目里的图片读成 data URI,内嵌进演示(本地看板、build、share 都带着它,不依赖外网)。
|
|
3
|
+
* 路径相对于调用 asset() 的源文件,和 import 一样。作者模块在 Node 里执行,这里按需取 node:fs,
|
|
4
|
+
* 不在模块顶层静态导入,免得别处只为类型引用 kit 时牵出 Node 依赖。
|
|
5
|
+
*/
|
|
6
|
+
import { callsite } from "./node.js";
|
|
7
|
+
const MIME = {
|
|
8
|
+
jpg: 'image/jpeg',
|
|
9
|
+
jpeg: 'image/jpeg',
|
|
10
|
+
png: 'image/png',
|
|
11
|
+
gif: 'image/gif',
|
|
12
|
+
webp: 'image/webp',
|
|
13
|
+
avif: 'image/avif',
|
|
14
|
+
svg: 'image/svg+xml',
|
|
15
|
+
};
|
|
16
|
+
/** 单个文件的上限:再大会拖慢看板加载并撑大发布包,应先压缩。 */
|
|
17
|
+
export const ASSET_MAX_BYTES = 2 * 1024 * 1024;
|
|
18
|
+
function builtin(name) {
|
|
19
|
+
const get = globalThis.process?.getBuiltinModule;
|
|
20
|
+
if (!get)
|
|
21
|
+
throw new Error('asset() runs while Joplo loads the project in Node (22.3 or newer)');
|
|
22
|
+
return get(name);
|
|
23
|
+
}
|
|
24
|
+
/** 读入一张图片,返回 data URI(用于 <Image src={…}>,也可以写进 style 的 backgroundImage: `url(${asset(…)})`)。 */
|
|
25
|
+
export function asset(path, from) {
|
|
26
|
+
const fs = builtin('node:fs');
|
|
27
|
+
const p = builtin('node:path');
|
|
28
|
+
const { Buffer } = builtin('node:buffer');
|
|
29
|
+
const caller = from ?? callsite()?.replace(/:\d+:\d+$/, '');
|
|
30
|
+
const file = caller ? p.resolve(p.dirname(caller), path) : p.resolve(path);
|
|
31
|
+
const ext = p.extname(file).slice(1).toLowerCase();
|
|
32
|
+
const mime = MIME[ext];
|
|
33
|
+
if (!mime)
|
|
34
|
+
throw new Error(`asset("${path}"): unsupported image type ".${ext}" (use ${Object.keys(MIME).join(', ')})`);
|
|
35
|
+
let size;
|
|
36
|
+
try {
|
|
37
|
+
const st = fs.statSync(file);
|
|
38
|
+
if (!st.isFile())
|
|
39
|
+
throw new Error('not a file');
|
|
40
|
+
size = st.size;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
throw new Error(`asset("${path}"): file not found at ${file} (paths are relative to the file that calls asset())`);
|
|
44
|
+
}
|
|
45
|
+
if (size > ASSET_MAX_BYTES)
|
|
46
|
+
throw new Error(`asset("${path}"): ${Math.round(size / 1024)} KB is over the ${ASSET_MAX_BYTES / 1024 / 1024} MB limit; compress or resize the image first`);
|
|
47
|
+
return `data:${mime};base64,${Buffer.from(fs.readFileSync(file)).toString('base64')}`;
|
|
48
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { type Bind, type CondExpr, type JNode } from './node.js';
|
|
13
13
|
export { $, and, empty, eq, Fragment, neq, not, notEmpty, oneOf, or, Outlet, Slot } from './node.js';
|
|
14
14
|
export type { Bind, CondExpr, JNode } from './node.js';
|
|
15
|
+
export { asset } from './asset.js';
|
|
15
16
|
type Json = string | number | boolean | null | Json[] | {
|
|
16
17
|
[k: string]: Json;
|
|
17
18
|
};
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { bindPath, callsite, isBind } from "./node.js";
|
|
13
13
|
export { $, and, empty, eq, Fragment, neq, not, notEmpty, oneOf, or, Outlet, Slot } from "./node.js";
|
|
14
|
+
export { asset } from "./asset.js";
|
|
14
15
|
export const config = (c) => ({ kind: 'config', ...c, source: callsite() });
|
|
15
16
|
export const layout = (tree) => ({ kind: 'layout', tree, source: callsite() });
|
|
16
17
|
export const page = (meta, tree) => ({
|