Compare commits

..

1 Commits
main ... mars3d

Author SHA1 Message Date
小冰cc ccf0c9f6e3 对应vite-plugin-mars3d@3.0.0 2023-07-13 14:30:20 +08:00
1 changed files with 182 additions and 279 deletions

View File

@ -1,24 +1,36 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path';
import externalGlobals from 'rollup-plugin-external-globals'; import externalGlobals from 'rollup-plugin-external-globals';
import serveStatic from 'serve-static'; import serveStatic from 'serve-static';
import path from 'path';
import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite'; import { HtmlTagDescriptor, normalizePath, Plugin, UserConfig } from 'vite';
interface BaseOptions { /**
rebuildCesium?: boolean; * mars3dPlugin插件构造参数
devMinifyCesium?: boolean;
cesiumBuildRootPath?: string;
cesiumBuildPath?: string;
}
interface VitePluginCesiumOptions extends BaseOptions {
/**
* [build编译时] 使CDN引入资源,object定义各库的cdn的版本号
*/ */
useCDN?: boolean | { cesium?: string }; interface mars3dPluginOptions {
} /**
* mars3d包名 "mars3d"
*/
mars3dPackageName?: string;
/**
* mars3d库运行时的目录名称 mars3dPackageName
*/
mars3dRunPath?: string;
interface VitePluginMars3dOptions extends BaseOptions { /**
* cesium包名 "mars3d-cesium"
*/
cesiumPackageName?: string;
/**
* cesium库运行时的目录名称 cesiumPackageName
*/
cesiumRunPath?: string;
/**
* [build编译时]使mars3d
*/
useStatic?: boolean;
/** /**
* [build编译时] 使CDN引入资源,object定义各库的cdn的版本号 * [build编译时] 使CDN引入资源,object定义各库的cdn的版本号
*/ */
@ -26,33 +38,41 @@ interface VitePluginMars3dOptions extends BaseOptions {
| boolean | boolean
| { | {
mars3d?: string; mars3d?: string;
mars3dCesium?: string; cesium?: string;
turf?: string; turf?: string;
}; };
} }
/** /**
* mars3d * mars3d库在vite技术栈下的处理插件
* @param options *
* @returns * @export
* @param {mars3dPluginOptions} [options={}]
* @return {*} {Plugin}
*/ */
export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin { export function mars3dPlugin(options: mars3dPluginOptions = {}): Plugin {
let CESIUM_NAME = 'mars3d-cesium'; let {
const { mars3dPackageName = 'mars3d',
rebuildCesium = false, mars3dRunPath,
devMinifyCesium = false, cesiumPackageName = 'mars3d-cesium',
cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`, cesiumRunPath,
cesiumBuildPath = `${cesiumBuildRootPath}/Cesium/`,
useCDN = null useStatic = false,
useCDN = false
} = options; } = options;
// 默认使用最新版本,可指定版本 if (!mars3dRunPath) {
let cdnVersion = Object.assign({ mars3d: null, mars3dCesium: null, turf: null }, useCDN); mars3dRunPath = `/${mars3dPackageName}/`;
}
if (!cesiumRunPath) {
cesiumRunPath = `/${cesiumPackageName}/`;
}
let CESIUM_BASE_URL = `${CESIUM_NAME}/`; const cesiumNpmPath = `node_modules/${cesiumPackageName}/Build/Cesium/`;
let MARS3D_BASE_URL = `mars3d/`; const mars3dNpmPath = `node_modules/${mars3dPackageName}/dist/`;
let outDir = 'dist';
let base: string = '/'; let outDir: string, base: string, CESIUM_BASE_URL: string, MARS3D_BASE_URL: string;
let isBuild: boolean = false; let isBuild = false;
return { return {
name: 'vite-plugin-mars3d', name: 'vite-plugin-mars3d',
@ -61,300 +81,183 @@ export function mars3dPlugin(options: VitePluginMars3dOptions = {}): Plugin {
isBuild = command === 'build'; isBuild = command === 'build';
if (c.base) { if (c.base) {
base = c.base; base = c.base;
if (base === '') base = './'; if (base === '') {
base = './';
} }
} else {
base = '/';
}
if (c.build?.outDir) { if (c.build?.outDir) {
outDir = c.build.outDir; outDir = c.build.outDir;
} else {
outDir = 'dist';
} }
CESIUM_BASE_URL = path.posix.join(base, CESIUM_BASE_URL);
MARS3D_BASE_URL = path.posix.join(base, MARS3D_BASE_URL); CESIUM_BASE_URL = path.posix.join(base, cesiumRunPath || `/${cesiumPackageName}/`);
const userConfig: UserConfig = {}; MARS3D_BASE_URL = path.posix.join(base, mars3dRunPath || `/${mars3dPackageName}/`);
if (!isBuild) {
// -----------dev----------- const userConfig: UserConfig = {
userConfig.define = { build: {
assetsInlineLimit: 0,
chunkSizeWarningLimit: 4000
},
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) MARS3D_BASE_URL: JSON.stringify(MARS3D_BASE_URL)
};
} else {
// -----------build------------
if (rebuildCesium) {
// build 1) rebuild cesium library
userConfig.build = {
assetsInlineLimit: 0,
chunkSizeWarningLimit: 5000,
rollupOptions: {
output: {
intro: `window.CESIUM_BASE_URL = "${CESIUM_BASE_URL}";`
}
} }
}; };
} else {
// build 2) copy Cesium.js later if (isBuild) {
const external = [CESIUM_NAME, 'mars3d']; const external = [cesiumPackageName];
const globalsLibs = { const globals: any = {};
mars3d: 'mars3d', globals[cesiumPackageName] = 'Cesium';
'mars3d-cesium': 'Cesium'
}; if (useStatic) {
external.push(mars3dPackageName);
globals[mars3dPackageName] = 'mars3d';
}
userConfig.build = { userConfig.build = {
rollupOptions: { rollupOptions: {
external: external, external: external,
plugins: [externalGlobals(globalsLibs)] plugins: [externalGlobals(globals)]
} }
}; };
} } else {
userConfig.optimizeDeps = {
include: [mars3dPackageName],
exclude: [cesiumPackageName]
};
} }
return userConfig; return userConfig;
}, },
configureServer({ middlewares }) { configResolved(resolvedConfig) {
const cesiumPath = path.join(cesiumBuildRootPath, 'Cesium'); outDir = path.join(resolvedConfig.root, resolvedConfig.build.outDir);
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath)); },
const mars3dPath = path.join(`node_modules/mars3d`, 'dist'); configureServer({ middlewares }) {
middlewares.use(path.posix.join('/', MARS3D_BASE_URL), serveStatic(mars3dPath)); middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumNpmPath));
if (useStatic) {
middlewares.use(path.posix.join('/', MARS3D_BASE_URL), serveStatic(mars3dNpmPath));
}
}, },
async closeBundle() { async closeBundle() {
if (isBuild && !useCDN) { if (isBuild && !useCDN) {
try { try {
await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(outDir, `${CESIUM_NAME}/Assets`)); await fs.copy(path.join(cesiumNpmPath, 'Assets'), path.join(outDir, `${cesiumRunPath}/Assets`));
await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(outDir, `${CESIUM_NAME}/ThirdParty`)); await fs.copy(path.join(cesiumNpmPath, 'ThirdParty'), path.join(outDir, `${cesiumRunPath}/ThirdParty`));
await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(outDir, `${CESIUM_NAME}/Workers`)); await fs.copy(path.join(cesiumNpmPath, 'Workers'), path.join(outDir, `${cesiumRunPath}/Workers`));
await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(outDir, `${CESIUM_NAME}/Widgets`)); await fs.copy(path.join(cesiumNpmPath, 'Widgets'), path.join(outDir, `${cesiumRunPath}/Widgets`));
if (!rebuildCesium) { await fs.copy(path.join(cesiumNpmPath, 'Cesium.js'), path.join(outDir, `${cesiumRunPath}/Cesium.js`));
await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(outDir, `${CESIUM_NAME}/Cesium.js`));
}
await fs.copy(path.join(`node_modules/mars3d/`, 'dist'), path.join(outDir, 'mars3d')); if (useStatic) {
await fs.copy(path.join(mars3dNpmPath, 'img'), path.join(outDir, `${mars3dRunPath}/img`));
await fs.copy(path.join(mars3dNpmPath, 'mars3d.css'), path.join(outDir, `${mars3dRunPath}/mars3d.css`));
await fs.copy(path.join(mars3dNpmPath, 'mars3d.js'), path.join(outDir, `${mars3dRunPath}/mars3d.js`));
}
} catch (err) { } catch (err) {
console.error('copy failed', err); console.error(`拷贝 ${cesiumPackageName} 库失败`, err);
} }
} }
}, },
transformIndexHtml() { transformIndexHtml() {
const tags: HtmlTagDescriptor[] = []; const tags: HtmlTagDescriptor[] = [];
if (!isBuild) {
return tags;
}
if (useCDN) { if (useCDN) {
let cesiumVersion = cdnVersion.mars3dCesium ? `@${cdnVersion.mars3dCesium}` : ''; // 默认使用的版本号
let mars3dVersion = cdnVersion.mars3d ? `@${cdnVersion.mars3d}` : ''; let cdnVersion_cesium;
let turfVersion = cdnVersion.turf ? `@${cdnVersion.turf}` : ''; let cdnVersion_mars3d;
let cdnVersion_turf;
tags.push( if (useCDN instanceof Object) {
{ cdnVersion_cesium = useCDN.cesium ? `@${useCDN.cesium}` : '';
tag: 'link', cdnVersion_mars3d = useCDN.mars3d ? `@${useCDN.mars3d}` : '';
attrs: { cdnVersion_turf = useCDN.turf ? `@${useCDN.turf}` : '';
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`
}
}
);
tags.push(
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: `https://unpkg.com/mars3d${mars3dVersion}/dist/mars3d.css`
}
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/mars3d${mars3dVersion}/dist/mars3d.js`
}
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/@turf/turf${turfVersion}/turf.min.js`
}
}
);
} else { } else {
tags.push({ cdnVersion_cesium = '';
cdnVersion_mars3d = '';
cdnVersion_turf = '';
}
tags.push(
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: `https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium/Widgets/widgets.css`
}
},
{
tag: 'script',
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium'`
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/${cesiumPackageName}${cdnVersion_cesium}/Build/Cesium/Cesium.js`
}
}
);
if (useStatic) {
tags.push(
{
tag: 'script',
attrs: {
src: `https://unpkg.com/@turf/turf${cdnVersion_turf}/turf.min.js`
}
},
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: `https://unpkg.com/${mars3dPackageName}${cdnVersion_mars3d}/dist/mars3d.css`
}
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/${mars3dPackageName}${cdnVersion_mars3d}/dist/mars3d.js`
}
}
);
}
} else {
tags.push(
{
tag: 'link', tag: 'link',
attrs: { attrs: {
rel: 'stylesheet', rel: 'stylesheet',
href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css')) href: normalizePath(path.join(CESIUM_BASE_URL, 'Widgets/widgets.css'))
} }
}); },
if (isBuild && !rebuildCesium) { {
tags.push({
tag: 'script', tag: 'script',
children: `window['CESIUM_BASE_URL'] = '${CESIUM_BASE_URL}'` attrs: { src: normalizePath(path.join(CESIUM_BASE_URL, 'Cesium.js')) }
});
tags.push({
tag: 'script',
attrs: {
src: normalizePath(path.join(CESIUM_BASE_URL, `Cesium.js`))
}
});
} }
);
tags.push({ if (useStatic) {
tags.push(
{
tag: 'link', tag: 'link',
attrs: { attrs: {
rel: 'stylesheet', rel: 'stylesheet',
href: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.css')) href: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.css'))
} }
});
if (isBuild) {
tags.push({
tag: 'script',
attrs: {
src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js'))
}
});
}
}
return tags;
}
};
}
/**
* cesium
* @param options
* @returns
*/
export function cesiumPlugin(options: VitePluginCesiumOptions = {}): Plugin {
let CESIUM_NAME = 'cesium';
const {
rebuildCesium = false,
devMinifyCesium = false,
cesiumBuildRootPath = `node_modules/${CESIUM_NAME}/Build`,
cesiumBuildPath = `${cesiumBuildRootPath}/Cesium/`,
useCDN = null
} = options;
// 默认使用最新版本,可指定任意版本
let cdnVersion = Object.assign({ cesium: null }, useCDN);
let CESIUM_BASE_URL = `${CESIUM_NAME}/`;
let outDir = 'dist';
let base: string = '/';
let isBuild: boolean = false;
return {
name: 'vite-plugin-cesium',
config(c, { command }) {
isBuild = command === 'build';
if (c.base) {
base = c.base;
if (base === '') base = './';
}
if (c.build?.outDir) {
outDir = c.build.outDir;
}
CESIUM_BASE_URL = path.posix.join(base, CESIUM_BASE_URL);
const userConfig: UserConfig = {};
if (!isBuild) {
// -----------dev-----------
userConfig.define = {
CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
};
} else {
// -----------build------------
if (rebuildCesium) {
// build 1) rebuild cesium library
userConfig.build = {
assetsInlineLimit: 0,
chunkSizeWarningLimit: 5000,
rollupOptions: {
output: {
intro: `window.CESIUM_BASE_URL = "${CESIUM_BASE_URL}";`
}
}
};
} else {
// build 2) copy Cesium.js later
let external = [CESIUM_NAME];
let plugins = [externalGlobals({ cesium: 'Cesium' })];
userConfig.build = {
rollupOptions: {
external: external,
plugins: plugins
}
};
}
}
return userConfig;
},
configureServer({ middlewares }) {
const cesiumPath = path.join(cesiumBuildRootPath, devMinifyCesium ? 'Cesium' : 'CesiumUnminified');
middlewares.use(path.posix.join('/', CESIUM_BASE_URL), serveStatic(cesiumPath));
},
async closeBundle() {
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`));
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_NAME}/Cesium.js`));
}
} catch (err) {
console.error('copy failed', err);
}
}
},
transformIndexHtml() {
const tags: HtmlTagDescriptor[] = [];
if (useCDN) {
let cesiumVersion = cdnVersion.cesium ? `@${cdnVersion.cesium}` : '';
tags.push(
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Widgets/widgets.css`
}
}, },
{ {
tag: 'script', tag: 'script',
children: `window['CESIUM_BASE_URL'] = 'https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium'` attrs: { src: normalizePath(path.join(MARS3D_BASE_URL, 'mars3d.js')) }
},
{
tag: 'script',
attrs: {
src: `https://unpkg.com/${CESIUM_NAME}${cesiumVersion}/Build/Cesium/Cesium.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',
attrs: {
src: normalizePath(path.join(base, `${CESIUM_NAME}/Cesium.js`))
}
});
} }
} }