📦 NEW: add rebuildCesium option
This commit is contained in:
62
src/index.ts
62
src/index.ts
@@ -1,8 +1,11 @@
|
||||
import path from 'path';
|
||||
import { Plugin } from 'vite';
|
||||
import { Plugin, UserConfig, HtmlTagDescriptor } from 'vite';
|
||||
import fs from 'fs-extra';
|
||||
import externalGlobals from "rollup-plugin-external-globals";
|
||||
|
||||
export default function (): Plugin {
|
||||
type options = { rebuildCesium: boolean; };
|
||||
|
||||
export default function ({ rebuildCesium }: options = { rebuildCesium: false }): Plugin {
|
||||
|
||||
const cesiumBuildPath = 'node_modules/cesium/Build/Cesium/';
|
||||
let publicPath = 'public';
|
||||
@@ -10,45 +13,58 @@ export default function (): Plugin {
|
||||
return {
|
||||
name: 'vite-plugin-cesium',
|
||||
|
||||
config(config, { command }) {
|
||||
const userConfig: UserConfig = {
|
||||
build: {
|
||||
assetsInlineLimit: 0,
|
||||
chunkSizeWarningLimit: 4000
|
||||
},
|
||||
define: {
|
||||
CESIUM_BASE_URL: '/cesium/'
|
||||
}
|
||||
};
|
||||
if (command === 'build' && !rebuildCesium) {
|
||||
userConfig.build!.rollupOptions = {
|
||||
external: ['cesium'],
|
||||
plugins: [
|
||||
externalGlobals({ cesium: "Cesium" })
|
||||
]
|
||||
};
|
||||
}
|
||||
return userConfig;
|
||||
},
|
||||
|
||||
configResolved(resolvedConfig) {
|
||||
publicPath = resolvedConfig.publicDir;
|
||||
},
|
||||
|
||||
async buildStart(options) {
|
||||
try {
|
||||
const exists = await fs.pathExists(path.join(publicPath, 'cesium/Assets'));
|
||||
const exists = await fs.pathExists(path.join(publicPath, 'cesium'));
|
||||
if (!exists) {
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(publicPath, 'cesium/Assets'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(publicPath, 'cesium/ThirdParty'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(publicPath, 'cesium/Workers'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(publicPath, 'cesium/Widgets'));
|
||||
|
||||
}
|
||||
if (rebuildCesium) {
|
||||
await fs.remove(path.join(publicPath, 'cesium/Cesium.js'));
|
||||
} else {
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(publicPath, 'cesium/Cesium.js'));
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('copy failed');
|
||||
console.error('copy failed', err);
|
||||
}
|
||||
},
|
||||
|
||||
config() {
|
||||
return {
|
||||
build: {
|
||||
assetsInlineLimit: 0,
|
||||
chunkSizeWarningLimit: 4000
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
transformIndexHtml() {
|
||||
return [
|
||||
{
|
||||
tag: 'link',
|
||||
attrs: { rel: 'stylesheet', href: 'cesium/Widgets/widgets.css' },
|
||||
},
|
||||
{
|
||||
tag: 'script',
|
||||
children: 'window.CESIUM_BASE_URL = "/cesium/"'
|
||||
}
|
||||
];
|
||||
const tags: HtmlTagDescriptor[] = [{ tag: 'link', attrs: { rel: 'stylesheet', href: 'cesium/Widgets/widgets.css' } }];
|
||||
if (!rebuildCesium) {
|
||||
tags.push({ tag: 'script', attrs: { src: 'cesium/Cesium.js' } });
|
||||
}
|
||||
return tags;
|
||||
},
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user