Skip to content

Commit 24449e5

Browse files
authored
perf!: move generated options to compile time constants (#3568)
1 parent 6bda12f commit 24449e5

File tree

13 files changed

+79
-91
lines changed

13 files changed

+79
-91
lines changed

docs/content/docs/02.guide/90.migrating.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,23 @@ This feature has been disabled and the option to enable it has been removed, see
4848
### Drop `experimental.generatedLocaleFilePathFormat`
4949
File paths (e.g. locale files, vue-i18n configs) configured for this module are now removed entirely making this option obsolete.
5050

51-
### Removed `nuxtI18nOptions` from generated options files.
51+
### Removed exports from generated options files.
5252
The generated options files in your projects are meant for internal use by this module at runtime and should never be used, more properties may be removed in the future.
5353

54-
The `nuxtI18nOptions` export has been removed from the generated options files (`#build/i18n.options.mjs` and `#internal/i18n/options.mjs`), since it is no longer used by the module and might expose vulnerable information in the final build.
54+
The following exports have been removed from the generated options files (`#build/i18n.options.mjs` and `#internal/i18n/options.mjs`):
55+
* `nuxtI18nOptions`
56+
* `NUXT_I18N_MODULE_ID`
57+
* `parallelPlugin`
58+
* `isSSG`
59+
* `hasPages`
60+
* `DEFAULT_COOKIE_KEY`
61+
* `DYNAMIC_PARAMS_KEY`
62+
* `SWITCH_LOCALE_PATH_LINK_IDENTIFIER`
63+
64+
Reasons for removal:
65+
* These are no longer used by the module and might expose vulnerable information in the final build
66+
* Some options are now used as static values for better tree-shaking resulting in a smaller project build.
67+
5568

5669
## Upgrading from `nuxtjs/i18n` v8.x to v9.x
5770

internals.d.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ declare module '#build/i18n.options.mjs' {
88
export const vueI18nConfigs: VueI18nConfig[]
99
export const localeCodes: string[]
1010
export const normalizedLocales: LocaleObject[]
11-
export const isSSG: boolean
12-
export const hasPages: boolean
13-
export const parallelPlugin: boolean
14-
15-
export const NUXT_I18N_MODULE_ID: string
16-
export const DYNAMIC_PARAMS_KEY: string
17-
export const DEFAULT_COOKIE_KEY: string
18-
export const SWITCH_LOCALE_PATH_LINK_IDENTIFIER: string
1911
}
2012

2113
declare module '#internal/i18n/options.mjs' {
@@ -26,14 +18,6 @@ declare module '#internal/i18n/options.mjs' {
2618
export const vueI18nConfigs: VueI18nConfig[]
2719
export const localeCodes: string[]
2820
export const normalizedLocales: LocaleObject[]
29-
export const isSSG: boolean
30-
export const hasPages: boolean
31-
export const parallelPlugin: boolean
32-
33-
export const NUXT_I18N_MODULE_ID: string
34-
export const DYNAMIC_PARAMS_KEY: string
35-
export const DEFAULT_COOKIE_KEY: string
36-
export const SWITCH_LOCALE_PATH_LINK_IDENTIFIER: string
3721
}
3822

3923
declare module '#internal/i18n/locale.detector.mjs' {

src/bundler.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import type { PluginOptions } from '@intlify/unplugin-vue-i18n'
1313
import type { BundlerPluginOptions } from './transform/utils'
1414
import type { I18nNuxtContext } from './context'
1515
import type { NuxtI18nOptions } from './types'
16+
import {
17+
DEFAULT_COOKIE_KEY,
18+
DYNAMIC_PARAMS_KEY,
19+
NUXT_I18N_MODULE_ID,
20+
SWITCH_LOCALE_PATH_LINK_IDENTIFIER
21+
} from './constants'
1622

1723
const debug = createDebug('@nuxtjs/i18n:bundler')
1824

@@ -110,7 +116,15 @@ export function createLogger(label) {
110116
export function getDefineConfig(options: NuxtI18nOptions, server = false, nuxt = useNuxt()) {
111117
const common = {
112118
__DEBUG__: String(!!options.debug),
113-
__TEST__: String(!!options.debug || nuxt.options._i18nTest)
119+
__TEST__: String(!!options.debug || nuxt.options._i18nTest),
120+
__IS_SSG__: String(nuxt.options._generate),
121+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
122+
__HAS_PAGES__: String(nuxt.options.pages.toString()),
123+
__PARALLEL_PLUGIN__: String(options.parallelPlugin),
124+
__DYNAMIC_PARAMS_KEY__: JSON.stringify(DYNAMIC_PARAMS_KEY),
125+
__DEFAULT_COOKIE_KEY__: JSON.stringify(DEFAULT_COOKIE_KEY),
126+
__NUXT_I18N_MODULE_ID__: JSON.stringify(NUXT_I18N_MODULE_ID),
127+
__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__: JSON.stringify(SWITCH_LOCALE_PATH_LINK_IDENTIFIER)
114128
}
115129

116130
if (nuxt.options.ssr || !server) {

src/env.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ declare namespace NodeJS {
66

77
declare let __DEBUG__: boolean
88
declare let __TEST__: boolean
9+
10+
declare let __IS_SSG__: boolean
11+
declare let __HAS_PAGES__: boolean
12+
declare let __PARALLEL_PLUGIN__: boolean
13+
14+
declare let __DYNAMIC_PARAMS_KEY__: string
15+
declare let __DEFAULT_COOKIE_KEY__: string
16+
declare let __NUXT_I18N_MODULE_ID__: string
17+
declare let __SWITCH_LOCALE_PATH_LINK_IDENTIFIER__: string

src/runtime/components/SwitchLocalePathLink.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { SWITCH_LOCALE_PATH_LINK_IDENTIFIER } from '#build/i18n.options.mjs'
21
import { useSwitchLocalePath, type Locale } from '#i18n'
32
import { defineNuxtLink } from '#imports'
43
import { Comment, defineComponent, h } from 'vue'
@@ -21,9 +20,9 @@ export default defineComponent({
2120
const switchLocalePath = useSwitchLocalePath()
2221

2322
return () => [
24-
h(Comment, `${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-[${props.locale}]`),
23+
h(Comment, `${__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__}-[${props.locale}]`),
2524
h(NuxtLink, { ...attrs, to: encodeURI(switchLocalePath(props.locale)) }, slots.default),
26-
h(Comment, `/${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}`)
25+
h(Comment, `/${__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__}`)
2726
]
2827
}
2928
})

src/runtime/internal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isString } from '@intlify/shared'
22
import { useCookie, useNuxtApp, useRequestHeader, useRuntimeConfig } from '#imports'
3-
import { DEFAULT_COOKIE_KEY, isSSG, localeCodes, normalizedLocales } from '#build/i18n.options.mjs'
3+
import { localeCodes, normalizedLocales } from '#build/i18n.options.mjs'
44
import { findBrowserLocale, getRoutePathLocaleRegex } from '#i18n-kit/routing'
55
import { createLogger } from '#nuxt-i18n/logger'
66

@@ -43,7 +43,7 @@ export function getBrowserLocale(): string | undefined {
4343

4444
export function createI18nCookie() {
4545
const detect = (useRuntimeConfig().public.i18n as I18nPublicRuntimeConfig).detectBrowserLanguage
46-
const cookieKey = (detect && detect.cookieKey) || DEFAULT_COOKIE_KEY
46+
const cookieKey = (detect && detect.cookieKey) || __DEFAULT_COOKIE_KEY__
4747
const date = new Date()
4848
const cookieOptions: CookieOptions<string | null | undefined> & { readonly: false } = {
4949
path: '/',
@@ -128,7 +128,7 @@ export function detectBrowserLanguage(
128128
__DEBUG__ && logger.log({ firstAccess })
129129

130130
// detection ignored during nuxt generate
131-
if (isSSG && firstAccess && strategy === 'no_prefix' && import.meta.server) {
131+
if (__IS_SSG__ && firstAccess && strategy === 'no_prefix' && import.meta.server) {
132132
return { locale: '', error: 'detect_ignore_on_ssg' }
133133
}
134134

src/runtime/plugins/i18n.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { computed, isRef, ref, watch } from 'vue'
22
import { createI18n } from 'vue-i18n'
33
import { defineNuxtPlugin, useNuxtApp } from '#imports'
4-
import {
5-
localeCodes,
6-
vueI18nConfigs,
7-
hasPages,
8-
localeLoaders,
9-
parallelPlugin,
10-
normalizedLocales
11-
} from '#build/i18n.options.mjs'
4+
import { localeCodes, vueI18nConfigs, localeLoaders, normalizedLocales } from '#build/i18n.options.mjs'
125
import { loadVueI18nOptions, loadLocale } from '../messages'
136
import {
147
loadAndSetLocale,
@@ -33,7 +26,7 @@ import type { LocaleObject, I18nPublicRuntimeConfig, I18nHeadOptions } from '#in
3326

3427
export default defineNuxtPlugin({
3528
name: 'i18n:plugin',
36-
parallel: parallelPlugin,
29+
parallel: __PARALLEL_PLUGIN__,
3730
async setup() {
3831
const logger = /*#__PURE__*/ createLogger('plugin:i18n')
3932
const nuxt = useNuxtApp()
@@ -114,7 +107,7 @@ export default defineNuxtPlugin({
114107
composer.setLocale = async (locale: string) => {
115108
await loadAndSetLocale(locale, i18n.__firstAccess)
116109

117-
if (composer.strategy === 'no_prefix' || !hasPages) {
110+
if (composer.strategy === 'no_prefix' || !__HAS_PAGES__) {
118111
await composer.loadLocaleMessages(locale)
119112
i18n.__setLocale(locale)
120113
return

src/runtime/plugins/route-locale-detect.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { unref } from 'vue'
2-
import { hasPages } from '#build/i18n.options.mjs'
32
import { addRouteMiddleware, defineNuxtPlugin, defineNuxtRouteMiddleware, useNuxtApp } from '#imports'
43
import { createLogger } from '#nuxt-i18n/logger'
54
import { makeFallbackLocaleCodes } from '../messages'
@@ -41,26 +40,26 @@ export default defineNuxtPlugin({
4140
await handleRouteDetect(currentRoute.value)
4241

4342
// app has no pages - do not register route middleware
44-
if (!hasPages) {
45-
return
46-
}
47-
48-
const localeChangeMiddleware = defineNuxtRouteMiddleware(async (to, from) => {
49-
__DEBUG__ && logger.log('locale-changing middleware', to, from)
43+
if (!__HAS_PAGES__) return
5044

51-
const locale = await nuxt.runWithContext(() => handleRouteDetect(to))
45+
addRouteMiddleware(
46+
'locale-changing',
47+
defineNuxtRouteMiddleware(async (to, from) => {
48+
__DEBUG__ && logger.log('locale-changing middleware', to, from)
5249

53-
const redirectPath = await nuxt.runWithContext(() =>
54-
detectRedirect({ to, from, locale, routeLocale: nuxt._vueI18n.__localeFromRoute(to) }, true)
55-
)
50+
const locale = await nuxt.runWithContext(() => handleRouteDetect(to))
5651

57-
nuxt._vueI18n.__firstAccess = false
52+
const redirectPath = await nuxt.runWithContext(() =>
53+
detectRedirect({ to, from, locale, routeLocale: nuxt._vueI18n.__localeFromRoute(to) }, true)
54+
)
5855

59-
__DEBUG__ && logger.log('redirectPath on locale-changing middleware', redirectPath)
56+
nuxt._vueI18n.__firstAccess = false
6057

61-
return await nuxt.runWithContext(() => navigate({ nuxt, redirectPath, locale, route: to }))
62-
})
58+
__DEBUG__ && logger.log('redirectPath on locale-changing middleware', redirectPath)
6359

64-
addRouteMiddleware('locale-changing', localeChangeMiddleware, { global: true })
60+
return await nuxt.runWithContext(() => navigate({ nuxt, redirectPath, locale, route: to }))
61+
}),
62+
{ global: true }
63+
)
6564
}
6665
})

src/runtime/plugins/ssg-detect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { unref } from 'vue'
2-
import { isSSG } from '#build/i18n.options.mjs'
32
import { defineNuxtPlugin, useNuxtApp } from '#imports'
43
import { createLogger } from '#nuxt-i18n/logger'
54
import { detectBrowserLanguage } from '../internal'
@@ -10,7 +9,7 @@ export default defineNuxtPlugin({
109
enforce: 'post',
1110
setup() {
1211
const nuxt = useNuxtApp()
13-
if (!isSSG || nuxt.$i18n.strategy !== 'no_prefix' || !nuxt.$config.public.i18n.detectBrowserLanguage) return
12+
if (!__IS_SSG__ || nuxt.$i18n.strategy !== 'no_prefix' || !nuxt.$config.public.i18n.detectBrowserLanguage) return
1413

1514
const logger = /*#__PURE__*/ createLogger('plugin:i18n:ssg-detect')
1615
const localeCookie = nuxt.$i18n.getLocaleCookie()

src/runtime/plugins/switch-locale-path-ssr.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineNuxtPlugin, useNuxtApp } from '#imports'
22
import { useSwitchLocalePath } from '#i18n'
3-
import { SWITCH_LOCALE_PATH_LINK_IDENTIFIER } from '#build/i18n.options.mjs'
43

54
// Replace `SwitchLocalePathLink` href in rendered html for SSR support
65
export default defineNuxtPlugin({
@@ -10,9 +9,9 @@ export default defineNuxtPlugin({
109
const nuxt = useNuxtApp()
1110
const switchLocalePathLinkWrapperExpr = new RegExp(
1211
[
13-
`<!--${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-\\[(\\w+)\\]-->`,
12+
`<!--${__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__}-\\[(\\w+)\\]-->`,
1413
`.+?`,
15-
`<!--/${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-->`
14+
`<!--/${__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__}-->`
1615
].join(''),
1716
'g'
1817
)

0 commit comments

Comments
 (0)