Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor module #3117

Merged
merged 4 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import {
} from './constants'

import type { Nuxt } from '@nuxt/schema'
import type { NuxtI18nOptions } from './types'
import type { I18nNuxtContext } from './context'

const debug = createDebug('@nuxtjs/i18n:alias')

export async function setupAlias(nuxt: Nuxt, options: NuxtI18nOptions) {
export async function setupAlias({ userOptions: options, isDev, isPrepare }: I18nNuxtContext, nuxt: Nuxt) {
const runtimeOnly = options.bundle?.runtimeOnly
const modules: Record<string, string> = {}

modules[VUE_I18N_PKG] =
nuxt.options.dev || nuxt.options._prepare
isDev || isPrepare
? `${VUE_I18N_PKG}/dist/vue-i18n.mjs`
: `${VUE_I18N_PKG}/dist/vue-i18n${runtimeOnly ? '.runtime' : ''}.mjs`
modules[SHARED_PKG] = `${SHARED_PKG}/dist/shared.mjs`
Expand Down
4 changes: 2 additions & 2 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { getLayerLangPaths } from './layers'

import type { Nuxt } from '@nuxt/schema'
import type { PluginOptions } from '@intlify/unplugin-vue-i18n'
import type { NuxtI18nOptions } from './types'
import type { BundlerPluginOptions } from './transform/utils'
import type { I18nNuxtContext } from './context'

const debug = createDebug('@nuxtjs/i18n:bundler')

export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required<NuxtI18nOptions>) {
export async function extendBundler({ options: nuxtOptions }: I18nNuxtContext, nuxt: Nuxt) {
const langPaths = getLayerLangPaths(nuxt)
debug('langPaths -', langPaths)
const i18nModulePaths =
Expand Down
51 changes: 51 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Resolver } from '@nuxt/kit'
import type { LocaleInfo, LocaleObject, NuxtI18nOptions, VueI18nConfigPathInfo } from './types'
import type { Nuxt } from '@nuxt/schema'
import { createResolver, useLogger } from '@nuxt/kit'
import { NUXT_I18N_MODULE_ID } from './constants'
import createDebug from 'debug'

export interface I18nNuxtContext {
resolver: Resolver
logger: ReturnType<(typeof import('@nuxt/kit'))['useLogger']>
debug: ReturnType<typeof import('debug')>
userOptions: NuxtI18nOptions
options: Required<NuxtI18nOptions>
isDev: boolean
isSSR: boolean
isPrepare: boolean
isSSG: boolean
isBuild: boolean
isTest: boolean
genTemplate: (isServer: boolean, lazy?: boolean) => string
normalizedLocales: LocaleObject<string>[]
localeCodes: string[]
localeInfo: LocaleInfo[]
vueI18nConfigPaths: Required<VueI18nConfigPathInfo>[]
}

const debug = createDebug('@nuxtjs/i18n:context')
const resolver = createResolver(import.meta.url)

export function createContext(userOptions: NuxtI18nOptions, nuxt: Nuxt): I18nNuxtContext {
const options = userOptions as Required<NuxtI18nOptions>

return {
resolver,
logger: useLogger(NUXT_I18N_MODULE_ID),
debug,
userOptions,
options,
isDev: nuxt.options.dev,
isSSR: nuxt.options.ssr,
isPrepare: nuxt.options._prepare,
isSSG: nuxt.options._generate,
isBuild: nuxt.options._build,
isTest: nuxt.options.test,
genTemplate: undefined!,
normalizedLocales: undefined!,
localeCodes: undefined!,
localeInfo: undefined!,
vueI18nConfigPaths: undefined!
}
}
Loading