Skip to content

Commit 0355466

Browse files
authoredMar 21, 2025··
fix: only optimize configured locale files (#3433)
1 parent b9e008c commit 0355466

File tree

13 files changed

+84
-29
lines changed

13 files changed

+84
-29
lines changed
 

‎specs/fixtures/issues/3404/app.vue

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<NuxtRouteAnnouncer />
4+
<NuxtPage />
5+
</div>
6+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import en from './locales/en'
2+
import nl from './locales/nl'
3+
4+
export default defineI18nConfig(() => ({
5+
messages: {
6+
en,
7+
nl
8+
}
9+
}))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
hello: 'Hello!'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import greeting from './greeting'
2+
3+
export default {
4+
greeting
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
hello: 'Hallo!'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import greeting from './greeting'
2+
3+
export default {
4+
greeting
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://nuxt.com/docs/api/configuration/nuxt-config
2+
export default defineNuxtConfig({
3+
modules: ['@nuxtjs/i18n'],
4+
i18n: {
5+
locales: ['en', 'nl']
6+
}
7+
})
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "fixture-3404",
3+
"private": true,
4+
"scripts": {
5+
"build": "nuxt build",
6+
"dev": "nuxt dev",
7+
"generate": "nuxt generate",
8+
"preview": "nuxt preview"
9+
},
10+
"devDependencies": {
11+
"@nuxtjs/i18n": "latest",
12+
"nuxt": "latest"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<h1 id="translated-heading">{{ $t('greeting.hello') }}</h1>
4+
</div>
5+
</template>

‎specs/issues/3404.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test, expect, describe } from 'vitest'
2+
import { fileURLToPath } from 'node:url'
3+
import { createPage, setup, url } from '../utils'
4+
import { getText } from '../helper'
5+
6+
describe('#3404', async () => {
7+
await setup({
8+
rootDir: fileURLToPath(new URL(`../fixtures/issues/3404`, import.meta.url)),
9+
browser: true
10+
})
11+
12+
test('resource optimization excludes files not configured in `i18n.locales.*.files`', async () => {
13+
const page = await createPage('/en')
14+
const heading = await getText(page, '#translated-heading')
15+
expect(heading).toEqual(`Hello!`)
16+
17+
await page.goto(url('/nl'))
18+
const heading2 = await getText(page, '#translated-heading')
19+
expect(heading2).toEqual(`Hallo!`)
20+
})
21+
})

‎src/bundler.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22
import createDebug from 'debug'
3-
import { resolve } from 'pathe'
43
import { extendViteConfig, addWebpackPlugin, addBuildPlugin, addTemplate, addRspackPlugin } from '@nuxt/kit'
54
import VueI18nPlugin from '@intlify/unplugin-vue-i18n'
65
import { toArray } from './utils'
76
import { TransformMacroPlugin } from './transform/macros'
87
import { ResourcePlugin } from './transform/resource'
98
import { TransformI18nFunctionPlugin } from './transform/i18n-function-injection'
109
import { asI18nVirtual } from './transform/utils'
11-
import { getLayerLangPaths } from './layers'
1210

1311
import type { Nuxt } from '@nuxt/schema'
1412
import type { PluginOptions } from '@intlify/unplugin-vue-i18n'
@@ -19,13 +17,8 @@ const debug = createDebug('@nuxtjs/i18n:bundler')
1917

2018
export async function extendBundler(ctx: I18nNuxtContext, nuxt: Nuxt) {
2119
const { options: nuxtOptions } = ctx
22-
const langPaths = getLayerLangPaths(nuxt)
23-
debug('langPaths -', langPaths)
24-
const i18nModulePaths =
25-
nuxtOptions?.i18nModules?.map(module => resolve(nuxt.options._layers[0].config.rootDir, module.langDir ?? '')) ?? []
26-
debug('i18nModulePaths -', i18nModulePaths)
27-
const localePaths = [...langPaths, ...i18nModulePaths]
28-
const localeIncludePaths = localePaths.length ? localePaths.map(x => resolve(x, './**')) : undefined
20+
const localePaths = [...new Set([...ctx.localeInfo.flatMap(x => x.meta!.map(m => m.path))])]
21+
const localeIncludePaths = localePaths.length ? localePaths : undefined
2922

3023
const sourceMapOptions: BundlerPluginOptions = {
3124
sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client

‎src/layers.ts

-16
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,6 @@ const mergeLayerLocales = (options: NuxtI18nOptions, nuxt: Nuxt) => {
145145
return mergeConfigLocales(configs)
146146
}
147147

148-
/**
149-
* Returns an array of absolute paths to each layers `langDir`
150-
*/
151-
export const getLayerLangPaths = (nuxt: Nuxt) => {
152-
const langPaths: string[] = []
153-
154-
for (const layer of nuxt.options._layers) {
155-
const i18n = getLayerI18n(layer)
156-
if (i18n?.restructureDir === false && i18n?.langDir == null) continue
157-
158-
langPaths.push(resolveLayerLangDir(layer, i18n || {}))
159-
}
160-
161-
return langPaths
162-
}
163-
164148
export async function resolveLayerVueI18nConfigInfo(options: NuxtI18nOptions) {
165149
const logger = useLogger(NUXT_I18N_MODULE_ID)
166150
const nuxt = useNuxt()

‎src/transform/resource.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
NUXT_I18N_VIRTUAL_PREFIX
1010
} from '../constants'
1111
import { resolve, dirname } from 'pathe'
12-
import { parseSync } from '../utils/parse'
12+
import { findStaticImports } from 'mlly'
1313
import { resolvePath, tryUseNuxt } from '@nuxt/kit'
1414
import { transform as esbuildTransform } from 'esbuild'
1515
import type { SameShape, TransformOptions, TransformResult } from 'esbuild'
@@ -77,10 +77,10 @@ export const ResourcePlugin = (options: BundlerPluginOptions, ctx: I18nNuxtConte
7777
debug('transform', id)
7878
let code = _code
7979

80-
const parsed = parseSync(id, _code)
8180
// ensure imported resources are transformed as well
82-
for (const x of parsed.module.staticImports) {
83-
i18nPathSet.add(await resolvePath(resolve(dirname(id), x.moduleRequest.value)))
81+
const staticImports = findStaticImports(_code)
82+
for (const x of staticImports) {
83+
i18nPathSet.add(await resolvePath(resolve(dirname(id), x.specifier)))
8484
}
8585

8686
// transform typescript

0 commit comments

Comments
 (0)
Please sign in to comment.