Skip to content

Commit

Permalink
fix: use generated tsconfig and virtual file augmentations (#3112)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Sep 20, 2024
1 parent 68c9ee9 commit 6296331
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Setup develoment
- name: Setup development
run: pnpm dev:prepare

- name: Linting
Expand Down
3 changes: 2 additions & 1 deletion .nuxtrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# enable TypeScript bundler module resolution - https://www.typescriptlang.org/docs/handbook/modules/reference.html#bundler
experimental.typescriptBundlerResolution=true

future.typescriptBundlerResolution=true
16 changes: 14 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ import eslintConfigPrettier from 'eslint-config-prettier'
export default [
// ignores
{
ignores: ['.nuxt', 'dist', 'playground', 'specs', 'test/fixtures', 'coverage', 'src/runtime/templates/**', 'docs']
ignores: [
'.nuxt',
'dist',
'playground',
'specs',
'test',
'coverage',
'docs',
// TODO: figure out how to get eslint to work in these files
// eslint parsing errors in these files, possibly due to generated tsconfig in .nuxt?
'src/runtime/server',
'src/runtime/composables/server.ts'
]
},

// for global and envrionment
// for global and environment
{
languageOptions: {
globals: {
Expand Down
76 changes: 76 additions & 0 deletions internals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
declare module '#build/i18n.options.mjs' {
import type { DeepRequired } from 'ts-essentials'
/**
* stub type definition for @nuxtjs/i18n internally
*/

type VueI18nConfig = import('./dist/types').VueI18nConfig
type NuxtI18nOptions = import('./dist/types').NuxtI18nOptions
type LocaleObject = import('./dist/types').LocaleObject

type LocaleLoader = {
key: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
load: () => Promise<any>
cache: boolean
}

export const localeLoaders: Record<string, LocaleLoader[]>

export const vueI18nConfigs: VueI18nConfig[]

export const localeCodes: string[]
export const nuxtI18nOptions: DeepRequired<NuxtI18nOptions<Context>>
export const normalizedLocales: LocaleObject[]
export const isSSG = false
export const parallelPlugin: boolean

export const NUXT_I18N_MODULE_ID = ''
export const DEFAULT_DYNAMIC_PARAMS_KEY: string
export const DEFAULT_COOKIE_KEY: string
export const SWITCH_LOCALE_PATH_LINK_IDENTIFIER: string
}

declare module '#internal/i18n/options.mjs' {
import type { DeepRequired } from 'ts-essentials'

type VueI18nConfig = import('./dist/types').VueI18nConfig
type NuxtI18nOptions = import('./dist/types').NuxtI18nOptions
type LocaleObject = import('./dist/types').LocaleObject

/**
* stub type definition for @nuxtjs/i18n internally
*/

type LocaleLoader = {
key: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
load: () => Promise<any>
cache: boolean
}

export const localeLoaders: Record<string, LocaleLoader[]>

export const vueI18nConfigs: VueI18nConfig[]

export const localeCodes: string[]
export const nuxtI18nOptions: DeepRequired<NuxtI18nOptions<Context>>
export const normalizedLocales: LocaleObject[]
export const isSSG = false
export const parallelPlugin: boolean

export const NUXT_I18N_MODULE_ID = ''
export const DEFAULT_DYNAMIC_PARAMS_KEY: string
export const DEFAULT_COOKIE_KEY: string
export const SWITCH_LOCALE_PATH_LINK_IDENTIFIER: string
}

declare module '#internal/i18n/locale.detector.mjs' {
export const localeDetector: import('./dist/runtime/composables/server').LocaleDetector
}

declare module 'virtual:nuxt-i18n-logger' {
import type { ConsolaInstance } from 'consola'

export function createLogger(label: string): ConsolaInstance
}
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"require": "./dist/module.cjs",
"types": "./dist/types.d.ts"
},
"./internals": {
"types": "./internals.d.ts"
},
"./package.json": "./package.json"
},
"imports": {
Expand All @@ -47,17 +50,18 @@
"types": "./dist/types.d.ts",
"type": "module",
"files": [
"dist"
"dist",
"internals.d.ts"
],
"scripts": {
"prepare": "git config --local core.hooksPath .githooks",
"build": "nuxi build-module .",
"stub": "nuxi build-module --stub .",
"build": "nuxt-module-build prepare && nuxt-module-build build .",
"stub": "nuxt-module-build build --stub .",
"prepack": "pnpm build",
"release": "bumpp --commit \"release: v%s\" --push --tag",
"changelog": "gh-changelogen --repo=nuxt-community/i18n-module",
"dev": "pnpm dev:prepare && pnpm build --sourcemap && nuxi dev playground",
"dev:prepare": "nuxi prepare",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare",
"dev:build": "nuxi build playground",
"dev:generate": "nuxi generate playground",
"dev:preview": "nuxi preview playground",
Expand All @@ -73,7 +77,7 @@
"lint:prettier": "prettier . --check",
"lint:eslint": "eslint .",
"test": "pnpm dev:prepare && run-s test:types test:unit test:spec",
"test:types": "tsc --noEmit --project tsconfig.test.json",
"test:types": "tsc --noEmit",
"test:unit": "vitest run test",
"test:spec": "vitest run specs"
},
Expand Down
2 changes: 1 addition & 1 deletion specs/basic_usage_compat_4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
waitForTransition,
waitForURL
} from './helper'
import { RouteLocation } from 'vue-router'
import type { RouteLocation } from 'vue-router'

describe('basic usage - compatibilityVersion: 4', async () => {
await setup({
Expand Down
5 changes: 0 additions & 5 deletions src/logger.d.ts

This file was deleted.

15 changes: 10 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
addTemplate,
addTypeTemplate,
addImports,
useLogger
useLogger,
createResolver
} from '@nuxt/kit'
import { resolve, relative } from 'pathe'
import { defu } from 'defu'
Expand All @@ -33,7 +34,7 @@ import {
getLocaleFiles,
filterLocales
} from './utils'
import { distDir, runtimeDir } from './dirs'
import { runtimeDir } from './dirs'
import { applyLayerOptions, checkLayerOptions, resolveLayerVueI18nConfigInfo } from './layers'
import { generateTemplateNuxtI18nOptions } from './template'
import { i18nVirtualLoggerPlugin, RESOLVED_VIRTUAL_NUXT_I18N_LOGGER, VIRTUAL_NUXT_I18N_LOGGER } from './virtual-logger'
Expand All @@ -59,6 +60,10 @@ export default defineNuxtModule<NuxtI18nOptions>({
async setup(i18nOptions, nuxt) {
const logger = useLogger(NUXT_I18N_MODULE_ID)

nuxt.hook('prepare:types', ({ references }) => {
references.push({ types: `${NUXT_I18N_MODULE_ID}/internals` })
})

const options = i18nOptions as Required<NuxtI18nOptions>
applyOptionOverrides(options, nuxt)
debug('options', options)
Expand Down Expand Up @@ -180,8 +185,9 @@ export default defineNuxtModule<NuxtI18nOptions>({
addPlugin(resolve(runtimeDir, 'plugins/i18n'))
addPlugin(resolve(runtimeDir, 'plugins/switch-locale-path-ssr'))

const resolver = createResolver(import.meta.url)
// for composables
nuxt.options.alias['#i18n'] = resolve(distDir, 'runtime/composables/index.js')
nuxt.options.alias['#i18n'] = resolver.resolve('./runtime/composables/index')
nuxt.options.build.transpile.push('#i18n')
nuxt.options.build.transpile.push(VIRTUAL_NUXT_I18N_LOGGER)

Expand All @@ -206,7 +212,6 @@ export default defineNuxtModule<NuxtI18nOptions>({
})
}

// @ts-expect-error type error
nuxt.options.runtimeConfig.public.i18n.locales = simplifyLocaleOptions(nuxt, defu({}, options))

addTemplate({
Expand Down Expand Up @@ -448,7 +453,7 @@ declare module '@nuxt/schema' {
['i18n']?: Partial<UserNuxtI18nOptions>
}
interface NuxtOptions {
['i18n']?: UserNuxtI18nOptions
['i18n']: UserNuxtI18nOptions
}
interface NuxtHooks extends ModuleHooks {}
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
Expand Down
32 changes: 0 additions & 32 deletions src/options.d.ts

This file was deleted.

6 changes: 3 additions & 3 deletions 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 '#build/i18n.options.mjs'
import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '../../types'
import type { HeadParam } from '../utils'

export * from 'vue-i18n'
Expand Down Expand Up @@ -246,7 +246,7 @@ export function useLocalePath(): LocalePathFunction {
* The parameter signature of this function is same as {@link localeRoute}.
*
* @param route - A route location. The path or name of the route or an object for more complex routes.
* @param locale - A locale optinal, if not specified, uses the current locale.
* @param locale - A locale optional, if not specified, uses the current locale.
*
* @returns the route object for a given route, the route object is resolved by vue-router rather than just a full route path.
*
Expand Down Expand Up @@ -305,7 +305,7 @@ export function useLocaleLocation(): LocaleLocationFunction {
}

/**
* The functin that swtich locale path.
* The function that switch locale path.
*
* @remarks
* The parameter signature of this function is same as {@link switchLocalePath}.
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type LocaleConfig = {
* @remarks
* That is set by `fallbackLocale` option that is loaded with `vueI18n` option (i18n.config). If these do not resolve the fallback locale, use as default `false
*/
fallbackLocale: FallbackLocale
fallbackLocale?: FallbackLocale
}

/**
Expand All @@ -35,7 +35,7 @@ export type LocaleDetector = (event: H3Event, config: LocaleConfig) => string
* Define locale detector for server-side locale detection
*
* @remarks
* The locale detector fucntion is used to detect the locale on server-side. It's called per request on the server.
* The locale detector function is used to detect the locale on server-side. It's called per request on the server.
*
* @param detector - The {@link LocaleDetector | locale detector}
*
Expand Down
13 changes: 7 additions & 6 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { initCommonComposableOptions, type CommonComposableOptions } from './uti
import { createLogger } from 'virtual:nuxt-i18n-logger'

import type { Locale } from 'vue-i18n'
import type { DetectBrowserLanguageOptions, LocaleObject } from '#build/i18n.options.mjs'
import type { DetectBrowserLanguageOptions, LocaleObject } from '../types'
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
import type { CookieRef, NuxtApp } from 'nuxt/app'
import type { ModulePublicRuntimeConfig } from '../module'
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
const config = runtimeConfig.public.i18n as ModulePublicRuntimeConfig['i18n']
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
opts: ModulePublicRuntimeConfig['i18n'] = useRuntimeConfig().public.i18n as ModulePublicRuntimeConfig['i18n']
) => {
if (opts?.detectBrowserLanguage === false) return false

Expand All @@ -385,8 +385,8 @@ export const runtimeDetectBrowserLanguage = (
* Removes default routes depending on domain
*/
export function setupMultiDomainLocales(nuxtContext: NuxtApp, defaultLocaleDomain: string) {
const { multiDomainLocales, strategy, routesNameSeparator, defaultLocaleRouteNameSuffix } =
nuxtContext.$config.public.i18n
const { multiDomainLocales, strategy, routesNameSeparator, defaultLocaleRouteNameSuffix } = nuxtContext.$config.public
.i18n as ModulePublicRuntimeConfig['i18n']

// feature disabled
if (!multiDomainLocales) return
Expand Down Expand Up @@ -419,7 +419,8 @@ 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
const { locales, defaultLocale, multiDomainLocales } = nuxtContext.$config.public
.i18n as ModulePublicRuntimeConfig['i18n']

let defaultLocaleDomain: string = defaultLocale || ''

Expand Down
Loading

0 comments on commit 6296331

Please sign in to comment.