Skip to content

Commit

Permalink
fix: move shared types to runtime (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Sep 21, 2024
1 parent eb31e71 commit 00638ab
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 535 deletions.
18 changes: 8 additions & 10 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ export const UTILS_PKG = '@intlify/utils'
export const UTILS_H3_PKG = '@intlify/utils/h3'
export const UFO_PKG = 'ufo'
export const IS_HTTPS_PKG = 'is-https'
import {
STRATEGIES,
STRATEGY_PREFIX,
STRATEGY_PREFIX_EXCEPT_DEFAULT,
STRATEGY_PREFIX_AND_DEFAULT,
STRATEGY_NO_PREFIX
} from './runtime/shared-types'

// Options
const STRATEGY_PREFIX = 'prefix'
const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default'
const STRATEGY_PREFIX_AND_DEFAULT = 'prefix_and_default'
const STRATEGY_NO_PREFIX = 'no_prefix'
export const STRATEGIES = {
PREFIX: STRATEGY_PREFIX,
PREFIX_EXCEPT_DEFAULT: STRATEGY_PREFIX_EXCEPT_DEFAULT,
PREFIX_AND_DEFAULT: STRATEGY_PREFIX_AND_DEFAULT,
NO_PREFIX: STRATEGY_NO_PREFIX
} as const
export { STRATEGIES, STRATEGY_PREFIX, STRATEGY_PREFIX_EXCEPT_DEFAULT, STRATEGY_PREFIX_AND_DEFAULT, STRATEGY_NO_PREFIX }

export const DEFAULT_DYNAMIC_PARAMS_KEY = 'nuxtI18nInternal'
export const DEFAULT_COOKIE_KEY = 'i18n_redirected'
Expand Down
88 changes: 2 additions & 86 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setupNitro } from './nitro'
import { extendBundler } from './bundler'
import { NUXT_I18N_MODULE_ID, DEFAULT_OPTIONS } from './constants'
import type { HookResult } from '@nuxt/schema'
import type { LocaleObject, NuxtI18nOptions } from './types'
import type { I18nPublicRuntimeConfig, LocaleObject, NuxtI18nOptions } from './types'
import type { Locale } from 'vue-i18n'
import { createContext } from './context'
import { prepareOptions } from './prepare/options'
Expand Down Expand Up @@ -116,91 +116,7 @@ type UserNuxtI18nOptions = Omit<NuxtI18nOptions, 'locales'> & { locales?: string
export interface ModuleOptions extends UserNuxtI18nOptions {}

export interface ModulePublicRuntimeConfig {
i18n: {
baseUrl: NuxtI18nOptions['baseUrl']
rootRedirect: NuxtI18nOptions['rootRedirect']
multiDomainLocales?: NuxtI18nOptions['multiDomainLocales']

/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
domainLocales: { [key: Locale]: { domain: string | undefined } }

/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
experimental: NonNullable<NuxtI18nOptions['experimental']>
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
locales: NonNullable<Required<NuxtI18nOptions<unknown>>['locales']>
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
differentDomains: Required<NuxtI18nOptions>['differentDomains']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
skipSettingLocaleOnNavigate: Required<NuxtI18nOptions>['skipSettingLocaleOnNavigate']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
defaultLocale: Required<NuxtI18nOptions>['defaultLocale']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
lazy: Required<NuxtI18nOptions>['lazy']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
defaultDirection: Required<NuxtI18nOptions>['defaultDirection']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
detectBrowserLanguage: Required<NuxtI18nOptions>['detectBrowserLanguage']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
strategy: Required<NuxtI18nOptions>['strategy']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
routesNameSeparator: Required<NuxtI18nOptions>['routesNameSeparator']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
defaultLocaleRouteNameSuffix: Required<NuxtI18nOptions>['defaultLocaleRouteNameSuffix']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
trailingSlash: Required<NuxtI18nOptions>['trailingSlash']
}
i18n: I18nPublicRuntimeConfig
}
export interface ModuleHooks {
'i18n:registerModule': (
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getLocale, getLocales, getComposer } from '../compatibility'
import type { Ref } from 'vue'
import type { Locale } from 'vue-i18n'
import type { RouteLocation, RouteLocationNormalizedLoaded, RouteLocationRaw, Router } from 'vue-router'
import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '../../types'
import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '../shared-types'
import type { HeadParam } from '../utils'

export * from 'vue-i18n'
Expand Down
13 changes: 6 additions & 7 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { initCommonComposableOptions, type CommonComposableOptions } from './uti
import { createLogger } from 'virtual:nuxt-i18n-logger'

import type { Locale } from 'vue-i18n'
import type { DetectBrowserLanguageOptions, LocaleObject } from '../types'
import type { DetectBrowserLanguageOptions, LocaleObject } from './shared-types'
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
import type { CookieRef, NuxtApp } from 'nuxt/app'
import type { ModulePublicRuntimeConfig } from '../module'
import type { I18nPublicRuntimeConfig } from './shared-types'

export function formatMessage(message: string) {
return NUXT_I18N_MODULE_ID + ' ' + message
Expand Down Expand Up @@ -350,7 +350,7 @@ export function getDomainFromLocale(localeCode: Locale): string | undefined {
const nuxtApp = useNuxtApp()
const host = getHost()
// lookup the `differentDomain` origin associated with given locale.
const config = runtimeConfig.public.i18n as ModulePublicRuntimeConfig['i18n']
const config = runtimeConfig.public.i18n as I18nPublicRuntimeConfig
const lang = normalizedLocales.find(locale => locale.code === localeCode)
const domain = config?.domainLocales?.[localeCode]?.domain || lang?.domain || lang?.domains?.find(v => v === host)

Expand All @@ -374,7 +374,7 @@ export function getDomainFromLocale(localeCode: Locale): string | undefined {
}

export const runtimeDetectBrowserLanguage = (
opts: ModulePublicRuntimeConfig['i18n'] = useRuntimeConfig().public.i18n as ModulePublicRuntimeConfig['i18n']
opts: I18nPublicRuntimeConfig = useRuntimeConfig().public.i18n as I18nPublicRuntimeConfig
) => {
if (opts?.detectBrowserLanguage === false) return false

Expand All @@ -386,7 +386,7 @@ export const runtimeDetectBrowserLanguage = (
*/
export function setupMultiDomainLocales(nuxtContext: NuxtApp, defaultLocaleDomain: string) {
const { multiDomainLocales, strategy, routesNameSeparator, defaultLocaleRouteNameSuffix } = nuxtContext.$config.public
.i18n as ModulePublicRuntimeConfig['i18n']
.i18n as I18nPublicRuntimeConfig

// feature disabled
if (!multiDomainLocales) return
Expand Down Expand Up @@ -419,8 +419,7 @@ export function setupMultiDomainLocales(nuxtContext: NuxtApp, defaultLocaleDomai
* Returns default locale for the current domain, returns `defaultLocale` by default
*/
export function getDefaultLocaleForDomain(nuxtContext: NuxtApp) {
const { locales, defaultLocale, multiDomainLocales } = nuxtContext.$config.public
.i18n as ModulePublicRuntimeConfig['i18n']
const { locales, defaultLocale, multiDomainLocales } = nuxtContext.$config.public.i18n as I18nPublicRuntimeConfig

let defaultLocaleDomain: string = defaultLocale || ''

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createLogger } from 'virtual:nuxt-i18n-logger'
import type { I18nOptions, Locale, FallbackLocale, LocaleMessages, DefineLocaleMessage } from 'vue-i18n'
import type { NuxtApp } from '#app'
import type { DeepRequired } from 'ts-essentials'
import type { VueI18nConfig, NuxtI18nOptions } from '../types'
import type { VueI18nConfig, NuxtI18nOptions } from './shared-types'
import type { CoreContext } from '@intlify/h3'

type MessageLoaderFunction<T = DefineLocaleMessage> = (locale: Locale) => Promise<LocaleMessages<T>>
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { createLogger } from 'virtual:nuxt-i18n-logger'
import type { NuxtI18nPluginInjections } from '../injections'
import type { Locale, I18nOptions } from 'vue-i18n'
import type { NuxtApp } from '#app'
import type { LocaleObject } from '../../types'
import type { ModulePublicRuntimeConfig } from '../../module'
import type { LocaleObject } from '../shared-types'
import type { I18nPublicRuntimeConfig } from '../shared-types'

// from https://github.com/nuxt/nuxt/blob/2466af53b0331cdb8b17c2c3b08675c5985deaf3/packages/nuxt/src/core/templates.ts#L152
type Decorate<T extends Record<string, unknown>> = { [K in keyof T as K extends string ? `$${K}` : never]: T[K] }
Expand Down Expand Up @@ -59,7 +59,7 @@ export default defineNuxtPlugin<NuxtI18nPluginInjections>({

// Fresh copy per request to prevent reusing mutated options
const runtimeI18n = {
...(nuxtContext.$config.public.i18n as ModulePublicRuntimeConfig['i18n']),
...(nuxtContext.$config.public.i18n as I18nPublicRuntimeConfig),
defaultLocale: defaultLocaleDomain
}
// @ts-expect-error type incompatible
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/routing/compatibles/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { getRouteBaseName, localeRoute, switchLocalePath } from './routing'
import { getComposer, getLocale, getLocales } from '../../compatibility'

import type { I18n } from 'vue-i18n'
import type { I18nHeadMetaInfo, MetaAttrs, LocaleObject, I18nHeadOptions } from '../../../types'
import type { I18nHeadMetaInfo, MetaAttrs, LocaleObject, I18nHeadOptions } from '../../shared-types'
import type { CommonComposableOptions } from '../../utils'

/**
* Returns localized head properties for locale-related aspects.
*
* @param common - Common options used internally by composable functions.
* @param options - An options, see about details {@link I18nHeadOptions}.
*
* @returns The localized {@link I18nHeadMetaInfo | head properties}.
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/routing/compatibles/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { resolve, routeToObject } from './utils'
import { getLocaleRouteName, getRouteName } from '../utils'
import { extendPrefixable, extendSwitchLocalePathIntercepter, type CommonComposableOptions } from '../../utils'

import type { Strategies, PrefixableOptions, SwitchLocalePathIntercepter } from '../../../types'
import type { Strategies, PrefixableOptions, SwitchLocalePathIntercepter } from '../../shared-types'
import type { Locale } from 'vue-i18n'
import type {
RouteLocation,
Expand All @@ -20,7 +20,7 @@ import type {
RouteLocationNormalizedLoaded,
RouteLocationNormalized
} from 'vue-router'
import type { ModulePublicRuntimeConfig } from '../../../module'
import type { I18nPublicRuntimeConfig } from '../../shared-types'

const RESOLVED_PREFIXED = new Set<Strategies>(['prefix_and_default', 'prefix_except_default'])

Expand Down Expand Up @@ -114,6 +114,7 @@ export function localeRoute(
* @remarks
* If `locale` is not specified, uses current locale.
*
* @param common - Common options used internally by composable functions.
* @param route - A route.
* @param locale - A locale, optional.
*
Expand All @@ -133,8 +134,7 @@ export function localeLocation(
export function resolveRoute(common: CommonComposableOptions, route: RouteLocationRaw, locale: Locale | undefined) {
const { router, i18n } = common
const _locale = locale || getLocale(i18n)
const { defaultLocale, strategy, trailingSlash } = common.runtimeConfig.public
.i18n as ModulePublicRuntimeConfig['i18n']
const { defaultLocale, strategy, trailingSlash } = common.runtimeConfig.public.i18n as I18nPublicRuntimeConfig
const prefixable = extendPrefixable(common.runtimeConfig)
// if route parameter is a string, check if it's a path or name of route.
let _route: RouteLocationPathRaw | RouteLocationNamedRaw
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/routing/compatibles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assign } from '@intlify/shared'

import type { Locale } from 'vue-i18n'
import type { RouteLocationNormalizedLoaded, RouteLocationPathRaw } from 'vue-router'
import type { Strategies } from '../../../types'
import type { Strategies } from '../../shared-types'
import type { CommonComposableOptions } from '../../utils'

function split(str: string, index: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/routing/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isString, isSymbol, isFunction } from '@intlify/shared'

import type { LocaleObject, Strategies, BaseUrlResolveHandler } from '../../types'
import type { LocaleObject, Strategies, BaseUrlResolveHandler } from '../shared-types'
import type { Locale } from 'vue-i18n'

export const inBrowser = typeof window !== 'undefined'
Expand Down
Loading

0 comments on commit 00638ab

Please sign in to comment.