Skip to content

Commit

Permalink
chore: refactor module (#3117)
Browse files Browse the repository at this point in the history
* chore: refactor module

* chore: module only with context reference

* chore: fix build

* chore: move all prepare modules to prepare folder
  • Loading branch information
userquin authored Sep 21, 2024
1 parent 17fe394 commit 3f9e018
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 266 deletions.
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

0 comments on commit 3f9e018

Please sign in to comment.