cesium-xs-sdk 1.0.4 → 1.0.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/vite-plugin.js +29 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cesium-xs-sdk",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "基于 Cesium 1.137 封装的 SDK",
6
6
  "main": "dist/cesium-xs-sdk.cjs.js",
@@ -18,7 +18,7 @@ import { fileURLToPath } from 'url';
18
18
  import { createRequire } from 'module';
19
19
 
20
20
  const __filename = fileURLToPath(import.meta.url);
21
- const require = createRequire(import.meta.url);
21
+ const pluginRequire = createRequire(import.meta.url);
22
22
 
23
23
  const CONTENT_TYPE_MAP = {
24
24
  '.js': 'application/javascript',
@@ -57,17 +57,30 @@ function copyDir(src, dest) {
57
57
  }
58
58
  }
59
59
 
60
- function resolveCesiumRoot(userRoot) {
60
+ /**
61
+ * 从指定项目根目录解析 cesium 包位置。
62
+ * 不使用插件文件所在位置解析,避免本地软链/本地路径引入时解析到 SDK 自己的 node_modules。
63
+ */
64
+ function resolveCesiumRoot(projectRoot, userRoot) {
61
65
  if (userRoot) {
62
66
  return path.resolve(userRoot);
63
67
  }
64
- // 优先用 Node 模块解析,支持 monorepo / workspace / pnpm 等 hoist 场景
68
+
69
+ // 1. 优先从 Vite 项目根目录解析(支持 monorepo / workspace / 依赖提升)
65
70
  try {
66
- const pkgJsonPath = require.resolve('cesium/package.json');
67
- return path.dirname(pkgJsonPath);
68
- } catch (e) {
69
- console.warn('[cesium-xs-sdk/vite] 无法通过 require.resolve 定位 cesium,回退到当前工作目录 node_modules');
70
- return path.resolve(process.cwd(), 'node_modules/cesium');
71
+ const projectRequire = createRequire(path.join(projectRoot, 'package.json'));
72
+ return path.dirname(projectRequire.resolve('cesium/package.json'));
73
+ } catch {
74
+ // 2. 回退到插件自身所在位置解析(npm 正常安装场景)
75
+ try {
76
+ return path.dirname(pluginRequire.resolve('cesium/package.json'));
77
+ } catch {
78
+ // 3. 最后回退到 projectRoot/node_modules/cesium
79
+ console.warn(
80
+ `[cesium-xs-sdk/vite] 无法通过模块解析定位 cesium,回退到: ${path.join(projectRoot, 'node_modules/cesium')}`
81
+ );
82
+ return path.join(projectRoot, 'node_modules/cesium');
83
+ }
71
84
  }
72
85
  }
73
86
 
@@ -80,14 +93,13 @@ function normalizeBaseUrl(url) {
80
93
 
81
94
  /**
82
95
  * @param {object} [options]
83
- * @param {string} [options.cesiumPackageRoot] Cesium npm 包根目录,默认 `node_modules/cesium`
96
+ * @param {string} [options.cesiumPackageRoot] Cesium npm 包根目录,默认按 Vite 项目根目录解析
84
97
  * @param {string} [options.cesiumBaseUrl] Cesium 资源基础路径,默认 `/Cesium/`
85
98
  * @returns {import('vite').Plugin}
86
99
  */
87
100
  export function cesiumXsSdkPlugin(options = {}) {
88
101
  const cesiumBaseUrl = normalizeBaseUrl(options.cesiumBaseUrl);
89
- const cesiumRoot = resolveCesiumRoot(options.cesiumPackageRoot);
90
- const cesiumBuildPath = path.join(cesiumRoot, 'Build/Cesium');
102
+ let cesiumBuildPath = '';
91
103
 
92
104
  return {
93
105
  name: 'cesium-xs-sdk',
@@ -103,7 +115,11 @@ export function cesiumXsSdkPlugin(options = {}) {
103
115
  }
104
116
  };
105
117
  },
106
- configResolved() {
118
+ configResolved(config) {
119
+ const projectRoot = config.root || process.cwd();
120
+ const cesiumRoot = resolveCesiumRoot(projectRoot, options.cesiumPackageRoot);
121
+ cesiumBuildPath = path.join(cesiumRoot, 'Build/Cesium');
122
+
107
123
  if (!fs.existsSync(cesiumBuildPath)) {
108
124
  console.warn(
109
125
  `[cesium-xs-sdk/vite] 未找到 Cesium 构建目录: ${cesiumBuildPath}\n` +
@@ -134,7 +150,7 @@ export function cesiumXsSdkPlugin(options = {}) {
134
150
  });
135
151
  },
136
152
  writeBundle(bundleOptions) {
137
- if (!fs.existsSync(cesiumBuildPath)) {
153
+ if (!cesiumBuildPath || !fs.existsSync(cesiumBuildPath)) {
138
154
  this.warn(`Cesium 构建目录不存在,跳过复制: ${cesiumBuildPath}`);
139
155
  return;
140
156
  }