Compare commits
2 Commits
0cb418be2a
...
6df2af8285
Author | SHA1 | Date |
---|---|---|
小冰cc | 6df2af8285 | |
小冰cc | 88b60948f1 |
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "vite-plugin-earth",
|
"name": "vite-plugin-earth",
|
||||||
"version": "2.1.2",
|
"version": "2.2.0",
|
||||||
"description": "Cesium & Mars3D library plugin for Vite",
|
"description": "Cesium & Mars3D library plugin for Vite",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"module": "./dist/index.mjs",
|
"module": "./dist/index.mjs",
|
||||||
|
@ -17,7 +17,11 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "tsup --watch",
|
"dev": "tsup --watch",
|
||||||
|
"dev:cesium": "pnpm run --filter cesium-demo dev",
|
||||||
|
"dev:mars3d": "pnpm run --filter mars3d-demo dev",
|
||||||
"build": "tsup",
|
"build": "tsup",
|
||||||
|
"build:cesium": "pnpm run --filter cesium-demo build",
|
||||||
|
"build:mars3d": "pnpm run --filter mars3d-demo build",
|
||||||
"prepare": "npm run build"
|
"prepare": "npm run build"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|
|
@ -11,6 +11,6 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"cesium": "^1.105.0",
|
"cesium": "^1.105.0",
|
||||||
"vite": "^4.3.4",
|
"vite": "^4.3.4",
|
||||||
"vite-plugin-earth": "link:.."
|
"vite-plugin-earth": "workspace:*"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,5 +2,5 @@ import { defineConfig } from 'vite';
|
||||||
import { cesiumPlugin } from 'vite-plugin-earth';
|
import { cesiumPlugin } from 'vite-plugin-earth';
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [cesiumPlugin()]
|
plugins: [cesiumPlugin({ useCDN: true })]
|
||||||
});
|
});
|
|
@ -10,7 +10,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"vite": "^4.3.4",
|
"vite": "^4.3.4",
|
||||||
"vite-plugin-earth": "link:.."
|
"vite-plugin-earth": "workspace:*"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@turf/turf": "^6.5.0",
|
"@turf/turf": "^6.5.0",
|
|
@ -2,5 +2,9 @@ import { defineConfig } from 'vite';
|
||||||
import { mars3dPlugin } from 'vite-plugin-earth';
|
import { mars3dPlugin } from 'vite-plugin-earth';
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [mars3dPlugin()]
|
plugins: [
|
||||||
|
mars3dPlugin({
|
||||||
|
useCDN: true
|
||||||
|
})
|
||||||
|
]
|
||||||
});
|
});
|
|
@ -0,0 +1,3 @@
|
||||||
|
packages:
|
||||||
|
# all packages in direct subdirs of packages/
|
||||||
|
- 'packages/*'
|
33
src/index.ts
33
src/index.ts
|
@ -45,8 +45,8 @@ export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin {
|
||||||
useCDN = null
|
useCDN = null
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
// 默认使用的版本号
|
// 默认使用最新版本,可指定版本
|
||||||
let cdnVersion = Object.assign({ mars3d: '3.5.4', mars3dCesium: '1.104.3', turf: '6.5.0' }, useCDN);
|
let cdnVersion = Object.assign({ mars3d: null, mars3dCesium: null, turf: null }, useCDN);
|
||||||
|
|
||||||
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
|
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
|
||||||
let MARS3D_BASE_URL = `mars3d/`;
|
let MARS3D_BASE_URL = `mars3d/`;
|
||||||
|
@ -136,23 +136,26 @@ export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin {
|
||||||
transformIndexHtml() {
|
transformIndexHtml() {
|
||||||
const tags: HtmlTagDescriptor[] = [];
|
const tags: HtmlTagDescriptor[] = [];
|
||||||
if (useCDN) {
|
if (useCDN) {
|
||||||
let cesiumVersion = cdnVersion.mars3dCesium;
|
let cesiumVersion = cdnVersion.mars3dCesium ? `@${cdnVersion.mars3dCesium}` : '';
|
||||||
|
let mars3dVersion = cdnVersion.mars3d ? `@${cdnVersion.mars3d}` : '';
|
||||||
|
let turfVersion = cdnVersion.turf ? `@${cdnVersion.turf}` : '';
|
||||||
|
|
||||||
tags.push(
|
tags.push(
|
||||||
{
|
{
|
||||||
tag: 'link',
|
tag: 'link',
|
||||||
attrs: {
|
attrs: {
|
||||||
rel: 'stylesheet',
|
rel: 'stylesheet',
|
||||||
href: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
|
href: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium'`
|
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium'`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
attrs: {
|
attrs: {
|
||||||
src: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Cesium.js`
|
src: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Cesium.js`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -162,19 +165,19 @@ export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin {
|
||||||
tag: 'link',
|
tag: 'link',
|
||||||
attrs: {
|
attrs: {
|
||||||
rel: 'stylesheet',
|
rel: 'stylesheet',
|
||||||
href: `https://unpkg.com/mars3d@${cdnVersion.mars3d}/dist/mars3d.css`
|
href: `https://unpkg.com/mars3d${mars3dVersion}/dist/mars3d.css`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
attrs: {
|
attrs: {
|
||||||
src: `https://unpkg.com/mars3d@${cdnVersion.mars3d}/dist/mars3d.js`
|
src: `https://unpkg.com/mars3d${mars3dVersion}/dist/mars3d.js`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
attrs: {
|
attrs: {
|
||||||
src: `https://unpkg.com/@turf/turf@${cdnVersion.turf}/turf.min.js`
|
src: `https://unpkg.com/@turf/turf${turfVersion}/turf.min.js`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -237,8 +240,8 @@ export function cesiumPlugin(options: VitePluginCesiumOptions = {}): Plugin {
|
||||||
useCDN = null
|
useCDN = null
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
// 默认使用的版本号
|
// 默认使用最新版本,可指定任意版本
|
||||||
let cdnVersion = Object.assign({ cesium: '1.105.0' }, useCDN);
|
let cdnVersion = Object.assign({ cesium: null }, useCDN);
|
||||||
|
|
||||||
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
|
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
|
||||||
let outDir = 'dist';
|
let outDir = 'dist';
|
||||||
|
@ -317,23 +320,23 @@ export function cesiumPlugin(options: VitePluginCesiumOptions = {}): Plugin {
|
||||||
transformIndexHtml() {
|
transformIndexHtml() {
|
||||||
const tags: HtmlTagDescriptor[] = [];
|
const tags: HtmlTagDescriptor[] = [];
|
||||||
if (useCDN) {
|
if (useCDN) {
|
||||||
let cesiumVersion = cdnVersion.cesium;
|
let cesiumVersion = cdnVersion.cesium ? `@${cdnVersion.cesium}` : '';
|
||||||
tags.push(
|
tags.push(
|
||||||
{
|
{
|
||||||
tag: 'link',
|
tag: 'link',
|
||||||
attrs: {
|
attrs: {
|
||||||
rel: 'stylesheet',
|
rel: 'stylesheet',
|
||||||
href: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
|
href: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium'`
|
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium'`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
attrs: {
|
attrs: {
|
||||||
src: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Cesium.js`
|
src: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Cesium.js`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
267
src/mars3d.ts
267
src/mars3d.ts
|
@ -1,267 +0,0 @@
|
||||||
import fs from 'fs-extra';
|
|
||||||
import externalGlobals from 'rollup-plugin-external-globals';
|
|
||||||
import serveStatic from 'serve-static';
|
|
||||||
|
|
||||||
import path from 'path';
|
|
||||||
import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* mars3dPlugin插件构造参数
|
|
||||||
*/
|
|
||||||
interface mars3dPluginOptions {
|
|
||||||
/**
|
|
||||||
* mars3d包名,默认值为 "mars3d"
|
|
||||||
*/
|
|
||||||
mars3dPackageName?: string;
|
|
||||||
/**
|
|
||||||
* mars3d库运行时的目录名称,默认为 mars3dPackageName
|
|
||||||
*/
|
|
||||||
mars3dRunPath?: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* cesium包名,默认值为 "mars3d-cesium"
|
|
||||||
*/
|
|
||||||
cesiumPackageName?: string;
|
|
||||||
/**
|
|
||||||
* cesium库运行时的目录名称,默认为 cesiumPackageName
|
|
||||||
*/
|
|
||||||
cesiumRunPath?: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [仅build编译时]使用静态资源方式引入mars3d
|
|
||||||
*/
|
|
||||||
useStatic?: boolean;
|
|
||||||
/**
|
|
||||||
* [仅build编译时] 是否使用CDN引入资源,也可以配置object定义各库的cdn的版本号
|
|
||||||
*/
|
|
||||||
useCDN?:
|
|
||||||
| boolean
|
|
||||||
| {
|
|
||||||
mars3d?: string;
|
|
||||||
cesium?: string;
|
|
||||||
turf?: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* mars3d库在vite技术栈下的处理插件
|
|
||||||
*
|
|
||||||
* @export
|
|
||||||
* @param {mars3dPluginOptions} [options={}] 参数
|
|
||||||
* @return {*} {Plugin}
|
|
||||||
*/
|
|
||||||
export function mars3dPlugin(options: mars3dPluginOptions = {}): Plugin {
|
|
||||||
let {
|
|
||||||
mars3dPackageName = 'mars3d',
|
|
||||||
mars3dRunPath,
|
|
||||||
cesiumPackageName = 'mars3d-cesium',
|
|
||||||
cesiumRunPath,
|
|
||||||
|
|
||||||
useStatic = false,
|
|
||||||
useCDN = false
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
if (!mars3dRunPath) {
|
|
||||||
mars3dRunPath = `/${mars3dPackageName}/`;
|
|
||||||
}
|
|
||||||
if (!cesiumRunPath) {
|
|
||||||
cesiumRunPath = `/${cesiumPackageName}/`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cesiumNpmPath = `node_modules/${cesiumPackageName}/Build/Cesium/`;
|
|
||||||
const mars3dNpmPath = `node_modules/${mars3dPackageName}/dist/`;
|
|
||||||
|
|
||||||
let outDir: string, base: string, CESIUM_BASE_URL: string, MARS3D_BASE_URL: string;
|
|
||||||
let isBuild = false;
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: 'vite-plugin-mars3d',
|
|
||||||
|
|
||||||
config(c, { command }) {
|
|
||||||
isBuild = command === 'build';
|
|
||||||
if (c.base) {
|
|
||||||
base = c.base;
|
|
||||||
if (base === '') {
|
|
||||||
base = './';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
base = '/';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c.build?.outDir) {
|
|
||||||
outDir = c.build.outDir;
|
|
||||||
} else {
|
|
||||||
outDir = 'dist';
|
|
||||||
}
|
|
||||||
|
|
||||||
CESIUM_BASE_URL = path.posix.join(base, cesiumRunPath || `/${cesiumPackageName}/`);
|
|
||||||
MARS3D_BASE_URL = path.posix.join(base, mars3dRunPath || `/${mars3dPackageName}/`);
|
|
||||||
|
|
||||||
const userConfig: UserConfig = {
|
|
||||||
build: {
|
|
||||||
assetsInlineLimit: 0,
|
|
||||||
chunkSizeWarningLimit: 4000
|
|
||||||
},
|
|
||||||
define: {
|
|
||||||
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL),
|
|
||||||
MARS3D_BASE_URL: JSON.stringify(MARS3D_BASE_URL)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isBuild) {
|
|
||||||
const external = [cesiumPackageName];
|
|
||||||
const globals: any = {};
|
|
||||||
globals[cesiumPackageName] = 'Cesium';
|
|
||||||
|
|
||||||
if (useStatic) {
|
|
||||||
external.push(mars3dPackageName);
|
|
||||||
globals[mars3dPackageName] = 'mars3d';
|
|
||||||
}
|
|
||||||
|
|
||||||
userConfig.build = {
|
|
||||||
rollupOptions: {
|
|
||||||
external: external,
|
|
||||||
plugins: [externalGlobals(globals)]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
userConfig.optimizeDeps = {
|
|
||||||
include: [mars3dPackageName],
|
|
||||||
exclude: [cesiumPackageName]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return userConfig;
|
|
||||||
},
|
|
||||||
|
|
||||||
configResolved(resolvedConfig) {
|
|
||||||
outDir = path.join(resolvedConfig.root, resolvedConfig.build.outDir);
|
|
||||||
},
|
|
||||||
|
|
||||||
configureServer({ middlewares }) {
|
|
||||||
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumNpmPath));
|
|
||||||
|
|
||||||
if (useStatic) {
|
|
||||||
middlewares.use(path.posix.join('/', MARS3D_BASE_URL), serveStatic(mars3dNpmPath));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async closeBundle() {
|
|
||||||
if (isBuild && !useCDN) {
|
|
||||||
try {
|
|
||||||
await fs.copy(path.join(cesiumNpmPath, 'Assets'), path.join(outDir, `${cesiumRunPath}/Assets`));
|
|
||||||
await fs.copy(path.join(cesiumNpmPath, 'ThirdParty'), path.join(outDir, `${cesiumRunPath}/ThirdParty`));
|
|
||||||
await fs.copy(path.join(cesiumNpmPath, 'Workers'), path.join(outDir, `${cesiumRunPath}/Workers`));
|
|
||||||
await fs.copy(path.join(cesiumNpmPath, 'Widgets'), path.join(outDir, `${cesiumRunPath}/Widgets`));
|
|
||||||
await fs.copy(path.join(cesiumNpmPath, 'Cesium.js'), path.join(outDir, `${cesiumRunPath}/Cesium.js`));
|
|
||||||
|
|
||||||
if (useStatic) {
|
|
||||||
await fs.copy(path.join(mars3dNpmPath, 'img'), path.join(outDir, `${mars3dRunPath}/img`));
|
|
||||||
await fs.copy(path.join(mars3dNpmPath, 'mars3d.css'), path.join(outDir, `${mars3dRunPath}/mars3d.css`));
|
|
||||||
await fs.copy(path.join(mars3dNpmPath, 'mars3d.js'), path.join(outDir, `${mars3dRunPath}/mars3d.js`));
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`拷贝 ${cesiumPackageName} 库失败`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
transformIndexHtml() {
|
|
||||||
const tags: HtmlTagDescriptor[] = [];
|
|
||||||
if (!isBuild) {
|
|
||||||
return tags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useCDN) {
|
|
||||||
// 默认使用的版本号
|
|
||||||
let cdnVersion_cesium;
|
|
||||||
let cdnVersion_mars3d;
|
|
||||||
let cdnVersion_turf;
|
|
||||||
if (useCDN instanceof Object) {
|
|
||||||
cdnVersion_cesium = useCDN.cesium ? `@${useCDN.cesium}` : '';
|
|
||||||
cdnVersion_mars3d = useCDN.mars3d ? `@${useCDN.mars3d}` : '';
|
|
||||||
cdnVersion_turf = useCDN.turf ? `@${useCDN.turf}` : '';
|
|
||||||
} else {
|
|
||||||
cdnVersion_cesium = '';
|
|
||||||
cdnVersion_mars3d = '';
|
|
||||||
cdnVersion_turf = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
tags.push(
|
|
||||||
{
|
|
||||||
tag: 'link',
|
|
||||||
attrs: {
|
|
||||||
rel: 'stylesheet',
|
|
||||||
href: `https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium/Widgets/widgets.css`
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium'`
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
attrs: {
|
|
||||||
src: `https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium/Cesium.js`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (useStatic) {
|
|
||||||
tags.push(
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
attrs: {
|
|
||||||
src: `https://unpkg.com/@turf/turf${cdnVersion_turf}/turf.min.js`
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'link',
|
|
||||||
attrs: {
|
|
||||||
rel: 'stylesheet',
|
|
||||||
href: `https://unpkg.com/${mars3dPackageName}${cdnVersion_mars3d}/dist/mars3d.css`
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
attrs: {
|
|
||||||
src: `https://unpkg.com/${mars3dPackageName}${cdnVersion_mars3d}/dist/mars3d.js`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tags.push(
|
|
||||||
{
|
|
||||||
tag: 'link',
|
|
||||||
attrs: {
|
|
||||||
rel: 'stylesheet',
|
|
||||||
href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css'))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
attrs: { src: normalizePath(path.join(CESIUM_BASE_URL, 'Cesium.js')) }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (useStatic) {
|
|
||||||
tags.push(
|
|
||||||
{
|
|
||||||
tag: 'link',
|
|
||||||
attrs: {
|
|
||||||
rel: 'stylesheet',
|
|
||||||
href: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.css'))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: 'script',
|
|
||||||
attrs: { src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js')) }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tags;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Reference in New Issue