Skip to content

Commit 2f45ae7

Browse files
committed
feat: configurable vue app root container selector
1 parent e7fd84a commit 2f45ae7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

packages/unplugin-vue-i18n/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ If do you will use this option, you need to enable `jitCompilation` option.
616616

617617
> [!IMPORTANT] > Vite support only
618618
619+
### `appRootContainer`
620+
621+
- **Type:** `string`
622+
- **Default:** `'#app'`
623+
624+
The selector used find the Vue app root container during HMR.
625+
619626
### `useVueI18nImportName` (Experimental)
620627

621628
- **Type:** `boolean`

packages/unplugin-vue-i18n/src/core/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function resolveOptions(options: PluginOptions) {
5858

5959
const allowDynamic = !!options.allowDynamic
6060
const hmr = !!options.hmr
61+
const appRootContainer = options.appRootContainer ?? '#app'
6162

6263
const strictMessage = isBoolean(options.strictMessage)
6364
? options.strictMessage
@@ -85,6 +86,7 @@ export function resolveOptions(options: PluginOptions) {
8586
include,
8687
exclude,
8788
hmr,
89+
appRootContainer,
8890
module: moduleType,
8991
onlyLocales,
9092
forceStringify,

packages/unplugin-vue-i18n/src/core/resource.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function resourcePlugin(
5151
exclude,
5252
module,
5353
forceStringify,
54+
appRootContainer,
5455
defaultSFCLang,
5556
globalSFCScope,
5657
runtimeOnly,
@@ -231,6 +232,16 @@ export function resourcePlugin(
231232
}
232233
}
233234
},
235+
configureServer(server) {
236+
// client could not find vue app root during HMR
237+
server.ws.on('unplugin-vue-i18n:app-root-missing', _ => {
238+
console.error(
239+
`[unplugin-vue-i18n] Unable to find vue-i18n instance on vue app root container with selector "${appRootContainer}" - skipping HMR and reloading page.\n` +
240+
`The selector used can be configured using the \`appRootcontainer\` option.`
241+
)
242+
server.ws.send({ type: 'full-reload' })
243+
})
244+
},
234245

235246
async handleHotUpdate({ file, server }) {
236247
if (/\.(json5?|ya?ml)$/.test(file)) {
@@ -328,7 +339,8 @@ export function resourcePlugin(
328339
forceStringify,
329340
strictMessage,
330341
escapeHtml,
331-
hmr
342+
hmr,
343+
appRootContainer
332344
}
333345
)
334346
// TODO: support virtual import identifier
@@ -529,6 +541,7 @@ async function generateBundleResources(
529541
escapeHtml = false,
530542
jit = true,
531543
hmr = false,
544+
appRootContainer,
532545
transformI18nBlock = undefined
533546
}: {
534547
forceStringify?: boolean
@@ -538,6 +551,7 @@ async function generateBundleResources(
538551
escapeHtml?: boolean
539552
jit?: boolean
540553
hmr?: boolean
554+
appRootContainer: string
541555
transformI18nBlock?: PluginOptions['transformI18nBlock']
542556
}
543557
) {
@@ -582,7 +596,11 @@ if(import.meta.hot) {
582596
583597
import.meta.hot.accept(mod => {
584598
// retrieve global i18n instance
585-
const i18n = document.querySelector('#app').__vue_app__.__VUE_I18N__.global
599+
const i18n = document.querySelector('${appRootContainer}')?.__vue_app__?.__VUE_I18N__?.global
600+
if(i18n == null) {
601+
import.meta.hot.send('unplugin-vue-i18n:app-root-missing', {})
602+
return
603+
}
586604
587605
// locale keys of both original and updated merged messages
588606
const localeKeys = uniqueKeys(merged, mod.default)

packages/unplugin-vue-i18n/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface PluginOptions {
1111
compositionOnly?: boolean
1212
ssr?: boolean
1313
hmr?: boolean
14+
appRootContainer?: string
1415
fullInstall?: boolean
1516
forceStringify?: boolean
1617
defaultSFCLang?: SFCLangFormat

0 commit comments

Comments
 (0)