-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Sandcastle build config updates #12904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
f490f63
814cb73
f7c4d62
aabcb3d
f3f4075
4de8de1
dcc4ec1
97f606d
4aab565
2fcefac
20f3edc
d350020
63c8ccb
1bd41e9
d83fb76
26a011d
0f7a9c8
c708661
b586c14
c4e8eea
aaf4ae1
b695f5d
61d330a
f3ecf5b
00dce5a
dde0e07
fd85408
bee0278
3b6dd1e
da2dd8a
4530967
b74d62d
ba045e7
b478c3f
8b212bd
22df371
5cc27ef
ba5b5d7
9675ce5
c59e106
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ labels: | |
| - Development | ||
| - Entities | ||
| thumbnail: thumbnail.jpg | ||
| development: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ description: Control fog parameters. | |
| labels: | ||
| - Development | ||
| thumbnail: thumbnail.jpg | ||
| development: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { build, defineConfig } from "vite"; | ||
| import baseConfig from "../vite.config.js"; | ||
| import { fileURLToPath } from "url"; | ||
| import { viteStaticCopy } from "vite-plugin-static-copy"; | ||
| import { dirname, join } from "path"; | ||
| import { cesiumPathReplace, insertImportMap } from "../vite-plugins.js"; | ||
| import typescriptCompile from "./typescriptCompile.js"; | ||
|
|
||
| /** @import { UserConfig } from 'vite' */ | ||
|
|
||
| /** | ||
| * @typedef {Object} ImportObject | ||
| * @property {string} path The path to use for the import map. ie the path the app can expect to find this at | ||
| * @property {string} typesPath The path to use for intellisense types in monaco | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {Object<string, ImportObject>} ImportList | ||
ggetz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
|
|
||
| /** | ||
| * Check if the given key is in the imports list and throw an error if not | ||
| * @param {ImportList} imports | ||
| * @param {string} name | ||
| */ | ||
| function checkForImport(imports, name) { | ||
| if (!imports[name]) { | ||
| throw new Error(`Missing import for ${name}`); | ||
| } | ||
| } | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| /** | ||
| * Create the Vite configuration for building Sandcastle. | ||
| * Set where it should build to and the base path for vite and CesiumJS files. | ||
| * | ||
| * Most importantly specify the paths the app can find the library imports. | ||
| * | ||
| * If you are copying files to the built directory ensure the source files exist BEFORE attempting to build Sandcastle | ||
| * | ||
| * @param {object} options | ||
| * @param {string} options.outDir Path to build files into | ||
| * @param {string} options.viteBase Base path for files/routes | ||
jjspace marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param {string} options.cesiumBaseUrl Base path for CesiumJS. This should include the CesiumJS assets and workers etc. | ||
| * @param {string} [options.commitSha] Optional commit hash to display in the top right of the application | ||
| * @param {ImportList} options.imports Set of imports to add to the import map for the iframe and standalone html pages. These paths should match the URL where it can be accessed within the current environment. | ||
| * @param {{src: string, dest: string}[]} [options.copyExtraFiles] Extra paths passed to viteStaticCopy. Use this to consolidate files for a singular static deployment (ie during production). Source paths should be absolute, dest paths should be relative to the page root. It is up to you to ensure these files exist BEFORE building sandcastle. | ||
| */ | ||
| export function createSandcastleConfig({ | ||
| outDir, | ||
| viteBase, | ||
| cesiumBaseUrl, | ||
| commitSha, | ||
| imports, | ||
| copyExtraFiles = [], | ||
| }) { | ||
| /** @type {UserConfig} */ | ||
| const config = { ...baseConfig }; | ||
|
|
||
| config.base = viteBase; | ||
|
|
||
| config.build = { | ||
| ...config.build, | ||
| outDir: outDir, | ||
| }; | ||
|
|
||
| const copyPlugin = viteStaticCopy({ | ||
| targets: [ | ||
| { src: "templates/Sandcastle.(d.ts|js)", dest: "templates" }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the TS build output to the |
||
| ...copyExtraFiles, | ||
| ], | ||
| }); | ||
|
|
||
| checkForImport(imports, "cesium"); | ||
| checkForImport(imports, "@cesium/engine"); | ||
| checkForImport(imports, "@cesium/widgets"); | ||
| if (imports["Sandcastle"]) { | ||
| throw new Error( | ||
| "Don't specify the Sandcastle import this is taken care of internally", | ||
| ); | ||
| } | ||
|
|
||
| /** @type {Object<string, string>} */ | ||
| const importMap = { | ||
| Sandcastle: `${viteBase}/templates/Sandcastle.js`, | ||
jjspace marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| /** @type {Object<string, string>} */ | ||
| const typePaths = { | ||
| Sandcastle: "templates/Sandcastle.d.ts", | ||
| }; | ||
| for (const [key, value] of Object.entries(imports)) { | ||
| importMap[key] = value.path; | ||
| typePaths[key] = value.typesPath; | ||
| } | ||
|
|
||
| config.define = { | ||
| ...config.define, | ||
| __VITE_TYPE_IMPORT_PATHS__: JSON.stringify(typePaths), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we'er bending over backwards to get the type definition locations. I'm thinking this would be a lot easier if we build out the definitions relative to the bundled files—Then, we wouldn't need to configure the path to const cesiumModulePath = import.meta.resolve("cesium");
const tsdPath = new URL("./Cesium.d.ts", cesiumModulePath).href;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of this change but it would involve some larger changes with the rest of our build systems. Happy to take a look at it at some point but I think I'd prefer to leave this as is for this current PR. Maybe it's rather verbose in the config but at least it should be clear what it's doing. |
||
| __COMMIT_SHA__: JSON.stringify(commitSha ?? undefined), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason not to use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may not always want to use it. For example in Prod we don't want to show it but it's currently set in the github workflow. Doing it this way makes it customizable and explicit from wherever we call this function. Using the env variable directly creates more, almost, "side effect" logic that people have to be aware of |
||
| }; | ||
|
|
||
| const plugins = config.plugins ?? []; | ||
| config.plugins = [ | ||
| ...plugins, | ||
| copyPlugin, | ||
| cesiumPathReplace(cesiumBaseUrl), | ||
| insertImportMap(importMap, ["bucket.html", "standalone.html"]), | ||
| ]; | ||
|
|
||
| return defineConfig(config); | ||
| } | ||
|
|
||
| /** | ||
| * Build Sandcastle out to a specified location as static files. | ||
| * The config should be generated with the <code>createSandcastleConfig</code> function. | ||
| * | ||
| * The build will only set up the paths for "external" resources from the app. | ||
| * If you are copying files to the built directory ensure the source files exist BEFORE attempting to build Sandcastle | ||
| * | ||
| * @param {UserConfig} config | ||
| */ | ||
| export async function buildStatic(config) { | ||
| // We have to do the compile for the Sandcastle API outside of the vite build | ||
| // because we need to reference the js file and types directly from the app | ||
| // and we don't want them bundled with the rest of the code | ||
| const exitCode = typescriptCompile( | ||
| join(__dirname, "../templates/tsconfig.lib.json"), | ||
| ); | ||
|
|
||
| if (exitCode === 0) { | ||
| console.log(`Sandcastle typescript build complete`); | ||
| } else { | ||
| throw new Error("Sandcastle typescript build failed"); | ||
| } | ||
|
|
||
| await build({ | ||
| ...config, | ||
| root: join(__dirname, "../"), | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.