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