2022-02-10 13:08:07 +08:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import path from 'path';
|
|
|
|
import externalGlobals from 'rollup-plugin-external-globals';
|
|
|
|
import serveStatic from 'serve-static';
|
|
|
|
import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite';
|
|
|
|
|
|
|
|
interface VitePluginCesiumOptions {
|
|
|
|
/**
|
|
|
|
* rebuild cesium library, default: false
|
|
|
|
*/
|
|
|
|
rebuildCesium?: boolean;
|
|
|
|
devMinifyCesium?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
function vitePluginCesium(
|
|
|
|
options: VitePluginCesiumOptions = {
|
|
|
|
rebuildCesium: false,
|
|
|
|
devMinifyCesium: false
|
|
|
|
}
|
|
|
|
): Plugin {
|
|
|
|
const { rebuildCesium, devMinifyCesium } = options;
|
|
|
|
|
|
|
|
const cesiumBuildRootPath = 'node_modules/mars3d-cesium/Build';
|
2022-02-10 15:35:43 +08:00
|
|
|
const cesiumBuildPath = 'node_modules/mars3d-cesium/Build/Cesium/';const mars3dBuildPath = 'node_modules/mars3d/dist/';
|
2022-02-10 13:08:07 +08:00
|
|
|
|
2022-02-10 15:35:43 +08:00
|
|
|
let CESIUM_BASE_URL = '/mars3d-cesium/';
|
2022-02-10 13:08:07 +08:00
|
|
|
let MARS3D_BASE_URL = '/mars3d/';
|
|
|
|
let outDir = 'dist';
|
|
|
|
let base: string = '/';
|
|
|
|
let isBuild: boolean = false;
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: 'vite-plugin-ice-mars3d',
|
|
|
|
|
|
|
|
config(c, { command }) {
|
|
|
|
isBuild = command === 'build';
|
|
|
|
if (c.base) {
|
|
|
|
base = c.base;
|
|
|
|
}
|
2022-02-10 15:35:43 +08:00
|
|
|
if (base === '') base = './';
|
|
|
|
if (isBuild) CESIUM_BASE_URL = path.join(base, CESIUM_BASE_URL);
|
|
|
|
const userConfig: UserConfig = {
|
|
|
|
build: {
|
|
|
|
assetsInlineLimit: 0,
|
|
|
|
chunkSizeWarningLimit: 4000
|
|
|
|
},
|
|
|
|
define: {
|
|
|
|
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
|
|
|
|
}
|
|
|
|
};
|
2022-02-10 13:08:07 +08:00
|
|
|
if (!isBuild) {
|
|
|
|
userConfig.optimizeDeps = {
|
|
|
|
exclude: ['mars3d-cesium']
|
|
|
|
};
|
2022-02-10 15:35:43 +08:00
|
|
|
}
|
|
|
|
if (isBuild && !rebuildCesium) {
|
|
|
|
userConfig.build!.rollupOptions = {
|
|
|
|
external: ['cesium','mars3d'],
|
|
|
|
plugins: [externalGlobals({ cesium: 'Cesium' }),externalGlobals({ mars3d: 'mars3d' })]
|
2022-02-10 13:08:07 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return userConfig;
|
|
|
|
},
|
|
|
|
|
2022-02-10 15:35:43 +08:00
|
|
|
async load(id: string) {
|
|
|
|
if (!rebuildCesium) return null;
|
|
|
|
// replace CESIUM_BASE_URL variable in 'cesium/Source/Core/buildModuleUrl.js'
|
|
|
|
if (id.includes('buildModuleUrl')) {
|
|
|
|
let file = fs.readFileSync(id, { encoding: 'utf8' });
|
|
|
|
file = file.replace(
|
|
|
|
/CESIUM_BASE_URL/g,
|
|
|
|
JSON.stringify(CESIUM_BASE_URL)
|
|
|
|
);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2022-02-10 13:08:07 +08:00
|
|
|
configureServer({ middlewares }) {
|
|
|
|
const cesiumPath = path.join(
|
|
|
|
cesiumBuildRootPath,
|
2022-02-10 15:35:43 +08:00
|
|
|
'Cesium'
|
|
|
|
// devMinifyCesium ? 'Cesium' : 'CesiumUnminified'
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
|
|
|
middlewares.use(CESIUM_BASE_URL, serveStatic(cesiumPath));
|
|
|
|
},
|
|
|
|
|
|
|
|
async closeBundle() {
|
|
|
|
if (isBuild) {
|
|
|
|
try {
|
|
|
|
await fs.copy(
|
|
|
|
path.join(cesiumBuildPath, 'Assets'),
|
2022-02-10 15:35:43 +08:00
|
|
|
path.join(outDir, 'mars3d-cesium/Assets')
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
|
|
|
await fs.copy(
|
|
|
|
path.join(cesiumBuildPath, 'ThirdParty'),
|
2022-02-10 15:35:43 +08:00
|
|
|
path.join(outDir, 'mars3d-cesium/ThirdParty')
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
|
|
|
await fs.copy(
|
|
|
|
path.join(cesiumBuildPath, 'Workers'),
|
2022-02-10 15:35:43 +08:00
|
|
|
path.join(outDir, 'mars3d-cesium/Workers')
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
|
|
|
await fs.copy(
|
|
|
|
path.join(cesiumBuildPath, 'Widgets'),
|
2022-02-10 15:35:43 +08:00
|
|
|
path.join(outDir, 'mars3d-cesium/Widgets')
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
|
|
|
if (!rebuildCesium) {
|
|
|
|
await fs.copy(
|
|
|
|
path.join(cesiumBuildPath, 'Cesium.js'),
|
2022-02-10 15:35:43 +08:00
|
|
|
path.join(outDir, 'mars3d-cesium/Cesium.js')
|
2022-02-10 13:08:07 +08:00
|
|
|
);
|
2022-02-10 15:35:43 +08:00
|
|
|
}
|
2022-02-10 13:08:07 +08:00
|
|
|
await fs.copy(
|
|
|
|
path.join(mars3dBuildPath, ''),
|
|
|
|
path.join(outDir, 'mars3d')
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('copy failed', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
transformIndexHtml() {
|
|
|
|
const tags: HtmlTagDescriptor[] = [
|
|
|
|
{
|
|
|
|
tag: 'link',
|
|
|
|
attrs: {
|
|
|
|
rel: 'stylesheet',
|
|
|
|
href: normalizePath(
|
|
|
|
path.join(CESIUM_BASE_URL, 'Widgets/widgets.css')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'link',
|
|
|
|
attrs: {
|
|
|
|
rel: 'stylesheet',
|
|
|
|
href: normalizePath(
|
|
|
|
path.join(MARS3D_BASE_URL, 'mars3d.css')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
if (isBuild && !rebuildCesium) {
|
|
|
|
tags.push({
|
|
|
|
tag: 'script',
|
2022-02-10 15:35:43 +08:00
|
|
|
attrs: { src: normalizePath(path.join(base, 'mars3d-cesium/Cesium.js')) }
|
2022-02-10 13:08:07 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (isBuild) {
|
|
|
|
tags.push({
|
|
|
|
tag: 'script',
|
|
|
|
attrs: { src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js')) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default vitePluginCesium;
|