Skip to content

Commit

Permalink
fix: resolve vue-i18n related modules on nuxt layers (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 25, 2022
1 parent 46bd940 commit e93ce7b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 1 deletion.
7 changes: 7 additions & 0 deletions specs/fixtures/issues/1740/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts">
const appConfig = useAppConfig()
</script>

<template>
<p id="render">{{ appConfig.myProject.name }}</p>
</template>
4 changes: 4 additions & 0 deletions specs/fixtures/issues/1740/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
extends: './playground'
})
12 changes: 12 additions & 0 deletions specs/fixtures/issues/1740/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"scripts": {
"dev": "nuxi prepare",
"build": "nuxi build",
"generate": "nuxi generate",
"preview": "nuxi preview"
},
"devDependencies": {
"nuxt": "latest"
}
}
14 changes: 14 additions & 0 deletions specs/fixtures/issues/1740/playground/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default defineAppConfig({
myProject: {
name: 'This is Nuxt layer'
}
})

declare module '@nuxt/schema' {
interface AppConfigInput {
myProject?: {
/** Project name */
name?: string
}
}
}
3 changes: 3 additions & 0 deletions specs/fixtures/issues/1740/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n']
})
15 changes: 15 additions & 0 deletions specs/fixtures/issues/1740/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"private": true,
"type": "module",
"main": "./nuxt.config.ts",
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
19 changes: 19 additions & 0 deletions specs/issues/1740.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test, describe, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, createPage, url } from '@nuxt/test-utils'
import { getText } from '../helper'

describe('#1740', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/issues/1740`, import.meta.url)),
browser: true
})

test('should be loaded vue-i18n related modules', async () => {
const home = url('/')
const page = await createPage()
await page.goto(home)

expect(await getText(page, '#render')).toEqual('This is Nuxt layer')
})
})
12 changes: 11 additions & 1 deletion src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolvePath } from '@nuxt/kit'
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'
import { tryResolve, getLayerRootDirs, getPackageManagerType } from './utils'

import type { Nuxt } from '@nuxt/schema'
import type { PackageManager } from './utils'
Expand Down Expand Up @@ -56,6 +56,8 @@ export async function resolveVueI18nAlias(pkgModulesDir: string, nuxt: Nuxt, pkg
? `${VUE_I18N_PKG}/dist/vue-i18n.mjs`
: `${VUE_I18N_PKG}/dist/vue-i18n.runtime.mjs`
const targets = [
// for Nuxt layer
...getLayerRootDirs(nuxt).map(root => resolve(root, 'node_modules', modulePath)),
// 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)
Expand All @@ -72,6 +74,9 @@ async function resolveVueI18nBridgeAlias(pkgModulesDir: string, nuxt: Nuxt, pkgM
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, 'node_modules', modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath)),
// 1st, try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// 2nd, try to resolve from `node_modules/vue-i18n-routing` (not hoisted case)
Expand All @@ -93,6 +98,9 @@ async function resolveVueRouterBridgeAlias(pkgModulesDir: string, nuxt: Nuxt, pk
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, 'node_modules', modulePath)),
...getLayerRootDirs(nuxt).map(root => resolve(root, `${VUE_I18N_ROUTING_PKG}/node_modules`, modulePath)),
// 1st, try to resolve from `node_modules` (hoisted case)
resolve(rootDir, 'node_modules', modulePath),
// 2nd, try to resolve from `node_modules/vue-i18n-routing` (not hoisted case)
Expand All @@ -114,6 +122,8 @@ export async function resolveVueI18nRoutingAlias(pkgModulesDir: string, nuxt: Nu
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)),
// 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)
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NUXT_I18N_MODULE_ID } from './constants'

import type { LocaleObject } from 'vue-i18n-routing'
import type { NuxtI18nOptions, LocaleInfo } from './types'
import type { Nuxt } from '@nuxt/schema'

const PackageManagerLockFiles = {
'npm-shrinkwrap.json': 'npm-legacy',
Expand Down Expand Up @@ -66,6 +67,11 @@ export async function resolveLocales(path: string, locales: LocaleObject[]): Pro
})
}

export function getLayerRootDirs(nuxt: Nuxt) {
const layers = nuxt.options._layers
return layers.length > 1 ? layers.map(layer => layer.config.rootDir) : []
}

export async function tryResolve(id: string, targets: string[], pkgMgr: PackageManager, extention = '') {
for (const target of targets) {
if (await isExists(target + extention)) {
Expand Down

0 comments on commit e93ce7b

Please sign in to comment.