feat: vite-plugin-earth@1.1.0
This commit is contained in:
parent
328aca1489
commit
2ec92f0020
10
package.json
10
package.json
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "vite-plugin-earth",
|
"name": "vite-plugin-earth",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"description": "mars3d&cesium library plugin for Vite",
|
"description": "cesium & mars3d library plugin for Vite",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -12,12 +12,10 @@
|
||||||
"dev": "tsc -w"
|
"dev": "tsc -w"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"vite",
|
|
||||||
"vite-plugin",
|
|
||||||
"cesium",
|
"cesium",
|
||||||
"mars3d"
|
"mars3d"
|
||||||
],
|
],
|
||||||
"author": "xuwoool@qq.com",
|
"author": "xuxb",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -38,7 +36,7 @@
|
||||||
"@types/serve-static": "^1.13.9",
|
"@types/serve-static": "^1.13.9",
|
||||||
"prettier": "^2.2.1",
|
"prettier": "^2.2.1",
|
||||||
"typescript": "^4.2.2",
|
"typescript": "^4.2.2",
|
||||||
"vite": "^2.7.1"
|
"vite": "^2.8.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"mars3d-cesium": "^1.90.0",
|
"mars3d-cesium": "^1.90.0",
|
||||||
|
|
176
src/index.ts
176
src/index.ts
|
@ -1,158 +1,104 @@
|
||||||
import fs from 'fs-extra';
|
/**
|
||||||
|
* @Author: xuwoool@qq.com
|
||||||
|
* @Date: 2022-04-22 13:50:15
|
||||||
|
*/
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import externalGlobals from 'rollup-plugin-external-globals';
|
import fs from 'fs-extra';
|
||||||
import serveStatic from 'serve-static';
|
import serveStatic from 'serve-static';
|
||||||
import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite';
|
import { HtmlTagDescriptor, normalizePath, Plugin } from 'vite';
|
||||||
|
|
||||||
interface VitePluginCesiumOptions {
|
interface VitePluginCesiumOptions {
|
||||||
/**
|
libsPath?: String;
|
||||||
* rebuild cesium library, default: false
|
useUnminified?: Boolean;
|
||||||
*/
|
npmPkgName?: String;
|
||||||
rebuildCesium?: boolean;
|
useMars3d?: Boolean;
|
||||||
devMinifyCesium?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function vitePluginCesium(
|
function vitePluginCesium(
|
||||||
options: VitePluginCesiumOptions = {
|
options: VitePluginCesiumOptions = {
|
||||||
rebuildCesium: false,
|
libsPath: 'libs',
|
||||||
devMinifyCesium: false
|
useUnminified: false,
|
||||||
|
npmPkgName: 'mars3d-cesium',
|
||||||
|
useMars3d: false
|
||||||
}
|
}
|
||||||
): Plugin {
|
): Plugin {
|
||||||
const { rebuildCesium, devMinifyCesium } = options;
|
const cesiumBuildPath = `./node_modules/${options.npmPkgName}/Build`;
|
||||||
|
let base = '/';
|
||||||
const cesiumBuildRootPath = 'node_modules/mars3d-cesium/Build';
|
|
||||||
const cesiumBuildPath = 'node_modules/mars3d-cesium/Build/Cesium/';const mars3dBuildPath = 'node_modules/mars3d/dist/';
|
|
||||||
|
|
||||||
let CESIUM_BASE_URL = '/mars3d-cesium/';
|
|
||||||
let MARS3D_BASE_URL = '/mars3d/';
|
|
||||||
let outDir = 'dist';
|
let outDir = 'dist';
|
||||||
let base: string = '/';
|
let isBuild = false;
|
||||||
let isBuild: boolean = false;
|
let libsPath = options.libsPath || 'libs';
|
||||||
|
let useUnminified = options.useUnminified || false;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'vite-plugin-ice-mars3d',
|
name: 'vite-plugin-cesium',
|
||||||
|
config(config, { command }) {
|
||||||
config(c, { command }) {
|
|
||||||
isBuild = command === 'build';
|
isBuild = command === 'build';
|
||||||
if (c.base) {
|
base = config.base || '/';
|
||||||
base = c.base;
|
outDir = config.build?.outDir || 'dist';
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (!isBuild) {
|
|
||||||
userConfig.optimizeDeps = {
|
|
||||||
exclude: ['mars3d-cesium']
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (isBuild && !rebuildCesium) {
|
|
||||||
userConfig.build!.rollupOptions = {
|
|
||||||
external: ['cesium','mars3d'],
|
|
||||||
plugins: [externalGlobals({ cesium: 'Cesium' }),externalGlobals({ mars3d: 'mars3d' })]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return userConfig;
|
|
||||||
},
|
|
||||||
|
|
||||||
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 }) {
|
configureServer({ middlewares }) {
|
||||||
const cesiumPath = path.join(
|
middlewares.use(
|
||||||
cesiumBuildRootPath,
|
`/${libsPath}/Cesium`,
|
||||||
'Cesium'
|
serveStatic(
|
||||||
// devMinifyCesium ? 'Cesium' : 'CesiumUnminified'
|
normalizePath(
|
||||||
|
path.join(
|
||||||
|
cesiumBuildPath,
|
||||||
|
useUnminified ? 'CesiumUnminified' : 'Cesium'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
middlewares.use(CESIUM_BASE_URL, serveStatic(cesiumPath));
|
|
||||||
},
|
},
|
||||||
|
closeBundle() {
|
||||||
async closeBundle() {
|
|
||||||
if (isBuild) {
|
if (isBuild) {
|
||||||
try {
|
try {
|
||||||
await fs.copy(
|
fs.copySync(
|
||||||
path.join(cesiumBuildPath, 'Assets'),
|
path.join(cesiumBuildPath, 'Cesium', 'Assets'),
|
||||||
path.join(outDir, 'Cesium/Assets')
|
path.join(outDir, String(libsPath), 'Cesium', 'Assets')
|
||||||
);
|
);
|
||||||
await fs.copy(
|
fs.copySync(
|
||||||
path.join(cesiumBuildPath, 'ThirdParty'),
|
path.join(cesiumBuildPath, 'Cesium', 'ThirdParty'),
|
||||||
path.join(outDir, 'Cesium/ThirdParty')
|
path.join(outDir, String(libsPath), 'Cesium', 'ThirdParty')
|
||||||
);
|
);
|
||||||
await fs.copy(
|
fs.copySync(
|
||||||
path.join(cesiumBuildPath, 'Workers'),
|
path.join(cesiumBuildPath, 'Cesium', 'Widgets'),
|
||||||
path.join(outDir, 'Cesium/Workers')
|
path.join(outDir, String(libsPath), 'Cesium', 'Widgets')
|
||||||
);
|
);
|
||||||
await fs.copy(
|
fs.copySync(
|
||||||
path.join(cesiumBuildPath, 'Widgets'),
|
path.join(cesiumBuildPath, 'Cesium', 'Workers'),
|
||||||
path.join(outDir, 'Cesium/Widgets')
|
path.join(outDir, String(libsPath), 'Cesium', 'Workers')
|
||||||
);
|
);
|
||||||
if (!rebuildCesium) {
|
fs.copySync(
|
||||||
await fs.copy(
|
path.join(cesiumBuildPath, 'Cesium', 'Cesium.js'),
|
||||||
path.join(cesiumBuildPath, 'Cesium.js'),
|
path.join(outDir, String(libsPath), 'Cesium', 'Cesium.js')
|
||||||
path.join(outDir, 'Cesium/Cesium.js')
|
|
||||||
);
|
);
|
||||||
}
|
} catch (e) {}
|
||||||
await fs.copy(
|
|
||||||
path.join(mars3dBuildPath, ''),
|
|
||||||
path.join(outDir, 'mars3d')
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('copy failed', err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
transformIndexHtml() {
|
transformIndexHtml() {
|
||||||
const tags: HtmlTagDescriptor[] = [];
|
let tags: HtmlTagDescriptor[] = [];
|
||||||
if (isBuild && !rebuildCesium) {
|
|
||||||
tags.push({
|
tags.push({
|
||||||
tag: 'script',
|
tag: 'script',
|
||||||
attrs: { src: normalizePath(path.join(base, 'Cesium/Cesium.js')) }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (isBuild) {
|
|
||||||
tags.push( {
|
|
||||||
tag: 'link',
|
|
||||||
attrs: {
|
attrs: {
|
||||||
rel: 'stylesheet',
|
src: normalizePath(
|
||||||
href: normalizePath(
|
path.join(base, String(libsPath), 'Cesium', 'Cesium.js')
|
||||||
path.join('/Cesium/', 'Widgets/widgets.css')
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
injectTo: 'head'
|
||||||
|
});
|
||||||
|
|
||||||
|
tags.push({
|
||||||
tag: 'link',
|
tag: 'link',
|
||||||
attrs: {
|
attrs: {
|
||||||
rel: 'stylesheet',
|
rel: 'stylesheet',
|
||||||
href: normalizePath(
|
href: normalizePath(
|
||||||
path.join(MARS3D_BASE_URL, 'mars3d.css')
|
path.join(base, String(libsPath), 'Cesium', 'Widgets/widgets.css')
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
injectTo: 'head'
|
||||||
});
|
});
|
||||||
tags.push({
|
|
||||||
tag: 'script',
|
|
||||||
attrs: { src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js')) }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue