Skip to content

Commit

Permalink
fix: not reoslve vue-i18n module (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 12, 2022
1 parent 4844368 commit 226e24b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
72 changes: 30 additions & 42 deletions src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createDebug from 'debug'
import { resolvePath } from '@nuxt/kit'
import { resolveVueI18nPkgPath, pkgModulesDir } from './dirs'
import { pkgModulesDir } from './dirs'
import { resolve } from 'pathe'
import { VUE_I18N_PKG, VUE_I18N_BRIDGE_PKG, VUE_ROUTER_BRIDGE_PKG, VUE_I18N_ROUTING_PKG } from './constants'
import { tryResolve, getPackageManagerType } from './utils'
Expand All @@ -15,7 +15,7 @@ export async function setupAlias(nuxt: Nuxt) {
debug('setupAlias: pkgMgr', pkgMgr)

// resolve vue-i18@v9
nuxt.options.alias[VUE_I18N_PKG] = await resolveVueI18nAlias(nuxt)
nuxt.options.alias[VUE_I18N_PKG] = await resolveVueI18nAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(VUE_I18N_PKG)
debug('vue-i18n alias', nuxt.options.alias[VUE_I18N_PKG])

Expand All @@ -25,55 +25,51 @@ export async function setupAlias(nuxt: Nuxt) {
debug('@intlify/shared alias', nuxt.options.alias['@intlify/shared'])

// resolve @intlify/vue-router-bridge
nuxt.options.alias[VUE_ROUTER_BRIDGE_PKG] = await resolveVueRouterBridgeAlias(
pkgModulesDir,
nuxt.options.rootDir,
nuxt.options.workspaceDir,
pkgMgr
)
nuxt.options.alias[VUE_ROUTER_BRIDGE_PKG] = await resolveVueRouterBridgeAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(VUE_ROUTER_BRIDGE_PKG)
debug('@intlify/vue-router-bridge alias', nuxt.options.alias[VUE_ROUTER_BRIDGE_PKG])

// resolve @intlify/vue-i18n-bridge
nuxt.options.alias[VUE_I18N_BRIDGE_PKG] = await resolveVueI18nBridgeAlias(
pkgModulesDir,
nuxt.options.rootDir,
nuxt.options.workspaceDir,
pkgMgr
)
nuxt.options.alias[VUE_I18N_BRIDGE_PKG] = await resolveVueI18nBridgeAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(VUE_I18N_BRIDGE_PKG)
debug('@intlify/vue-i18n-bridge alias', nuxt.options.alias[VUE_I18N_BRIDGE_PKG])

// resolve vue-i18n-routing
nuxt.options.alias[VUE_I18N_ROUTING_PKG] = await resolveVueI18nRoutingAlias(
pkgModulesDir,
nuxt.options.rootDir,
nuxt.options.workspaceDir,
pkgMgr
)
nuxt.options.alias[VUE_I18N_ROUTING_PKG] = await resolveVueI18nRoutingAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(VUE_I18N_ROUTING_PKG)
debug('vue-i18n-routing alias', nuxt.options.alias[VUE_I18N_ROUTING_PKG])
}

export async function resolveVueI18nAlias(nuxt: Nuxt) {
return resolve(await resolveVueI18nPkgPath(), nuxt.options.dev ? 'dist/vue-i18n.mjs' : 'dist/vue-i18n.runtime.mjs')
}

/**
* NOTE:
* The following packages may not be able to resolve the file paths of the target ES modules with `resolvePath`
* so they resolve them on their own (sometimes, these are resolved as `cjs`)
* - `vue-i18n`
* - `vue-i18n-routing`
* - `@intlify/vue-i18n-bridge`
* - `@intlify/vue-router-bridge`
*/

async function resolveVueI18nBridgeAlias(
pkgModulesDir: string,
rootDir: string,
workspaceDir: string,
pkgMgr: PackageManager
) {
export async function resolveVueI18nAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = nuxt.options.dev
? `${VUE_I18N_PKG}/dist/vue-i18n.mjs`
: `${VUE_I18N_PKG}/dist/vue-i18n.runtime.mjs`
const targets = [
// 1st, try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// 2nd, try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${VUE_I18N_PKG} resolving from ...`, targets)

return tryResolve(VUE_I18N_PKG, targets, pkgMgr)
}

async function resolveVueI18nBridgeAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${VUE_I18N_BRIDGE_PKG}/lib/index.mjs` as const
const targets = [
// 1st, try to resolve from `node_modules` (hoisted case)
Expand All @@ -93,12 +89,8 @@ async function resolveVueI18nBridgeAlias(
return tryResolve(VUE_I18N_BRIDGE_PKG, targets, pkgMgr)
}

async function resolveVueRouterBridgeAlias(
pkgModulesDir: string,
rootDir: string,
workspaceDir: string,
pkgMgr: PackageManager
) {
async function resolveVueRouterBridgeAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${VUE_ROUTER_BRIDGE_PKG}/lib/index.mjs` as const
const targets = [
// 1st, try to resolve from `node_modules` (hoisted case)
Expand All @@ -118,12 +110,8 @@ async function resolveVueRouterBridgeAlias(
return tryResolve(VUE_ROUTER_BRIDGE_PKG, targets, pkgMgr)
}

export async function resolveVueI18nRoutingAlias(
pkgModulesDir: string,
rootDir: string,
workspaceDir: string,
pkgMgr: PackageManager
) {
export async function resolveVueI18nRoutingAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${VUE_I18N_ROUTING_PKG}/dist/vue-i18n-routing.mjs` as const
const targets = [
// 1st, try to resolve from `node_modules` (hoisted case)
Expand Down
7 changes: 0 additions & 7 deletions src/dirs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import createDebug from 'debug'
import { fileURLToPath } from 'node:url'
import { dirname, resolve, parse as parsePath } from 'pathe'
import { resolvePath } from '@nuxt/kit'
import { VUE_I18N_ROUTING_PKG } from './constants'
import { getPackageManagerType, tryResolve } from './utils'

Expand All @@ -17,12 +16,6 @@ debug('runtimeDir', runtimeDir)
debug('pkgDir', pkgDir)
debug('pkgModulesDir', pkgModulesDir)

export async function resolveVueI18nPkgPath() {
const p = await resolvePath('vue-i18n')
debug('vue-i18n resolved path', p)
return resolve(p, '../..')
}

export async function resolveVueI18nRoutingDtsPath(id: string, rootDir: string) {
const pkgMgr = await getPackageManagerType()
const dtsPath = `${VUE_I18N_ROUTING_PKG}/dist/${id}`
Expand Down
9 changes: 6 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { extendMessages } from './messages'
import { extendBundler } from './bundler'
import { generateLoaderOptions } from './gen'
import { NUXT_I18N_MODULE_ID, DEFAULT_OPTIONS } from './constants'
import { formatMessage, getNormalizedLocales, resolveLocales } from './utils'
import { distDir, runtimeDir } from './dirs'
import { formatMessage, getNormalizedLocales, resolveLocales, getPackageManagerType } from './utils'
import { distDir, runtimeDir, pkgModulesDir } from './dirs'

import type { NuxtI18nOptions } from './types'
import type { DefineLocaleMessage, LocaleMessages } from 'vue-i18n'
Expand Down Expand Up @@ -204,7 +204,10 @@ export default defineNuxtModule<NuxtI18nOptions>({
* auto imports
*/

const vueI18nPath = await resolveVueI18nAlias(nuxt)
const pkgMgr = await getPackageManagerType()
const vueI18nPath = await resolveVueI18nAlias(pkgModulesDir, nuxt, pkgMgr)
debug('vueI18nPath for auto-import', vueI18nPath)

await addImports([
{ name: 'useI18n', from: vueI18nPath },
...[
Expand Down

0 comments on commit 226e24b

Please sign in to comment.