Skip to content

Commit

Permalink
refactor: remove unused constants and options (nuxt-modules#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Feb 14, 2024
1 parent 933a092 commit 3121b9f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 86 deletions.
23 changes: 2 additions & 21 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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: {
Expand Down Expand Up @@ -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',
Expand Down
26 changes: 6 additions & 20 deletions src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -24,25 +22,13 @@ export const vueI18nConfigs: VueI18nConfig[]

export const localeCodes: string[] = []
export const nuxtI18nOptions: DeepRequired<NuxtI18nOptions<Context>> = {}
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'
6 changes: 3 additions & 3 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
unref
} from '#imports'
import {
nuxtI18nOptionsDefault,
NUXT_I18N_MODULE_ID,
DEFAULT_COOKIE_KEY,
isSSG,
localeCodes,
nuxtI18nOptions,
Expand Down Expand Up @@ -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<string, any> = {
expires: new Date(date.setDate(date.getDate() + 365)),
Expand Down Expand Up @@ -135,7 +135,7 @@ export function getLocaleCookie(cookieRef: CookieRef<string | undefined>): strin
}

export function setLocaleCookie(cookieRef: CookieRef<string | undefined>, locale: string) {
const { useCookie } = nuxtI18nOptions.detectBrowserLanguage || nuxtI18nOptionsDefault.detectBrowserLanguage
const useCookie = nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie

if (!useCookie) {
return
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/routing/compatibles/head.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<string, LocaleObject>()
for (const locale of locales) {
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/routing/extends/i18n.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -71,7 +70,7 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
{
locales = [],
localeCodes = [],
baseUrl = DEFAULT_BASE_URL,
baseUrl = '',
hooks = {},
context = {} as Context
}: VueI18nExtendOptions<Context> = {}
Expand Down
18 changes: 5 additions & 13 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -120,7 +115,7 @@ export async function loadAndSetLocale(
}

// abort if different domains option enabled
if (!initial && _differentDomains) {
if (!initial && differentDomains) {
return [ret, oldLocale]
}

Expand Down Expand Up @@ -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

Expand Down
27 changes: 2 additions & 25 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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)}
`
}

0 comments on commit 3121b9f

Please sign in to comment.