mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-03-21 03:30:42 +00:00
Minify backend (#17054)
* wip * Update build.js * Update build.js * [minify-backend用] フィジビリティ検証 (#16878) * fix: minify-backend * 間違えて入れちゃったのを戻す * 追従 * fix --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com> * test * use node 24 * Revert "use node 24" This reverts commit7ae2debf23. * Revert "test" This reverts commitd919879091. * Update package.json * wip * Update compile_config.js * Revert "Update compile_config.js" This reverts commit0ee286f02b. * Update config.ts * wip * Update .swcrc * Update ClientServerService.ts * [ci skip] update CHANGELOG --------- Co-authored-by: おさむのひと <46447427+samunohito@users.noreply.github.com>
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
*/
|
||||
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { dirname } from 'node:path';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import * as fs from 'node:fs';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import ms from 'ms';
|
||||
import sharp from 'sharp';
|
||||
@@ -69,13 +70,28 @@ import type { FastifyError, FastifyInstance, FastifyPluginOptions, FastifyReply
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
const _dirname = dirname(_filename);
|
||||
|
||||
const staticAssets = `${_dirname}/../../../assets/`;
|
||||
const clientAssets = `${_dirname}/../../../../frontend/assets/`;
|
||||
const assets = `${_dirname}/../../../../../built/_frontend_dist_/`;
|
||||
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
|
||||
const frontendViteOut = `${_dirname}/../../../../../built/_frontend_vite_/`;
|
||||
const frontendEmbedViteOut = `${_dirname}/../../../../../built/_frontend_embed_vite_/`;
|
||||
const tarball = `${_dirname}/../../../../../built/tarball/`;
|
||||
let rootDir = _dirname;
|
||||
// 見つかるまで上に遡る
|
||||
while (!fs.existsSync(resolve(rootDir, 'packages'))) {
|
||||
const parentDir = dirname(rootDir);
|
||||
if (parentDir === rootDir) {
|
||||
throw new Error('Cannot find root directory');
|
||||
}
|
||||
rootDir = parentDir;
|
||||
}
|
||||
|
||||
const backendRootDir = resolve(rootDir, 'packages/backend');
|
||||
const frontendRootDir = resolve(rootDir, 'packages/frontend');
|
||||
|
||||
const staticAssets = resolve(backendRootDir, 'assets');
|
||||
const clientAssets = resolve(frontendRootDir, 'assets');
|
||||
const assets = resolve(rootDir, 'built/_frontend_dist_');
|
||||
const swAssets = resolve(rootDir, 'built/_sw_dist_');
|
||||
const fluentEmojisDir = resolve(rootDir, 'fluent-emojis/dist');
|
||||
const twemojiDir = resolve(backendRootDir, 'node_modules/@discordapp/twemoji/dist/svg');
|
||||
const frontendViteOut = resolve(rootDir, 'built/_frontend_vite_');
|
||||
const frontendEmbedViteOut = resolve(rootDir, 'built/_frontend_embed_vite_');
|
||||
const tarball = resolve(rootDir, 'built/tarball');
|
||||
|
||||
@Injectable()
|
||||
export class ClientServerService {
|
||||
@@ -207,6 +223,7 @@ export class ClientServerService {
|
||||
|
||||
//#region vite assets
|
||||
if (this.config.frontendEmbedManifestExists) {
|
||||
console.log(`[ClientServerService] Using built frontend vite assets. ${frontendViteOut}`);
|
||||
fastify.register((fastify, options, done) => {
|
||||
fastify.register(fastifyStatic, {
|
||||
root: frontendViteOut,
|
||||
@@ -226,6 +243,7 @@ export class ClientServerService {
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
console.log('[ClientServerService] Proxying to Vite dev server.');
|
||||
const urlOriginWithoutPort = configUrl.origin.replace(/:\d+$/, '');
|
||||
|
||||
const port = (process.env.VITE_PORT ?? '5173');
|
||||
@@ -297,7 +315,7 @@ export class ClientServerService {
|
||||
|
||||
reply.header('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\'');
|
||||
|
||||
return await reply.sendFile(path, `${_dirname}/../../../../../fluent-emojis/dist/`, {
|
||||
return reply.sendFile(path, fluentEmojisDir, {
|
||||
maxAge: ms('30 days'),
|
||||
});
|
||||
});
|
||||
@@ -312,7 +330,7 @@ export class ClientServerService {
|
||||
|
||||
reply.header('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\'');
|
||||
|
||||
return await reply.sendFile(path, `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`, {
|
||||
return reply.sendFile(path, twemojiDir, {
|
||||
maxAge: ms('30 days'),
|
||||
});
|
||||
});
|
||||
@@ -326,7 +344,7 @@ export class ClientServerService {
|
||||
}
|
||||
|
||||
const mask = await sharp(
|
||||
`${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/${path.replace('.png', '')}.svg`,
|
||||
`${twemojiDir}/${path.replace('.png', '')}.svg`,
|
||||
{ density: 1000 },
|
||||
)
|
||||
.resize(488, 488)
|
||||
|
||||
Reference in New Issue
Block a user