Skip to content

Commit 31f06ec

Browse files
authored
fix: warn only when vueI18n property is set but not found (#2468)
1 parent f0edcf0 commit 31f06ec

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

src/layers.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { isString } from '@intlify/shared'
1414
import { NUXT_I18N_MODULE_ID } from './constants'
1515

1616
import type { Nuxt, NuxtConfigLayer } from '@nuxt/schema'
17-
import type { NuxtI18nOptions } from './types'
17+
import type { NuxtI18nOptions, VueI18nConfigPathInfo } from './types'
1818

1919
const debug = createDebug('@nuxtjs/i18n:layers')
2020

@@ -133,14 +133,36 @@ export const getLayerLangPaths = (nuxt: Nuxt) => {
133133
}
134134

135135
export async function resolveLayerVueI18nConfigInfo(nuxt: Nuxt, buildDir: string) {
136+
const logger = useLogger(NUXT_I18N_MODULE_ID)
136137
const layers = [...nuxt.options._layers]
137138
const project = layers.shift() as NuxtConfigLayer
138139
const i18nLayers = [project, ...layers.filter(layer => getLayerI18n(layer))]
139140

140-
return await Promise.all(
141-
i18nLayers.map(layer => {
142-
const i18n = getLayerI18n(layer)
143-
return resolveVueI18nConfigInfo(i18n || {}, buildDir, layer.config.rootDir)
144-
})
141+
const resolved = await Promise.all(
142+
i18nLayers
143+
.map(layer => {
144+
const i18n = getLayerI18n(layer) as NuxtI18nOptions
145+
146+
return [layer, i18n, resolveVueI18nConfigInfo(i18n || {}, buildDir, layer.config.rootDir)] as [
147+
NuxtConfigLayer,
148+
NuxtI18nOptions,
149+
Promise<VueI18nConfigPathInfo | undefined>
150+
]
151+
})
152+
.map(async ([layer, i18n, resolver]) => {
153+
const res = await resolver
154+
155+
if (res == null && i18n?.vueI18n != null) {
156+
logger.warn(
157+
`Ignore Vue I18n configuration file does not exist at ${i18n.vueI18n} in ${layer.config.rootDir}. Skipping...`
158+
)
159+
160+
return undefined
161+
}
162+
163+
return res
164+
})
145165
)
166+
167+
return resolved.filter((x): x is Required<VueI18nConfigPathInfo> => x != null)
146168
}

src/module.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ export default defineNuxtModule<NuxtI18nOptions>({
168168
*/
169169

170170
const vueI18nConfigPaths = await resolveLayerVueI18nConfigInfo(nuxt, nuxt.options.buildDir)
171-
for (const vueI18nConfigPath of vueI18nConfigPaths) {
172-
if (vueI18nConfigPath.absolute === '') {
173-
logger.warn(
174-
`Ignore Vue I18n configuration file does not exist at ${vueI18nConfigPath.relative} in ${vueI18nConfigPath.rootDir}. Skipping...`
175-
)
176-
}
177-
}
178171
debug('VueI18nConfigPaths', vueI18nConfigPaths)
179172

180173
/**

src/utils.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,24 @@ export async function resolveVueI18nConfigInfo(options: NuxtI18nOptions, buildDi
248248
}
249249

250250
const absolutePath = await resolvePath(configPathInfo.relative, { cwd: rootDir, extensions: EXECUTABLE_EXTENSIONS })
251-
if (await isExists(absolutePath)) {
252-
const parsed = parsePath(absolutePath)
253-
const loadPath = join(configPathInfo.relativeBase, relative(rootDir, absolutePath))
254-
255-
configPathInfo.absolute = absolutePath
256-
configPathInfo.type = getLocaleType(absolutePath)
257-
configPathInfo.hash = getHash(loadPath)
258-
259-
const key = `${normalizeWithUnderScore(configPathInfo.relative)}_${configPathInfo.hash}`
260-
261-
configPathInfo.meta = {
262-
path: absolutePath,
263-
type: configPathInfo.type,
264-
hash: configPathInfo.hash,
265-
loadPath,
266-
parsed,
267-
key
268-
}
251+
if (!(await isExists(absolutePath))) return undefined
252+
253+
const parsed = parsePath(absolutePath)
254+
const loadPath = join(configPathInfo.relativeBase, relative(rootDir, absolutePath))
255+
256+
configPathInfo.absolute = absolutePath
257+
configPathInfo.type = getLocaleType(absolutePath)
258+
configPathInfo.hash = getHash(loadPath)
259+
260+
const key = `${normalizeWithUnderScore(configPathInfo.relative)}_${configPathInfo.hash}`
261+
262+
configPathInfo.meta = {
263+
path: absolutePath,
264+
type: configPathInfo.type,
265+
hash: configPathInfo.hash,
266+
loadPath,
267+
parsed,
268+
key
269269
}
270270

271271
return configPathInfo

test/gen.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test('basic', async () => {
5252
const localeInfo = await resolveLocales('/test/srcDir', LOCALE_INFO, '..')
5353
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
5454
const code = generateLoaderOptions({
55-
vueI18nConfigPaths: [vueI18nConfig],
55+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
5656
localeInfo,
5757
nuxtI18nOptions: { ...NUXT_I18N_OPTIONS, lazy: false }
5858
})
@@ -64,7 +64,7 @@ test('lazy', async () => {
6464
const localeInfo = await resolveLocales('/test/srcDir', LOCALE_INFO, '..')
6565
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
6666
const code = generateLoaderOptions({
67-
vueI18nConfigPaths: [vueI18nConfig],
67+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
6868
localeInfo,
6969
nuxtI18nOptions: { ...NUXT_I18N_OPTIONS, lazy: true }
7070
})
@@ -98,7 +98,7 @@ test('multiple files', async () => {
9898
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
9999

100100
const code = generateLoaderOptions({
101-
vueI18nConfigPaths: [vueI18nConfig],
101+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
102102
localeInfo,
103103
nuxtI18nOptions: { ...NUXT_I18N_OPTIONS, lazy: true }
104104
})
@@ -132,7 +132,7 @@ test('files with cache configuration', async () => {
132132
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
133133

134134
const code = generateLoaderOptions({
135-
vueI18nConfigPaths: [vueI18nConfig],
135+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
136136
localeInfo,
137137
nuxtI18nOptions: { ...NUXT_I18N_OPTIONS, lazy: true }
138138
})
@@ -165,7 +165,7 @@ test('locale file in nested', async () => {
165165

166166
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
167167
const code = generateLoaderOptions({
168-
vueI18nConfigPaths: [vueI18nConfig],
168+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
169169
localeInfo,
170170
nuxtI18nOptions: { ...NUXT_I18N_OPTIONS, lazy: true }
171171
})
@@ -207,7 +207,7 @@ test('vueI18n option', async () => {
207207
test('toCode: function (arrow)', async () => {
208208
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
209209
const code = generateLoaderOptions({
210-
vueI18nConfigPaths: [vueI18nConfig],
210+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
211211
localeInfo: [],
212212
nuxtI18nOptions: {
213213
...NUXT_I18N_OPTIONS,
@@ -227,7 +227,7 @@ test('toCode: function (arrow)', async () => {
227227
test('toCode: function (named)', async () => {
228228
const vueI18nConfig = await resolveVueI18nConfigInfo({ vueI18n: NUXT_I18N_VUE_I18N_CONFIG.relative }, '.nuxt', '.')
229229
const code = generateLoaderOptions({
230-
vueI18nConfigPaths: [vueI18nConfig],
230+
vueI18nConfigPaths: [vueI18nConfig].filter((x): x is Required<VueI18nConfigPathInfo> => x != null),
231231
localeInfo: [],
232232
nuxtI18nOptions: {
233233
...NUXT_I18N_OPTIONS,

0 commit comments

Comments
 (0)