From 008c2da719a68af190bf0223702afd8566f196ee Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Mon, 17 Feb 2025 15:52:29 -0400 Subject: [PATCH] fix: reload lang file (#329) --- src/common/it/utils.ts | 12 +----------- src/langs/handler.ts | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/common/it/utils.ts b/src/common/it/utils.ts index 2972a40..fb505b8 100644 --- a/src/common/it/utils.ts +++ b/src/common/it/utils.ts @@ -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; diff --git a/src/langs/handler.ts b/src/langs/handler.ts index f0633d3..ef247f5 100644 --- a/src/langs/handler.ts +++ b/src/langs/handler.ts @@ -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) {