1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-24 13:54:12 +02:00

Remove js-yaml from runtime dependencies, use pre-compiled JSON instead

Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-01 02:11:02 +00:00
parent 5e6fe5bef6
commit 69c8d8e102
8 changed files with 83 additions and 40 deletions

View File

@@ -11,6 +11,7 @@ import * as esbuild from 'esbuild';
import { build } from 'esbuild';
import { execa } from 'execa';
import { globSync } from 'glob';
import * as yaml from 'js-yaml';
import { generateLocaleInterface } from './scripts/generateLocaleInterface.js';
import type { BuildOptions, BuildResult, Plugin, PluginBuild } from 'esbuild';
@@ -49,7 +50,18 @@ if (args.includes('--watch')) {
await buildSrc();
}
function copyLocales(): void {
/**
* 何故か文字列にバックスペース文字が混入することがあり、YAMLが壊れるので取り除く
*/
function clean(text: string) {
return text.replace(new RegExp(String.fromCodePoint(0x08), 'g'), '');
}
/**
* Convert locale YAML files to JSON during build
* This allows runtime to avoid loading js-yaml
*/
function compileLocales(): void {
const srcDir = _localesDir;
const destDir = resolve(_dirname, 'built/locales');
@@ -57,9 +69,12 @@ function copyLocales(): void {
const files = fs.readdirSync(srcDir).filter(f => f.endsWith('.yml'));
for (const file of files) {
fs.copyFileSync(resolve(srcDir, file), resolve(destDir, file));
const yamlContent = clean(fs.readFileSync(resolve(srcDir, file), 'utf-8'));
const jsonContent = yaml.load(yamlContent);
const jsonFile = file.replace(/\.yml$/, '.json');
fs.writeFileSync(resolve(destDir, jsonFile), JSON.stringify(jsonContent), 'utf-8');
}
console.log(`[${_package.name}] locales copied (${files.length} files).`);
console.log(`[${_package.name}] locales compiled to JSON (${files.length} files).`);
}
/**
@@ -87,7 +102,7 @@ async function buildSrc(): Promise<void> {
process.exit(1);
});
copyLocales();
compileLocales();
await writeFrontendLocalesJson();
if (process.env.NODE_ENV === 'production') {
@@ -123,7 +138,7 @@ async function watchSrc(): Promise<void> {
localesWatcher.on('all', async (event, path) => {
if (!path.endsWith('.yml')) return;
console.log(`[${_package.name}] locales changed: ${event} ${path}`);
copyLocales();
compileLocales();
await writeFrontendLocalesJson();
await generateLocaleInterface(_localesDir);
});

View File

@@ -32,11 +32,9 @@
"esbuild": "0.27.0",
"execa": "9.6.0",
"glob": "11.1.0",
"js-yaml": "4.1.1",
"nodemon": "3.1.11",
"tsx": "4.20.6",
"typescript": "5.9.3"
},
"dependencies": {
"js-yaml": "4.1.1"
}
}

View File

@@ -8,7 +8,6 @@
*/
import * as fs from 'node:fs';
import * as yaml from 'js-yaml';
import type { Locale } from './autogen/locale.js';
import type { ILocale, ParameterizedString } from './types.js';
@@ -71,13 +70,6 @@ function merge<T extends ILocale>(...args: (T | ILocale | undefined)[]): T {
}), {} as ILocale) as T;
}
/**
* 何故か文字列にバックスペース文字が混入することがあり、YAMLが壊れるので取り除く
*/
function clean (text: string) {
return text.replace(new RegExp(String.fromCodePoint(0x08), 'g'), '');
}
/**
* 空文字列が入ることがあり、フォールバックが動作しなくなるのでプロパティごと消す
*/
@@ -98,7 +90,7 @@ function build(): Record<Language, Locale> {
// https://github.com/misskey-dev/misskey/pull/14057#issuecomment-2192833785
const metaUrl = import.meta.url;
const locales = languages.reduce<Locales>((a, lang) => {
a[lang] = (yaml.load(clean(fs.readFileSync(new URL(`./locales/${lang}.yml`, metaUrl), 'utf-8'))) ?? {}) as ILocale;
a[lang] = (JSON.parse(fs.readFileSync(new URL(`./locales/${lang}.json`, metaUrl), 'utf-8')) ?? {}) as ILocale;
return a;
}, {} as Locales);