index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  22. mod
  23. ));
  24. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  25. // src/index.ts
  26. var src_exports = {};
  27. __export(src_exports, {
  28. default: () => vitePluginCesium
  29. });
  30. module.exports = __toCommonJS(src_exports);
  31. var import_fs_extra = __toESM(require("fs-extra"));
  32. var import_path = __toESM(require("path"));
  33. var import_rollup_plugin_external_globals = __toESM(require("rollup-plugin-external-globals"));
  34. var import_serve_static = __toESM(require("serve-static"));
  35. var import_vite = require("vite");
  36. function vitePluginCesium(options = {}) {
  37. const {
  38. rebuildCesium = false,
  39. devMinifyCesium = false,
  40. cesiumBuildRootPath = "node_modules/cesium/Build",
  41. cesiumBuildPath = "node_modules/cesium/Build/Cesium/"
  42. } = options;
  43. let CESIUM_BASE_URL = "cesium/";
  44. let outDir = "dist";
  45. let base = "/";
  46. let isBuild = false;
  47. return {
  48. name: "vite-plugin-cesium",
  49. config(c, { command }) {
  50. var _a;
  51. isBuild = command === "build";
  52. if (c.base) {
  53. base = c.base;
  54. if (base === "")
  55. base = "./";
  56. }
  57. if ((_a = c.build) == null ? void 0 : _a.outDir) {
  58. outDir = c.build.outDir;
  59. }
  60. CESIUM_BASE_URL = import_path.default.posix.join(base, CESIUM_BASE_URL);
  61. const userConfig = {};
  62. if (!isBuild) {
  63. userConfig.define = {
  64. CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL)
  65. };
  66. } else {
  67. if (rebuildCesium) {
  68. userConfig.build = {
  69. assetsInlineLimit: 0,
  70. chunkSizeWarningLimit: 5e3,
  71. rollupOptions: {
  72. output: {
  73. intro: `window.CESIUM_BASE_URL = "${CESIUM_BASE_URL}";`
  74. }
  75. }
  76. };
  77. } else {
  78. userConfig.build = {
  79. rollupOptions: {
  80. external: ["cesium"],
  81. plugins: [(0, import_rollup_plugin_external_globals.default)({ cesium: "Cesium" })]
  82. }
  83. };
  84. }
  85. }
  86. return userConfig;
  87. },
  88. configureServer({ middlewares }) {
  89. const cesiumPath = import_path.default.join(cesiumBuildRootPath, devMinifyCesium ? "Cesium" : "CesiumUnminified");
  90. middlewares.use(import_path.default.posix.join("/", CESIUM_BASE_URL), (0, import_serve_static.default)(cesiumPath));
  91. },
  92. async closeBundle() {
  93. if (isBuild) {
  94. try {
  95. await import_fs_extra.default.copy(import_path.default.join(cesiumBuildPath, "Assets"), import_path.default.join(outDir, "cesium/Assets"));
  96. await import_fs_extra.default.copy(import_path.default.join(cesiumBuildPath, "ThirdParty"), import_path.default.join(outDir, "cesium/ThirdParty"));
  97. await import_fs_extra.default.copy(import_path.default.join(cesiumBuildPath, "Workers"), import_path.default.join(outDir, "cesium/Workers"));
  98. await import_fs_extra.default.copy(import_path.default.join(cesiumBuildPath, "Widgets"), import_path.default.join(outDir, "cesium/Widgets"));
  99. if (!rebuildCesium) {
  100. await import_fs_extra.default.copy(import_path.default.join(cesiumBuildPath, "Cesium.js"), import_path.default.join(outDir, "cesium/Cesium.js"));
  101. }
  102. } catch (err) {
  103. console.error("copy failed", err);
  104. }
  105. }
  106. },
  107. transformIndexHtml() {
  108. const tags = [
  109. {
  110. tag: "link",
  111. attrs: {
  112. rel: "stylesheet",
  113. href: (0, import_vite.normalizePath)(import_path.default.join(CESIUM_BASE_URL, "Widgets/widgets.css"))
  114. }
  115. }
  116. ];
  117. if (isBuild && !rebuildCesium) {
  118. tags.push({
  119. tag: "script",
  120. attrs: {
  121. src: (0, import_vite.normalizePath)(import_path.default.join(CESIUM_BASE_URL, "Cesium.js"))
  122. }
  123. });
  124. }
  125. return tags;
  126. }
  127. };
  128. }
  129. // Annotate the CommonJS export names for ESM import in node:
  130. 0 && (module.exports = {});