runtime theme chunks should also be stored in map

There will be more than one runtime theme file when multiple theme
collections exist.
This commit is contained in:
RMidhunSuresh 2022-06-20 13:46:14 +05:30
parent e3372f0f2b
commit 93165cb947

View file

@ -46,7 +46,7 @@ function addThemesToConfig(bundle, manifestLocations, defaultThemes) {
function parseBundle(bundle) { function parseBundle(bundle) {
const chunkMap = new Map(); const chunkMap = new Map();
const assetMap = new Map(); const assetMap = new Map();
let runtimeThemeChunk; let runtimeThemeChunkMap = new Map();
for (const [fileName, info] of Object.entries(bundle)) { for (const [fileName, info] of Object.entries(bundle)) {
if (!fileName.endsWith(".css")) { if (!fileName.endsWith(".css")) {
continue; continue;
@ -60,18 +60,18 @@ function parseBundle(bundle) {
assetMap.set(info.name, info); assetMap.set(info.name, info);
continue; continue;
} }
const location = info.facadeModuleId?.match(/(.+)\/.+\.css/)?.[1];
if (!location) {
throw new Error("Cannot find location of css chunk!");
}
if (info.facadeModuleId?.includes("type=runtime")) { if (info.facadeModuleId?.includes("type=runtime")) {
/** /**
* We have a separate field in manifest.source just for the runtime theme, * We have a separate field in manifest.source just for the runtime theme,
* so store this separately. * so store this separately.
*/ */
runtimeThemeChunk = info; runtimeThemeChunkMap.set(location, info);
continue; continue;
} }
const location = info.facadeModuleId?.match(/(.+)\/.+\.css/)?.[1];
if (!location) {
throw new Error("Cannot find location of css chunk!");
}
const array = chunkMap.get(location); const array = chunkMap.get(location);
if (!array) { if (!array) {
chunkMap.set(location, [info]); chunkMap.set(location, [info]);
@ -80,7 +80,7 @@ function parseBundle(bundle) {
array.push(info); array.push(info);
} }
} }
return { chunkMap, assetMap, runtimeThemeChunk }; return { chunkMap, assetMap, runtimeThemeChunkMap };
} }
module.exports = function buildThemes(options) { module.exports = function buildThemes(options) {
@ -249,7 +249,7 @@ module.exports = function buildThemes(options) {
// assetMap: Mapping from asset-name (eg: element-dark.css) to AssetInfo // assetMap: Mapping from asset-name (eg: element-dark.css) to AssetInfo
// chunkMap: Mapping from theme-location (eg: hydrogen-web/src/.../css/themes/element) to a list of ChunkInfo // chunkMap: Mapping from theme-location (eg: hydrogen-web/src/.../css/themes/element) to a list of ChunkInfo
// types of AssetInfo and ChunkInfo can be found at https://rollupjs.org/guide/en/#generatebundle // types of AssetInfo and ChunkInfo can be found at https://rollupjs.org/guide/en/#generatebundle
const { assetMap, chunkMap, runtimeThemeChunk } = parseBundle(bundle); const { assetMap, chunkMap, runtimeThemeChunkMap } = parseBundle(bundle);
const manifestLocations = []; const manifestLocations = [];
for (const [location, chunkArray] of chunkMap) { for (const [location, chunkArray] of chunkMap) {
const manifest = require(`${location}/manifest.json`); const manifest = require(`${location}/manifest.json`);
@ -263,6 +263,7 @@ module.exports = function buildThemes(options) {
themeKey = name; themeKey = name;
builtAssets[`${name}-${variant}`] = assetMap.get(chunk.fileName).fileName; builtAssets[`${name}-${variant}`] = assetMap.get(chunk.fileName).fileName;
} }
const runtimeThemeChunk = runtimeThemeChunkMap.get(location);
manifest.source = { manifest.source = {
"built-assets": builtAssets, "built-assets": builtAssets,
"runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName, "runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName,