feat: use serve-static middleware support dev
This commit is contained in:
		
							
								
								
									
										173
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										173
									
								
								src/index.ts
									
									
									
									
									
								
							@@ -1,70 +1,129 @@
 | 
			
		||||
import path from 'path';
 | 
			
		||||
import { Plugin, UserConfig, HtmlTagDescriptor } from 'vite';
 | 
			
		||||
import fs from 'fs-extra';
 | 
			
		||||
import path from "path";
 | 
			
		||||
import { Plugin, UserConfig, HtmlTagDescriptor } from "vite";
 | 
			
		||||
import fs from "fs-extra";
 | 
			
		||||
import externalGlobals from "rollup-plugin-external-globals";
 | 
			
		||||
import serveStatic from "serve-static";
 | 
			
		||||
 | 
			
		||||
type options = { rebuildCesium: boolean; };
 | 
			
		||||
interface VitePluginCesiumOptions {
 | 
			
		||||
  rebuildCesium?: boolean;
 | 
			
		||||
  minifyCesium?: boolean;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default function ({ rebuildCesium }: options = { rebuildCesium: false }): Plugin {
 | 
			
		||||
function vitePluginCesium(
 | 
			
		||||
  options: VitePluginCesiumOptions = {
 | 
			
		||||
    rebuildCesium: false,
 | 
			
		||||
    minifyCesium: false,
 | 
			
		||||
  }
 | 
			
		||||
): Plugin {
 | 
			
		||||
  const { rebuildCesium, minifyCesium } = options;
 | 
			
		||||
 | 
			
		||||
    const cesiumBuildPath = 'node_modules/cesium/Build/Cesium/';
 | 
			
		||||
    let publicPath = 'public';
 | 
			
		||||
  const cesiumBuildRootPath = "node_modules/cesium/Build";
 | 
			
		||||
  const cesiumBuildPath = "node_modules/cesium/Build/Cesium/";
 | 
			
		||||
  const CESIUM_BASE_URL = "/cesium/";
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
        name: 'vite-plugin-cesium',
 | 
			
		||||
  let publicPath = "public";
 | 
			
		||||
  let outDir = "dist";
 | 
			
		||||
  let base: string;
 | 
			
		||||
  let isBuild: boolean = false;
 | 
			
		||||
 | 
			
		||||
        config(config, { command }) {
 | 
			
		||||
            const userConfig: UserConfig = {
 | 
			
		||||
                build: {
 | 
			
		||||
                    assetsInlineLimit: 0,
 | 
			
		||||
                    chunkSizeWarningLimit: 4000
 | 
			
		||||
                },
 | 
			
		||||
                define: {
 | 
			
		||||
                    CESIUM_BASE_URL: '/cesium/'
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
            if (command === 'build' && !rebuildCesium) {
 | 
			
		||||
                userConfig.build!.rollupOptions = {
 | 
			
		||||
                    external: ['cesium'],
 | 
			
		||||
                    plugins: [
 | 
			
		||||
                        externalGlobals({ cesium: "Cesium" })
 | 
			
		||||
                    ]
 | 
			
		||||
                };
 | 
			
		||||
            }
 | 
			
		||||
            return userConfig;
 | 
			
		||||
  return {
 | 
			
		||||
    name: "vite-plugin-cesium",
 | 
			
		||||
 | 
			
		||||
    config(_, { command }) {
 | 
			
		||||
      const userConfig: UserConfig = {
 | 
			
		||||
        build: {
 | 
			
		||||
          assetsInlineLimit: 0,
 | 
			
		||||
          chunkSizeWarningLimit: 4000,
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
        configResolved(resolvedConfig) {
 | 
			
		||||
            publicPath = resolvedConfig.publicDir;
 | 
			
		||||
        define: {
 | 
			
		||||
          CESIUM_BASE_URL,
 | 
			
		||||
        },
 | 
			
		||||
      };
 | 
			
		||||
      if (command === "build" && !rebuildCesium) {
 | 
			
		||||
        userConfig.build!.rollupOptions = {
 | 
			
		||||
          external: ["cesium"],
 | 
			
		||||
          plugins: [externalGlobals({ cesium: "Cesium" })],
 | 
			
		||||
        };
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
        async buildStart(options) {
 | 
			
		||||
            try {
 | 
			
		||||
                const exists = await fs.pathExists(path.join(publicPath, 'cesium'));
 | 
			
		||||
                if (!exists) {
 | 
			
		||||
                    await fs.copy(path.join(cesiumBuildPath, 'Assets'), path.join(publicPath, 'cesium/Assets'));
 | 
			
		||||
                    await fs.copy(path.join(cesiumBuildPath, 'ThirdParty'), path.join(publicPath, 'cesium/ThirdParty'));
 | 
			
		||||
                    await fs.copy(path.join(cesiumBuildPath, 'Workers'), path.join(publicPath, 'cesium/Workers'));
 | 
			
		||||
                    await fs.copy(path.join(cesiumBuildPath, 'Widgets'), path.join(publicPath, 'cesium/Widgets'));
 | 
			
		||||
      return userConfig;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                if (rebuildCesium) {
 | 
			
		||||
                    await fs.remove(path.join(publicPath, 'cesium/Cesium.js'));
 | 
			
		||||
                } else {
 | 
			
		||||
                    await fs.copy(path.join(cesiumBuildPath, 'Cesium.js'), path.join(publicPath, 'cesium/Cesium.js'));
 | 
			
		||||
                }
 | 
			
		||||
    configResolved(resolvedConfig) {
 | 
			
		||||
      publicPath = resolvedConfig.publicDir;
 | 
			
		||||
      outDir = resolvedConfig.build.outDir;
 | 
			
		||||
      base = resolvedConfig.base;
 | 
			
		||||
      isBuild =
 | 
			
		||||
        resolvedConfig.isProduction || resolvedConfig.command === "build";
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
            } catch (err) {
 | 
			
		||||
                console.error('copy failed', err);
 | 
			
		||||
            }
 | 
			
		||||
    configureServer({ middlewares }) {
 | 
			
		||||
      const cesiumPath = path.join(
 | 
			
		||||
        cesiumBuildRootPath,
 | 
			
		||||
        minifyCesium ? "Cesium" : "CesiumUnminified"
 | 
			
		||||
      );
 | 
			
		||||
      middlewares.use(CESIUM_BASE_URL, serveStatic(cesiumPath));
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    async buildStart() {
 | 
			
		||||
      if (isBuild) {
 | 
			
		||||
        const exists = await fs.pathExists(path.join(publicPath, "cesium"));
 | 
			
		||||
 | 
			
		||||
        if (!exists) {
 | 
			
		||||
          try {
 | 
			
		||||
            await fs.copy(
 | 
			
		||||
              path.join(cesiumBuildPath, "Assets"),
 | 
			
		||||
              path.join(publicPath, "cesium/Assets")
 | 
			
		||||
            );
 | 
			
		||||
            await fs.copy(
 | 
			
		||||
              path.join(cesiumBuildPath, "ThirdParty"),
 | 
			
		||||
              path.join(publicPath, "cesium/ThirdParty")
 | 
			
		||||
            );
 | 
			
		||||
            await fs.copy(
 | 
			
		||||
              path.join(cesiumBuildPath, "Workers"),
 | 
			
		||||
              path.join(publicPath, "cesium/Workers")
 | 
			
		||||
            );
 | 
			
		||||
            await fs.copy(
 | 
			
		||||
              path.join(cesiumBuildPath, "Widgets"),
 | 
			
		||||
              path.join(publicPath, "cesium/Widgets")
 | 
			
		||||
            );
 | 
			
		||||
          } catch (err) {
 | 
			
		||||
            console.error("copy failed", err);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (rebuildCesium) {
 | 
			
		||||
          await fs.remove(path.join(publicPath, "cesium/Cesium.js"));
 | 
			
		||||
        } else {
 | 
			
		||||
          await fs.copy(
 | 
			
		||||
            path.join(cesiumBuildPath, "Cesium.js"),
 | 
			
		||||
            path.join(publicPath, "cesium/Cesium.js")
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    async renderStart() {
 | 
			
		||||
      const existOutDir = await fs.pathExists(path.join(outDir, "cesium"));
 | 
			
		||||
      if (existOutDir) {
 | 
			
		||||
        await fs.remove(path.join(publicPath, "cesium"));
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    transformIndexHtml() {
 | 
			
		||||
      const tags: HtmlTagDescriptor[] = [
 | 
			
		||||
        {
 | 
			
		||||
          tag: "link",
 | 
			
		||||
          attrs: {
 | 
			
		||||
            rel: "stylesheet",
 | 
			
		||||
            href: base + "cesium/Widgets/widgets.css",
 | 
			
		||||
          },
 | 
			
		||||
        },
 | 
			
		||||
      ];
 | 
			
		||||
      if (!rebuildCesium) {
 | 
			
		||||
        tags.push({ tag: "script", attrs: { src: base + "cesium/Cesium.js" } });
 | 
			
		||||
      }
 | 
			
		||||
      return tags;
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
        transformIndexHtml() {
 | 
			
		||||
            const tags: HtmlTagDescriptor[] = [{ tag: 'link', attrs: { rel: 'stylesheet', href: 'cesium/Widgets/widgets.css' } }];
 | 
			
		||||
            if (!rebuildCesium) {
 | 
			
		||||
                tags.push({ tag: 'script', attrs: { src: 'cesium/Cesium.js' } });
 | 
			
		||||
            }
 | 
			
		||||
            return tags;
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
export default vitePluginCesium;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user