fix: reload lang file (#329)

This commit is contained in:
MARCROCK22 2025-02-17 15:52:29 -04:00 committed by GitHub
parent 3c4043b2b1
commit 008c2da719
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 15 deletions

View File

@ -279,17 +279,7 @@ export const ReplaceRegex = {
};
export async function magicImport(path: string) {
try {
if ('Deno' in globalThis) throw new Error('https://github.com/denoland/deno/issues/26136');
return require(path);
} catch (e: any) {
// (bun)dows moment
if (!('Bun' in globalThis) || e.message.includes('is unsupported. use "await import()" instead.'))
return new Function('path', 'return import(`file:///${path}?update=${Date.now()}`)')(
path.split('\\').join('\\\\'),
);
throw new Error(`Cannot import ${path}`, { cause: e });
}
return new Function('path', 'return import(`file:///${path}?update=${Date.now()}`)')(path);
}
export type OnFailCallback = (error: unknown) => any;

View File

@ -1,3 +1,4 @@
import { basename } from 'node:path';
import type { FileLoaded } from '../commands/handler';
import { BaseHandler, isCloudfareWorker, magicImport } from '../common';
import type { Locale, LocaleString } from '../types';
@ -63,11 +64,18 @@ export class LangsHandler extends BaseHandler {
if (isCloudfareWorker()) {
throw new Error('Reload in cloudfare worker is not supported');
}
const value = this.__paths[lang];
const path = this.__paths[lang];
if (!path) return null;
delete require.cache[path];
const value = await magicImport(path).then(x =>
this.onFile(lang, {
file: x,
name: basename(path),
path,
} satisfies LangInstance),
);
if (!value) return null;
delete require.cache[value];
return (this.values[lang] = await magicImport(value).then(x => this.onFile(lang, x)));
return (this.values[lang] = value.file);
}
async reloadAll(stopIfFail = true) {