Files
mastodon/config/vite/plugin-mastodon-themes.ts
jcoffey-dev 319ed76dbd
Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Import Mastodon v4.7.3
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from
https://github.com/mastodon/mastodon, unmodified.
2026-10-05 22:03:13 -07:00

165 lines
4.5 KiB
TypeScript

/* This plugins handles Mastodon's theme system
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import * as yaml from 'js-yaml';
import type { Plugin } from 'vite';
type Themes = Record<string, string>;
export function MastodonThemes(): Plugin {
let projectRoot = '';
let jsRoot = '';
return {
name: 'mastodon-themes',
async config(userConfig) {
if (!userConfig.root || !userConfig.envDir) {
throw new Error('Unknown project directory');
}
projectRoot = userConfig.envDir;
jsRoot = userConfig.root;
let entrypoints: Record<string, string> = {};
const existingInputs = userConfig.build?.rolldownOptions?.input;
if (typeof existingInputs === 'string') {
entrypoints[path.basename(existingInputs)] = existingInputs;
} else if (Array.isArray(existingInputs)) {
for (const input of existingInputs) {
if (typeof input === 'string') {
entrypoints[path.basename(input)] = input;
}
}
} else if (typeof existingInputs === 'object') {
entrypoints = existingInputs;
}
// Get all files mentioned in the themes.yml file.
const themes = await loadThemesFromConfig(projectRoot);
for (const [themeName, themePath] of Object.entries(themes)) {
entrypoints[`themes/${themeName}`] = path.resolve(jsRoot, themePath);
}
return {
build: {
rolldownOptions: {
input: entrypoints,
},
},
};
},
async configureServer(server) {
const themes = await loadThemesFromConfig(projectRoot);
server.middlewares.use((req, res, next) => {
if (!req.url?.startsWith('/packs-dev/themes/')) {
next();
return;
}
// Rewrite the URL to the entrypoint if it matches a theme.
if (isThemeFile(req.url ?? '', themes)) {
const themeName = pathToThemeName(req.url ?? '');
req.url = `/packs-dev/${themes[themeName]}`;
}
next();
});
},
async handleHotUpdate({ modules, server }) {
if (modules.length === 0) {
return;
}
const themes = await loadThemesFromConfig(projectRoot);
const themePathToName = new Map(
Object.entries(themes).map(([themeName, themePath]) => [
path.resolve(jsRoot, themePath),
`/themes/${themeName}`,
]),
);
const themeNames = new Set<string>();
const addIfMatches = (file: string | null) => {
if (!file) {
return false;
}
const themeName = themePathToName.get(file);
if (themeName) {
themeNames.add(themeName);
return true;
}
return false;
};
for (const module of modules) {
if (!addIfMatches(module.file)) {
for (const importer of module.importers) {
addIfMatches(importer.file);
}
}
}
if (themeNames.size > 0) {
server.ws.send({
type: 'update',
updates: Array.from(themeNames).map((themeName) => ({
type: 'css-update',
path: themeName,
acceptedPath: themeName,
timestamp: Date.now(),
})),
});
}
},
};
}
async function loadThemesFromConfig(root: string) {
const themesFile = path.resolve(root, 'config/themes.yml');
const themes: Themes = {};
const themesString = await fs.readFile(themesFile, 'utf8');
const themesObject = yaml.load(themesString, {
filename: 'themes.yml',
schema: yaml.FAILSAFE_SCHEMA,
});
if (!themesObject || typeof themes !== 'object') {
throw new Error('Invalid themes.yml file');
}
for (const [themeName, themePath] of Object.entries(themesObject)) {
if (
typeof themePath !== 'string' ||
themePath.split('.').length !== 2 || // Ensure it has exactly one period
!themePath.endsWith('css')
) {
console.warn(`Invalid theme path "${themePath}" in themes.yml, skipping`);
continue;
}
themes[themeName] = themePath;
}
if (Object.keys(themes).length === 0) {
throw new Error('No valid themes found in themes.yml');
}
return themes;
}
function pathToThemeName(file: string) {
const basename = path.basename(file);
return basename.split(/[.?]/)[0] ?? '';
}
function isThemeFile(file: string, themes: Themes) {
if (!file.includes('/themes/')) {
return false;
}
const basename = pathToThemeName(file);
return basename in themes;
}