|
| 1 | +import { join } from "path"; |
| 2 | +import { finished } from "stream/promises"; |
| 3 | + |
| 4 | +import gulp from "gulp"; |
| 5 | +import gulpReplace from "gulp-replace"; |
| 6 | +import { buildSandcastleApp } from "./scripts/buildSandcastle.js"; |
| 7 | +import { mkdirp } from "mkdirp"; |
| 8 | +import { bundleWorkers, defaultESBuildOptions } from "./scripts/build.js"; |
| 9 | +import { build as esbuild } from "esbuild"; |
| 10 | + |
| 11 | +const isProduction = process.env.PROD === "true"; |
| 12 | + |
| 13 | +// Print an esbuild warning |
| 14 | +function printBuildWarning({ location, text }) { |
| 15 | + const { column, file, line, lineText, suggestion } = location; |
| 16 | + |
| 17 | + let message = `\n |
| 18 | + > ${file}:${line}:${column}: warning: ${text} |
| 19 | + ${lineText} |
| 20 | + `; |
| 21 | + |
| 22 | + if (suggestion && suggestion !== "") { |
| 23 | + message += `\n${suggestion}`; |
| 24 | + } |
| 25 | + |
| 26 | + console.log(message); |
| 27 | +} |
| 28 | + |
| 29 | +// Ignore `eval` warnings in third-party code we don't have control over |
| 30 | +function handleBuildWarnings(result) { |
| 31 | + for (const warning of result.warnings) { |
| 32 | + if ( |
| 33 | + !warning.location.file.includes("protobufjs.js") && |
| 34 | + !warning.location.file.includes("Build/Cesium") |
| 35 | + ) { |
| 36 | + printBuildWarning(warning); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function buildLegacySandcastle() { |
| 42 | + const streams = []; |
| 43 | + let appStream = gulp.src( |
| 44 | + [ |
| 45 | + "Apps/Sandcastle/**", |
| 46 | + "!Apps/Sandcastle/load-cesium-es6.js", |
| 47 | + "!Apps/Sandcastle/images/**", |
| 48 | + "!Apps/Sandcastle/gallery/**.jpg", |
| 49 | + ], |
| 50 | + { |
| 51 | + encoding: false, |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + if (isProduction) { |
| 56 | + // Remove swap out ESM modules for the IIFE build |
| 57 | + appStream = appStream |
| 58 | + .pipe( |
| 59 | + gulpReplace( |
| 60 | + ' <script type="module" src="../load-cesium-es6.js"></script>', |
| 61 | + ' <script src="../CesiumUnminified/Cesium.js"></script>\n' + |
| 62 | + ' <script>window.CESIUM_BASE_URL = "../CesiumUnminified/";</script>', |
| 63 | + ), |
| 64 | + ) |
| 65 | + .pipe( |
| 66 | + gulpReplace( |
| 67 | + ' <script type="module" src="load-cesium-es6.js"></script>', |
| 68 | + ' <script src="CesiumUnminified/Cesium.js"></script>\n' + |
| 69 | + ' <script>window.CESIUM_BASE_URL = "CesiumUnminified/";</script>', |
| 70 | + ), |
| 71 | + ) |
| 72 | + // Fix relative paths for new location |
| 73 | + .pipe(gulpReplace("../../../Build", "..")) |
| 74 | + .pipe(gulpReplace("../../../Source", "../CesiumUnminified")) |
| 75 | + .pipe(gulpReplace("../../Source", ".")) |
| 76 | + .pipe(gulpReplace("../../../ThirdParty", "./ThirdParty")) |
| 77 | + .pipe(gulpReplace("../../ThirdParty", "./ThirdParty")) |
| 78 | + .pipe(gulpReplace("../ThirdParty", "./ThirdParty")) |
| 79 | + .pipe(gulpReplace("../Apps/Sandcastle", ".")) |
| 80 | + .pipe(gulpReplace("../../SampleData", "../SampleData")) |
| 81 | + .pipe( |
| 82 | + gulpReplace("../../Build/Documentation", "/learn/cesiumjs/ref-doc/"), |
| 83 | + ) |
| 84 | + .pipe(gulp.dest("Build/Sandcastle")); |
| 85 | + } else { |
| 86 | + // Remove swap out ESM modules for the IIFE build |
| 87 | + appStream = appStream |
| 88 | + .pipe( |
| 89 | + gulpReplace( |
| 90 | + ' <script type="module" src="../load-cesium-es6.js"></script>', |
| 91 | + ' <script src="../../../Build/CesiumUnminified/Cesium.js"></script>\n' + |
| 92 | + ' <script>window.CESIUM_BASE_URL = "../../../Build/CesiumUnminified/";</script>', |
| 93 | + ), |
| 94 | + ) |
| 95 | + .pipe( |
| 96 | + gulpReplace( |
| 97 | + ' <script type="module" src="load-cesium-es6.js"></script>', |
| 98 | + ' <script src="../../CesiumUnminified/Cesium.js"></script>\n' + |
| 99 | + ' <script>window.CESIUM_BASE_URL = "../../CesiumUnminified/";</script>', |
| 100 | + ), |
| 101 | + ) |
| 102 | + // Fix relative paths for new location |
| 103 | + .pipe(gulpReplace("../../../Build", "../../..")) |
| 104 | + .pipe(gulpReplace("../../Source", "../../../Source")) |
| 105 | + .pipe(gulpReplace("../../ThirdParty", "../../../ThirdParty")) |
| 106 | + .pipe(gulpReplace("../../SampleData", "../../../../Apps/SampleData")) |
| 107 | + .pipe(gulpReplace("Build/Documentation", "Documentation")) |
| 108 | + .pipe(gulp.dest("Build/Apps/Sandcastle")); |
| 109 | + } |
| 110 | + streams.push(appStream); |
| 111 | + |
| 112 | + let imageStream = gulp.src( |
| 113 | + ["Apps/Sandcastle/gallery/**.jpg", "Apps/Sandcastle/images/**"], |
| 114 | + { |
| 115 | + base: "Apps/Sandcastle", |
| 116 | + encoding: false, |
| 117 | + }, |
| 118 | + ); |
| 119 | + if (isProduction) { |
| 120 | + imageStream = imageStream.pipe(gulp.dest("Build/Sandcastle")); |
| 121 | + } else { |
| 122 | + imageStream = imageStream.pipe(gulp.dest("Build/Apps/Sandcastle")); |
| 123 | + } |
| 124 | + streams.push(imageStream); |
| 125 | + |
| 126 | + if (isProduction) { |
| 127 | + const fileStream = gulp |
| 128 | + .src(["ThirdParty/**"], { encoding: false }) |
| 129 | + .pipe(gulp.dest("Build/Sandcastle/ThirdParty")); |
| 130 | + streams.push(fileStream); |
| 131 | + |
| 132 | + const dataStream = gulp |
| 133 | + .src(["Apps/SampleData/**"], { encoding: false }) |
| 134 | + .pipe(gulp.dest("Build/Sandcastle/SampleData")); |
| 135 | + streams.push(dataStream); |
| 136 | + } |
| 137 | + |
| 138 | + let standaloneStream = gulp |
| 139 | + .src(["Apps/Sandcastle/standalone.html"]) |
| 140 | + .pipe(gulpReplace("../../../", ".")) |
| 141 | + .pipe( |
| 142 | + gulpReplace( |
| 143 | + ' <script type="module" src="load-cesium-es6.js"></script>', |
| 144 | + ' <script src="../CesiumUnminified/Cesium.js"></script>\n' + |
| 145 | + ' <script>window.CESIUM_BASE_URL = "../CesiumUnminified/";</script>', |
| 146 | + ), |
| 147 | + ) |
| 148 | + .pipe(gulpReplace("../../Build", ".")); |
| 149 | + if (isProduction) { |
| 150 | + standaloneStream = standaloneStream.pipe(gulp.dest("Build/Sandcastle")); |
| 151 | + } else { |
| 152 | + standaloneStream = standaloneStream.pipe( |
| 153 | + gulp.dest("Build/Apps/Sandcastle"), |
| 154 | + ); |
| 155 | + } |
| 156 | + streams.push(standaloneStream); |
| 157 | + |
| 158 | + return Promise.all(streams.map((s) => finished(s))); |
| 159 | +} |
| 160 | + |
| 161 | +async function buildCesiumViewer() { |
| 162 | + const cesiumViewerOutputDirectory = isProduction |
| 163 | + ? "Build/CesiumViewer" |
| 164 | + : "Build/Apps/CesiumViewer"; |
| 165 | + mkdirp.sync(cesiumViewerOutputDirectory); |
| 166 | + |
| 167 | + const config = defaultESBuildOptions(); |
| 168 | + config.entryPoints = [ |
| 169 | + "Apps/CesiumViewer/CesiumViewer.js", |
| 170 | + "Apps/CesiumViewer/CesiumViewer.css", |
| 171 | + ]; |
| 172 | + config.bundle = true; // Tree-shaking is enabled automatically |
| 173 | + config.minify = true; |
| 174 | + config.loader = { |
| 175 | + ".gif": "text", |
| 176 | + ".png": "text", |
| 177 | + }; |
| 178 | + config.format = "iife"; |
| 179 | + // Configure Cesium base path to use built |
| 180 | + config.define = { CESIUM_BASE_URL: `"."` }; |
| 181 | + config.outdir = cesiumViewerOutputDirectory; |
| 182 | + config.outbase = "Apps/CesiumViewer"; |
| 183 | + config.logLevel = "error"; // print errors immediately, and collect warnings so we can filter out known ones |
| 184 | + const result = await esbuild(config); |
| 185 | + |
| 186 | + handleBuildWarnings(result); |
| 187 | + |
| 188 | + await esbuild({ |
| 189 | + entryPoints: ["packages/widgets/Source/InfoBox/InfoBoxDescription.css"], |
| 190 | + minify: true, |
| 191 | + bundle: true, |
| 192 | + loader: { |
| 193 | + ".gif": "text", |
| 194 | + ".png": "text", |
| 195 | + }, |
| 196 | + outdir: join(cesiumViewerOutputDirectory, "Widgets"), |
| 197 | + outbase: "packages/widgets/Source/", |
| 198 | + }); |
| 199 | + |
| 200 | + await bundleWorkers({ |
| 201 | + minify: true, |
| 202 | + removePragmas: true, |
| 203 | + path: cesiumViewerOutputDirectory, |
| 204 | + }); |
| 205 | + |
| 206 | + const stream = gulp |
| 207 | + .src( |
| 208 | + [ |
| 209 | + "Apps/CesiumViewer/**", |
| 210 | + "!Apps/CesiumViewer/Images", |
| 211 | + "!Apps/CesiumViewer/**/*.js", |
| 212 | + "!Apps/CesiumViewer/**/*.css", |
| 213 | + ], |
| 214 | + { |
| 215 | + encoding: false, |
| 216 | + }, |
| 217 | + ) |
| 218 | + .pipe( |
| 219 | + gulp.src( |
| 220 | + [ |
| 221 | + "Build/Cesium/Assets/**", |
| 222 | + "Build/Cesium/Workers/**", |
| 223 | + "Build/Cesium/ThirdParty/**", |
| 224 | + "Build/Cesium/Widgets/**", |
| 225 | + "!Build/Cesium/Widgets/**/*.css", |
| 226 | + ], |
| 227 | + { |
| 228 | + base: "Build/Cesium", |
| 229 | + nodir: true, |
| 230 | + encoding: false, |
| 231 | + }, |
| 232 | + ), |
| 233 | + ) |
| 234 | + .pipe(gulp.src(["web.config"])) |
| 235 | + .pipe(gulp.dest(cesiumViewerOutputDirectory)); |
| 236 | + |
| 237 | + await finished(stream); |
| 238 | + return stream; |
| 239 | +} |
| 240 | + |
| 241 | +export async function buildSandcastle() { |
| 242 | + return buildSandcastleApp({ |
| 243 | + outputToBuildDir: isProduction, |
| 244 | + includeDevelopment: !isProduction, |
| 245 | + }); |
| 246 | +} |
| 247 | + |
| 248 | +export const buildApps = gulp.parallel( |
| 249 | + buildCesiumViewer, |
| 250 | + buildLegacySandcastle, |
| 251 | + buildSandcastle, |
| 252 | +); |
0 commit comments