支持cdn方式打包
This commit is contained in:
		
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							@ -1,6 +1,6 @@
 | 
			
		||||
MIT License
 | 
			
		||||
 | 
			
		||||
Copyright (c) 2021 Nshen
 | 
			
		||||
Copyright (c) 2021 xuwoool@qq.com
 | 
			
		||||
 | 
			
		||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										136
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										136
									
								
								README.md
									
									
									
									
									
								
							@ -1,65 +1,70 @@
 | 
			
		||||
# ⚡ vite-plugin-cesium
 | 
			
		||||
# ⚡ vite-plugin-earth
 | 
			
		||||
 | 
			
		||||
[](https://www.npmjs.com/package/vite-plugin-cesium)
 | 
			
		||||
[](https://www.npmjs.com/package/vite-plugin-cesium)
 | 
			
		||||
[](https://www.npmjs.com/package/vite-plugin-earth)
 | 
			
		||||
[](https://www.npmjs.com/package/vite-plugin-earth)
 | 
			
		||||
 | 
			
		||||
Easily set up a [`Cesium`] project in [`Vite`].
 | 
			
		||||
Easily set up a [`Cesium`] & [`Mars3D`] project in [`Vite`].
 | 
			
		||||
 | 
			
		||||
[`cesium`]: https://github.com/CesiumGS/cesium
 | 
			
		||||
[`vite`]: https://github.com/vitejs/vite
 | 
			
		||||
[`cesium`]: https://cesium.com/platform/cesiumjs/
 | 
			
		||||
[`vite`]: https://vitejs.dev/
 | 
			
		||||
[`Mars3D`]: http://mars3d.cn/
 | 
			
		||||
 | 
			
		||||
**update:** if you just wanna a scaffolding by using this plugin, try a simply command `yarn create cesium`, click [create-cesium](https://www.npmjs.com/package/create-cesium) for more info.
 | 
			
		||||
## Cesium
 | 
			
		||||
 | 
			
		||||
Chinese tutorial: [中文教程](https://zhuanlan.zhihu.com/p/354856692)
 | 
			
		||||
 | 
			
		||||
## Install
 | 
			
		||||
### Install
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
npm i cesium vite-plugin-cesium vite -D
 | 
			
		||||
# yarn add cesium vite-plugin-cesium vite -D
 | 
			
		||||
npm i cesium vite-plugin-earth vite -D
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Usage
 | 
			
		||||
### Usage
 | 
			
		||||
 | 
			
		||||
add this plugin to `vite.config.js`
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import { defineConfig } from 'vite';
 | 
			
		||||
import cesium from 'vite-plugin-cesium';
 | 
			
		||||
import earth from 'vite-plugin-earth';
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  plugins: [cesium()]
 | 
			
		||||
  plugins: [earth()]
 | 
			
		||||
});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
add dev command to `package.json`
 | 
			
		||||
## Mars3D
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
"scripts": {
 | 
			
		||||
  "dev": "vite",
 | 
			
		||||
  "build": "vite build"
 | 
			
		||||
}
 | 
			
		||||
### Install
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
npm i mars3d mars3d-cesium vite-plugin-earth vite -D
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
run:
 | 
			
		||||
### Usage
 | 
			
		||||
 | 
			
		||||
`yarn dev`
 | 
			
		||||
 | 
			
		||||
## Options
 | 
			
		||||
 | 
			
		||||
**rebuildCesium**
 | 
			
		||||
 | 
			
		||||
- **Type :** `boolean`
 | 
			
		||||
- **Default :** `false`
 | 
			
		||||
 | 
			
		||||
Default copy min cesium file to dist. if `true` will rebuild cesium from source.
 | 
			
		||||
add this plugin to `vite.config.js`
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import { defineConfig } from 'vite';
 | 
			
		||||
import cesium from 'vite-plugin-cesium';
 | 
			
		||||
import earth from 'vite-plugin-earth';
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  plugins: [earth({ useMars3D: true })]
 | 
			
		||||
});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Options
 | 
			
		||||
 | 
			
		||||
**useCDN**
 | 
			
		||||
 | 
			
		||||
- **Type :** `object`
 | 
			
		||||
- **Default :** `{ mars3d: '3.5.0', mars3dCesium: '1.103.1', cesium: '1.103.0', turf: '6.5.0' }`
 | 
			
		||||
 | 
			
		||||
打包时使用 cdn 方式
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import { defineConfig } from 'vite';
 | 
			
		||||
import earth from 'vite-plugin-earth';
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  plugins: [
 | 
			
		||||
    cesium({
 | 
			
		||||
      rebuildCesium: true
 | 
			
		||||
    earth({
 | 
			
		||||
      useCDN: {}
 | 
			
		||||
    })
 | 
			
		||||
  ]
 | 
			
		||||
});
 | 
			
		||||
@ -67,70 +72,31 @@ export default defineConfig({
 | 
			
		||||
 | 
			
		||||
## Demo
 | 
			
		||||
 | 
			
		||||
`src/index.js`
 | 
			
		||||
### Cesium
 | 
			
		||||
 | 
			
		||||
```html
 | 
			
		||||
<div id="cesiumContainer"></div>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import { Viewer } from 'cesium';
 | 
			
		||||
import './css/main.css';
 | 
			
		||||
 | 
			
		||||
const viewer = new Viewer('cesiumContainer');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
> or if you like global Cesium object you can write as
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import * as Cesium from 'cesium';
 | 
			
		||||
const viewer = new Cesium.Viewer('cesiumContainer');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
`index.html`
 | 
			
		||||
### Mars3D
 | 
			
		||||
 | 
			
		||||
```html
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
  <head>
 | 
			
		||||
    <meta charset="UTF-8" />
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 | 
			
		||||
    <title>cesium-vite</title>
 | 
			
		||||
    <script type="module" src="/src/index.js"></script>
 | 
			
		||||
  </head>
 | 
			
		||||
 | 
			
		||||
  <body>
 | 
			
		||||
    <div id="cesiumContainer"></div>
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
<div id="mars3dContainer"></div>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
`src/css/main.css`
 | 
			
		||||
```js
 | 
			
		||||
import * as mars3d from 'mars3d';
 | 
			
		||||
import 'mars3d/dist/mars3d.css';
 | 
			
		||||
 | 
			
		||||
```css
 | 
			
		||||
html,
 | 
			
		||||
body,
 | 
			
		||||
#cesiumContainer {
 | 
			
		||||
  width: 100%;
 | 
			
		||||
  height: 100%;
 | 
			
		||||
  margin: 0;
 | 
			
		||||
  padding: 0;
 | 
			
		||||
  overflow: hidden;
 | 
			
		||||
}
 | 
			
		||||
const map = new mars3d.Map('mars3dContainer', {});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Add `dev` and `build` commands to `package.json`
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
"scripts": {
 | 
			
		||||
    "dev": "vite",
 | 
			
		||||
    "build": "vite build"
 | 
			
		||||
},
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Run `yarn dev`
 | 
			
		||||
 | 
			
		||||
For full demo project please check [./demo](https://github.com/nshen/vite-plugin-cesium/tree/main/demo) folder.
 | 
			
		||||
 | 
			
		||||
##
 | 
			
		||||
 | 
			
		||||
## License
 | 
			
		||||
 | 
			
		||||
MIT
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,8 @@ import earth from 'vite-plugin-earth';
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  plugins: [
 | 
			
		||||
    earth({
 | 
			
		||||
      useMars3D: true
 | 
			
		||||
      useMars3D: true,
 | 
			
		||||
      useCDN: { mars3d: '3.5.1' }
 | 
			
		||||
    })
 | 
			
		||||
  ]
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -2,5 +2,5 @@ import { defineConfig } from 'vite';
 | 
			
		||||
import earth from 'vite-plugin-earth';
 | 
			
		||||
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  plugins: [earth()]
 | 
			
		||||
  plugins: [earth({ useCDN: {} })]
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "vite-plugin-earth",
 | 
			
		||||
  "version": "2.0.0",
 | 
			
		||||
  "description": "cesium & mars3d library plugin for Vite",
 | 
			
		||||
  "version": "2.0.4",
 | 
			
		||||
  "description": "Cesium & Mars3D library plugin for Vite",
 | 
			
		||||
  "main": "./dist/index.js",
 | 
			
		||||
  "module": "./dist/index.mjs",
 | 
			
		||||
  "types": "./dist/index.d.ts",
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										79
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										79
									
								
								src/index.ts
									
									
									
									
									
								
							@ -13,6 +13,12 @@ interface VitePluginEarthOptions {
 | 
			
		||||
  cesiumBuildRootPath?: string;
 | 
			
		||||
  cesiumBuildPath?: string;
 | 
			
		||||
  useMars3D?: boolean;
 | 
			
		||||
  useCDN?: {
 | 
			
		||||
    mars3d?: string;
 | 
			
		||||
    mars3dCesium?: string;
 | 
			
		||||
    cesium?: string;
 | 
			
		||||
    turf?: string;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default function vitePluginEarth(options: VitePluginEarthOptions = {}): Plugin {
 | 
			
		||||
@ -21,9 +27,17 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
    rebuildCesium = false,
 | 
			
		||||
    devMinifyCesium = false,
 | 
			
		||||
    cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`,
 | 
			
		||||
    cesiumBuildPath = `node_modules/${CESIUM_NAME}/Build/Cesium/`
 | 
			
		||||
    cesiumBuildPath = `node_modules/${CESIUM_NAME}/Build/Cesium/`,
 | 
			
		||||
    useMars3D = false,
 | 
			
		||||
    useCDN = null
 | 
			
		||||
  } = options;
 | 
			
		||||
 | 
			
		||||
  // 默认使用的版本号
 | 
			
		||||
  let cdnVersion = Object.assign(
 | 
			
		||||
    { mars3d: '3.5.0', mars3dCesium: '1.103.1', cesium: '1.103.0', turf: '6.5.0' },
 | 
			
		||||
    useCDN
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
 | 
			
		||||
  let MARS3D_BASE_URL = `mars3d/`;
 | 
			
		||||
  let outDir = 'dist';
 | 
			
		||||
@ -69,7 +83,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
          let external = ['cesium'];
 | 
			
		||||
          let plugins = [externalGlobals({ cesium: 'Cesium' })];
 | 
			
		||||
 | 
			
		||||
          if (options.useMars3D) {
 | 
			
		||||
          if (useMars3D) {
 | 
			
		||||
            external.push('mars3d');
 | 
			
		||||
            plugins.push(externalGlobals({ mars3d: 'mars3d' }));
 | 
			
		||||
          }
 | 
			
		||||
@ -86,7 +100,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    configureServer({ middlewares }) {
 | 
			
		||||
      if (options.useMars3D) {
 | 
			
		||||
      if (useMars3D) {
 | 
			
		||||
        const cesiumPath = path.join(cesiumBuildRootPath, 'Cesium');
 | 
			
		||||
        middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
 | 
			
		||||
 | 
			
		||||
@ -99,7 +113,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    async closeBundle() {
 | 
			
		||||
      if (isBuild) {
 | 
			
		||||
      if (isBuild && !useCDN) {
 | 
			
		||||
        try {
 | 
			
		||||
          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`));
 | 
			
		||||
@ -109,7 +123,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
            await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`));
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          if (options.useMars3D) {
 | 
			
		||||
          if (useMars3D) {
 | 
			
		||||
            await fs.copy(path.join(`node_modules/mars3d/`, 'dist'), path.join(outDir, 'mars3d'));
 | 
			
		||||
          }
 | 
			
		||||
        } catch (err) {
 | 
			
		||||
@ -119,15 +133,60 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    transformIndexHtml() {
 | 
			
		||||
      const tags: HtmlTagDescriptor[] = [
 | 
			
		||||
      const tags: HtmlTagDescriptor[] = [];
 | 
			
		||||
      if (useCDN) {
 | 
			
		||||
        let cesiumVersion = useMars3D ? cdnVersion.mars3dCesium : cdnVersion.cesium;
 | 
			
		||||
        tags.push(
 | 
			
		||||
          {
 | 
			
		||||
            tag: 'link',
 | 
			
		||||
            attrs: {
 | 
			
		||||
              rel: 'stylesheet',
 | 
			
		||||
              href: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            tag: 'script',
 | 
			
		||||
            children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium'`
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            tag: 'script',
 | 
			
		||||
            attrs: {
 | 
			
		||||
              src: `https://unpkg.com/${CESIUM_NAME}@${cesiumVersion}/Build/Cesium/Cesium.js`
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        if (useMars3D) {
 | 
			
		||||
          tags.push(
 | 
			
		||||
            {
 | 
			
		||||
              tag: 'link',
 | 
			
		||||
              attrs: {
 | 
			
		||||
                rel: 'stylesheet',
 | 
			
		||||
                href: `https://unpkg.com/mars3d@${cdnVersion.mars3d}/dist/mars3d.css`
 | 
			
		||||
              }
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              tag: 'script',
 | 
			
		||||
              attrs: {
 | 
			
		||||
                src: `https://unpkg.com/mars3d@${cdnVersion.mars3d}/dist/mars3d.js`
 | 
			
		||||
              }
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              tag: 'script',
 | 
			
		||||
              attrs: {
 | 
			
		||||
                src: `https://unpkg.com/@turf/turf@${cdnVersion.turf}/turf.min.js`
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
      } else {
 | 
			
		||||
        tags.push({
 | 
			
		||||
          tag: 'link',
 | 
			
		||||
          attrs: {
 | 
			
		||||
            rel: 'stylesheet',
 | 
			
		||||
            href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css'))
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      ];
 | 
			
		||||
        });
 | 
			
		||||
        if (isBuild && !rebuildCesium) {
 | 
			
		||||
          tags.push({
 | 
			
		||||
            tag: 'script',
 | 
			
		||||
@ -137,7 +196,7 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
      if (options.useMars3D) {
 | 
			
		||||
        if (useMars3D) {
 | 
			
		||||
          tags.push(
 | 
			
		||||
            {
 | 
			
		||||
              tag: 'link',
 | 
			
		||||
@ -154,6 +213,8 @@ export default function vitePluginEarth(options: VitePluginEarthOptions = {}): P
 | 
			
		||||
            }
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return tags;
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user