2022-03-25 07:05:27 +01:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function readCSSSource(location) {
|
|
|
|
const fs = require("fs").promises;
|
|
|
|
const path = require("path");
|
|
|
|
const resolvedLocation = path.resolve(__dirname, "../../", `${location}/theme.css`);
|
|
|
|
const data = await fs.readFile(resolvedLocation);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function appendVariablesToCSS(variables, cssSource) {
|
|
|
|
return cssSource + `:root{\n${Object.entries(variables).reduce((acc, [key, value]) => acc + `--${key}: ${value};\n`, "")} }\n\n`;
|
|
|
|
}
|
|
|
|
|
2022-04-01 10:57:24 +02:00
|
|
|
function parseBundle(bundle) {
|
|
|
|
const chunkMap = new Map();
|
|
|
|
const assetMap = new Map();
|
|
|
|
let runtimeThemeChunk;
|
|
|
|
for (const [fileName, info] of Object.entries(bundle)) {
|
|
|
|
if (!fileName.endsWith(".css")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info.type === "asset") {
|
|
|
|
/**
|
|
|
|
* So this is the css assetInfo that contains the asset hashed file name.
|
|
|
|
* We'll store it in a separate map indexed via fileName (unhashed) to avoid
|
|
|
|
* searching through the bundle array later.
|
|
|
|
*/
|
|
|
|
assetMap.set(info.name, info);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info.facadeModuleId?.includes("type=runtime")) {
|
|
|
|
/**
|
|
|
|
* We have a separate field in manifest.source just for the runtime theme,
|
|
|
|
* so store this separately.
|
|
|
|
*/
|
|
|
|
runtimeThemeChunk = info;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const location = info.facadeModuleId?.match(/(.+)\/.+\.css/)?.[1];
|
|
|
|
if (!location) {
|
|
|
|
throw new Error("Cannot find location of css chunk!");
|
|
|
|
}
|
|
|
|
const array = chunkMap.get(location);
|
|
|
|
if (!array) {
|
|
|
|
chunkMap.set(location, [info]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
array.push(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { chunkMap, assetMap, runtimeThemeChunk };
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:05:27 +01:00
|
|
|
module.exports = function buildThemes(options) {
|
2022-03-28 14:32:53 +02:00
|
|
|
let manifest, variants, defaultDark, defaultLight;
|
2022-03-25 07:05:27 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: "build-themes",
|
|
|
|
enforce: "pre",
|
|
|
|
|
|
|
|
async buildStart() {
|
|
|
|
const { manifestLocations } = options;
|
|
|
|
for (const location of manifestLocations) {
|
|
|
|
manifest = require(`${location}/manifest.json`);
|
|
|
|
variants = manifest.values.variants;
|
2022-03-29 08:16:06 +02:00
|
|
|
const themeName = manifest.name;
|
2022-03-28 14:32:53 +02:00
|
|
|
for (const [variant, details] of Object.entries(variants)) {
|
|
|
|
const fileName = `theme-${themeName}-${variant}.css`;
|
|
|
|
if (details.default) {
|
2022-04-05 12:57:23 +02:00
|
|
|
// This is one of the default variants for this theme.
|
2022-03-28 14:32:53 +02:00
|
|
|
if (details.dark) {
|
|
|
|
defaultDark = fileName;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defaultLight = fileName;
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 07:05:27 +01:00
|
|
|
// emit the css as built theme bundle
|
|
|
|
this.emitFile({
|
|
|
|
type: "chunk",
|
|
|
|
id: `${location}/theme.css?variant=${variant}`,
|
2022-03-28 14:32:53 +02:00
|
|
|
fileName,
|
2022-03-25 07:05:27 +01:00
|
|
|
});
|
|
|
|
}
|
2022-03-29 08:16:06 +02:00
|
|
|
// emit the css as runtime theme bundle
|
|
|
|
this.emitFile({
|
|
|
|
type: "chunk",
|
2022-04-01 10:57:24 +02:00
|
|
|
id: `${location}/theme.css?type=runtime`,
|
2022-03-29 08:16:06 +02:00
|
|
|
fileName: `theme-${themeName}-runtime.css`,
|
|
|
|
});
|
2022-03-25 07:05:27 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async load(id) {
|
|
|
|
const result = id.match(/(.+)\/theme.css\?variant=(.+)/);
|
|
|
|
if (result) {
|
|
|
|
const [, location, variant] = result;
|
|
|
|
const cssSource = await readCSSSource(location);
|
|
|
|
const config = variants[variant];
|
|
|
|
return await appendVariablesToCSS(config.variables, cssSource);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2022-03-28 14:32:53 +02:00
|
|
|
|
|
|
|
transformIndexHtml(_, ctx) {
|
|
|
|
let darkThemeLocation, lightThemeLocation;
|
|
|
|
for (const [, bundle] of Object.entries(ctx.bundle)) {
|
|
|
|
if (bundle.name === defaultDark) {
|
|
|
|
darkThemeLocation = bundle.fileName;
|
|
|
|
}
|
|
|
|
if (bundle.name === defaultLight) {
|
|
|
|
lightThemeLocation = bundle.fileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: "link",
|
|
|
|
attrs: {
|
|
|
|
rel: "stylesheet",
|
|
|
|
type: "text/css",
|
|
|
|
media: "(prefers-color-scheme: dark)",
|
|
|
|
href: `./${darkThemeLocation}`,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "link",
|
|
|
|
attrs: {
|
|
|
|
rel: "stylesheet",
|
|
|
|
type: "text/css",
|
|
|
|
media: "(prefers-color-scheme: light)",
|
|
|
|
href: `./${lightThemeLocation}`,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
2022-04-01 10:57:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
generateBundle(_, bundle) {
|
|
|
|
const { assetMap, chunkMap, runtimeThemeChunk } = parseBundle(bundle);
|
|
|
|
for (const [location, chunkArray] of chunkMap) {
|
|
|
|
const manifest = require(`${location}/manifest.json`);
|
2022-04-03 13:00:35 +02:00
|
|
|
const compiledVariables = options.compiledVariables.get(location);
|
|
|
|
const derivedVariables = compiledVariables["derived-variables"];
|
|
|
|
const icon = compiledVariables["icon"];
|
2022-04-01 10:57:24 +02:00
|
|
|
manifest.source = {
|
|
|
|
"built-asset": chunkArray.map(chunk => assetMap.get(chunk.fileName).fileName),
|
|
|
|
"runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName,
|
2022-04-01 16:57:33 +02:00
|
|
|
"derived-variables": derivedVariables,
|
2022-04-03 13:00:35 +02:00
|
|
|
"icon": icon
|
2022-04-01 10:57:24 +02:00
|
|
|
};
|
|
|
|
const name = `theme-${manifest.name}.json`;
|
|
|
|
this.emitFile({
|
|
|
|
type: "asset",
|
|
|
|
name,
|
|
|
|
source: JSON.stringify(manifest),
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 14:32:53 +02:00
|
|
|
}
|
2022-03-25 07:05:27 +01:00
|
|
|
}
|
|
|
|
}
|