From d79a9c2ee254ec52e421508f1189a8dd71883e63 Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 14 Nov 2024 14:01:56 +0100 Subject: [PATCH] fix: alias shared types for runtime --- src/constants.ts | 20 +- src/prepare/runtime.ts | 2 + src/runtime/composables/index.ts | 4 +- src/runtime/internal.ts | 4 +- src/runtime/messages.ts | 2 +- src/runtime/plugins/i18n.ts | 5 +- src/runtime/routing/compatibles/head.ts | 2 +- src/runtime/routing/compatibles/routing.ts | 4 +- src/runtime/routing/compatibles/utils.ts | 2 +- src/runtime/routing/utils.ts | 2 +- src/runtime/types.ts | 2 +- src/runtime/utils.ts | 4 +- src/types.ts | 528 ++++++++++++++++++++- 13 files changed, 554 insertions(+), 27 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index f89004601..862df771a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -9,16 +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 -export { STRATEGIES, STRATEGY_PREFIX, STRATEGY_PREFIX_EXCEPT_DEFAULT, STRATEGY_PREFIX_AND_DEFAULT, STRATEGY_NO_PREFIX } +export const STRATEGY_PREFIX = 'prefix' +export const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default' +export const STRATEGY_PREFIX_AND_DEFAULT = 'prefix_and_default' +export 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 const DEFAULT_DYNAMIC_PARAMS_KEY = 'nuxtI18nInternal' export const DEFAULT_COOKIE_KEY = 'i18n_redirected' diff --git a/src/prepare/runtime.ts b/src/prepare/runtime.ts index 497dfd990..e359db96d 100644 --- a/src/prepare/runtime.ts +++ b/src/prepare/runtime.ts @@ -15,7 +15,9 @@ export function prepareRuntime(ctx: I18nNuxtContext, nuxt: Nuxt) { // for composables nuxt.options.alias['#i18n'] = resolver.resolve('./runtime/composables/index') + nuxt.options.alias['#i18n-shared-types'] = resolver.resolve('./types') nuxt.options.build.transpile.push('#i18n') + nuxt.options.build.transpile.push('#i18n-shared-types') nuxt.options.build.transpile.push(VIRTUAL_NUXT_I18N_LOGGER) const genTemplate = (isServer: boolean, lazy?: boolean) => { diff --git a/src/runtime/composables/index.ts b/src/runtime/composables/index.ts index 29dd9a670..0cb9a32e4 100644 --- a/src/runtime/composables/index.ts +++ b/src/runtime/composables/index.ts @@ -24,7 +24,7 @@ import { getLocale, getLocales, getComposer } from '../compatibility' import type { Ref } from 'vue' import type { Locale } from 'vue-i18n' -import type { I18nHeadMetaInfo, I18nHeadOptions, LocaleObject, SeoAttributesOptions } from '../shared-types' +import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '#i18n-shared-types' import type { RouteLocationAsRelativeI18n, RouteLocationResolvedI18n, RouteMapI18n } from 'vue-router' import type { HeadParam } from '../utils' @@ -392,7 +392,7 @@ export function useBrowserLocale(): string | null { const headers = useRequestHeaders(['accept-language']) return ( findBrowserLocale( - normalizedLocales as LocaleObject[], + normalizedLocales, import.meta.client ? (navigator.languages as string[]) : parseAcceptLanguage(headers['accept-language'] || '') ) || null ) diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index 20203c240..f6d3fe64a 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -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 './shared-types' +import type { DetectBrowserLanguageOptions, LocaleObject } from '#i18n-shared-types' import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router' import type { CookieRef, NuxtApp } from 'nuxt/app' -import type { I18nPublicRuntimeConfig } from './shared-types' +import type { I18nPublicRuntimeConfig } from '#i18n-shared-types' export function formatMessage(message: string) { return NUXT_I18N_MODULE_ID + ' ' + message diff --git a/src/runtime/messages.ts b/src/runtime/messages.ts index 6de0b496b..009566e1a 100644 --- a/src/runtime/messages.ts +++ b/src/runtime/messages.ts @@ -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 './shared-types' +import type { VueI18nConfig, NuxtI18nOptions } from '#i18n-shared-types' import type { CoreContext } from '@intlify/h3' type MessageLoaderFunction = (locale: Locale) => Promise> diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts index 54a3f6921..6b37dc503 100644 --- a/src/runtime/plugins/i18n.ts +++ b/src/runtime/plugins/i18n.ts @@ -30,8 +30,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 '../shared-types' -import type { I18nPublicRuntimeConfig } from '../shared-types' +import type { LocaleObject } from '#i18n-shared-types' +import type { I18nPublicRuntimeConfig } from '#i18n-shared-types' // from https://github.com/nuxt/nuxt/blob/2466af53b0331cdb8b17c2c3b08675c5985deaf3/packages/nuxt/src/core/templates.ts#L152 type Decorate> = { [K in keyof T as K extends string ? `$${K}` : never]: T[K] } @@ -153,7 +153,6 @@ export default defineNuxtPlugin({ const _localeCodes = ref(localeCodes) const _baseUrl = ref('') - // @ts-expect-error type mismatch composer.locales = computed(() => _locales.value) composer.localeCodes = computed(() => _localeCodes.value) composer.baseUrl = computed(() => _baseUrl.value) diff --git a/src/runtime/routing/compatibles/head.ts b/src/runtime/routing/compatibles/head.ts index 94193c773..a1c5643d2 100644 --- a/src/runtime/routing/compatibles/head.ts +++ b/src/runtime/routing/compatibles/head.ts @@ -7,7 +7,7 @@ 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 '../../shared-types' +import type { I18nHeadMetaInfo, MetaAttrs, LocaleObject, I18nHeadOptions } from '#i18n-shared-types' import type { CommonComposableOptions } from '../../utils' /** diff --git a/src/runtime/routing/compatibles/routing.ts b/src/runtime/routing/compatibles/routing.ts index c15b150c7..0bf67bdfa 100644 --- a/src/runtime/routing/compatibles/routing.ts +++ b/src/runtime/routing/compatibles/routing.ts @@ -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 } from '../../shared-types' +import type { Strategies, PrefixableOptions } from '#i18n-shared-types' import type { Locale } from 'vue-i18n' import type { RouteLocation, @@ -20,7 +20,7 @@ import type { RouteLocationNormalizedLoaded, RouteLocationNormalized } from 'vue-router' -import type { I18nPublicRuntimeConfig } from '../../shared-types' +import type { I18nPublicRuntimeConfig } from '#i18n-shared-types' const RESOLVED_PREFIXED = new Set(['prefix_and_default', 'prefix_except_default']) diff --git a/src/runtime/routing/compatibles/utils.ts b/src/runtime/routing/compatibles/utils.ts index 8e6433f1c..819add434 100644 --- a/src/runtime/routing/compatibles/utils.ts +++ b/src/runtime/routing/compatibles/utils.ts @@ -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 '../../shared-types' +import type { Strategies } from '#i18n-shared-types' import type { CommonComposableOptions } from '../../utils' function split(str: string, index: number) { diff --git a/src/runtime/routing/utils.ts b/src/runtime/routing/utils.ts index 9e1daa03a..5f3ca40e6 100644 --- a/src/runtime/routing/utils.ts +++ b/src/runtime/routing/utils.ts @@ -1,6 +1,6 @@ import { isString, isSymbol, isFunction } from '@intlify/shared' -import type { LocaleObject, Strategies, BaseUrlResolveHandler } from '../shared-types' +import type { LocaleObject, Strategies, BaseUrlResolveHandler } from '#i18n-shared-types' import type { Locale } from 'vue-i18n' export const inBrowser = typeof window !== 'undefined' diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 7986b1ad6..ce6e893f8 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -1,6 +1,6 @@ import type { NuxtApp } from '#app' import type { ComputedRef } from 'vue' -import type { Directions, LocaleObject, Strategies } from './shared-types' +import type { Directions, LocaleObject, Strategies } from '#i18n-shared-types' import type { Locale } from 'vue-i18n' /** diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index b8b274e7d..81e8995dc 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -44,7 +44,7 @@ import type { DetectLocaleContext } from './internal' import type { HeadSafe } from '@unhead/vue' import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router' import type { RuntimeConfig } from 'nuxt/schema' -import type { I18nPublicRuntimeConfig } from './shared-types' +import type { I18nPublicRuntimeConfig } from '#i18n-shared-types' import type { RootRedirectOptions, PrefixableOptions, @@ -52,7 +52,7 @@ import type { BaseUrlResolveHandler, Strategies, LocaleObject -} from './shared-types' +} from '#i18n-shared-types' /** * Common options used internally by composable functions, these diff --git a/src/types.ts b/src/types.ts index 5c5e67722..ebc7ae955 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1 +1,527 @@ -export * from './runtime/shared-types' +import type { Locale, I18nOptions } from 'vue-i18n' +import type { ParsedPath } from 'path' +import type { PluginOptions } from '@intlify/unplugin-vue-i18n' +import type { NuxtPage } from '@nuxt/schema' +import type { RouteMapGeneric, RouteMapI18n } from 'vue-router' +import { STRATEGIES } from './constants' +export type { + STRATEGY_NO_PREFIX, + STRATEGY_PREFIX, + STRATEGY_PREFIX_AND_DEFAULT, + STRATEGY_PREFIX_EXCEPT_DEFAULT, + STRATEGIES +} from './constants' + +// Options +// export const STRATEGY_PREFIX = 'prefix' +// export const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default' +// export const STRATEGY_PREFIX_AND_DEFAULT = 'prefix_and_default' +// export 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 type RedirectOnOptions = 'all' | 'root' | 'no prefix' + +export interface DetectBrowserLanguageOptions { + alwaysRedirect?: boolean + cookieCrossOrigin?: boolean + cookieDomain?: string | null + cookieKey?: string + cookieSecure?: boolean + fallbackLocale?: Locale | null + redirectOn?: RedirectOnOptions + useCookie?: boolean +} + +export type LocaleType = 'static' | 'dynamic' | 'unknown' + +export type LocaleFile = { path: string; cache?: boolean } + +export type LocaleInfo = { + /** + * NOTE: + * The following fields are for `file` in the nuxt i18n module `locales` option + */ + path?: string // abolute path + hash?: string + type?: LocaleType + /** + * NOTE: + * The following fields are for `files` (excludes nuxt layers) in the nuxt i18n module `locales` option. + */ + paths?: string[] + hashes?: string[] + types?: LocaleType[] +} & Omit & { + code: Locale + files: LocaleFile[] + meta?: (FileMeta & { file: LocaleFile })[] + } + +export type FileMeta = { + path: string + loadPath: string + hash: string + type: LocaleType + parsed: ParsedPath + key: string +} + +export type VueI18nConfigPathInfo = { + relative?: string + absolute?: string + hash?: string + type?: LocaleType + rootDir: string + relativeBase: string + meta: FileMeta +} + +export interface RootRedirectOptions { + path: string + statusCode: number +} + +type RouteLocationAsStringTypedListI18n = { + [N in keyof T]?: Partial> | false +} +export type CustomRoutePages = RouteLocationAsStringTypedListI18n + +export interface ExperimentalFeatures { + localeDetector?: string + switchLocalePathLinkSSR?: boolean + /** + * Automatically imports/initializes `$t`, `$rt`, `$d`, `$n`, `$tm` and `$te` functions in `