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