Skip to content

Commit

Permalink
fix(alias): resolve module using nuxt kit (nuxt-modules#2504)
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli authored Oct 19, 2023
1 parent b4a5692 commit 2ea8c39
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 321 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"magic-string": "^0.30.4",
"mlly": "^1.4.2",
"pathe": "^1.1.1",
"pkg-types": "^1.0.3",
"ufo": "^1.3.1",
"unplugin": "^1.5.0",
"unstorage": "^1.9.0",
Expand Down
70 changes: 2 additions & 68 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

232 changes: 25 additions & 207 deletions src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,228 +1,46 @@
import { tryResolveModule } from '@nuxt/kit'
import createDebug from 'debug'
import { resolve } from 'pathe'
import {
VUE_I18N_PKG,
VUE_I18N_BRIDGE_PKG,
VUE_ROUTER_BRIDGE_PKG,
VUE_I18N_ROUTING_PKG,
SHARED_PKG,
MESSAGE_COMPILER_PKG,
CORE_BASE_PKG
CORE_BASE_PKG,
JS_COOKIE_PKG,
COOKIE_ES_PKG,
UFO_PKG
} from './constants'
import { pkgModulesDir } from './dirs'
import { tryResolve, getLayerRootDirs, getPackageManagerType } from './utils'

import type { Nuxt } from '@nuxt/schema'
import type { PackageManager } from './utils'
import type { NuxtI18nOptions } from './types'

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

export async function setupAlias(nuxt: Nuxt, options: NuxtI18nOptions) {
const pkgMgr = await getPackageManagerType()
debug('setupAlias: pkgMgr', pkgMgr)

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

// resolve @intlify/shared
nuxt.options.alias[SHARED_PKG] = await resolveSharedAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(SHARED_PKG)
debug('@intlify/shared alias', nuxt.options.alias[SHARED_PKG])

// resolve @intlify/message-compiler
nuxt.options.alias['@intlify/message-compiler'] = await resolveMessageCompilerAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(MESSAGE_COMPILER_PKG)
debug('@intlify/message-compiler alias', nuxt.options.alias[MESSAGE_COMPILER_PKG])

// resolve @intlify/core-base
nuxt.options.alias['@intlify/core-base'] = await resolveCoreBaseAlias(pkgModulesDir, nuxt, pkgMgr)
nuxt.options.build.transpile.push(CORE_BASE_PKG)
debug('@intlify/core-base alias', nuxt.options.alias[CORE_BASE_PKG])

// resolve @intlify/vue-router-bridge
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, 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, pkgMgr)
nuxt.options.build.transpile.push(VUE_I18N_ROUTING_PKG)
debug('vue-i18n-routing alias', nuxt.options.alias[VUE_I18N_ROUTING_PKG])
}

/**
* NOTE:
* The following packages maybe installed in directories
* where the package manager `node_modules` installation algorithm cannot resolve the file path of the target ES module with `resolvePath` (`@nuxt/kit`).
* (e.g. npm peerDependencies).
* - `vue-i18n`
* - `vue-i18n-routing`
* - `@intlify/shared`
* - `@intlify/message-compiler`
* - `@intlify/core-base`
* - `@intlify/vue-i18n-bridge`
* - `@intlify/vue-router-bridge`
*/

