release: v2.1.0

This commit is contained in:
小冰cc 2023-06-05 17:20:36 +08:00
parent 4d2294e6b2
commit c3cea5b56c
4 changed files with 209 additions and 79 deletions

View File

@ -1,6 +1,6 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import earth from 'vite-plugin-earth'; import { cesiumPlugin } from 'vite-plugin-earth';
export default defineConfig({ export default defineConfig({
plugins: [earth({ useCDN: {} })] plugins: [cesiumPlugin()]
}); });

View File

@ -1,10 +1,6 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import earth from 'vite-plugin-earth'; import { mars3dPlugin } from 'vite-plugin-earth';
export default defineConfig({ export default defineConfig({
plugins: [ plugins: [mars3dPlugin()]
earth({
useMars3D: true
})
]
}); });

View File

@ -1,6 +1,6 @@
{ {
"name": "vite-plugin-earth", "name": "vite-plugin-earth",
"version": "2.0.5", "version": "2.1.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",

View File

@ -4,40 +4,49 @@ import externalGlobals from 'rollup-plugin-external-globals';
import serveStatic from 'serve-static'; import serveStatic from 'serve-static';
import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite'; import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite';
interface VitePluginEarthOptions { interface BaseOptions {
/**
* rebuild cesium library, default: false
*/
rebuildCesium?: boolean; rebuildCesium?: boolean;
devMinifyCesium?: boolean; devMinifyCesium?: boolean;
cesiumBuildRootPath?: string; cesiumBuildRootPath?: string;
cesiumBuildPath?: string; cesiumBuildPath?: string;
useMars3D?: boolean; }
interface VitePluginCesiumOptions extends BaseOptions {
/**
* [build编译时] 使CDN引入资源,object定义各库的cdn的版本号
*/
useCDN?: boolean | { cesium?: string };
}
interface VitePluginMars3dOptions extends BaseOptions {
/**
* [build编译时] 使CDN引入资源,object定义各库的cdn的版本号
*/
useCDN?: useCDN?:
| boolean
| { | {
mars3d?: string; mars3d?: string;
mars3dCesium?: string; mars3dCesium?: string;
turf?: string; turf?: string;
};
} }
| { cesium?: string }; /**
} * mars3d
* @param options
export default function vitePluginEarth(options: VitePluginEarthOptions = {}): Plugin { * @returns
let CESIUM_NAME = options.useMars3D ? 'mars3d-cesium' : 'cesium'; */
export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin {
let CESIUM_NAME = 'mars3d-cesium';
const { const {
rebuildCesium = false, rebuildCesium = false,
devMinifyCesium = false, devMinifyCesium = false,
cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`, cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`,
cesiumBuildPath = `${cesiumBuildRootPath}/Cesium/`, cesiumBuildPath = `${cesiumBuildRootPath}/Cesium/`,
useMars3D = false,
useCDN = null useCDN = null
} = options; } = options;
// 默认使用的版本号 // 默认使用的版本号
let cdnVersion = Object.assign( let cdnVersion = Object.assign({ mars3d: '3.5.4', mars3dCesium: '1.104.3', turf: '6.5.0' }, useCDN);
{ mars3d: '3.5.0', mars3dCesium: '1.103.1', cesium: '1.105.0', turf: '6.5.0' },
useCDN
);
let CESIUM_BASE_URL = `${CESIUM_NAME}/`; let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
let MARS3D_BASE_URL = `mars3d/`; let MARS3D_BASE_URL = `mars3d/`;
@ -46,7 +55,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
let isBuild: boolean = false; let isBuild: boolean = false;
return { return {
name: 'vite-plugin-earth', name: 'vite-plugin-mars3d',
config(c, { command }) { config(c, { command }) {
isBuild = command === 'build'; isBuild = command === 'build';
@ -84,10 +93,8 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
let external = [CESIUM_NAME]; let external = [CESIUM_NAME];
let plugins = [externalGlobals({ cesium: 'Cesium' })]; let plugins = [externalGlobals({ cesium: 'Cesium' })];
if (useMars3D) {
external.push('mars3d'); external.push('mars3d');
plugins.push(externalGlobals({ mars3d: 'mars3d' })); plugins.push(externalGlobals({ mars3d: 'mars3d' }));
}
userConfig.build = { userConfig.build = {
rollupOptions: { rollupOptions: {
@ -101,16 +108,11 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
}, },
configureServer({ middlewares }) { configureServer({ middlewares }) {
if (useMars3D) {
const cesiumPath = path.join(cesiumBuildRootPath, 'Cesium'); const cesiumPath = path.join(cesiumBuildRootPath, 'Cesium');
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath)); middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
const mars3dPath = path.join(`node_modules/mars3d`, 'dist'); const mars3dPath = path.join(`node_modules/mars3d`, 'dist');
middlewares.use(path.posix.join('/', MARS3D_BASE_URL), serveStatic(mars3dPath)); middlewares.use(path.posix.join('/', MARS3D_BASE_URL), serveStatic(mars3dPath));
} else {
const cesiumPath = path.join(cesiumBuildRootPath, devMinifyCesium ? 'Cesium' : 'CesiumUnminified');
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
}
}, },
async closeBundle() { async closeBundle() {
@ -124,9 +126,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`)); await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`));
} }
if (useMars3D) {
await fs.copy(path.join(`node_modules/mars3d/`, 'dist'), path.join(outDir, 'mars3d')); await fs.copy(path.join(`node_modules/mars3d/`, 'dist'), path.join(outDir, 'mars3d'));
}
} catch (err) { } catch (err) {
console.error('copy failed', err); console.error('copy failed', err);
} }
@ -136,7 +136,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
transformIndexHtml() { transformIndexHtml() {
const tags: HtmlTagDescriptor[] = []; const tags: HtmlTagDescriptor[] = [];
if (useCDN) { if (useCDN) {
let cesiumVersion = useMars3D ? cdnVersion.mars3dCesium : cdnVersion.cesium; let cesiumVersion = cdnVersion.mars3dCesium;
tags.push( tags.push(
{ {
tag: 'link', tag: 'link',
@ -157,7 +157,6 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
} }
); );
if (useMars3D) {
tags.push( tags.push(
{ {
tag: 'link', tag: 'link',
@ -179,7 +178,6 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
} }
} }
); );
}
} else { } else {
tags.push({ tags.push({
tag: 'link', tag: 'link',
@ -197,22 +195,158 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
}); });
} }
if (useMars3D) { tags.push({
tags.push(
{
tag: 'link', tag: 'link',
attrs: { attrs: {
rel: 'stylesheet', rel: 'stylesheet',
href: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.css')) href: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.css'))
} }
}, });
{
if (isBuild && !rebuildCesium) {
tags.push({
tag: 'script', tag: 'script',
attrs: { attrs: {
src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js')) src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js'))
} }
} });
); }
}
return tags;
}
};
}
/**
* cesium
* @param options
* @returns
*/
export function cesiumPlugin(options: VitePluginCesiumOptions = {}): Plugin {
let CESIUM_NAME = 'cesium';
const {
rebuildCesium = false,
devMinifyCesium = false,
cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`,
cesiumBuildPath = `${cesiumBuildRootPath}/Cesium/`,
useCDN = null
} = options;
// 默认使用的版本号
let cdnVersion = Object.assign({ cesium: '1.105.0' }, useCDN);
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
let outDir = 'dist';
let base: string = '/';
let isBuild: boolean = false;
return {
name: 'vite-plugin-cesium',
config(c, { command }) {
isBuild = command === 'build';
if (c.base) {
base = c.base;
if (base === '') base = './';
}
if (c.build?.outDir) {
outDir = c.build.outDir;
}
CESIUM_BASE_URL = path.posix.join(base, CESIUM_BASE_URL);
const userConfig: UserConfig = {};
if (!isBuild) {
// -----------dev-----------
userConfig.define = {
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
};
} else {
// -----------build------------
if (rebuildCesium) {
// build 1) rebuild cesium library
userConfig.build = {
assetsInlineLimit: 0,
chunkSizeWarningLimit: 5000,
rollupOptions: {
output: {
intro: `window.CESIUM_BASE_URL = "${CESIUM_BASE_URL}";`
}
}
};
} else {
// build 2) copy Cesium.js later
let external = [CESIUM_NAME];
let plugins = [externalGlobals({ cesium: 'Cesium' })];
userConfig.build = {
rollupOptions: {
external: external,
plugins: plugins
}
};
}
}
return userConfig;
},
configureServer({ middlewares }) {
const cesiumPath = path.join(cesiumBuildRootPath, devMinifyCesium ? 'Cesium' : 'CesiumUnminified');
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
},
async closeBundle() {
if (isBuild && !useCDN) {
try {
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(outDir, `${CESIUM_NAME}/Assets`));
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(outDir, `${CESIUM_NAME}/ThirdParty`));
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(outDir, `${CESIUM_NAME}/Workers`));
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(outDir, `${CESIUM_NAME}/Widgets`));
if (!rebuildCesium) {
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`));
}
} catch (err) {
console.error('copy failed', err);
}
}
},
transformIndexHtml() {
const tags: HtmlTagDescriptor[] = [];
if (useCDN) {
let cesiumVersion = cdnVersion.cesium;
tags.push(
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
}
},
{
tag: 'script',
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium'`
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Cesium.js`
}
}
);
} else {
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css'))
}
});
if (isBuild && !rebuildCesium) {
tags.push({
tag: 'script',
attrs: {
src: normalizePath(path.join(base, `${CESIUM_NAME}/Cesium.js`))
}
});
} }
} }