@spatialwalk/avatarkit 1.0.0-beta.62 → 1.0.0-beta.64

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/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@spatialwalk/avatarkit",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.62",
4
+ "version": "1.0.0-beta.64",
5
+ "packageManager": "pnpm@10.18.2",
5
6
  "description": "SPAvatar SDK - 3D Gaussian Splatting Avatar Rendering SDK",
6
7
  "author": "SPAvatar Team",
7
8
  "license": "MIT",
@@ -18,6 +19,10 @@
18
19
  "types": "./dist/index.d.ts",
19
20
  "import": "./dist/index.js"
20
21
  },
22
+ "./vite": {
23
+ "types": "./vite.d.ts",
24
+ "import": "./vite.js"
25
+ },
21
26
  "./*": {
22
27
  "types": "./dist/*.d.ts",
23
28
  "import": "./dist/*.js"
@@ -29,10 +34,24 @@
29
34
  "files": [
30
35
  "README.md",
31
36
  "CHANGELOG.md",
32
- "dist"
37
+ "dist",
38
+ "vite.js",
39
+ "vite.d.ts"
33
40
  ],
41
+ "scripts": {
42
+ "build": "SDK_BUILD=true vite build --mode library && npm run build:vite-plugin",
43
+ "build:vite-plugin": "tsc vite.ts --outDir . --module esnext --target es2020 --moduleResolution bundler --esModuleInterop --skipLibCheck --declaration --declarationMap",
44
+ "dev": "vite build --mode library --watch",
45
+ "clean": "rm -rf dist",
46
+ "typecheck": "tsc --noEmit",
47
+ "test": "cd tests && pnpm test",
48
+ "test:watch": "cd tests && pnpm run test:watch",
49
+ "test:e2e": "cd tests && pnpm run test:e2e",
50
+ "test:perf": "cd tests && pnpm run test:perf"
51
+ },
34
52
  "peerDependencies": {
35
- "@webgpu/types": "*"
53
+ "@webgpu/types": "*",
54
+ "vite": "^5.0.0"
36
55
  },
37
56
  "dependencies": {
38
57
  "@bufbuild/protobuf": "^2.10.0",
@@ -47,15 +66,5 @@
47
66
  "typescript": "^5.0.0",
48
67
  "vite": "^5.0.0",
49
68
  "vite-plugin-dts": "^4.5.4"
50
- },
51
- "scripts": {
52
- "build": "SDK_BUILD=true vite build --mode library",
53
- "dev": "vite build --mode library --watch",
54
- "clean": "rm -rf dist",
55
- "typecheck": "tsc --noEmit",
56
- "test": "cd tests && pnpm test",
57
- "test:watch": "cd tests && pnpm run test:watch",
58
- "test:e2e": "cd tests && pnpm run test:e2e",
59
- "test:perf": "cd tests && pnpm run test:perf"
60
69
  }
61
- }
70
+ }
package/vite.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * Vite plugin for @spatialwalk/avatarkit
4
+ * Automatically handles WASM file configuration for development and production builds
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { defineConfig } from 'vite'
9
+ * import { avatarkitVitePlugin } from '@spatialwalk/avatarkit/vite'
10
+ *
11
+ * export default defineConfig({
12
+ * plugins: [
13
+ * avatarkitVitePlugin()
14
+ * ]
15
+ * })
16
+ * ```
17
+ */
18
+ export declare function avatarkitVitePlugin(): Plugin;
19
+ export default avatarkitVitePlugin;
20
+ //# sourceMappingURL=vite.d.ts.map
package/vite.js ADDED
@@ -0,0 +1,110 @@
1
+ import { copyFileSync, existsSync, writeFileSync, readdirSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ /**
4
+ * Vite plugin for @spatialwalk/avatarkit
5
+ * Automatically handles WASM file configuration for development and production builds
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { defineConfig } from 'vite'
10
+ * import { avatarkitVitePlugin } from '@spatialwalk/avatarkit/vite'
11
+ *
12
+ * export default defineConfig({
13
+ * plugins: [
14
+ * avatarkitVitePlugin()
15
+ * ]
16
+ * })
17
+ * ```
18
+ */
19
+ export function avatarkitVitePlugin() {
20
+ let rootDir;
21
+ return {
22
+ name: 'avatarkit-wasm',
23
+ // 保存项目根目录
24
+ configResolved(config) {
25
+ rootDir = config.root;
26
+ },
27
+ // 开发服务器 MIME 类型配置
28
+ configureServer(server) {
29
+ server.middlewares.use((req, res, next) => {
30
+ if (req.url?.endsWith('.wasm')) {
31
+ res.setHeader('Content-Type', 'application/wasm');
32
+ }
33
+ next();
34
+ });
35
+ },
36
+ // 构建时自动复制 WASM 文件和生成 headers
37
+ closeBundle() {
38
+ if (!rootDir)
39
+ return;
40
+ // 查找 node_modules 中的 WASM 文件
41
+ const wasmSource = join(rootDir, 'node_modules/@spatialwalk/avatarkit/dist/avatar_core_wasm.wasm');
42
+ // 查找 node_modules 中的 WASM JS glue 文件
43
+ // 注意:Vite 可能会给 JS 文件加 hash,所以我们需要查找所有可能的文件
44
+ const wasmJsSourcePattern = join(rootDir, 'node_modules/@spatialwalk/avatarkit/dist');
45
+ // 目标路径 - 根据实际使用情况,WASM 文件应该在 assets 目录
46
+ // 因为 Vite 通常会把资源放在 assets 目录,而 SDK 代码使用相对路径导入
47
+ const wasmDest = join(rootDir, 'dist/assets/avatar_core_wasm.wasm');
48
+ const wasmJsDest = join(rootDir, 'dist/assets/avatar_core_wasm.js');
49
+ const headersDest = join(rootDir, 'dist/_headers');
50
+ // 复制 WASM 文件
51
+ if (existsSync(wasmSource)) {
52
+ copyFileSync(wasmSource, wasmDest);
53
+ console.log('✅ [avatarkit] Copied WASM file to dist/assets/avatar_core_wasm.wasm');
54
+ }
55
+ else {
56
+ console.warn('⚠️ [avatarkit] WASM file not found at:', wasmSource);
57
+ }
58
+ // 尝试查找并复制 WASM JS glue 文件
59
+ // 由于 Vite 可能会给文件加 hash,我们尝试查找匹配的文件
60
+ try {
61
+ if (existsSync(wasmJsSourcePattern)) {
62
+ const files = readdirSync(wasmJsSourcePattern);
63
+ const wasmJsFile = files.find((f) => f.startsWith('avatar_core_wasm') && f.endsWith('.js'));
64
+ if (wasmJsFile) {
65
+ const wasmJsSource = join(wasmJsSourcePattern, wasmJsFile);
66
+ copyFileSync(wasmJsSource, wasmJsDest);
67
+ console.log(`✅ [avatarkit] Copied WASM JS file to dist/assets/avatar_core_wasm.js (from ${wasmJsFile})`);
68
+ }
69
+ else {
70
+ console.log('ℹ️ [avatarkit] WASM JS file not found (may be handled by Vite):', wasmJsSourcePattern);
71
+ }
72
+ }
73
+ }
74
+ catch (error) {
75
+ // 如果读取目录失败,不报错,因为 Vite 可能已经处理了 JS 文件
76
+ console.log('ℹ️ [avatarkit] Could not find WASM JS file (may be handled by Vite)');
77
+ }
78
+ // 生成 _headers 文件(用于 Cloudflare Pages 等平台)
79
+ const headersContent = '/*.wasm\n Content-Type: application/wasm\n';
80
+ writeFileSync(headersDest, headersContent);
81
+ console.log('✅ [avatarkit] Created _headers file for Cloudflare Pages');
82
+ },
83
+ // 自动配置 Vite 选项
84
+ config() {
85
+ return {
86
+ optimizeDeps: {
87
+ exclude: ['@spatialwalk/avatarkit']
88
+ },
89
+ assetsInclude: ['**/*.wasm'],
90
+ build: {
91
+ assetsInlineLimit: 0, // 确保 WASM 文件不被内联
92
+ rollupOptions: {
93
+ output: {
94
+ assetFileNames: (assetInfo) => {
95
+ // WASM 文件使用固定名称
96
+ if (assetInfo.name?.endsWith('.wasm')) {
97
+ return 'assets/[name][extname]';
98
+ }
99
+ // 其他资源使用 hash
100
+ return 'assets/[name]-[hash][extname]';
101
+ }
102
+ }
103
+ }
104
+ }
105
+ };
106
+ }
107
+ };
108
+ }
109
+ // 默认导出
110
+ export default avatarkitVitePlugin;