hydrogen-web/scripts/build-plugins/rollup-plugin-build-themes.js

282 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2022-03-25 11:35:27 +05:30
/*
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.
*/
const path = require('path').posix;
2022-03-25 11:35:27 +05:30
async function readCSSSource(location) {
const fs = require("fs").promises;
const path = require("path");
2022-04-10 14:49:19 +05:30
const resolvedLocation = path.resolve(__dirname, "../../", `${location}/theme.css`);
2022-03-25 11:35:27 +05:30
const data = await fs.readFile(resolvedLocation);
return data;
}
2022-04-10 14:49:19 +05:30
function getRootSectionWithVariables(variables) {
return `:root{\n${Object.entries(variables).reduce((acc, [key, value]) => acc + `--${key}: ${value};\n`, "")} }\n\n`;
}
function appendVariablesToCSS(variables, cssSource) {
return cssSource + getRootSectionWithVariables(variables);
}
2022-05-11 12:40:32 +05:30
function addThemesToConfig(bundle, manifestLocations, defaultThemes) {
2022-04-25 14:29:31 +05:30
for (const [fileName, info] of Object.entries(bundle)) {
2022-05-12 16:02:03 +05:30
if (fileName === "config.json") {
2022-04-25 14:29:31 +05:30
const source = new TextDecoder().decode(info.source);
const config = JSON.parse(source);
2022-05-11 12:40:32 +05:30
config["themeManifests"] = manifestLocations;
2022-04-25 15:56:45 +05:30
config["defaultTheme"] = defaultThemes;
info.source = new TextEncoder().encode(JSON.stringify(config, undefined, 2));
2022-04-25 14:29:31 +05:30
}
}
}
2022-04-01 14:27:24 +05:30
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 11:35:27 +05:30
module.exports = function buildThemes(options) {
2022-04-25 16:01:02 +05:30
let manifest, variants, defaultDark, defaultLight, defaultThemes = {};
2022-04-10 14:49:19 +05:30
let isDevelopment = false;
const virtualModuleId = '@theme/'
const resolvedVirtualModuleId = '\0' + virtualModuleId;
2022-03-25 11:35:27 +05:30
return {
name: "build-themes",
enforce: "pre",
2022-04-10 14:49:19 +05:30
configResolved(config) {
if (config.command === "serve") {
isDevelopment = true;
}
},
2022-03-25 11:35:27 +05:30
async buildStart() {
if (isDevelopment) { return; }
2022-04-13 12:40:49 +05:30
const { themeConfig } = options;
for (const [name, location] of Object.entries(themeConfig.themes)) {
2022-03-25 11:35:27 +05:30
manifest = require(`${location}/manifest.json`);
variants = manifest.values.variants;
2022-03-28 18:02:53 +05:30
for (const [variant, details] of Object.entries(variants)) {
2022-04-13 12:40:49 +05:30
const fileName = `theme-${name}-${variant}.css`;
if (name === themeConfig.default && details.default) {
// This is the default theme, stash the file name for later
2022-03-28 18:02:53 +05:30
if (details.dark) {
defaultDark = fileName;
2022-04-25 15:56:45 +05:30
defaultThemes["dark"] = `${name}-${variant}`;
2022-03-28 18:02:53 +05:30
}
else {
defaultLight = fileName;
2022-04-25 15:56:45 +05:30
defaultThemes["light"] = `${name}-${variant}`;
2022-03-28 18:02:53 +05:30
}
}
2022-03-25 11:35:27 +05:30
// emit the css as built theme bundle
this.emitFile({
type: "chunk",
id: `${location}/theme.css?variant=${variant}${details.dark? "&dark=true": ""}`,
2022-03-28 18:02:53 +05:30
fileName,
2022-03-25 11:35:27 +05:30
});
}
2022-03-29 11:46:06 +05:30
// emit the css as runtime theme bundle
this.emitFile({
type: "chunk",
2022-04-01 14:27:24 +05:30
id: `${location}/theme.css?type=runtime`,
2022-04-13 12:40:49 +05:30
fileName: `theme-${name}-runtime.css`,
2022-03-29 11:46:06 +05:30
});
2022-03-25 11:35:27 +05:30
}
},
2022-04-10 14:49:19 +05:30
resolveId(id) {
if (id.startsWith(virtualModuleId)) {
return '\0' + id;
2022-03-25 11:35:27 +05:30
}
},
2022-03-28 18:02:53 +05:30
2022-04-10 14:49:19 +05:30
async load(id) {
if (isDevelopment) {
2022-04-13 13:39:20 +05:30
/**
* To load the theme during dev, we need to take a different approach because emitFile is not supported in dev.
* We solve this by resolving virtual file "@theme/name/variant" into the necessary css import.
* This virtual file import is removed when hydrogen is built (see transform hook).
*/
2022-04-10 14:49:19 +05:30
if (id.startsWith(resolvedVirtualModuleId)) {
let [theme, variant, file] = id.substr(resolvedVirtualModuleId.length).split("/");
if (theme === "default") {
2022-04-13 12:56:14 +05:30
theme = options.themeConfig.default;
2022-04-10 14:49:19 +05:30
}
2022-04-13 12:56:14 +05:30
const location = options.themeConfig.themes[theme];
const manifest = require(`${location}/manifest.json`);
const variants = manifest.values.variants;
2022-04-10 14:49:19 +05:30
if (!variant || variant === "default") {
2022-04-13 12:56:14 +05:30
// choose the first default variant for now
// this will need to support light/dark variants as well
variant = Object.keys(variants).find(variantName => variants[variantName].default);
2022-04-10 14:49:19 +05:30
}
if (!file) {
file = "index.js";
}
switch (file) {
case "index.js": {
2022-04-13 12:56:14 +05:30
const isDark = variants[variant].dark;
return `import "${path.resolve(`${location}/theme.css`)}${isDark? "?dark=true": ""}";` +
2022-04-10 14:49:19 +05:30
`import "@theme/${theme}/${variant}/variables.css"`;
}
case "variables.css": {
2022-04-13 12:56:14 +05:30
const variables = variants[variant].variables;
2022-04-10 14:49:19 +05:30
const css = getRootSectionWithVariables(variables);
return css;
}
}
2022-03-28 18:02:53 +05:30
}
2022-04-10 14:49:19 +05:30
}
else {
const result = id.match(/(.+)\/theme.css\?variant=([^&]+)/);
2022-04-10 14:49:19 +05:30
if (result) {
const [, location, variant] = result;
const cssSource = await readCSSSource(location);
const config = variants[variant];
2022-04-13 12:56:14 +05:30
return appendVariablesToCSS(config.variables, cssSource);
2022-03-28 18:02:53 +05:30
}
2022-04-10 14:49:19 +05:30
return null;
2022-03-28 18:02:53 +05:30
}
2022-04-10 14:52:26 +05:30
},
transform(code, id) {
if (isDevelopment) {
return;
}
2022-04-13 13:39:20 +05:30
/**
* Removes develop-only script tag; this cannot be done in transformIndexHtml hook because
* by the time that hook runs, the import is added to the bundled js file which would
* result in a runtime error.
*/
const devScriptTag =
/<script type="module"> import "@theme\/.+"; <\/script>/;
if (id.endsWith("index.html")) {
const htmlWithoutDevScript = code.replace(devScriptTag, "");
2022-04-13 13:39:20 +05:30
return htmlWithoutDevScript;
}
},
2022-04-10 14:52:26 +05:30
transformIndexHtml(_, ctx) {
if (isDevelopment) {
// Don't add default stylesheets to index.html on dev
return;
}
let darkThemeLocation, lightThemeLocation;
for (const [, bundle] of Object.entries(ctx.bundle)) {
if (bundle.name === defaultDark) {
darkThemeLocation = bundle.fileName;
2022-04-10 14:49:19 +05:30
}
2022-04-10 14:52:26 +05:30
if (bundle.name === defaultLight) {
lightThemeLocation = bundle.fileName;
2022-04-10 14:49:19 +05:30
}
2022-04-10 14:52:26 +05:30
}
return [
{
tag: "link",
attrs: {
rel: "stylesheet",
type: "text/css",
media: "(prefers-color-scheme: dark)",
href: `./${darkThemeLocation}`,
class: "theme",
2022-04-10 14:52:26 +05:30
}
},
{
tag: "link",
attrs: {
rel: "stylesheet",
type: "text/css",
media: "(prefers-color-scheme: light)",
href: `./${lightThemeLocation}`,
class: "theme",
2022-04-10 14:52:26 +05:30
}
},
];
2022-04-13 12:56:14 +05:30
},
2022-04-10 14:49:19 +05:30
2022-04-10 14:52:26 +05:30
generateBundle(_, bundle) {
2022-06-03 12:08:10 +05:30
// 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
// types of AssetInfo and ChunkInfo can be found at https://rollupjs.org/guide/en/#generatebundle
2022-04-10 14:52:26 +05:30
const { assetMap, chunkMap, runtimeThemeChunk } = parseBundle(bundle);
2022-05-11 12:40:32 +05:30
const manifestLocations = [];
2022-04-10 14:52:26 +05:30
for (const [location, chunkArray] of chunkMap) {
const manifest = require(`${location}/manifest.json`);
const compiledVariables = options.compiledVariables.get(location);
const derivedVariables = compiledVariables["derived-variables"];
const icon = compiledVariables["icon"];
2022-05-12 14:31:28 +05:30
const builtAssets = {};
for (const chunk of chunkArray) {
const [, name, variant] = chunk.fileName.match(/theme-(.+)-(.+)\.css/);
2022-05-12 14:31:28 +05:30
builtAssets[`${name}-${variant}`] = assetMap.get(chunk.fileName).fileName;
}
2022-04-10 14:52:26 +05:30
manifest.source = {
2022-05-12 14:31:28 +05:30
"built-assets": builtAssets,
2022-04-10 14:52:26 +05:30
"runtime-asset": assetMap.get(runtimeThemeChunk.fileName).fileName,
"derived-variables": derivedVariables,
"icon": icon
};
const name = `theme-${manifest.name}.json`;
2022-05-11 12:40:32 +05:30
manifestLocations.push(`assets/${name}`);
2022-04-10 14:52:26 +05:30
this.emitFile({
type: "asset",
name,
source: JSON.stringify(manifest),
});
}
2022-05-11 12:40:32 +05:30
addThemesToConfig(bundle, manifestLocations, defaultThemes);
2022-04-10 14:52:26 +05:30
},
2022-03-25 11:35:27 +05:30
}
}