diff --git a/build/navaraAssets.js b/build/navaraAssets.js index 9e81cd2b0..08070c937 100644 --- a/build/navaraAssets.js +++ b/build/navaraAssets.js @@ -43,3 +43,23 @@ export function navaraWorkerAssets({ assetsDir } = {}) { }, }; } + +const NAVARA_MODULE = /\/src\/globe\/navara\//; + +/** + * Rollup chunkFileNames: the lazy chunk holding the Navara engine + * (src/globe/navara, with the Navara packages it pulls in) is named + * `navara-engine-[hash].js`; every other chunk keeps Vite's default name. + * Naming only (not manualChunks, which would pull modules shared with the + * Cesium entry into the Navara chunk), so the chunking is unchanged. + * @param {{ isEntry?: boolean, moduleIds?: string[] }} chunk + * @returns {string} + */ +export function navaraChunkFileNames(chunk) { + const navara = + !chunk.isEntry && + (chunk.moduleIds ?? []).some((id) => + NAVARA_MODULE.test(id.replaceAll('\\', '/')), + ); + return navara ? 'assets/navara-engine-[hash].js' : 'assets/[name]-[hash].js'; +} diff --git a/build/vite.js b/build/vite.js index 07536abf4..5929c979d 100644 --- a/build/vite.js +++ b/build/vite.js @@ -3,7 +3,7 @@ import cesium from 'vite-plugin-cesium'; import { DEFAULT_ALLOWED_HOSTS, hostCheckPlugin } from './allowedHosts.js'; import { embedFramingPlugin } from './embed-framing.js'; import { panelBuildPlugin } from './panel.js'; -import { navaraWorkerAssets } from './navaraAssets.js'; +import { navaraChunkFileNames, navaraWorkerAssets } from './navaraAssets.js'; /** * Content-Security-Policy for every document the dev/preview server serves. @@ -130,7 +130,11 @@ export function createBrowserViteConfig({ chunkSizeWarningLimit: 1500, // Build only: the wasm Navara's workers load by name (the Navara // engine is a separate chunk loaded on ?engine=navara). - rollupOptions: { plugins: [navaraWorkerAssets()] }, + // The lazy chunk with the Navara engine is named navara-engine. + rollupOptions: { + plugins: [navaraWorkerAssets()], + output: { chunkFileNames: navaraChunkFileNames }, + }, }, }; } diff --git a/src/tooling/navaraAssets.test.mjs b/src/tooling/navaraAssets.test.mjs index 977042bf3..878e0482a 100644 --- a/src/tooling/navaraAssets.test.mjs +++ b/src/tooling/navaraAssets.test.mjs @@ -54,3 +54,33 @@ test('the browser build runs the Navara asset copy as a Rollup plugin', () => { ); assert.equal(build.chunkSizeWarningLimit, 1500); }); + +test('the lazy chunk with the Navara engine is named navara-engine', async () => { + const { navaraChunkFileNames } = await import('../../build/navaraAssets.js'); + for (const ids of [ + ['/repo/src/globe/navara/index.js', '/repo/src/globe/navara/maps.js'], + [ + '/repo/node_modules/@navaramap/three/dist/index.js', + '/repo/src/globe/navara/index.js', + ], + ]) + assert.equal( + navaraChunkFileNames({ isEntry: false, moduleIds: ids }), + 'assets/navara-engine-[hash].js', + ); + for (const chunk of [ + { isEntry: false, moduleIds: ['/repo/src/app/navaraMode.js'] }, + { isEntry: false, moduleIds: ['/repo/src/globe/contract.js'] }, + // A package chunk split off by the bundler keeps its own name. + { + isEntry: false, + moduleIds: ['/repo/node_modules/@navaramap/three/dist/spark.js'], + }, + { isEntry: true, moduleIds: ['/repo/src/globe/navara/index.js'] }, + { isEntry: false }, + ]) + assert.equal(navaraChunkFileNames(chunk), 'assets/[name]-[hash].js'); + const { build } = createBrowserViteConfig({}); + assert.equal(build.rollupOptions.output.chunkFileNames, navaraChunkFileNames); + assert.equal(build.rollupOptions.output.manualChunks, undefined); +});