cesium-xs-sdk 1.0.3 → 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 +40 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cesium-xs-sdk",
3
- "version": "1.0.3",
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",
@@ -15,8 +15,10 @@
15
15
  import fs from 'fs';
16
16
  import path from 'path';
17
17
  import { fileURLToPath } from 'url';
18
+ import { createRequire } from 'module';
18
19
 
19
20
  const __filename = fileURLToPath(import.meta.url);
21
+ const pluginRequire = createRequire(import.meta.url);
20
22
 
21
23
  const CONTENT_TYPE_MAP = {
22
24
  '.js': 'application/javascript',
@@ -55,12 +57,31 @@ function copyDir(src, dest) {
55
57
  }
56
58
  }
57
59
 
58
- function resolveCesiumRoot(userRoot) {
60
+ /**
61
+ * 从指定项目根目录解析 cesium 包位置。
62
+ * 不使用插件文件所在位置解析,避免本地软链/本地路径引入时解析到 SDK 自己的 node_modules。
63
+ */
64
+ function resolveCesiumRoot(projectRoot, userRoot) {
59
65
  if (userRoot) {
60
66
  return path.resolve(userRoot);
61
67
  }
62
- // 默认从当前工作目录的 node_modules 解析
63
- return path.resolve(process.cwd(), 'node_modules/cesium');
68
+
69
+ // 1. 优先从 Vite 项目根目录解析(支持 monorepo / workspace / 依赖提升)
70
+ try {
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
+ }
84
+ }
64
85
  }
65
86
 
66
87
  function normalizeBaseUrl(url) {
@@ -72,14 +93,13 @@ function normalizeBaseUrl(url) {
72
93
 
73
94
  /**
74
95
  * @param {object} [options]
75
- * @param {string} [options.cesiumPackageRoot] Cesium npm 包根目录,默认 `node_modules/cesium`
96
+ * @param {string} [options.cesiumPackageRoot] Cesium npm 包根目录,默认按 Vite 项目根目录解析
76
97
  * @param {string} [options.cesiumBaseUrl] Cesium 资源基础路径,默认 `/Cesium/`
77
98
  * @returns {import('vite').Plugin}
78
99
  */
79
100
  export function cesiumXsSdkPlugin(options = {}) {
80
101
  const cesiumBaseUrl = normalizeBaseUrl(options.cesiumBaseUrl);
81
- const cesiumRoot = resolveCesiumRoot(options.cesiumPackageRoot);
82
- const cesiumBuildPath = path.join(cesiumRoot, 'Build/Cesium');
102
+ let cesiumBuildPath = '';
83
103
 
84
104
  return {
85
105
  name: 'cesium-xs-sdk',
@@ -95,6 +115,18 @@ export function cesiumXsSdkPlugin(options = {}) {
95
115
  }
96
116
  };
97
117
  },
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
+
123
+ if (!fs.existsSync(cesiumBuildPath)) {
124
+ console.warn(
125
+ `[cesium-xs-sdk/vite] 未找到 Cesium 构建目录: ${cesiumBuildPath}\n` +
126
+ `请确认已安装 cesium,或在插件选项中指定 cesiumPackageRoot。`
127
+ );
128
+ }
129
+ },
98
130
  configureServer(server) {
99
131
  server.middlewares.use((req, res, next) => {
100
132
  if (!req.url || !req.url.startsWith(cesiumBaseUrl)) {
@@ -113,11 +145,12 @@ export function cesiumXsSdkPlugin(options = {}) {
113
145
  return;
114
146
  }
115
147
 
148
+ console.warn(`[cesium-xs-sdk/vite] 未找到 Cesium 资源: ${filePath} (请求: ${req.url})`);
116
149
  next();
117
150
  });
118
151
  },
119
152
  writeBundle(bundleOptions) {
120
- if (!fs.existsSync(cesiumBuildPath)) {
153
+ if (!cesiumBuildPath || !fs.existsSync(cesiumBuildPath)) {
121
154
  this.warn(`Cesium 构建目录不存在,跳过复制: ${cesiumBuildPath}`);
122
155
  return;
123
156
  }