You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
8.1 KiB

Bringing Hometown up to date with mastodon/mastodon (#1371) This is a massive PR, so apologies for the poor performance viewing it! This merges in the content of Mastodon 4.3.0. *This PR isn't ready to deploy to production*, and I don't recommend trying it in a production environment. Since I started by merging 4.3.0, this will likely have reverted at least some of the content of the post-4.3.0 security patches in the 4.2.x series; those will be brought back as I merge in 4.4.0 and 4.5.0. I haven't yet tested this PR, so it may not actually run without additional tweaks. I'm putting it up now for discussion and so multiple people can test if we want. --- Here's a few notes on things I ran into and thoughts I had while working on it: * We may want to look at `server_banner.jsx,` where I ended up undoing the v3-style rewrite due to the new react component being used. I'm not sure what the current goal for it is. * I may not have 100% merged in all content from `status_action_bar` correctly. * There's a number of places where I updated the links for remote usernames, and they should be tested to make sure they work. They're all `<Permalink>` elements. I have in my notes that `status_content` might need double-checking. * I may also not have gotten the spoiler display right in `status_content`. * It looks like previous versions of Hometown had customizations to the `media_item` display, not all of which was preserved as-is due to surrounding rewrites. I'm not sure what the vision is and could use another set of eyes. * The character count rendering clashed with the newly-introduced handling of a character count variable upstream, and I'm not sure if the version I've got here 100% works or not. * The navigation bar was completely rewritten and no longer has the components Hometown was customizing; did I properly catch any replacements? Did we handle all links to accounts to make sure they go to the remote instance? * Polls: I reverted the single/multiple choice toggle because upstream has an actual proper UI for this that should be integrated in one of the versions we’re merging in. Please double-check that the current version looks fine to you. * The font icons will need double-checking. Hometown made some custom use of the font-awesome icons, while Mastodon 4.3.0 replaced font-awesome with something else. I may not have caught all places that font-awesome icons were being used. * We’ll want to make sure the post display is correct, including Hometown customizations, because the previous static post page has been replaced with the React one. --------- Co-authored-by: Claire <claire.github-309c@sitedethib.com> Co-authored-by: Matt Jankowski <matt@jankowski.online> Co-authored-by: Renaud Chaput <renchap@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: David Roetzel <david@roetzel.de> Co-authored-by: Emelia Smith <ThisIsMissEm@users.noreply.github.com> Co-authored-by: Jeong Arm <kjwonmail@gmail.com> Co-authored-by: Christian Schmidt <github@chsc.dk> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: GitHub Actions <noreply@github.com> Co-authored-by: Eugen Rochko <eugen@zeonfederated.com> Co-authored-by: Darius Kazemi <darius.kazemi@gmail.com> Co-authored-by: diondiondion <mail@diondiondion.com> Co-authored-by: Echo <ChaosExAnima@users.noreply.github.com> Co-authored-by: Shugo Maeda <shugo.maeda@gmail.com> Co-authored-by: emilweth <7402764+emilweth@users.noreply.github.com> Co-authored-by: Shlee <github@shl.ee> Co-authored-by: Joshua Rogers <MegaManSec@users.noreply.github.com> Co-authored-by: Jessica Stokes <hello@jessicastokes.net> Co-authored-by: PGray <77597544+PGrayCS@users.noreply.github.com>
3 weeks ago
import path from 'node:path';
import { readdir } from 'node:fs/promises';
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin';
import legacy from '@vitejs/plugin-legacy';
import react from '@vitejs/plugin-react';
import postcssPresetEnv from 'postcss-preset-env';
import Compress from 'rollup-plugin-gzip';
import { visualizer } from 'rollup-plugin-visualizer';
import {
PluginOption,
defineConfig,
UserConfigFnPromise,
UserConfig,
} from 'vite';
import manifestSRI from 'vite-plugin-manifest-sri';
import { VitePWA } from 'vite-plugin-pwa';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
import { MastodonServiceWorkerLocales } from './config/vite/plugin-sw-locales';
import { MastodonEmojiCompressed } from './config/vite/plugin-emoji-compressed';
import { MastodonThemes } from './config/vite/plugin-mastodon-themes';
import { MastodonNameLookup } from './config/vite/plugin-name-lookup';
import { MastodonAssetsManifest } from './config/vite/plugin-assets-manifest';
const jsRoot = path.resolve(__dirname, 'app/javascript');
const cssAliasClasses: ReadonlyArray<string> = ['components', 'features'];
export const config: UserConfigFnPromise = async ({ mode, command }) => {
const isProdBuild = mode === 'production' && command === 'build';
let outDirName = 'packs-dev';
if (mode === 'test') {
outDirName = 'packs-test';
} else if (mode === 'production') {
outDirName = 'packs';
}
const outDir = path.resolve('public', outDirName);
return {
root: jsRoot,
base: `/${outDirName}/`,
envDir: __dirname,
resolve: {
alias: {
'~/': `${jsRoot}/`,
'@/': `${jsRoot}/`,
},
},
css: {
modules: {
generateScopedName(name, filename) {
let prefix = '';
// Use the top two segments of the path as the prefix.
const [parentDirName, dirName] = path
.dirname(filename)
.split(path.sep)
.slice(-2)
.map((dir) => dir.toLowerCase());
// If the parent directory is in the cssAliasClasses list, use
// the first four letters of it as the prefix, otherwise use the full name.
if (parentDirName) {
if (cssAliasClasses.includes(parentDirName)) {
prefix = parentDirName.slice(0, 4);
} else {
prefix = parentDirName;
}
}
// If we have a directory name, append it to the prefix.
if (dirName) {
prefix = `${prefix}_${dirName}`;
}
// If the file is not styles.module.scss or style.module.scss,
// append the file base name to the prefix.
const baseName = path.basename(
filename,
`.module${path.extname(filename)}`,
);
if (baseName !== 'styles' && baseName !== 'style') {
prefix = `${prefix}_${baseName}`;
}
return `_${prefix}__${name}`;
},
},
postcss: {
plugins: [
postcssPresetEnv({
features: {
'logical-properties-and-values': false,
},
}),
],
},
},
server: {
headers: {
// This is needed in dev environment because we load the worker from `/dev-sw/dev-sw.js`,
// but it needs to be scoped to the whole domain
'Service-Worker-Allowed': '/',
},
hmr: {
// Forcing the protocol to be insecure helps if you are proxying your dev server with SSL,
// because Vite still tries to connect to localhost.
protocol: 'ws',
},
port: 3036,
},
build: {
commonjsOptions: { transformMixedEsModules: true },
chunkSizeWarningLimit: 1 * 1024 * 1024, // 1MB
sourcemap: true,
emptyOutDir: mode !== 'production',
manifest: true,
outDir,
assetsDir: 'assets',
rollupOptions: {
input: await findEntrypoints(),
output: {
chunkFileNames({ facadeModuleId, name }) {
if (!facadeModuleId) {
return '[name]-[hash].js';
}
if (/mastodon\/locales\/[a-zA-Z\-]+\.json/.exec(facadeModuleId)) {
// put all locale files in `intl/`
return 'intl/[name]-[hash].js';
} else if (/node_modules\/@formatjs\//.exec(facadeModuleId)) {
// use a custom name for formatjs polyfill files
const newName = /node_modules\/@formatjs\/([^/]+)\//.exec(
facadeModuleId,
);
if (newName?.[1]) {
return `intl/[name]-${newName[1]}-[hash].js`;
}
} else if (name === 'index') {
// Use a custom name for chunks, to avoid having too many of them called "index"
const parts = facadeModuleId.split('/');
const parent = parts.at(-2);
if (parent) {
return `${parent}-[name]-[hash].js`;
}
}
return '[name]-[hash].js';
},
},
},
},
experimental: {
/**
* Setting this causes Vite to not rely on the base config for import URLs,
* and instead uses import.meta.url, which is what we want for proper CDN support.
* @see https://github.com/mastodon/mastodon/pull/37310
*/
renderBuiltUrl: () => undefined,
},
worker: {
format: 'es',
},
plugins: [
tsconfigPaths({ projects: [path.resolve(__dirname, 'tsconfig.json')] }),
react({
babel: {
plugins: ['formatjs', 'transform-react-remove-prop-types'],
},
}),
MastodonThemes(),
MastodonAssetsManifest(),
MastodonServiceWorkerLocales(),
MastodonEmojiCompressed(),
legacy({
renderLegacyChunks: false,
modernPolyfills: true,
}),
isProdBuild && (Compress() as PluginOption),
command === 'build' &&
manifestSRI({
manifestPaths: ['.vite/manifest.json'],
}),
VitePWA({
srcDir: path.resolve(jsRoot, 'mastodon/service_worker'),
// We need to use injectManifest because we use our own service worker
strategies: 'injectManifest',
manifest: false,
injectRegister: false,
injectManifest: {
// Do not inject a manifest, we don't use precache
injectionPoint: undefined,
buildPlugins: {
vite: [
// Provide a virtual import with only the locales used in the ServiceWorker
MastodonServiceWorkerLocales(),
MastodonEmojiCompressed(),
],
},
},
// Force the output location, because we have a symlink in `public/sw.js`
outDir: path.resolve(__dirname, 'public/packs'),
devOptions: {
enabled: true,
type: 'module',
},
}),
svgr(),
// Old library types need to be converted
optimizeLodashImports() as PluginOption,
!!process.env.ANALYZE_BUNDLE_SIZE && (visualizer() as PluginOption),
MastodonNameLookup(),
],
} satisfies UserConfig;
};
async function findEntrypoints() {
const entrypoints: Record<string, string> = {};
// First, JS entrypoints
const jsEntrypointsDir = path.resolve(jsRoot, 'entrypoints');
const jsEntrypoints = await readdir(jsEntrypointsDir, {
withFileTypes: true,
});
const jsExtTest = /\.[jt]sx?$/;
for (const file of jsEntrypoints) {
if (file.isFile() && jsExtTest.test(file.name)) {
entrypoints[file.name.replace(jsExtTest, '')] = path.resolve(
jsEntrypointsDir,
file.name,
);
}
}
// Next, SCSS entrypoints
const scssEntrypointsDir = path.resolve(jsRoot, 'styles/entrypoints');
const scssEntrypoints = await readdir(scssEntrypointsDir, {
withFileTypes: true,
});
const scssExtTest = /\.s?css$/;
for (const file of scssEntrypoints) {
if (file.isFile() && scssExtTest.test(file.name)) {
entrypoints[file.name.replace(scssExtTest, '')] = path.resolve(
scssEntrypointsDir,
file.name,
);
}
}
return entrypoints;
}
export default defineConfig(config);