Skip to content

Commit c1187ef

Browse files
authored
fix: skip optimizing dynamic locale files (#4058)
1 parent d957629 commit c1187ef

7 files changed

Lines changed: 31 additions & 4 deletions

File tree

File renamed without changes.
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { test, expect, describe } from 'vitest'
2+
import { fileURLToPath } from 'node:url'
3+
import { setup, $fetch } from '../utils'
4+
5+
describe('lazy loading (dev mode + app directory)', async () => {
6+
await setup({
7+
rootDir: fileURLToPath(new URL(`../fixtures/lazy`, import.meta.url)),
8+
dev: true
9+
})
10+
11+
test('(#4049) dynamic locale files are loaded during dev SSR', async () => {
12+
const html: string = await $fetch('/en-GB')
13+
// dynamic `.js` locale file fetching `/api/en-GB`
14+
expect(html).toContain('Profile1')
15+
// static `.ts` locale file
16+
expect(html).toContain('Profile2')
17+
// dynamic locale file using `useRuntimeConfig`
18+
expect(html).toContain('runtime-config-value')
19+
})
20+
})

src/bundler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export async function extendBundler(ctx: I18nNuxtContext, nuxt: Nuxt) {
3434
/**
3535
* shared plugins (vite/webpack/rspack)
3636
*/
37-
const localePaths = getLocaleFilePaths(ctx.localeInfo)
37+
// exclude dynamic locale files - optimization is a no-op for these, and since vite 8 matching them
38+
// makes unplugin-vue-i18n load them raw during dev SSR, skipping the `defineI18nLocale` transform (#4049)
39+
const localePaths = getLocaleFilePaths(ctx.localeInfo.map(x => ({ ...x, meta: x.meta.filter(m => m.type !== 'dynamic') })))
3840
ctx.fullStatic = ctx.localeInfo.flatMap(x => x.meta).every(x => x.type === 'static' || x.cache !== false)
3941

4042
const vueI18nPluginOptions: PluginOptions = {
@@ -47,7 +49,6 @@ export async function extendBundler(ctx: I18nNuxtContext, nuxt: Nuxt) {
4749
}
4850
addBuildPlugin({
4951
vite: () => VueI18nPlugin.vite(vueI18nPluginOptions),
50-
5152
webpack: () => VueI18nPlugin.webpack(vueI18nPluginOptions),
5253
})
5354
addBuildPlugin(TransformMacroPlugin(pluginOptions))

src/transform/resource.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import MagicString from 'magic-string'
22
import { createUnplugin } from 'unplugin'
3-
import { NUXT_I18N_VIRTUAL_PREFIX, VIRTUAL_PREFIX_HEX, asI18nVirtual } from './utils'
3+
import { VIRTUAL_PREFIX_HEX, asI18nVirtual } from './utils'
44
import { dirname, resolve } from 'pathe'
55
import { findStaticImports } from 'mlly'
66
import { resolvePath, tryUseNuxt } from '@nuxt/kit'
@@ -35,10 +35,16 @@ export const ResourcePlugin = (options: BundlerPluginOptions, ctx: I18nNuxtConte
3535

3636
// resolve virtual hash to file path
3737
resolveId(id) {
38-
if (!id || id.startsWith(VIRTUAL_PREFIX_HEX) || !id.startsWith(NUXT_I18N_VIRTUAL_PREFIX)) {
38+
if (!id || id.startsWith(VIRTUAL_PREFIX_HEX)) {
3939
return
4040
}
4141

42+
// claim i18n file paths so other plugins (e.g. `unplugin-vue-i18n` since vite 8) cannot
43+
// resolve these to virtual modules loaded from disk, which would skip our transforms (#4049)
44+
if (i18nPathSet.has(id)) {
45+
return id
46+
}
47+
4248
if (i18nFileHashSet.has(id)) {
4349
return i18nFileHashSet.get(id)
4450
}

0 commit comments

Comments
 (0)