demo-mars3d
This commit is contained in:
69
src/index.ts
69
src/index.ts
@ -12,17 +12,20 @@ interface VitePluginEarthOptions {
|
||||
devMinifyCesium?: boolean;
|
||||
cesiumBuildRootPath?: string;
|
||||
cesiumBuildPath?: string;
|
||||
useMars3D?: boolean;
|
||||
}
|
||||
|
||||
export default function vitePluginEarth(options: VitePluginEarthOptions = {}): Plugin {
|
||||
let CESIUM_NAME = options.useMars3D ? 'mars3d-cesium' : 'cesium';
|
||||
const {
|
||||
rebuildCesium = false,
|
||||
devMinifyCesium = false,
|
||||
cesiumBuildRootPath = 'node_modules/cesium/Build',
|
||||
cesiumBuildPath = 'node_modules/cesium/Build/Cesium/'
|
||||
cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`,
|
||||
cesiumBuildPath = `node_modules/${CESIUM_NAME}/Build/Cesium/`
|
||||
} = options;
|
||||
|
||||
let CESIUM_BASE_URL = 'cesium/';
|
||||
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
|
||||
let MARS3D_BASE_URL = `mars3d/`;
|
||||
let outDir = 'dist';
|
||||
let base: string = '/';
|
||||
let isBuild: boolean = false;
|
||||
@ -40,11 +43,13 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
|
||||
outDir = c.build.outDir;
|
||||
}
|
||||
CESIUM_BASE_URL = path.posix.join(base, CESIUM_BASE_URL);
|
||||
MARS3D_BASE_URL = path.posix.join(base, MARS3D_BASE_URL);
|
||||
const userConfig: UserConfig = {};
|
||||
if (!isBuild) {
|
||||
// -----------dev-----------
|
||||
userConfig.define = {
|
||||
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
|
||||
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL),
|
||||
MARS3D_BASE_URL: JSON.stringify(MARS3D_BASE_URL)
|
||||
};
|
||||
} else {
|
||||
// -----------build------------
|
||||
@ -61,10 +66,18 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
|
||||
};
|
||||
} else {
|
||||
// build 2) copy Cesium.js later
|
||||
let external = ['cesium'];
|
||||
let plugins = [externalGlobals({ cesium: 'Cesium' })];
|
||||
|
||||
if (options.useMars3D) {
|
||||
external.push('mars3d');
|
||||
plugins.push(externalGlobals({ mars3d: 'mars3d' }));
|
||||
}
|
||||
|
||||
userConfig.build = {
|
||||
rollupOptions: {
|
||||
external: ['cesium'],
|
||||
plugins: [externalGlobals({ cesium: 'Cesium' })]
|
||||
external: external,
|
||||
plugins: plugins
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -73,19 +86,31 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
|
||||
},
|
||||
|
||||
configureServer({ middlewares }) {
|
||||
const cesiumPath = path.join(cesiumBuildRootPath, devMinifyCesium ? 'Cesium' : 'CesiumUnminified');
|
||||
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
|
||||
if (options.useMars3D) {
|
||||
const cesiumPath = path.join(cesiumBuildRootPath, 'Cesium');
|
||||
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
|
||||
|
||||
const mars3dPath = path.join(`node_modules/mars3d`, 'dist');
|
||||
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() {
|
||||
if (isBuild) {
|
||||
try {
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(outDir, 'cesium/Assets'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(outDir, 'cesium/ThirdParty'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(outDir, 'cesium/Workers'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(outDir, 'cesium/Widgets'));
|
||||
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/Cesium.js'));
|
||||
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`));
|
||||
}
|
||||
|
||||
if (options.useMars3D) {
|
||||
await fs.copy(path.join(`node_modules/mars3d/`, 'dist'), path.join(outDir, 'mars3d'));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('copy failed', err);
|
||||
@ -111,6 +136,24 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (options.useMars3D) {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user