diff --git a/demo/index.html b/demo/index.html index 529bde4..3aac827 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,16 +1,14 @@ - - - - - + + + + cesium-vite - + - +
- - - \ No newline at end of file + + diff --git a/demo/package.json b/demo/package.json index c2c5f65..6e1611b 100644 --- a/demo/package.json +++ b/demo/package.json @@ -4,11 +4,12 @@ "license": "MIT", "scripts": { "dev": "vite", - "build": "vite build" + "build": "vite build", + "serve": "vite build && vite preview" }, "devDependencies": { - "cesium": "^1.83.0", - "vite": "^2.4.1", - "vite-plugin-cesium": "^1.2.5" + "cesium": "^1.85.0", + "vite": "^2.5.6", + "vite-plugin-cesium": "^1.2.6" } } diff --git a/demo/src/index.js b/demo/src/index.js index 2e14789..391e1ab 100644 --- a/demo/src/index.js +++ b/demo/src/index.js @@ -1,4 +1,11 @@ +/** + * You can also import Cesium Object like this + * + * import * as Cesium from 'cesium'; + * const viewer = new Cesium.Viewer('cesiumContainer'); + */ + import { Viewer } from 'cesium'; -import "./css/main.css"; +import './css/main.css'; const viewer = new Viewer('cesiumContainer'); diff --git a/demo/vite.config.js b/demo/vite.config.js index d4dcba3..3a93255 100644 --- a/demo/vite.config.js +++ b/demo/vite.config.js @@ -1,6 +1,5 @@ - import { defineConfig } from 'vite'; import cesium from 'vite-plugin-cesium'; export default defineConfig({ - plugins: [cesium()], + plugins: [cesium()] }); diff --git a/package.json b/package.json index a153766..61c47d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-cesium", - "version": "1.2.5", + "version": "1.2.6", "description": "Cesium library plugin for Vite", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -37,10 +37,9 @@ "@types/serve-static": "^1.13.9", "prettier": "^2.2.1", "typescript": "^4.2.2", - "vite": "^2.4.1" + "vite": "^2.5.6" }, "peerDependencies": { - "cesium": "^1.83.0", - "vite": "^2.4.1" + "cesium": "^1.85.0" } } diff --git a/src/index.ts b/src/index.ts index 9ed4ab0..71da6b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ -import path from 'path'; -import { Plugin, UserConfig, HtmlTagDescriptor } from 'vite'; import fs from 'fs-extra'; +import path from 'path'; import externalGlobals from 'rollup-plugin-external-globals'; import serveStatic from 'serve-static'; +import { HtmlTagDescriptor, Plugin, UserConfig } from 'vite'; interface VitePluginCesiumOptions { + /** + * rebuild cesium library, default: false + */ rebuildCesium?: boolean; devMinifyCesium?: boolean; } @@ -19,42 +22,53 @@ function vitePluginCesium( const cesiumBuildRootPath = 'node_modules/cesium/Build'; const cesiumBuildPath = 'node_modules/cesium/Build/Cesium/'; - const CESIUM_BASE_URL = '/cesium/'; + let CESIUM_BASE_URL = '/cesium/'; let outDir = 'dist'; - let base: string; + let base: string = '/'; let isBuild: boolean = false; return { name: 'vite-plugin-cesium', - config(_, { command }) { - const isBuild = command === 'build' + config(c, { command }) { + isBuild = command === 'build'; + if (c.build?.outDir) outDir = c.build.outDir; + if (c.base) { + base = c.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(isBuild ? base + CESIUM_BASE_URL : CESIUM_BASE_URL) + CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL) } }; - if (command === 'build' && !rebuildCesium) { + if (isBuild && !rebuildCesium) { userConfig.build!.rollupOptions = { external: ['cesium'], plugins: [externalGlobals({ cesium: 'Cesium' })] }; } - return userConfig; }, - configResolved(resolvedConfig) { - publicPath = resolvedConfig.publicDir; - outDir = resolvedConfig.build.outDir; - base = resolvedConfig.base; - isBuild = - resolvedConfig.isProduction || resolvedConfig.command === 'build'; + 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; }, configureServer({ middlewares }) { @@ -102,12 +116,15 @@ function vitePluginCesium( tag: 'link', attrs: { rel: 'stylesheet', - href: isBuild ? base + 'cesium/Widgets/widgets.css' : CESIUM_BASE_URL + 'Widgets/widgets.css' + href: path.join(CESIUM_BASE_URL, 'Widgets/widgets.css') } } ]; if (isBuild && !rebuildCesium) { - tags.push({ tag: 'script', attrs: { src: base + 'cesium/Cesium.js' } }); + tags.push({ + tag: 'script', + attrs: { src: path.join(base, 'cesium/Cesium.js') } + }); } return tags; } diff --git a/tsconfig.json b/tsconfig.json index 14e8a21..9e479cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,6 @@ "compilerOptions": { "outDir": "dist", "declaration": true, - "removeComments": true + "removeComments": false, }, } \ No newline at end of file