From 3121b9f3c8f90628d9b86af2301cadbfd5a54e91 Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 15 Feb 2024 00:43:26 +0100 Subject: [PATCH] refactor: remove unused constants and options (#2787) --- src/constants.ts | 23 ++------------------- src/options.d.ts | 26 ++++++------------------ src/runtime/internal.ts | 6 +++--- src/runtime/routing/compatibles/head.ts | 4 ++-- src/runtime/routing/extends/i18n.ts | 3 +-- src/runtime/utils.ts | 18 +++++------------ src/template.ts | 27 ++----------------------- 7 files changed, 21 insertions(+), 86 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index bb64d07ff..060005d52 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -import type { NuxtI18nOptionsDefault } from './options' - export const NUXT_I18N_MODULE_ID = '@nuxtjs/i18n' as const export const VUE_I18N_PKG = 'vue-i18n' as const export const SHARED_PKG = '@intlify/shared' as const @@ -24,25 +22,8 @@ export const STRATEGIES = { NO_PREFIX: STRATEGY_NO_PREFIX } as const -export const DEFAULT_LOCALE = '' -export const DEFAULT_STRATEGY = STRATEGIES.PREFIX_EXCEPT_DEFAULT -export const DEFAULT_TRAILING_SLASH = false -export const DEFAULT_ROUTES_NAME_SEPARATOR = '___' -export const DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = 'default' -export const DEFAULT_DETECTION_DIRECTION = 'ltr' -export const DEFAULT_BASE_URL = '' export const DEFAULT_DYNAMIC_PARAMS_KEY = 'nuxtI18n' - -const REDIRECT_ON_ALL = 'all' -const REDIRECT_ON_ROOT = 'root' -const REDIRECT_ON_NO_PREFIX = 'no prefix' -export const REDIRECT_ON_OPTIONS = { - ALL: REDIRECT_ON_ALL, - ROOT: REDIRECT_ON_ROOT, - NO_PREFIX: REDIRECT_ON_NO_PREFIX -} as const - -export const COMPONENT_OPTIONS_KEY = 'nuxtI18n' +export const DEFAULT_COOKIE_KEY = 'i18n_redirected' export const DEFAULT_OPTIONS = { experimental: { @@ -78,7 +59,7 @@ export const DEFAULT_OPTIONS = { alwaysRedirect: false, cookieCrossOrigin: false, cookieDomain: null, - cookieKey: 'i18n_redirected', + cookieKey: DEFAULT_COOKIE_KEY, cookieSecure: false, fallbackLocale: '', redirectOn: 'root', diff --git a/src/options.d.ts b/src/options.d.ts index 36b347722..5a32bb6bc 100644 --- a/src/options.d.ts +++ b/src/options.d.ts @@ -3,8 +3,6 @@ import type { NuxtI18nOptions, VueI18nConfig } from './types' import type { NuxtI18nOptionsDefault } from './constants' import type { DeepRequired } from 'ts-essentials' -import * as constants from './constants' - export type * from './types' /** @@ -24,25 +22,13 @@ export const vueI18nConfigs: VueI18nConfig[] export const localeCodes: string[] = [] export const nuxtI18nOptions: DeepRequired> = {} -export const nuxtI18nOptionsDefault: NuxtI18nOptionsDefault = {} export const normalizedLocales: LocaleObject[] = [] -export const NUXT_I18N_MODULE_ID = '' export const isSSG = false export const parallelPlugin: boolean -export const STRATEGIES: typeof constants.STRATEGIES -export const DEFAULT_LOCALE: typeof constants.DEFAULT_LOCALE -export const DEFAULT_STRATEGY: (typeof STRATEGIES)[keyof typeof STRATEGIES] -export const DEFAULT_TRAILING_SLASH: typeof constants.DEFAULT_TRAILING_SLASH -export const DEFAULT_ROUTES_NAME_SEPARATOR: typeof constants.DEFAULT_ROUTES_NAME_SEPARATOR -export const DEFAULT_LOCALE_ROUTE_NAME_SUFFIX: typeof constants.DEFAULT_LOCALE_ROUTE_NAME_SUFFIX -export const DEFAULT_DETECTION_DIRECTION: typeof constants.DEFAULT_DETECTION_DIRECTION -export const DEFAULT_BASE_URL: typeof constants.DEFAULT_BASE_URL -export const DEFAULT_DYNAMIC_PARAMS_KEY: typeof constants.DEFAULT_DYNAMIC_PARAMS_KEY - -export { - NuxtI18nOptions, - NuxtI18nOptionsDefault, - DetectBrowserLanguageOptions, - RootRedirectOptions -} from './types' +export const NUXT_I18N_MODULE_ID = '' +export const DEFAULT_DYNAMIC_PARAMS_KEY: string +export const DEFAULT_COOKIE_KEY: string + +export { NuxtI18nOptionsDefault } +export { NuxtI18nOptions, DetectBrowserLanguageOptions, RootRedirectOptions } from './types' diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index 6a70c95c5..d6e8b6cbe 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -12,8 +12,8 @@ import { unref } from '#imports' import { - nuxtI18nOptionsDefault, NUXT_I18N_MODULE_ID, + DEFAULT_COOKIE_KEY, isSSG, localeCodes, nuxtI18nOptions, @@ -96,7 +96,7 @@ export function getBrowserLocale(): string | undefined { export function getI18nCookie() { const detect = nuxtI18nOptions.detectBrowserLanguage - const cookieKey = (detect && detect.cookieKey) || nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey + const cookieKey = (detect && detect.cookieKey) || DEFAULT_COOKIE_KEY const date = new Date() const cookieOptions: Record = { expires: new Date(date.setDate(date.getDate() + 365)), @@ -135,7 +135,7 @@ export function getLocaleCookie(cookieRef: CookieRef): strin } export function setLocaleCookie(cookieRef: CookieRef, locale: string) { - const { useCookie } = nuxtI18nOptions.detectBrowserLanguage || nuxtI18nOptionsDefault.detectBrowserLanguage + const useCookie = nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie if (!useCookie) { return diff --git a/src/runtime/routing/compatibles/head.ts b/src/runtime/routing/compatibles/head.ts index e0e4e6aeb..161f2863c 100644 --- a/src/runtime/routing/compatibles/head.ts +++ b/src/runtime/routing/compatibles/head.ts @@ -1,5 +1,5 @@ import { unref, useNuxtApp } from '#imports' -import { nuxtI18nOptions, STRATEGIES } from '#build/i18n.options.mjs' +import { nuxtI18nOptions } from '#build/i18n.options.mjs' import { getComposer, getLocale, getLocales, getNormalizedLocales } from '../utils' import { getRouteBaseName, localeRoute, switchLocalePath } from './routing' @@ -86,7 +86,7 @@ export function getHreflangLinks( const { defaultLocale, strategy } = nuxtI18nOptions const links: MetaAttrs[] = [] - if (strategy === STRATEGIES.NO_PREFIX) return links + if (strategy === 'no_prefix') return links const localeMap = new Map() for (const locale of locales) { diff --git a/src/runtime/routing/extends/i18n.ts b/src/runtime/routing/extends/i18n.ts index 4918c06a6..f2139bf5a 100644 --- a/src/runtime/routing/extends/i18n.ts +++ b/src/runtime/routing/extends/i18n.ts @@ -1,6 +1,5 @@ import { isObject, isFunction, assign } from '@intlify/shared' import { computed, effectScope, ref, watch } from '#imports' -import { DEFAULT_BASE_URL } from '#build/i18n.options.mjs' import { resolveBaseUrl, isVueI18n, getComposer, inBrowser } from '../utils' import { getRouteBaseName, @@ -71,7 +70,7 @@ export function extendI18n( { locales = [], localeCodes = [], - baseUrl = DEFAULT_BASE_URL, + baseUrl = '', hooks = {}, context = {} as Context }: VueI18nExtendOptions = {} diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 3e364ca8c..2ecf3a214 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -3,7 +3,6 @@ import { joinURL, isEqual } from 'ufo' import { isString, isFunction, isObject } from '@intlify/shared' import { navigateTo, useNuxtApp, useRouter, useRuntimeConfig, useState } from '#imports' import { - nuxtI18nOptionsDefault, NUXT_I18N_MODULE_ID, isSSG, localeLoaders, @@ -104,14 +103,10 @@ export async function loadAndSetLocale( i18n: I18n, initial: boolean = false ): Promise<[boolean, string]> { - const _differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains - const skipSettingLocaleOnNavigate = - nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate - const lazy = nuxtI18nOptions.lazy ?? nuxtI18nOptionsDefault.lazy - const useCookie = - (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie) ?? - nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie + const { differentDomains, skipSettingLocaleOnNavigate, lazy, detectBrowserLanguage } = nuxtI18nOptions + const useCookie = detectBrowserLanguage && detectBrowserLanguage.useCookie const nuxtApp = useNuxtApp() + let ret = false const oldLocale = getLocale(i18n) __DEBUG__ && console.log('setLocale: new -> ', newLocale, ' old -> ', oldLocale, ' initial -> ', initial) @@ -120,7 +115,7 @@ export async function loadAndSetLocale( } // abort if different domains option enabled - if (!initial && _differentDomains) { + if (!initial && differentDomains) { return [ret, oldLocale] } @@ -336,10 +331,7 @@ export async function navigate( args: NavigateArgs, { status = 302, enableNavigate = false }: { status?: number; enableNavigate?: boolean } = {} ) { - const rootRedirect = nuxtI18nOptions.rootRedirect ?? nuxtI18nOptionsDefault.rootRedirect - const differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains - const skipSettingLocaleOnNavigate = - nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate + const { rootRedirect, differentDomains, skipSettingLocaleOnNavigate } = nuxtI18nOptions const { nuxtApp, i18n, locale, route } = args let { redirectPath } = args diff --git a/src/template.ts b/src/template.ts index 42644f4d7..4353d0c4e 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,17 +1,5 @@ import { generateLoaderOptions } from './gen' -import { - DEFAULT_OPTIONS, - STRATEGIES, - DEFAULT_LOCALE, - DEFAULT_TRAILING_SLASH, - DEFAULT_ROUTES_NAME_SEPARATOR, - DEFAULT_BASE_URL, - DEFAULT_DETECTION_DIRECTION, - DEFAULT_DYNAMIC_PARAMS_KEY, - DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, - DEFAULT_STRATEGY, - NUXT_I18N_MODULE_ID -} from './constants' +import { DEFAULT_DYNAMIC_PARAMS_KEY, DEFAULT_COOKIE_KEY, NUXT_I18N_MODULE_ID } from './constants' import type { LocaleObject } from './types' export type TemplateNuxtI18nOptions = { @@ -48,24 +36,13 @@ export const vueI18nConfigs = [ export const nuxtI18nOptions = ${JSON.stringify(options.nuxtI18nOptions, null, 2)} -export const nuxtI18nOptionsDefault = ${JSON.stringify(DEFAULT_OPTIONS, null, 2)} - export const normalizedLocales = ${JSON.stringify(options.normalizedLocales, null, 2)} export const NUXT_I18N_MODULE_ID = "${NUXT_I18N_MODULE_ID}" export const parallelPlugin = ${options.parallelPlugin} export const isSSG = ${options.isSSG} -export const STRATEGIES = ${JSON.stringify(STRATEGIES, null, 2)} -export const DEFAULT_LOCALE = ${JSON.stringify(DEFAULT_LOCALE)} -export const DEFAULT_STRATEGY = ${JSON.stringify(DEFAULT_STRATEGY)} -export const DEFAULT_TRAILING_SLASH = ${JSON.stringify(DEFAULT_TRAILING_SLASH)} -export const DEFAULT_ROUTES_NAME_SEPARATOR = ${JSON.stringify(DEFAULT_ROUTES_NAME_SEPARATOR)} -export const DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = ${JSON.stringify(DEFAULT_LOCALE_ROUTE_NAME_SUFFIX)} -export const DEFAULT_DETECTION_DIRECTION = ${JSON.stringify(DEFAULT_DETECTION_DIRECTION)} -export const DEFAULT_BASE_URL = ${JSON.stringify(DEFAULT_BASE_URL)} export const DEFAULT_DYNAMIC_PARAMS_KEY = ${JSON.stringify(DEFAULT_DYNAMIC_PARAMS_KEY)} - - +export const DEFAULT_COOKIE_KEY = ${JSON.stringify(DEFAULT_COOKIE_KEY)} ` }