export async function resolveVueI18nAlias(
pkgModulesDir: string,
options: NuxtI18nOptions,
nuxt: Nuxt,
pkgMgr: PackageManager
) {
const { rootDir, workspaceDir } = nuxt.options
const runtimeOnly = options.bundle?.runtimeOnly
const modulePath =
const modules: Record<string, string> = {}

modules[VUE_I18N_PKG] =
nuxt.options.dev || nuxt.options._prepare
? `${VUE_I18N_PKG}/dist/vue-i18n.mjs`
: `${VUE_I18N_PKG}/dist/vue-i18n${runtimeOnly ? '.runtime' : ''}.mjs`
const targets = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${VUE_I18N_PKG} resolving from ...`, targets)

return tryResolve(VUE_I18N_PKG, targets, pkgMgr)
}

export async function resolveSharedAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${SHARED_PKG}/dist/shared.mjs` as const
const targets = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, `${VUE_I18N_PKG}/node_modules`, modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, `${MESSAGE_COMPILER_PKG}/node_modules`, modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules/vue-i18n/node_modules` (not hoisted case)
resolve(pkgModulesDir, `${VUE_I18N_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules/@intlify/message-compiler/node_modules` (not hoisted case)
resolve(pkgModulesDir, `${MESSAGE_COMPILER_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', `${VUE_I18N_PKG}/node_modules`, modulePath),
resolve(workspaceDir, 'node_modules', `${MESSAGE_COMPILER_PKG}/node_modules`, modulePath),
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${SHARED_PKG} resolving from ...`, targets)

return tryResolve(SHARED_PKG, targets, pkgMgr)
}

export async function resolveCoreBaseAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${CORE_BASE_PKG}/dist/core-base.mjs` as const
const targets = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${CORE_BASE_PKG} resolving from ...`, targets)

return tryResolve(CORE_BASE_PKG, targets, pkgMgr)
}

export async function resolveMessageCompilerAlias(pkgModulesDir: string, nuxt: Nuxt, pkgMgr: PackageManager) {
const { rootDir, workspaceDir } = nuxt.options
const modulePath = `${MESSAGE_COMPILER_PKG}/dist/message-compiler.mjs` as const
const targets = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${MESSAGE_COMPILER_PKG} resolving from ...`, targets)

return tryResolve(MESSAGE_COMPILER_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 = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules/vue-i18n-routing` (not hoisted case)
resolve(pkgModulesDir, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules/vue-i18n-routing` (hoisted case)
resolve(rootDir, 'node_modules', `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${VUE_I18N_BRIDGE_PKG} resolving from ...`, targets)

return tryResolve(VUE_I18N_BRIDGE_PKG, targets, pkgMgr)
}

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 = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules/@nuxtjs/i18n/node_modules/vue-i18n-routing` (not hoisted case)
resolve(pkgModulesDir, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules/vue-i18n-routing` (hoisted case)
resolve(rootDir, 'node_modules', `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath),
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${VUE_ROUTER_BRIDGE_PKG} resolving from ...`, targets)

return tryResolve(VUE_ROUTER_BRIDGE_PKG, targets, pkgMgr)
}

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 = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// try to resolve from `node_modules/@nuxtjs/i18n` (not hoisted case)
resolve(pkgModulesDir, modulePath),
// try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// workspace directories
resolve(workspaceDir, 'node_modules', modulePath)
]
debug(`${VUE_I18N_ROUTING_PKG} resolving from ...`, targets)

return tryResolve(VUE_I18N_ROUTING_PKG, targets, pkgMgr)
modules[SHARED_PKG] = `${SHARED_PKG}/dist/shared.mjs`
modules[MESSAGE_COMPILER_PKG] = `${MESSAGE_COMPILER_PKG}/dist/message-compiler.mjs`
modules[CORE_BASE_PKG] = `${CORE_BASE_PKG}/dist/core-base.mjs`
modules[VUE_ROUTER_BRIDGE_PKG] = `${VUE_ROUTER_BRIDGE_PKG}/lib/index.mjs`
modules[VUE_I18N_BRIDGE_PKG] = `${VUE_I18N_BRIDGE_PKG}/lib/index.mjs`
modules[VUE_I18N_ROUTING_PKG] = `${VUE_I18N_ROUTING_PKG}/dist/vue-i18n-routing.mjs`
modules[JS_COOKIE_PKG] = JS_COOKIE_PKG
modules[COOKIE_ES_PKG] = COOKIE_ES_PKG
modules[UFO_PKG] = UFO_PKG

for (const [moduleName, moduleFile] of Object.entries(modules)) {
const module = await tryResolveModule(moduleFile, nuxt.options.modulesDir)
if (!module) throw new Error(`Could not resolve module "${moduleFile}"`)
nuxt.options.alias[moduleName] = module
nuxt.options.build.transpile.push(moduleName)
debug(`${moduleName} alias`, nuxt.options.alias[moduleName])
}
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const CORE_BASE_PKG = '@intlify/core-base' as const
export const VUE_I18N_BRIDGE_PKG = '@intlify/vue-i18n-bridge' as const
export const VUE_ROUTER_BRIDGE_PKG = '@intlify/vue-router-bridge' as const
export const VUE_I18N_ROUTING_PKG = 'vue-i18n-routing' as const
export const JS_COOKIE_PKG = 'js-cookie' as const
export const COOKIE_ES_PKG = 'cookie-es' as const
export const UFO_PKG = 'ufo' as const

// Options
const STRATEGY_PREFIX = 'prefix'
Expand Down
Loading

0 comments on commit 2ea8c39

Please sign in to comment.