Skip to content

Commit b3ceae8

Browse files
committed
copy cesium assets
1 parent efaf024 commit b3ceae8

5 files changed

Lines changed: 42 additions & 99 deletions

File tree

buildprocess/vite-plugins/cesiumPlugin.ts

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,4 @@
1-
import { cpSync, createReadStream, existsSync, statSync } from "node:fs";
2-
import path from "node:path";
3-
4-
import type { Plugin, ResolvedConfig } from "vite";
5-
6-
const MIME_TYPES: Record<string, string> = {
7-
".js": "application/javascript",
8-
".mjs": "application/javascript",
9-
".json": "application/json",
10-
".wasm": "application/wasm",
11-
".png": "image/png",
12-
".jpg": "image/jpeg",
13-
".gif": "image/gif",
14-
".svg": "image/svg+xml",
15-
".xml": "application/xml",
16-
".glb": "model/gltf-binary",
17-
".bin": "application/octet-stream"
18-
};
19-
20-
/**
21-
* Serves Cesium Workers, Assets, and ThirdParty from the terriajs-cesium
22-
* package and terriajs wwwroot static assets during dev mode.
23-
*
24-
* At runtime, Cesium resolves assets relative to `cesiumBaseUrl` which is
25-
* set to `build/TerriaJS/build/Cesium/build/` by the app's index.js.
26-
*/
27-
export function cesiumPlugin(options: {
28-
terriaJSBasePath: string;
29-
cesiumDir: string;
30-
}): Plugin {
31-
const { terriaJSBasePath, cesiumDir } = options;
32-
const terriaJSWwwroot = path.join(terriaJSBasePath, "wwwroot");
33-
let outDir = "";
34-
35-
return {
36-
name: "cesium-assets",
37-
38-
configResolved(config: ResolvedConfig) {
39-
outDir = path.resolve(config.root, config.build.outDir);
40-
},
41-
42-
closeBundle() {
43-
// Copy terriajs wwwroot → outDir/TerriaJS/ (mirrors gulp copy-terriajs-assets)
44-
const destPath = path.join(outDir, "TerriaJS");
45-
cpSync(terriaJSWwwroot, destPath, { recursive: true });
46-
},
47-
48-
configureServer(server) {
49-
// Serve terriajs wwwroot at /build/TerriaJS/
50-
// This mirrors the gulp `copy-terriajs-assets` task
51-
server.middlewares.use((req, res, next) => {
52-
if (!req.url) return next();
53-
54-
const url = decodeURIComponent(req.url.split("?")[0]);
55-
56-
// Serve terriajs wwwroot assets at /build/TerriaJS/
57-
if (url.startsWith("/build/TerriaJS/")) {
58-
const assetPath = url.slice("/build/TerriaJS/".length);
59-
const filePath = path.join(terriaJSWwwroot, assetPath);
60-
return serveFile(filePath, res, next);
61-
}
62-
63-
// Serve WASM with correct MIME type
64-
if (url.endsWith(".wasm")) {
65-
const wasmPath = url.startsWith("/build/")
66-
? path.join(terriaJSWwwroot, url.slice(1))
67-
: null;
68-
if (wasmPath && existsSync(wasmPath)) {
69-
return serveFile(wasmPath, res, next);
70-
}
71-
}
72-
73-
next();
74-
});
75-
}
76-
};
77-
78-
function serveFile(
79-
filePath: string,
80-
res: import("http").ServerResponse,
81-
next: () => void
82-
) {
83-
if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
84-
return next();
85-
}
86-
const ext = path.extname(filePath);
87-
const mime = MIME_TYPES[ext] || "application/octet-stream";
88-
res.setHeader("Content-Type", mime);
89-
res.setHeader("Cache-Control", "no-cache");
90-
createReadStream(filePath).pipe(res);
91-
}
92-
}
1+
import type { Plugin } from "vite";
932

