Skip to content

Commit

Permalink
Fix hotreloading
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Dec 15, 2023
1 parent 2257f4b commit 4ee5968
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel-translator",
"type": "module",
"version": "0.0.1",
"version": "0.0.2",
"description": "Laravel localization bridge for your frontend.",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const isServer = typeof window === 'undefined'

const defaultConfig: Config = {
locale: !isServer && document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : 'en',
fallbackLocale: !isServer && window ? window?.fallbackLocale.replace('-', '_') : null,
fallbackLocale: !isServer && window ? window?.fallbackLocale?.replace('-', '_') : null,
translations: translations,
}

Expand Down
22 changes: 14 additions & 8 deletions src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function laravelTranslator(options: string | VitePluginOptionsInt
const virtualModuleId = 'virtual:laravel-translations'
const resolvedVirtualModuleId = '\0' + virtualModuleId

let translations = null
const paths = [frameworkLangPath, langPath, ...additionalLangPaths]
return {
name: 'laravelTranslator',
enforce: 'post',
Expand All @@ -27,16 +27,22 @@ export default function laravelTranslator(options: string | VitePluginOptionsInt
},
load(id) {
if (id === resolvedVirtualModuleId) {
if (!translations) {
translations = exportTranslations(frameworkLangPath, langPath, ...additionalLangPaths)
}

return `export default ${JSON.stringify(translations)}`
return `export default ${JSON.stringify(exportTranslations(...paths))}`
}
},
handleHotUpdate(ctx) {
if (ctx.file === resolvedVirtualModuleId) {
translations = exportTranslations(frameworkLangPath, langPath, ...additionalLangPaths)
for (const lp of paths) {
const relative = path.relative(lp, ctx.file);
const isSub = relative && !relative.startsWith('..') && !path.isAbsolute(relative);
if (isSub) {
const virtualModule = ctx.server.moduleGraph.getModuleById(resolvedVirtualModuleId)!;
ctx.server.moduleGraph.invalidateModule(virtualModule)
ctx.server.ws.send({
type: 'full-reload',
path: '*'
});
return
}
}
}
}
Expand Down

0 comments on commit 4ee5968

Please sign in to comment.