Skip to content

Commit 329db4e

Browse files
sawa-koBobbieGoede
andauthored
fix(hmr): use virtual module ids in import.meta.hot.accept() for vite 8 (#4065)
Co-authored-by: Bobbie Goede <bobbiegoede@gmail.com>
1 parent 86a4d7e commit 329db4e

6 files changed

Lines changed: 52 additions & 76 deletions

File tree

src/gen.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ type LocaleLoaderData = {
2525
key: string
2626
load: string
2727
loadServer: string
28-
relative: string
28+
virtualId: string
2929
cache: boolean
3030
}
3131

3232
export function generateLoaderOptions(
3333
ctx: Pick<I18nNuxtContext, 'options' | 'vueI18nConfigPaths' | 'localeInfo' | 'normalizedLocales'>,
34-
nuxt: Nuxt,
3534
) {
3635
/**
3736
* Prepare locale file imports
@@ -45,13 +44,14 @@ export function generateLoaderOptions(
4544
if (!importMapper.has(meta.path)) {
4645
const identifier = `locale_${genSafeVariableName(basename(meta.path))}_${meta.hash}`
4746
const key = genString(identifier)
47+
const virtualId = asI18nVirtual(meta.hash)
4848

49-
importStatements.add(genImport(asI18nVirtual(meta.hash), identifier))
49+
importStatements.add(genImport(virtualId, identifier))
5050
importMapper.set(meta.path, {
5151
key,
52-
relative: relative(nuxt.options.buildDir, meta.path),
52+
virtualId,
5353
cache: meta.cache ?? true,
54-
load: genDynamicImport(asI18nVirtual(meta.hash), { comment: `webpackChunkName: ${key}` }),
54+
load: genDynamicImport(virtualId, { comment: `webpackChunkName: ${key}` }),
5555
loadServer: `() => Promise.resolve(${identifier})`,
5656
})
5757
}
@@ -67,12 +67,13 @@ export function generateLoaderOptions(
6767
const config = ctx.vueI18nConfigPaths[i]!
6868
const identifier = `config_${genSafeVariableName(basename(config.path))}_${config.hash}`
6969
const key = genString(identifier)
70+
const virtualId = asI18nVirtual(config.hash)
7071

71-
importStatements.add(genImport(asI18nVirtual(config.hash), identifier))
72+
importStatements.add(genImport(virtualId, identifier))
7273
vueI18nConfigs.push({
73-
importer: genDynamicImport(asI18nVirtual(config.hash), { comment: `webpackChunkName: ${key}` }),
74+
importer: genDynamicImport(virtualId, { comment: `webpackChunkName: ${key}` }),
7475
importerServer: `() => Promise.resolve(${identifier})`,
75-
relative: relative(nuxt.options.buildDir, config.path),
76+
virtualId,
7677
})
7778
}
7879

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default defineNuxtModule<NuxtI18nOptions>({
131131

132132
addTemplate({
133133
filename: 'i18n-options.mjs',
134-
getContents: () => generateTemplateNuxtI18nOptions(ctx, generateLoaderOptions(ctx, nuxt)),
134+
getContents: () => generateTemplateNuxtI18nOptions(ctx, generateLoaderOptions(ctx)),
135135
})
136136

137137
/**

src/nitro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { generateTemplateNuxtI18nOptions } from './template'
1818
export async function setupNitro(ctx: I18nNuxtContext, nuxt: Nuxt) {
1919
addServerTemplate({
2020
filename: '#internal/i18n-options.mjs',
21-
getContents: () => generateTemplateNuxtI18nOptions(ctx, generateLoaderOptions(ctx, nuxt), true),
21+
getContents: () => generateTemplateNuxtI18nOptions(ctx, generateLoaderOptions(ctx), true),
2222
})
2323

2424
addServerTemplate({

src/template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function genLocaleLoaderHMR(localeLoaders: TemplateNuxtI18nOptions['localeLoader
1919
const loader = localeLoaders[locale]![i]!
2020
statements.push(
2121
[
22-
` import.meta.hot.accept("${loader.relative}", async mod => {`,
22+
` import.meta.hot.accept("${loader.virtualId}", async mod => {`,
2323
// replace locale loader
2424
` localeLoaders["${locale}"][${i}].load = () => Promise.resolve(mod.default)`,
2525
// trigger locale messages reload for locale
@@ -39,7 +39,7 @@ function genVueI18nConfigHMR(configs: TemplateNuxtI18nOptions['vueI18nConfigs'])
3939
for (let i = 0; i < configs.length; i++) {
4040
statements.push(
4141
[
42-
` import.meta.hot.accept("${configs[i]!.relative}", async mod => {`,
42+
` import.meta.hot.accept("${configs[i]!.virtualId}", async mod => {`,
4343
// load configs before replacing loader
4444
` const [oldData, newData] = await Promise.all([loadCfg(vueI18nConfigs[${i}]), loadCfg(() => Promise.resolve(mod))]);`,
4545
// replace config loader

test/__snapshots__/gen.test.ts.snap

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`basic 1`] = `
1515
"key": ""locale_en_46json_f7535dc0"",
1616
"load": "() => import("#nuxt-i18n/f7535dc0" /* webpackChunkName: "locale_en_46json_f7535dc0" */)",
1717
"loadServer": "() => Promise.resolve(locale_en_46json_f7535dc0)",
18-
"relative": "../en.json",
18+
"virtualId": "#nuxt-i18n/f7535dc0",
1919
},
2020
],
2121
"fr": [
@@ -24,7 +24,7 @@ exports[`basic 1`] = `
2424
"key": ""locale_fr_46json_3d83f3b3"",
2525
"load": "() => import("#nuxt-i18n/3d83f3b3" /* webpackChunkName: "locale_fr_46json_3d83f3b3" */)",
2626
"loadServer": "() => Promise.resolve(locale_fr_46json_3d83f3b3)",
27-
"relative": "../fr.json",
27+
"virtualId": "#nuxt-i18n/3d83f3b3",
2828
},
2929
],
3030
"ja": [
@@ -33,7 +33,7 @@ exports[`basic 1`] = `
3333
"key": ""locale_ja_46json_5843ecf1"",
3434
"load": "() => import("#nuxt-i18n/5843ecf1" /* webpackChunkName: "locale_ja_46json_5843ecf1" */)",
3535
"loadServer": "() => Promise.resolve(locale_ja_46json_5843ecf1)",
36-
"relative": "../ja.json",
36+
"virtualId": "#nuxt-i18n/5843ecf1",
3737
},
3838
],
3939
},
@@ -52,7 +52,7 @@ exports[`basic 1`] = `
5252
{
5353
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
5454
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
55-
"relative": "../../path/to",
55+
"virtualId": "#nuxt-i18n/c3216fe6",
5656
},
5757
],
5858
}
@@ -75,7 +75,7 @@ exports[`files with cache configuration 1`] = `
7575
"key": ""locale_en_46json_7973e345"",
7676
"load": "() => import("#nuxt-i18n/7973e345" /* webpackChunkName: "locale_en_46json_7973e345" */)",
7777
"loadServer": "() => Promise.resolve(locale_en_46json_7973e345)",
78-
"relative": "../srcDir/locales/en.json",
78+
"virtualId": "#nuxt-i18n/7973e345",
7979
},
8080
],
8181
"es": [
@@ -84,7 +84,7 @@ exports[`files with cache configuration 1`] = `
8484
"key": ""locale_es_46json_cffd8a40"",
8585
"load": "() => import("#nuxt-i18n/cffd8a40" /* webpackChunkName: "locale_es_46json_cffd8a40" */)",
8686
"loadServer": "() => Promise.resolve(locale_es_46json_cffd8a40)",
87-
"relative": "../srcDir/locales/es.json",
87+
"virtualId": "#nuxt-i18n/cffd8a40",
8888
},
8989
],
9090
"es-AR": [
@@ -93,14 +93,14 @@ exports[`files with cache configuration 1`] = `
9393
"key": ""locale_es_46json_cffd8a40"",
9494
"load": "() => import("#nuxt-i18n/cffd8a40" /* webpackChunkName: "locale_es_46json_cffd8a40" */)",
9595
"loadServer": "() => Promise.resolve(locale_es_46json_cffd8a40)",
96-
"relative": "../srcDir/locales/es.json",
96+
"virtualId": "#nuxt-i18n/cffd8a40",
9797
},
9898
{
9999
"cache": true,
100100
"key": ""locale_es_45AR_46json_d3b7c72c"",
101101
"load": "() => import("#nuxt-i18n/d3b7c72c" /* webpackChunkName: "locale_es_45AR_46json_d3b7c72c" */)",
102102
"loadServer": "() => Promise.resolve(locale_es_45AR_46json_d3b7c72c)",
103-
"relative": "../srcDir/locales/es-AR.json",
103+
"virtualId": "#nuxt-i18n/d3b7c72c",
104104
},
105105
],
106106
"fr": [
@@ -109,7 +109,7 @@ exports[`files with cache configuration 1`] = `
109109
"key": ""locale_fr_46json_aa45ccf4"",
110110
"load": "() => import("#nuxt-i18n/aa45ccf4" /* webpackChunkName: "locale_fr_46json_aa45ccf4" */)",
111111
"loadServer": "() => Promise.resolve(locale_fr_46json_aa45ccf4)",
112-
"relative": "../srcDir/locales/fr.json",
112+
"virtualId": "#nuxt-i18n/aa45ccf4",
113113
},
114114
],
115115
"ja": [
@@ -118,7 +118,7 @@ exports[`files with cache configuration 1`] = `
118118
"key": ""locale_ja_46json_4c459061"",
119119
"load": "() => import("#nuxt-i18n/4c459061" /* webpackChunkName: "locale_ja_46json_4c459061" */)",
120120
"loadServer": "() => Promise.resolve(locale_ja_46json_4c459061)",
121-
"relative": "../srcDir/locales/ja.json",
121+
"virtualId": "#nuxt-i18n/4c459061",
122122
},
123123
],
124124
},
@@ -143,7 +143,7 @@ exports[`files with cache configuration 1`] = `
143143
{
144144
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
145145
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
146-
"relative": "../../path/to",
146+
"virtualId": "#nuxt-i18n/c3216fe6",
147147
},
148148
],
149149
}
@@ -164,7 +164,7 @@ exports[`lazy 1`] = `
164164
"key": ""locale_en_46json_f7535dc0"",
165165
"load": "() => import("#nuxt-i18n/f7535dc0" /* webpackChunkName: "locale_en_46json_f7535dc0" */)",
166166
"loadServer": "() => Promise.resolve(locale_en_46json_f7535dc0)",
167-
"relative": "../en.json",
167+
"virtualId": "#nuxt-i18n/f7535dc0",
168168
},
169169
],
170170
"fr": [
@@ -173,7 +173,7 @@ exports[`lazy 1`] = `
173173
"key": ""locale_fr_46json_3d83f3b3"",
174174
"load": "() => import("#nuxt-i18n/3d83f3b3" /* webpackChunkName: "locale_fr_46json_3d83f3b3" */)",
175175
"loadServer": "() => Promise.resolve(locale_fr_46json_3d83f3b3)",
176-
"relative": "../fr.json",
176+
"virtualId": "#nuxt-i18n/3d83f3b3",
177177
},
178178
],
179179
"ja": [
@@ -182,7 +182,7 @@ exports[`lazy 1`] = `
182182
"key": ""locale_ja_46json_5843ecf1"",
183183
"load": "() => import("#nuxt-i18n/5843ecf1" /* webpackChunkName: "locale_ja_46json_5843ecf1" */)",
184184
"loadServer": "() => Promise.resolve(locale_ja_46json_5843ecf1)",
185-
"relative": "../ja.json",
185+
"virtualId": "#nuxt-i18n/5843ecf1",
186186
},
187187
],
188188
},
@@ -201,7 +201,7 @@ exports[`lazy 1`] = `
201201
{
202202
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
203203
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
204-
"relative": "../../path/to",
204+
"virtualId": "#nuxt-i18n/c3216fe6",
205205
},
206206
],
207207
}
@@ -222,7 +222,7 @@ exports[`locale file in nested 1`] = `
222222
"key": ""locale_main_46json_291f935f"",
223223
"load": "() => import("#nuxt-i18n/291f935f" /* webpackChunkName: "locale_main_46json_291f935f" */)",
224224
"loadServer": "() => Promise.resolve(locale_main_46json_291f935f)",
225-
"relative": "../en/main.json",
225+
"virtualId": "#nuxt-i18n/291f935f",
226226
},
227227
],
228228
"fr": [
@@ -231,7 +231,7 @@ exports[`locale file in nested 1`] = `
231231
"key": ""locale_main_46json_7ba1ed37"",
232232
"load": "() => import("#nuxt-i18n/7ba1ed37" /* webpackChunkName: "locale_main_46json_7ba1ed37" */)",
233233
"loadServer": "() => Promise.resolve(locale_main_46json_7ba1ed37)",
234-
"relative": "../fr/main.json",
234+
"virtualId": "#nuxt-i18n/7ba1ed37",
235235
},
236236
],
237237
"ja": [
@@ -240,7 +240,7 @@ exports[`locale file in nested 1`] = `
240240
"key": ""locale_main_46json_cfa671d1"",
241241
"load": "() => import("#nuxt-i18n/cfa671d1" /* webpackChunkName: "locale_main_46json_cfa671d1" */)",
242242
"loadServer": "() => Promise.resolve(locale_main_46json_cfa671d1)",
243-
"relative": "../ja/main.json",
243+
"virtualId": "#nuxt-i18n/cfa671d1",
244244
},
245245
],
246246
},
@@ -259,7 +259,7 @@ exports[`locale file in nested 1`] = `
259259
{
260260
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
261261
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
262-
"relative": "../../path/to",
262+
"virtualId": "#nuxt-i18n/c3216fe6",
263263
},
264264
],
265265
}
@@ -282,7 +282,7 @@ exports[`multiple files 1`] = `
282282
"key": ""locale_en_46json_f7535dc0"",
283283
"load": "() => import("#nuxt-i18n/f7535dc0" /* webpackChunkName: "locale_en_46json_f7535dc0" */)",
284284
"loadServer": "() => Promise.resolve(locale_en_46json_f7535dc0)",
285-
"relative": "../en.json",
285+
"virtualId": "#nuxt-i18n/f7535dc0",
286286
},
287287
],
288288
"es": [
@@ -291,7 +291,7 @@ exports[`multiple files 1`] = `
291291
"key": ""locale_es_46json_234aa7ad"",
292292
"load": "() => import("#nuxt-i18n/234aa7ad" /* webpackChunkName: "locale_es_46json_234aa7ad" */)",
293293
"loadServer": "() => Promise.resolve(locale_es_46json_234aa7ad)",
294-
"relative": "../es.json",
294+
"virtualId": "#nuxt-i18n/234aa7ad",
295295
},
296296
],
297297
"es-AR": [
@@ -300,14 +300,14 @@ exports[`multiple files 1`] = `
300300
"key": ""locale_es_46json_234aa7ad"",
301301
"load": "() => import("#nuxt-i18n/234aa7ad" /* webpackChunkName: "locale_es_46json_234aa7ad" */)",
302302
"loadServer": "() => Promise.resolve(locale_es_46json_234aa7ad)",
303-
"relative": "../es.json",
303+
"virtualId": "#nuxt-i18n/234aa7ad",
304304
},
305305
{
306306
"cache": true,
307307
"key": ""locale_es_45AR_46json_dab30c5d"",
308308
"load": "() => import("#nuxt-i18n/dab30c5d" /* webpackChunkName: "locale_es_45AR_46json_dab30c5d" */)",
309309
"loadServer": "() => Promise.resolve(locale_es_45AR_46json_dab30c5d)",
310-
"relative": "../es-AR.json",
310+
"virtualId": "#nuxt-i18n/dab30c5d",
311311
},
312312
],
313313
"fr": [
@@ -316,7 +316,7 @@ exports[`multiple files 1`] = `
316316
"key": ""locale_fr_46json_3d83f3b3"",
317317
"load": "() => import("#nuxt-i18n/3d83f3b3" /* webpackChunkName: "locale_fr_46json_3d83f3b3" */)",
318318
"loadServer": "() => Promise.resolve(locale_fr_46json_3d83f3b3)",
319-
"relative": "../fr.json",
319+
"virtualId": "#nuxt-i18n/3d83f3b3",
320320
},
321321
],
322322
"ja": [
@@ -325,7 +325,7 @@ exports[`multiple files 1`] = `
325325
"key": ""locale_ja_46json_5843ecf1"",
326326
"load": "() => import("#nuxt-i18n/5843ecf1" /* webpackChunkName: "locale_ja_46json_5843ecf1" */)",
327327
"loadServer": "() => Promise.resolve(locale_ja_46json_5843ecf1)",
328-
"relative": "../ja.json",
328+
"virtualId": "#nuxt-i18n/5843ecf1",
329329
},
330330
],
331331
},
@@ -350,7 +350,7 @@ exports[`multiple files 1`] = `
350350
{
351351
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
352352
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
353-
"relative": "../../path/to",
353+
"virtualId": "#nuxt-i18n/c3216fe6",
354354
},
355355
],
356356
}
@@ -371,7 +371,7 @@ exports[`vueI18n option 1`] = `
371371
"key": ""locale_en_46json_f7535dc0"",
372372
"load": "() => import("#nuxt-i18n/f7535dc0" /* webpackChunkName: "locale_en_46json_f7535dc0" */)",
373373
"loadServer": "() => Promise.resolve(locale_en_46json_f7535dc0)",
374-
"relative": "../en.json",
374+
"virtualId": "#nuxt-i18n/f7535dc0",
375375
},
376376
],
377377
"fr": [
@@ -380,7 +380,7 @@ exports[`vueI18n option 1`] = `
380380
"key": ""locale_fr_46json_3d83f3b3"",
381381
"load": "() => import("#nuxt-i18n/3d83f3b3" /* webpackChunkName: "locale_fr_46json_3d83f3b3" */)",
382382
"loadServer": "() => Promise.resolve(locale_fr_46json_3d83f3b3)",
383-
"relative": "../fr.json",
383+
"virtualId": "#nuxt-i18n/3d83f3b3",
384384
},
385385
],
386386
"ja": [
@@ -389,7 +389,7 @@ exports[`vueI18n option 1`] = `
389389
"key": ""locale_ja_46json_5843ecf1"",
390390
"load": "() => import("#nuxt-i18n/5843ecf1" /* webpackChunkName: "locale_ja_46json_5843ecf1" */)",
391391
"loadServer": "() => Promise.resolve(locale_ja_46json_5843ecf1)",
392-
"relative": "../ja.json",
392+
"virtualId": "#nuxt-i18n/5843ecf1",
393393
},
394394
],
395395
},
@@ -408,17 +408,17 @@ exports[`vueI18n option 1`] = `
408408
{
409409
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
410410
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
411-
"relative": "../../path/to",
411+
"virtualId": "#nuxt-i18n/c3216fe6",
412412
},
413413
{
414414
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
415415
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
416-
"relative": "../../path/to",
416+
"virtualId": "#nuxt-i18n/c3216fe6",
417417
},
418418
{
419419
"importer": "() => import("#nuxt-i18n/c3216fe6" /* webpackChunkName: "config_to_c3216fe6" */)",
420420
"importerServer": "() => Promise.resolve(config_to_c3216fe6)",
421-
"relative": "../../path/to",
421+
"virtualId": "#nuxt-i18n/c3216fe6",
422422
},
423423
],
424424
}

0 commit comments

Comments
 (0)