Compare commits
6 Commits
v1.x
...
3c8bad430b
Author | SHA1 | Date | |
---|---|---|---|
3c8bad430b | |||
564a6ece40 | |||
436466b519 | |||
352ba1eba5 | |||
08e0da60dd | |||
![]() |
50b3f7d545 |
40
README.md
40
README.md
@@ -1,3 +1,43 @@
|
||||
# ⚡ vite-plugin-cesium-next
|
||||
|
||||
> 本仓库 fork 自 [nshen/vite-plugin-cesium](https://github.com/nshen/vite-plugin-cesium)
|
||||
|
||||
本仓库在原仓库代码基础上,主要针对性修复/优化了以下问题
|
||||
- 相对路径问题:本仓库已支持在 vite.config.ts 中配置以下类型的 base: `./`, `/`, `/foo/bar`, `(不设置)` (新创建的项目 base 默认为 `./`,而原仓库针对 `./` 没有做很好的处理)
|
||||
- 资源请求路径:当 base 形如 `/foo/bar` 时,cesium 静态文件由 `/cesium...` 改为请求 `/foo/bar/cesium...`
|
||||
鉴于原仓库作者可能不再维护此项目(详见:[issue](https://github.com/nshen/vite-plugin-cesium/issues/62#issuecomment-2957419669)),故 fork 本仓库 ([coder-xiaomo/vite-plugin-cesium-next](https://github.com/coder-xiaomo/vite-plugin-cesium-next)) 继续维护,欢迎提交 Issue / Pr !
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm i cesium vite-plugin-cesium-text vite -D
|
||||
# yarn add cesium vite-plugin-cesium-text vite -D
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
add this plugin to `vite.config.js`
|
||||
|
||||
```js
|
||||
import { defineConfig } from 'vite';
|
||||
import cesium from 'vite-plugin-cesium-next'; // 👈 添加这一行
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
// ...
|
||||
cesium(), // 👈 添加这一行
|
||||
|
||||
// 或者如果你需要自定义配置,可以这样写 👇
|
||||
// cesium({ /* 这里可以添加配置 */ }),
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
以下是原仓库 README
|
||||
|
||||
# ⚡ vite-plugin-cesium
|
||||
|
||||
[](https://www.npmjs.com/package/vite-plugin-cesium)
|
||||
|
@@ -9,8 +9,8 @@
|
||||
"serve": "vite build && vite preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cesium": "1.96.0",
|
||||
"cesium": "1.129.0",
|
||||
"vite": "3.0.4",
|
||||
"vite-plugin-cesium": "link:.."
|
||||
"vite-plugin-cesium-next": "link:.."
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { defineConfig } from 'vite';
|
||||
import cesium from 'vite-plugin-cesium';
|
||||
import cesium from 'vite-plugin-cesium-next';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [cesium()]
|
||||
|
3019
package-lock.json
generated
Normal file
3019
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
33
package.json
33
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "vite-plugin-cesium",
|
||||
"version": "1.2.23",
|
||||
"name": "vite-plugin-cesium-next",
|
||||
"version": "2.0.0",
|
||||
"description": "Cesium library plugin for Vite",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
@@ -25,29 +25,32 @@
|
||||
"vite-plugin",
|
||||
"cesium"
|
||||
],
|
||||
"author": "nshen <nshen121@gmail.com>",
|
||||
"author": "coderxiaomo <codingzhang@126.com>",
|
||||
"contributors": [
|
||||
"nshen <nshen121@gmail.com>"
|
||||
],
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nshen/vite-plugin-cesium"
|
||||
"url": "git+https://github.com/coder-xiaomo/vite-plugin-cesium-next"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/nshen/vite-plugin-cesium/issues"
|
||||
"url": "https://github.com/coder-xiaomo/vite-plugin-cesium-next/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"fs-extra": "^9.1.0",
|
||||
"fs-extra": "^11.3.0",
|
||||
"rollup-plugin-external-globals": "^0.6.1",
|
||||
"serve-static": "^1.14.1"
|
||||
"serve-static": "^1.16.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@types/fs-extra": "^9.0.8",
|
||||
"@types/node": "^14.14.31",
|
||||
"@types/serve-static": "^1.13.9",
|
||||
"prettier": "^2.2.1",
|
||||
"tsup": "^6.2.1",
|
||||
"typescript": "^4.2.2",
|
||||
"vite": "^3.0.4"
|
||||
"@tsconfig/node22": "^22.0.2",
|
||||
"@types/fs-extra": "^11.0.4",
|
||||
"@types/node": "^22.15.31",
|
||||
"@types/serve-static": "^1.15.8",
|
||||
"prettier": "^3.5.3",
|
||||
"tsup": "^8.5.0",
|
||||
"typescript": "^5.8.3",
|
||||
"vite": "^6.1.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"cesium": "^1.95.0",
|
||||
|
72
src/index.ts
72
src/index.ts
@@ -13,6 +13,7 @@ interface VitePluginCesiumOptions {
|
||||
cesiumBuildRootPath?: string;
|
||||
cesiumBuildPath?: string;
|
||||
cesiumBaseUrl?: string;
|
||||
viteBase?: string;
|
||||
}
|
||||
|
||||
export default function vitePluginCesium(options: VitePluginCesiumOptions = {}): Plugin {
|
||||
@@ -21,35 +22,52 @@ export default function vitePluginCesium(options: VitePluginCesiumOptions = {}):
|
||||
devMinifyCesium = false,
|
||||
cesiumBuildRootPath = 'node_modules/cesium/Build',
|
||||
cesiumBuildPath = 'node_modules/cesium/Build/Cesium/',
|
||||
cesiumBaseUrl = 'cesium/'
|
||||
cesiumBaseUrl = 'cesium/',
|
||||
viteBase = undefined,
|
||||
} = options;
|
||||
|
||||
let CESIUM_BASE_URL = cesiumBaseUrl;
|
||||
if (!CESIUM_BASE_URL.endsWith('/')) {
|
||||
CESIUM_BASE_URL += '/';
|
||||
const CESIUM_BASE_URL = cesiumBaseUrl.endsWith('/')
|
||||
? cesiumBaseUrl
|
||||
: (cesiumBaseUrl + '/');
|
||||
|
||||
const globalVars: {
|
||||
outDir: string,
|
||||
base: string,
|
||||
isBuild: boolean,
|
||||
cesiumRelativeUrl: string,
|
||||
} = {
|
||||
outDir: 'dist',
|
||||
base: '/',
|
||||
isBuild: false,
|
||||
cesiumRelativeUrl: '',
|
||||
}
|
||||
let outDir = 'dist';
|
||||
let base: string = '/';
|
||||
let isBuild: boolean = false;
|
||||
|
||||
return {
|
||||
name: 'vite-plugin-cesium',
|
||||
name: 'vite-plugin-cesium-next',
|
||||
|
||||
config(c, { command }) {
|
||||
isBuild = command === 'build';
|
||||
if (c.base !== undefined) {
|
||||
base = c.base;
|
||||
if (base === '') base = './';
|
||||
// 项目中 vite.config.ts 配置的 base 路径
|
||||
if (viteBase) {
|
||||
globalVars.base = viteBase;
|
||||
} else if (c.base) {
|
||||
globalVars.base = c.base;
|
||||
} else {
|
||||
globalVars.base = '/';
|
||||
}
|
||||
// 是否是打包命令
|
||||
globalVars.isBuild = command === 'build';
|
||||
// 输出路径
|
||||
if (c.build?.outDir) {
|
||||
outDir = c.build.outDir;
|
||||
globalVars.outDir = c.build.outDir;
|
||||
}
|
||||
CESIUM_BASE_URL = path.posix.join(base, CESIUM_BASE_URL);
|
||||
// 项目浏览器中运行时,Cesium 资源的相对路径
|
||||
globalVars.cesiumRelativeUrl = path.posix.join(globalVars.base, CESIUM_BASE_URL);
|
||||
|
||||
const userConfig: UserConfig = {};
|
||||
if (!isBuild) {
|
||||
if (!globalVars.isBuild) {
|
||||
// -----------dev-----------
|
||||
userConfig.define = {
|
||||
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
|
||||
CESIUM_BASE_URL: JSON.stringify(globalVars.cesiumRelativeUrl)
|
||||
};
|
||||
} else {
|
||||
// -----------build------------
|
||||
@@ -60,7 +78,7 @@ export default function vitePluginCesium(options: VitePluginCesiumOptions = {}):
|
||||
chunkSizeWarningLimit: 5000,
|
||||
rollupOptions: {
|
||||
output: {
|
||||
intro: `window.CESIUM_BASE_URL = ${JSON.stringify(CESIUM_BASE_URL)};`
|
||||
intro: `window.CESIUM_BASE_URL = ${JSON.stringify(globalVars.cesiumRelativeUrl)};`
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -79,7 +97,7 @@ export default function vitePluginCesium(options: VitePluginCesiumOptions = {}):
|
||||
|
||||
configureServer({ middlewares }) {
|
||||
const cesiumPath = path.join(cesiumBuildRootPath, devMinifyCesium ? 'Cesium' : 'CesiumUnminified');
|
||||
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath, {
|
||||
middlewares.use(path.posix.join('/', globalVars.cesiumRelativeUrl), serveStatic(cesiumPath, {
|
||||
setHeaders: (res, path, stat) => {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||
}
|
||||
@@ -87,14 +105,14 @@ export default function vitePluginCesium(options: VitePluginCesiumOptions = {}):
|
||||
},
|
||||
|
||||
async closeBundle() {
|
||||
if (isBuild) {
|
||||
if (globalVars.isBuild) {
|
||||
try {
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(outDir, CESIUM_BASE_URL, 'Assets'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(outDir, CESIUM_BASE_URL, 'ThirdParty'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(outDir, CESIUM_BASE_URL, 'Workers'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(outDir, CESIUM_BASE_URL, 'Widgets'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(globalVars.outDir, CESIUM_BASE_URL, 'Assets'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(globalVars.outDir, CESIUM_BASE_URL, 'ThirdParty'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(globalVars.outDir, CESIUM_BASE_URL, 'Workers'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(globalVars.outDir, CESIUM_BASE_URL, 'Widgets'));
|
||||
if (!rebuildCesium) {
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, CESIUM_BASE_URL, 'Cesium.js'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(globalVars.outDir, CESIUM_BASE_URL, 'Cesium.js'));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('copy failed', err);
|
||||
@@ -108,15 +126,15 @@ export default function vitePluginCesium(options: VitePluginCesiumOptions = {}):
|
||||
tag: 'link',
|
||||
attrs: {
|
||||
rel: 'stylesheet',
|
||||
href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css')),
|
||||
href: normalizePath(path.join(globalVars.cesiumRelativeUrl, 'Widgets/widgets.css')),
|
||||
}
|
||||
}
|
||||
];
|
||||
if (isBuild && !rebuildCesium) {
|
||||
if (globalVars.isBuild && !rebuildCesium) {
|
||||
tags.push({
|
||||
tag: 'script',
|
||||
attrs: {
|
||||
src: normalizePath(path.join(CESIUM_BASE_URL, 'Cesium.js')),
|
||||
src: normalizePath(path.join(globalVars.cesiumRelativeUrl, 'Cesium.js')),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user