Skip to content

Commit 9b900f5

Browse files
committed
refactor: virtuals and constants naming consistency
1 parent ad613b7 commit 9b900f5

22 files changed

+46
-63
lines changed

internals.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module '#build/i18n.options.mjs' {
1+
declare module '#build/i18n-options.mjs' {
22
import type { LocaleObject, VueI18nConfig } from '@nuxtjs/i18n'
33

44
export type { LocaleObject }
@@ -10,7 +10,7 @@ declare module '#build/i18n.options.mjs' {
1010
export const normalizedLocales: LocaleObject[]
1111
}
1212

13-
declare module '#internal/i18n/options.mjs' {
13+
declare module '#internal/i18n-options.mjs' {
1414
import type { LocaleObject, VueI18nConfig } from '@nuxtjs/i18n'
1515

1616
type LocaleLoader = { key: string; cache: boolean; load: () => Promise<never> }
@@ -20,20 +20,14 @@ declare module '#internal/i18n/options.mjs' {
2020
export const normalizedLocales: LocaleObject[]
2121
}
2222

23-
declare module '#internal/i18n/locale.detector.mjs' {
23+
declare module '#internal/i18n-locale-detector.mjs' {
2424
export const localeDetector: ((event: H3Event, config: LocaleConfig) => string) | undefined
2525
}
2626

2727
declare module '#internal/i18n-type-generation-options' {
2828
export const dtsFile: string
2929
}
3030

31-
declare module '#nuxt-i18n/logger' {
32-
import type { ConsolaInstance } from 'consola'
33-
34-
export function createLogger(label: string): ConsolaInstance
35-
}
36-
3731
declare module '#build/i18n-route-resources.mjs' {
3832
export const i18nPathToPath: Record<string, string>
3933
export const pathToI18nConfig: Record<string, Record<string, string | boolean>>

src/constants.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ export const DEFAULT_OPTIONS = {
8686
autoDeclare: true
8787
} as const
8888

89-
export const NUXT_I18N_TEMPLATE_OPTIONS_KEY = 'i18n.options.mjs'
90-
export const NUXT_I18N_COMPOSABLE_DEFINE_ROUTE = 'defineI18nRoute'
91-
export const NUXT_I18N_COMPOSABLE_DEFINE_LOCALE = 'defineI18nLocale'
92-
export const NUXT_I18N_COMPOSABLE_DEFINE_CONFIG = 'defineI18nConfig'
93-
export const NUXT_I18N_COMPOSABLE_DEFINE_LOCALE_DETECTOR = 'defineI18nLocaleDetector'
89+
export const DEFINE_I18N_ROUTE_FN = 'defineI18nRoute'
90+
export const DEFINE_I18N_LOCALE_FN = 'defineI18nLocale'
91+
export const DEFINE_I18N_CONFIG_FN = 'defineI18nConfig'
92+
export const DEFINE_LOCALE_DETECTOR_FN = 'defineI18nLocaleDetector'
9493
export const NUXT_I18N_VIRTUAL_PREFIX = '#nuxt-i18n'
9594

9695
const TS_EXTENSIONS = ['.ts', '.cts', '.mts']

src/nitro.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
H3_PKG,
1212
UTILS_H3_PKG,
1313
EXECUTABLE_EXTENSIONS,
14-
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE,
15-
NUXT_I18N_COMPOSABLE_DEFINE_CONFIG,
16-
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE_DETECTOR
14+
DEFINE_I18N_LOCALE_FN,
15+
DEFINE_I18N_CONFIG_FN,
16+
DEFINE_LOCALE_DETECTOR_FN
1717
} from './constants'
1818
import { resolveI18nDir } from './layers'
1919

@@ -25,9 +25,9 @@ export async function setupNitro(ctx: I18nNuxtContext, nuxt: Nuxt) {
2525
const [enableServerIntegration, localeDetectionPath] = await resolveLocaleDetectorPath(nuxt)
2626

2727
addServerTemplate({
28-
filename: '#internal/i18n/options.mjs',
28+
filename: '#internal/i18n-options.mjs',
2929
getContents: () =>
30-
nuxt.vfs['#build/i18n.options.mjs']?.replace(/\/\*\* client \*\*\/[\s\S]*\/\*\* client-end \*\*\//, '')
30+
nuxt.vfs['#build/i18n-options.mjs']?.replace(/\/\*\* client \*\*\/[\s\S]*\/\*\* client-end \*\*\//, '')
3131
})
3232

3333
addServerTemplate({
@@ -36,7 +36,7 @@ export async function setupNitro(ctx: I18nNuxtContext, nuxt: Nuxt) {
3636
})
3737

3838
addServerTemplate({
39-
filename: '#internal/i18n/locale.detector.mjs',
39+
filename: '#internal/i18n-locale-detector.mjs',
4040
getContents: () =>
4141
enableServerIntegration
4242
? `import localeDetector from ${JSON.stringify(localeDetectionPath)}
@@ -75,7 +75,7 @@ export { localeDetector }`
7575

7676
// `defineI18nLocale`, `defineI18nConfig`
7777
addServerImports(
78-
[NUXT_I18N_COMPOSABLE_DEFINE_LOCALE, NUXT_I18N_COMPOSABLE_DEFINE_CONFIG].map(key => ({
78+
[DEFINE_I18N_LOCALE_FN, DEFINE_I18N_CONFIG_FN].map(key => ({
7979
name: key,
8080
as: key,
8181
from: ctx.resolver.resolve('runtime/composables/shared')
@@ -85,8 +85,8 @@ export { localeDetector }`
8585
// `defineLocaleDetector`
8686
addServerImports([
8787
{
88-
name: NUXT_I18N_COMPOSABLE_DEFINE_LOCALE_DETECTOR,
89-
as: NUXT_I18N_COMPOSABLE_DEFINE_LOCALE_DETECTOR,
88+
name: DEFINE_LOCALE_DETECTOR_FN,
89+
as: DEFINE_LOCALE_DETECTOR_FN,
9090
from: ctx.resolver.resolve('runtime/composables/server')
9191
}
9292
])

src/pages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises'
77
import { getRoutePath, parseSegment } from './utils/route-parsing'
88
import { localizeRoutes } from './routing'
99
import { resolve, parse as parsePath, dirname } from 'pathe'
10-
import { NUXT_I18N_COMPOSABLE_DEFINE_ROUTE } from './constants'
10+
import { DEFINE_I18N_ROUTE_FN } from './constants'
1111
import { createRoutesContext } from 'unplugin-vue-router'
1212
import { resolveOptions } from 'unplugin-vue-router/options'
1313

@@ -386,7 +386,7 @@ function readComponent(target: string) {
386386
const content = readFileSync(target, 'utf-8')
387387
const { descriptor } = parseSFC(content)
388388

389-
if (!content.includes(NUXT_I18N_COMPOSABLE_DEFINE_ROUTE)) {
389+
if (!content.includes(DEFINE_I18N_ROUTE_FN)) {
390390
return undefined
391391
}
392392

@@ -399,7 +399,7 @@ function readComponent(target: string) {
399399
walk(ast, {
400400
enter(node: Node) {
401401
if (node.type !== 'CallExpression') return
402-
if (node.callee.type === 'Identifier' && node.callee.name === NUXT_I18N_COMPOSABLE_DEFINE_ROUTE) {
402+
if (node.callee.type === 'Identifier' && node.callee.name === DEFINE_I18N_ROUTE_FN) {
403403
const arg = node.arguments[0]
404404
if (
405405
arg.type === 'BooleanLiteral' ||

src/prepare/auto-imports.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
NUXT_I18N_COMPOSABLE_DEFINE_CONFIG,
3-
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE,
4-
NUXT_I18N_COMPOSABLE_DEFINE_ROUTE,
5-
VUE_I18N_PKG
6-
} from '../constants'
1+
import { DEFINE_I18N_CONFIG_FN, DEFINE_I18N_LOCALE_FN, DEFINE_I18N_ROUTE_FN, VUE_I18N_PKG } from '../constants'
72
import { addComponent, addImports, resolveModule, useNuxt } from '@nuxt/kit'
83
import type { I18nNuxtContext } from '../context'
94

@@ -32,9 +27,9 @@ export function prepareAutoImports({ resolver, userOptions: options, runtimeDir
3227
'useCookieLocale',
3328
'useSetI18nParams',
3429
'useI18nPreloadKeys',
35-
NUXT_I18N_COMPOSABLE_DEFINE_ROUTE,
36-
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE,
37-
NUXT_I18N_COMPOSABLE_DEFINE_CONFIG
30+
DEFINE_I18N_ROUTE_FN,
31+
DEFINE_I18N_LOCALE_FN,
32+
DEFINE_I18N_CONFIG_FN
3833
].map(key => ({ name: key, as: key, from: composablesIndex }))
3934
])
4035
}

src/prepare/runtime.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { I18nNuxtContext } from '../context'
33
import { addPlugin, addTemplate, addTypeTemplate, addVitePlugin, useNitro } from '@nuxt/kit'
44
import { generateTemplateNuxtI18nOptions } from '../template'
55
import { generateI18nTypes, generateLoaderOptions } from '../gen'
6-
import { NUXT_I18N_TEMPLATE_OPTIONS_KEY } from '../constants'
76

87
export function prepareRuntime(ctx: I18nNuxtContext, nuxt: Nuxt) {
98
const { options, resolver } = ctx
@@ -45,7 +44,7 @@ export function prepareRuntime(ctx: I18nNuxtContext, nuxt: Nuxt) {
4544
}
4645

4746
addTemplate({
48-
filename: NUXT_I18N_TEMPLATE_OPTIONS_KEY,
47+
filename: 'i18n-options.mjs',
4948
getContents: () => generateTemplateNuxtI18nOptions(ctx, generateLoaderOptions(ctx, nuxt))
5049
})
5150

src/runtime/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isRef, unref } from 'vue'
22

33
import { useState, useCookie, useRequestURL } from '#imports'
4-
import { localeLoaders } from '#build/i18n.options.mjs'
4+
import { localeLoaders } from '#build/i18n-options.mjs'
55
import { getLocaleMessagesMergedCached } from './shared/messages'
66
import { createBaseUrlGetter, createComposableContext } from './utils'
77
import { getI18nTarget } from './compatibility'

src/runtime/plugins/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vueI18nConfigs } from '#build/i18n.options.mjs'
1+
import { vueI18nConfigs } from '#build/i18n-options.mjs'
22
import { defineNuxtPlugin, useNuxtApp } from '#imports'
33
import { getComposer } from '../compatibility'
44
import { useNuxtI18nContext } from '../context'

src/runtime/plugins/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computed, ref, watch } from 'vue'
22
import { createI18n } from 'vue-i18n'
33
import { defineNuxtPlugin, prerenderRoutes, useNuxtApp, useRequestEvent, useRequestURL } from '#imports'
4-
import { localeCodes, normalizedLocales } from '#build/i18n.options.mjs'
4+
import { localeCodes, normalizedLocales } from '#build/i18n-options.mjs'
55
import { loadAndSetLocale, navigate } from '../utils'
66
import { extendI18n } from '../routing/i18n'
77
import { getI18nTarget } from '../compatibility'

src/runtime/plugins/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parse } from 'devalue'
22
import { unref } from 'vue'
33
import { useNuxtApp, defineNuxtPlugin } from '#app'
4-
import { localeCodes, localeLoaders } from '#build/i18n.options.mjs'
4+
import { localeCodes, localeLoaders } from '#build/i18n-options.mjs'
55
import { getLocaleMessagesMergedCached } from '../shared/messages'
66
import { useNuxtI18nContext, type NuxtI18nContext } from '../context'
77

src/runtime/server/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { setupVueI18nOptions } from '../shared/vue-i18n'
99
import { joinURL } from 'ufo'
1010
// @ts-expect-error virtual file
1111
import { appId } from '#internal/nuxt.config.mjs'
12-
import { localeDetector } from '#internal/i18n/locale.detector.mjs'
12+
import { localeDetector } from '#internal/i18n-locale-detector.mjs'
1313
import { resolveRootRedirect, useI18nDetection, useRuntimeI18n } from '../shared/utils'
1414
import { isFunction } from '@intlify/shared'
1515

src/runtime/server/type-generation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { deepCopy, isArray, isFunction, isObject } from '@intlify/shared'
2-
import { vueI18nConfigs, localeLoaders, normalizedLocales } from '#internal/i18n/options.mjs'
2+
import { vueI18nConfigs, localeLoaders, normalizedLocales } from '#internal/i18n-options.mjs'
33
import { dtsFile } from '#internal/i18n-type-generation-options'
44
import { loadVueI18nOptions } from '../shared/messages'
55
import { getMergedMessages } from './utils/messages'

src/runtime/server/utils/locale-detector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fetchMessages } from '../context'
22
import { deepCopy } from '@intlify/shared'
3-
import { localeDetector } from '#internal/i18n/locale.detector.mjs'
3+
import { localeDetector } from '#internal/i18n-locale-detector.mjs'
44
import { tryCookieLocale, tryHeaderLocale, tryQueryLocale } from '@intlify/h3'
55
import { findBrowserLocale } from '#i18n-kit/browser'
66
import { parseAcceptLanguage } from '@intlify/utils'
7-
import { normalizedLocales } from '#internal/i18n/options.mjs'
7+
import { normalizedLocales } from '#internal/i18n-options.mjs'
88

99
import type { H3Event } from 'h3'
1010
import type { CoreOptions, FallbackLocale, Locale } from '@intlify/core'

src/runtime/server/utils/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { deepCopy } from '@intlify/shared'
2-
import { localeLoaders } from '#internal/i18n/options.mjs'
2+
import { localeLoaders } from '#internal/i18n-options.mjs'
33
import { getLocaleMessagesMerged } from '../../shared/messages'
44
import { cachedFunctionI18n } from './cache'
55
import { isLocaleCacheable, isLocaleWithFallbacksCacheable } from '../../shared/locales'

src/runtime/shared/detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { getRequestURL, getRequestHeader } from 'h3'
3-
import { normalizedLocales } from '#build/i18n.options.mjs'
3+
import { normalizedLocales } from '#build/i18n-options.mjs'
44
import { getLocaleFromRoute, getLocaleFromRoutePath } from '#i18n-kit/routing'
55
import { findBrowserLocale } from '#i18n-kit/browser'
66
import { parseAcceptLanguage } from '@intlify/utils'

src/runtime/shared/domain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { hasProtocol } from 'ufo'
2-
import { normalizedLocales } from '#build/i18n.options.mjs'
2+
import { normalizedLocales } from '#build/i18n-options.mjs'
33
import { toArray } from './utils'
44

55
import type { LocaleObject } from '#internal-i18n-types'

src/runtime/shared/locales.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { localeCodes, localeLoaders, normalizedLocales } from '#build/i18n.options.mjs'
1+
import { localeCodes, localeLoaders, normalizedLocales } from '#build/i18n-options.mjs'
22
import { isArray, isString } from '@intlify/shared'
33
import type { FallbackLocale } from 'vue-i18n'
44

src/runtime/shared/vue-i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { loadVueI18nOptions } from './messages'
2-
import { vueI18nConfigs, localeCodes as _localeCodes } from '#build/i18n.options.mjs'
2+
import { vueI18nConfigs, localeCodes as _localeCodes } from '#build/i18n-options.mjs'
33

44
import type { I18nOptions } from 'vue-i18n'
55

src/transform/heist.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const HeistPlugin = (options: BundlerPluginOptions, ctx: I18nNuxtContext,
1717

1818
const replacementName = `__nuxtMock`
1919
const replacementMock = `const ${replacementName} = { runWithContext: async (fn) => await fn() };`
20+
const resources = ['i18n-route-resources.mjs', 'i18n-options.mjs']
2021

2122
return createUnplugin(() => ({
2223
name: 'nuxtjs:i18n-heist',
@@ -39,9 +40,9 @@ export const HeistPlugin = (options: BundlerPluginOptions, ctx: I18nNuxtContext,
3940
// replace `#app` with `__nuxtMock`
4041
s.replaceAll(/useNuxtApp\(\)/g, replacementName)
4142

42-
s.replaceAll(/#build\/i18n-route-resources\.mjs/g, '#internal/i18n-route-resources.mjs')
43-
// replace `#build/i18n.options.mjs` with `#internal/i18n/options.mjs`
44-
s.replaceAll(/#build\/i18n\.options\.mjs/g, '#internal/i18n/options.mjs')
43+
for (const resource of resources) {
44+
s.replaceAll(new RegExp(`#build/${resource}`, 'g'), `#internal/${resource}`)
45+
}
4546

4647
return {
4748
code: s.toString(),

src/transform/macros.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import MagicString from 'magic-string'
1010
import { createUnplugin } from 'unplugin'
1111
import { parse as parseSFC } from '@vue/compiler-sfc'
1212
import { VIRTUAL_PREFIX_HEX, isVue } from './utils'
13-
import { NUXT_I18N_COMPOSABLE_DEFINE_ROUTE } from '../constants'
13+
import { DEFINE_I18N_ROUTE_FN } from '../constants'
1414

1515
import type { BundlerPluginOptions } from './utils'
1616

17-
const I18N_MACRO_FN_RE = new RegExp(`\\b${NUXT_I18N_COMPOSABLE_DEFINE_ROUTE}\\s*\\(\\s*`)
17+
const I18N_MACRO_FN_RE = new RegExp(`\\b${DEFINE_I18N_ROUTE_FN}\\s*\\(\\s*`)
1818

1919
/**
2020
* TODO:

src/transform/resource.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import MagicString from 'magic-string'
22
import { createUnplugin } from 'unplugin'
33
import { asI18nVirtual, VIRTUAL_PREFIX_HEX } from './utils'
4-
import {
5-
NUXT_I18N_COMPOSABLE_DEFINE_LOCALE,
6-
NUXT_I18N_COMPOSABLE_DEFINE_CONFIG,
7-
NUXT_I18N_VIRTUAL_PREFIX
8-
} from '../constants'
4+
import { DEFINE_I18N_LOCALE_FN, DEFINE_I18N_CONFIG_FN, NUXT_I18N_VIRTUAL_PREFIX } from '../constants'
95
import { resolve, dirname } from 'pathe'
106
import { findStaticImports } from 'mlly'
117
import { resolvePath, tryUseNuxt } from '@nuxt/kit'
@@ -22,7 +18,7 @@ async function transform<T extends TransformOptions>(
2218
return await esbuildTransform(input, { ...tryUseNuxt()?.options.esbuild?.options, ...options })
2319
}
2420

25-
const pattern = [NUXT_I18N_COMPOSABLE_DEFINE_LOCALE, NUXT_I18N_COMPOSABLE_DEFINE_CONFIG].join('|')
21+
const pattern = [DEFINE_I18N_LOCALE_FN, DEFINE_I18N_CONFIG_FN].join('|')
2622
const DEFINE_I18N_FN_RE = new RegExp(`\\b(${pattern})\\s*\\((.+)\\s*\\)`, 'gms')
2723

2824
export const ResourcePlugin = (options: BundlerPluginOptions, ctx: I18nNuxtContext) =>

vitest.config.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export default defineConfig({
99
setupFiles: [...(vitestConfig.test?.setupFiles ?? []), resolve('./test/setup.ts')].filter(Boolean),
1010
alias: {
1111
...vitestConfig.test?.alias,
12-
'#build/i18n.options.mjs': resolve('./test/mocks/i18n.options.ts'),
13-
'#nuxt-i18n/logger': resolve('./test/mocks/i18n-logger.ts'),
12+
'#build/i18n-options.mjs': resolve('./test/mocks/i18n.options.ts'),
1413
'#app': 'nuxt'
1514
}
1615
}

0 commit comments

Comments
 (0)