943
/**
954
* Strip Cesium debug pragmas in production builds.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import showGlobalDisclaimer from "./lib/Views/showGlobalDisclaimer";
1313
import plugins from "./plugins";
1414

1515
const terriaOptions = {
16-
baseUrl: "build/TerriaJS"
16+
baseUrl: "build/TerriaJS",
17+
cesiumBaseUrl: "build/cesiumAssets"
1718
};
1819

1920
// we check exact match for development to reduce chances that production flag isn't set on builds(?)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"urijs": "^1.18.12",
7575
"vite": "^8.0.0",
7676
"vite-css-modules": "^1.14.0",
77+
"vite-plugin-static-copy": "^3.3.0",
7778
"yargs": "^17.7.2"
7879
},
7980
"scripts": {

vite.config.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import path from "node:path";
33
import react from "@vitejs/plugin-react";
44
import { defineConfig } from "vite";
55
import { patchCssModules } from "vite-css-modules";
6+
import { viteStaticCopy } from "vite-plugin-static-copy";
67

78
import {
89
momentLocalePlugin,
910
xmlRawPlugin
1011
} from "./buildprocess/vite-plugins/assetPlugins";
11-
import {
12-
cesiumDebugStripPlugin,
13-
cesiumPlugin
14-
} from "./buildprocess/vite-plugins/cesiumPlugin";
12+
import { cesiumDebugStripPlugin } from "./buildprocess/vite-plugins/cesiumPlugin";
1513
import { scssCssModulesPlugin } from "./buildprocess/vite-plugins/scssCssModulesPlugin";
1614
import { svgSpritePlugin } from "./buildprocess/vite-plugins/svgSpritePlugin";
1715

@@ -108,7 +106,26 @@ export default defineConfig(({ mode }) => {
108106
]),
109107
xmlRawPlugin(),
110108
momentLocalePlugin(),
111-
cesiumPlugin({ terriaJSBasePath, cesiumDir }),
109+
viteStaticCopy({
110+
targets: [
111+
{
112+
src: path.join(terriaJSBasePath, "wwwroot") + "/*",
113+
dest: "TerriaJS"
114+
},
115+
{
116+
src: path.join(cesiumDir, "Build", "Workers"),
117+
dest: "cesiumAssets"
118+
},
119+
{
120+
src: path.join(cesiumDir, "Source", "Assets"),
121+
dest: "cesiumAssets"
122+
},
123+
{
124+
src: path.join(cesiumDir, "Build", "ThirdParty"),
125+
dest: "cesiumAssets"
126+
}
127+
]
128+
}),
112129
cesiumDebugStripPlugin(cesiumDir),
113130
react({
114131
babel: {

yarn.lock

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3556,7 +3556,7 @@ chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.2:
35563556
ansi-styles "^4.1.0"
35573557
supports-color "^7.1.0"
35583558

3559-
chokidar@^3.5.3:
3559+
chokidar@^3.5.3, chokidar@^3.6.0:
35603560
version "3.6.0"
35613561
resolved "http://localhost:4873/chokidar/-/chokidar-3.6.0.tgz#197c6cc669ef2a8dc5e7b4d97ee4e092c3eb0d5b"
35623562
integrity sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==
@@ -7255,6 +7255,11 @@ p-locate@^5.0.0:
72557255
dependencies:
72567256
p-limit "^3.0.2"
72577257

7258+
p-map@^7.0.4:
7259+
version "7.0.4"
7260+
resolved "https://registry.yarnpkg.com/p-map/-/p-map-7.0.4.tgz#b81814255f542e252d5729dca4d66e5ec14935b8"
7261+
integrity sha512-tkAQEw8ysMzmkhgw8k+1U/iPhWNhykKnSk4Rd5zLoPJCuJaGRPo6YposrZgaxHKzDHdDWWZvE/Sk7hsL2X/CpQ==
7262+
72587263
p-try@^2.0.0:
72597264
version "2.2.0"
72607265
resolved "http://localhost:4873/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
@@ -9633,6 +9638,16 @@ vite-css-modules@^1.14.0:
96339638
postcss-modules-values "^4.0.0"
96349639
postcss-selector-parser "^7.1.1"
96359640

9641+
vite-plugin-static-copy@^3.3.0:
9642+
version "3.3.0"
9643+
resolved "https://registry.yarnpkg.com/vite-plugin-static-copy/-/vite-plugin-static-copy-3.3.0.tgz#5a95b7b21f9a726b23cd5ca3d64cdcfa6d1b6bde"
9644+
integrity sha512-XiAtZcev7nppxNFgKoD55rfL+ukVp/RtrnTJONRwRuzv/B2FK2h2ZRCYjvxhwBV/Oarse83SiyXBSxMTfeEM0Q==
9645+
dependencies:
9646+
chokidar "^3.6.0"
9647+
p-map "^7.0.4"
9648+
picocolors "^1.1.1"
9649+
tinyglobby "^0.2.15"
9650+
96369651
vite@^8.0.0:
96379652
version "8.0.0"
96389653
resolved "https://registry.yarnpkg.com/vite/-/vite-8.0.0.tgz#d749f9bf5be196635982bc16ec0c6faf2b31f3a4"

0 commit comments

Comments
 (0)