diff --git a/README.md b/README.md index dfa106e..21d5d5d 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,22 @@ Object.defineProperty(globalThis, 'CESIUM_BASE_URL', { To prevent it, see `customCesiumBaseUrl` in Options. +If using Cesium version 1.97 or older, you can set `css` option to `true`: + +```javascript +export default defineConfig({ + plugins: [ + cesium({ css: true }) + ] +}) +``` + +That will automatically add Cesium's css to your `index.html`: + +```html + +``` + ## :wrench: Options In addition, the plugin provides some configuration options: @@ -72,7 +88,11 @@ export default defineConfig({ /** * If `true`, you need to manually set the CESIUM_BASE_URL. */ - customCesiumBaseUrl: false + customCesiumBaseUrl: false, + /** + * If `true`, Cesium's css will be added automatically. + */ + css: boolean }) ] }) @@ -104,4 +124,5 @@ export default defineConfig({ ``` ## Others + If you are a Vue user, maybe try [cesium-use](https://s3xysteak.github.io/cesium-use/) ! diff --git a/README.zh-CN.md b/README.zh-CN.md index 4e2137c..fa9bb1a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -2,7 +2,7 @@ [English](README.md) | 简体中文 -这会将 `Cesium.js` 外部化,并在打包时自动拷贝 CesiumJS 的四大库和核心文件。 +这会将 `Cesium.js` 与其样式 `widget.css` 外部化,并在打包时自动拷贝 CesiumJS 的四大库和核心文件。 它还支持 [@cesium/engine](https://community.cesium.com/t/cesium-engine-and-cesium-widgets-are-now-available-for-testing/20898) ! @@ -49,6 +49,22 @@ Object.defineProperty(globalThis, 'CESIUM_BASE_URL', { 如果要阻止这个默认行为,见Options中的`customCesiumBaseUrl`。 +如果使用的Cesium版本为v1.97及更高,你可以将 `css` 选项设置为 `true`: + +```javascript +export default defineConfig({ + plugins: [ + cesium({ css: true }) + ] +}) +``` + +这将会自动将Cesium的样式添加到你的 `index.html`: + +```html + +``` + ## :wrench: 选项 除此以外,插件提供了一些配置项: @@ -72,7 +88,11 @@ export default defineConfig({ /** * 设置为true时,你需要自行设置CESIUM_BASE_URL */ - customCesiumBaseUrl: false + customCesiumBaseUrl: false, + /** + * 为 `true` 时, Cesium的样式将会被自动添加. + */ + css: boolean }) ] }) diff --git a/examples/preact/package.json b/examples/preact/package.json index aa724c6..d6c1b73 100644 --- a/examples/preact/package.json +++ b/examples/preact/package.json @@ -10,7 +10,7 @@ }, "dependencies": { "@preact/signals": "^1.3.0", - "cesium": "^1.119.0", + "cesium": "^1.121.1", "preact": "^10.22.0" }, "devDependencies": { diff --git a/examples/react/package.json b/examples/react/package.json index 486f619..f37a5a1 100644 --- a/examples/react/package.json +++ b/examples/react/package.json @@ -10,7 +10,7 @@ "preview": "vite preview" }, "dependencies": { - "cesium": "^1.119.0", + "cesium": "^1.121.1", "react": "^18.3.1", "react-dom": "^18.3.1" }, diff --git a/examples/solid/package.json b/examples/solid/package.json index 0553ba4..164d585 100644 --- a/examples/solid/package.json +++ b/examples/solid/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "dependencies": { - "cesium": "^1.119.0", + "cesium": "^1.121.1", "solid-js": "^1.8.17" }, "devDependencies": { diff --git a/examples/svelte/package.json b/examples/svelte/package.json index bfde4b1..7f8a1d6 100644 --- a/examples/svelte/package.json +++ b/examples/svelte/package.json @@ -20,6 +20,6 @@ "vite-plugin-cesium-build": "^0.4.3" }, "dependencies": { - "cesium": "^1.119.0" + "cesium": "^1.121.1" } } diff --git a/examples/vanilla/package.json b/examples/vanilla/package.json index f73623b..f825699 100644 --- a/examples/vanilla/package.json +++ b/examples/vanilla/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "dependencies": { - "cesium": "^1.119.0" + "cesium": "^1.121.1" }, "devDependencies": { "typescript": "^5.2.2", diff --git a/examples/vue/package.json b/examples/vue/package.json index 1372496..647aa0a 100644 --- a/examples/vue/package.json +++ b/examples/vue/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "dependencies": { - "cesium": "^1.119.0", + "cesium": "^1.121.1", "vue": "^3.4.29" }, "devDependencies": { diff --git a/src/core.ts b/src/core.ts index 4360e2a..823c4c6 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,4 +1,5 @@ export * from './core/copyCesium' export * from './core/importCesium' +export * from './core/importCss' export * from './core/resolveOptions' export * from './core/setBaseUrl' diff --git a/src/core/importCss.ts b/src/core/importCss.ts new file mode 100644 index 0000000..dd14225 --- /dev/null +++ b/src/core/importCss.ts @@ -0,0 +1,27 @@ +import type { Plugin, ResolvedConfig } from 'vite' +import type { BuildCesiumOptions } from './resolveOptions' +import { isString } from './utils' + +export function importCss(options: BuildCesiumOptions, path: (base: string) => string, apply?: Plugin['apply']): Plugin { + const { css } = options + let base: ResolvedConfig['base'] + + return { + name: `vite-plugin-cesium-build:importCss${isString(apply) ? `:${apply}` : ''}`, + apply, + configResolved(resolvedConfig) { + base = resolvedConfig.base + }, + transformIndexHtml: css + ? () => [ + { + tag: 'link', + attrs: { + rel: 'stylesheet', + href: path(base), + }, + }, + ] + : void 0, + } +} diff --git a/src/core/resolveOptions.ts b/src/core/resolveOptions.ts index 1148e72..58c1e9d 100644 --- a/src/core/resolveOptions.ts +++ b/src/core/resolveOptions.ts @@ -17,6 +17,12 @@ export interface BuildCesiumOptions { * @default false */ customCesiumBaseUrl: boolean | string + /** + * If `true`, cesium's css will be added automatically. + * + * @default false + */ + css: boolean } export function resolveOptions(options: Partial = {}, src: string): BuildCesiumOptions { @@ -24,11 +30,13 @@ export function resolveOptions(options: Partial = {}, src: s from = src, to = 'cesium-package', customCesiumBaseUrl = false, + css = false, } = options return { from: from.replace(/[/\\]$/, ''), to: to.replace(/^[/\\]|[/\\]$/, ''), customCesiumBaseUrl, + css, } } diff --git a/src/engine.ts b/src/engine.ts index 7a9ff6a..f908777 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -1,33 +1,50 @@ import type { Plugin } from 'vite' -import { type BuildCesiumOptions, copyCesium, resolveOptions, setBaseUrl } from './core' +import { type BuildCesiumOptions, copyCesium, importCss, resolveOptions, setBaseUrl } from './core' function pluginEntry(pluginOptions?: Partial): Plugin[] { const options = resolveOptions(pluginOptions, 'node_modules/@cesium/engine') + const customCopyList = [ + { + src: `${options.from}/Source/Assets/*`, + dest: `${options.to}/Assets/`, + }, + { + src: `${options.from}/Source/ThirdParty/*.wasm`, + dest: `${options.to}/ThirdParty/`, + }, + { + src: `${options.from}/Build/ThirdParty/*`, + dest: `${options.to}/ThirdParty/`, + }, + { + src: `${options.from}/Build/Workers/*`, + dest: `${options.to}/Workers/`, + }, + ] + + if (options.css) { + customCopyList.push({ + src: `${options.from}/Source/Widget/CesiumWidget.css`, + dest: `${options.to}/Widget/`, + }) + } + return [ ...copyCesium( options, [], - [ - { - src: `${options.from}/Source/Assets/*`, - dest: `${options.to}/Assets/`, - }, - { - src: `${options.from}/Source/ThirdParty/*.wasm`, - dest: `${options.to}/ThirdParty/`, - }, - { - src: `${options.from}/Build/ThirdParty/*`, - dest: `${options.to}/ThirdParty/`, - }, - { - src: `${options.from}/Build/Workers/*`, - dest: `${options.to}/Workers/`, - }, - ], + customCopyList, ), + + ...options.css + ? [ + importCss(options, base => `${base}${options.from}/Source/Widget/CesiumWidget.css`, 'serve'), + importCss(options, base => `${base}${options.to}/Widget/CesiumWidget.css`, 'build'), + ] + : [], + setBaseUrl(options), ] } diff --git a/src/index.ts b/src/index.ts index 6e7ad5f..3976d14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,25 +2,42 @@ import type { Plugin } from 'vite' import { viteExternalsPlugin } from 'vite-plugin-externals' -import { type BuildCesiumOptions, copyCesium, importCesium, resolveOptions, setBaseUrl } from './core' +import { type BuildCesiumOptions, copyCesium, importCesium, importCss, resolveOptions, setBaseUrl } from './core' function pluginEntry(pluginOptions?: Partial): Plugin[] { const options = resolveOptions(pluginOptions, 'node_modules/cesium/Build/Cesium') + const customCopyList = [ + { + src: `${options.from}/Cesium.js`, + dest: `${options.to}/`, + }, + ] + if (options.css) { + customCopyList.push({ + src: `${options.from}/Widgets/widgets.css`, + dest: `${options.to}/Widgets/`, + }) + } + return [ viteExternalsPlugin({ cesium: 'Cesium' }), ...copyCesium( options, ['Assets', 'ThirdParty', 'Widgets', 'Workers'], - { - src: `${options.from}/Cesium.js`, - dest: `${options.to}/`, - }, + customCopyList, ), importCesium(base => `${base}${options.from}Unminified/Cesium.js`, 'serve'), importCesium(base => `${base}${options.to}/Cesium.js`, 'build'), + ...options.css + ? [ + importCss(options, base => `${base}${options.from}/Widgets/widgets.css`, 'serve'), + importCss(options, base => `${base}${options.to}/Widgets/widgets.css`, 'build'), + ] + : [], + setBaseUrl(options), ] } diff --git a/test/playground/package.json b/test/playground/package.json index 892bdc2..2c9a3fb 100644 --- a/test/playground/package.json +++ b/test/playground/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "dependencies": { - "cesium": "^1.119.0" + "cesium": "^1.121.1" }, "devDependencies": { "typescript": "^5.5.3", diff --git a/test/playground/pnpm-lock.yaml b/test/playground/pnpm-lock.yaml index 7c03ca7..24b178f 100644 --- a/test/playground/pnpm-lock.yaml +++ b/test/playground/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: cesium: - specifier: ^1.119.0 - version: 1.119.0 + specifier: ^1.121.1 + version: 1.121.1 devDependencies: typescript: specifier: ^5.5.3 @@ -24,12 +24,12 @@ importers: packages: - '@cesium/engine@10.0.0': - resolution: {integrity: sha512-0bynRIV5nGjZGuRm0ajvZ4cyIwSW/Z29CMaWcruJUhjprEW9u9YvJ5xsJkTe1qD45sR2E1XEejVkPv056x5GNg==} + '@cesium/engine@11.0.0': + resolution: {integrity: sha512-0z3mJNNly407h3/bAvGoGnjA0ygprkVz32z9x1VNmPXf9AuybKu7Q4wRrChUhC/qsNj3L45kP6KqyDuHSe2bhw==} engines: {node: '>=14.0.0'} - '@cesium/widgets@7.0.0': - resolution: {integrity: sha512-h8ntI7irvzZbtXcYmwDXxJe8WDbrGreoV7Z+IyA18M+ko7wI5oFtWwVsqH6QwNPNqE7gk7YxqVbGUi0UsRPC8g==} + '@cesium/widgets@8.0.0': + resolution: {integrity: sha512-Rnid5BbLBZjwFdbr3lgOwK3fhql/BeEbf5l9ZZE+Zc7V8Fb6wgKM8QmrL+bbJ+BtuiELp+SkNxcF97Ktifxb4g==} engines: {node: '>=14.0.0'} '@esbuild/aix-ppc64@0.21.5': @@ -292,8 +292,8 @@ packages: cpu: [x64] os: [win32] - '@tweenjs/tween.js@23.1.2': - resolution: {integrity: sha512-kMCNaZCJugWI86xiEHaY338CU5JpD0B97p1j1IKNn/Zto8PgACjQx0UxbHjmOcLl/dDOBnItwD07KmCs75pxtQ==} + '@tweenjs/tween.js@25.0.0': + resolution: {integrity: sha512-XKLA6syeBUaPzx4j3qwMqzzq+V4uo72BnlbOjmuljLrRqdsd3qnzvZZoxvMHZ23ndsRS4aufU6JOZYpCbU6T1A==} '@types/estree@1.0.5': resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} @@ -328,8 +328,8 @@ packages: resolution: {integrity: sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==} engines: {node: '>=8'} - cesium@1.119.0: - resolution: {integrity: sha512-ifapkGusMJ2YhpyjAXGNiiZXFZ+QLzWro1OyBsqJjSGOK95Aab9dOBw0aPDXdzlaN7hxUFViN5+b2nN75fC9QA==} + cesium@1.121.1: + resolution: {integrity: sha512-WpdaGFd8qCOHiE9khV0sWWjCMXAX1U9Aou8MzMC/F0Akp6YqcOk4HLk4LLhZKeOjO/Bh27iTIW2mxEZ5+dtCxg==} engines: {node: '>=18.18.0'} chokidar@3.6.0: @@ -349,8 +349,8 @@ packages: draco3d@1.5.7: resolution: {integrity: sha512-m6WCKt/erDXcw+70IJXnG7M3awwQPAsZvJGX5zY7beBqpELw6RDGkYVU0W43AFxye4pDZ5i2Lbyc/NNGqwjUVQ==} - earcut@2.2.4: - resolution: {integrity: sha512-/pjZsA1b4RPHbeWZQn66SWS8nZZWLQQ23oE3Eam7aroEFGEvwKAsJfZ9ytiEMycfzXWpca4FA9QIOehf7PocBQ==} + earcut@3.0.0: + resolution: {integrity: sha512-41Fs7Q/PLq1SDbqjsgcY7GA42T0jvaCNGXgGtsNdvg+Yv8eIu06bxv4/PoREkZ9nMDNwnUSG9OFB9+yv8eKhDg==} es-module-lexer@0.4.1: resolution: {integrity: sha512-ooYciCUtfw6/d2w56UVeqHPcoCFAiJdz5XOkYpv/Txl1HMUozpXjz/2RIQgqwKdXNDPSF1W7mJCFse3G+HDyAA==} @@ -479,11 +479,11 @@ packages: queue-microtask@1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} - quickselect@2.0.0: - resolution: {integrity: sha512-RKJ22hX8mHe3Y6wH/N3wCM6BWtjaxIyyUIkpHOvfFnxdI4yD4tBXEBKSbriGujF6jnSVkJrffuo6vxACiSSxIw==} + quickselect@3.0.0: + resolution: {integrity: sha512-XdjUArbK4Bm5fLLvlm5KpTFOiOThgfWWI4axAZDWg4E/0mKdZyI9tNEfds27qCi1ze/vwTR16kvmmGhRra3c2g==} - rbush@3.0.1: - resolution: {integrity: sha512-XRaVO0YecOpEuIvbhbpTrZgoiI6xBlz6hnlr6EHhd+0x9ase6EmeN+hdwwUaJvLcsFFQ8iWVF1GAK1yB0BWi0w==} + rbush@4.0.1: + resolution: {integrity: sha512-IP0UpfeWQujYC8Jg162rMNc01Rf0gWMMAb2Uxus/Q0qOFw4lCcq6ZnQEZwUoJqWyUGJ9th7JjwI4yIWo+uvoAQ==} readdirp@3.6.0: resolution: {integrity: sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==} @@ -582,15 +582,15 @@ packages: snapshots: - '@cesium/engine@10.0.0': + '@cesium/engine@11.0.0': dependencies: - '@tweenjs/tween.js': 23.1.2 + '@tweenjs/tween.js': 25.0.0 '@zip.js/zip.js': 2.7.45 autolinker: 4.0.0 bitmap-sdf: 1.0.4 dompurify: 3.1.6 draco3d: 1.5.7 - earcut: 2.2.4 + earcut: 3.0.0 grapheme-splitter: 1.0.4 jsep: 1.3.8 kdbush: 4.0.2 @@ -600,13 +600,13 @@ snapshots: meshoptimizer: 0.21.0 pako: 2.1.0 protobufjs: 7.3.2 - rbush: 3.0.1 + rbush: 4.0.1 topojson-client: 3.1.0 urijs: 1.19.11 - '@cesium/widgets@7.0.0': + '@cesium/widgets@8.0.0': dependencies: - '@cesium/engine': 10.0.0 + '@cesium/engine': 11.0.0 nosleep.js: 0.12.0 '@esbuild/aix-ppc64@0.21.5': @@ -761,7 +761,7 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.1': optional: true - '@tweenjs/tween.js@23.1.2': {} + '@tweenjs/tween.js@25.0.0': {} '@types/estree@1.0.5': {} @@ -790,10 +790,10 @@ snapshots: dependencies: fill-range: 7.1.1 - cesium@1.119.0: + cesium@1.121.1: dependencies: - '@cesium/engine': 10.0.0 - '@cesium/widgets': 7.0.0 + '@cesium/engine': 11.0.0 + '@cesium/widgets': 8.0.0 chokidar@3.6.0: dependencies: @@ -815,7 +815,7 @@ snapshots: draco3d@1.5.7: {} - earcut@2.2.4: {} + earcut@3.0.0: {} es-module-lexer@0.4.1: {} @@ -962,11 +962,11 @@ snapshots: queue-microtask@1.2.3: {} - quickselect@2.0.0: {} + quickselect@3.0.0: {} - rbush@3.0.1: + rbush@4.0.1: dependencies: - quickselect: 2.0.0 + quickselect: 3.0.0 readdirp@3.6.0: dependencies: diff --git a/test/playground/src/main.ts b/test/playground/src/main.ts index 5a8a51e..4c42892 100644 --- a/test/playground/src/main.ts +++ b/test/playground/src/main.ts @@ -1,5 +1,4 @@ import * as Cesium from 'cesium' -import 'cesium/Build/Cesium/Widgets/widgets.css' import './main.css' diff --git a/test/playground/vite.config.ts b/test/playground/vite.config.ts index 1ce30a2..d36a106 100644 --- a/test/playground/vite.config.ts +++ b/test/playground/vite.config.ts @@ -3,5 +3,5 @@ import cesium from 'vite-plugin-cesium-build' export default defineConfig({ base: '', - plugins: [cesium()], + plugins: [cesium({ css: true })], })