mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-22 08:29:46 +00:00
fix(nuxt): make shared/
directories available within layers (#30843)
This commit is contained in:
parent
2ebdb27dff
commit
f56e29fb1c
@ -49,7 +49,19 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
|||||||
.map(m => m.entryPath!),
|
.map(m => m.entryPath!),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const sharedDirs = new Set<string>()
|
||||||
const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4
|
const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4
|
||||||
|
if (isNuxtV4 && (nuxt.options.nitro.imports !== false && nuxt.options.imports.scan !== false)) {
|
||||||
|
for (const layer of nuxt.options._layers) {
|
||||||
|
// Layer disabled scanning for itself
|
||||||
|
if (layer.config?.imports?.scan === false) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sharedDirs.add(resolve(layer.config.rootDir, 'shared', 'utils'))
|
||||||
|
sharedDirs.add(resolve(layer.config.rootDir, 'shared', 'types'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const nitroConfig: NitroConfig = defu(nuxt.options.nitro, {
|
const nitroConfig: NitroConfig = defu(nuxt.options.nitro, {
|
||||||
debug: nuxt.options.debug ? nuxt.options.debug.nitro : false,
|
debug: nuxt.options.debug ? nuxt.options.debug.nitro : false,
|
||||||
@ -68,12 +80,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
|||||||
},
|
},
|
||||||
imports: {
|
imports: {
|
||||||
autoImport: nuxt.options.imports.autoImport as boolean,
|
autoImport: nuxt.options.imports.autoImport as boolean,
|
||||||
dirs: isNuxtV4
|
dirs: [...sharedDirs],
|
||||||
? [
|
|
||||||
resolve(nuxt.options.rootDir, 'shared', 'utils'),
|
|
||||||
resolve(nuxt.options.rootDir, 'shared', 'types'),
|
|
||||||
]
|
|
||||||
: [],
|
|
||||||
imports: [
|
imports: [
|
||||||
{
|
{
|
||||||
as: '__buildAssetsURL',
|
as: '__buildAssetsURL',
|
||||||
|
57
packages/nuxt/test/shared-dir-config.test.ts
Normal file
57
packages/nuxt/test/shared-dir-config.test.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { fileURLToPath } from 'node:url'
|
||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { normalize } from 'pathe'
|
||||||
|
import type { NuxtConfig } from '@nuxt/schema'
|
||||||
|
import { loadNuxt } from '../src'
|
||||||
|
|
||||||
|
const fixtureDir = normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url)))
|
||||||
|
|
||||||
|
describe('loadNuxt', () => {
|
||||||
|
it('does not add shared directories to nitro auto-imports in v3', async () => {
|
||||||
|
const importDirs = await getNitroImportDirs({ future: { compatibilityVersion: 3 as any } })
|
||||||
|
expect(normalizePaths(importDirs)).toMatchInlineSnapshot(`[]`)
|
||||||
|
})
|
||||||
|
it('adds shared directories for layers to nitro auto-imports in v4', async () => {
|
||||||
|
const importDirs = await getNitroImportDirs({ future: { compatibilityVersion: 4 } })
|
||||||
|
expect(normalizePaths(importDirs)).toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
"<rootDir>/shared/utils",
|
||||||
|
"<rootDir>/shared/types",
|
||||||
|
"<rootDir>/extends/bar/shared/utils",
|
||||||
|
"<rootDir>/extends/bar/shared/types",
|
||||||
|
"<rootDir>/extends/node_modules/foo/shared/utils",
|
||||||
|
"<rootDir>/extends/node_modules/foo/shared/types",
|
||||||
|
"<rootDir>/layers/bar/shared/utils",
|
||||||
|
"<rootDir>/layers/bar/shared/types",
|
||||||
|
]
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
function normalizePaths (arr: unknown[]) {
|
||||||
|
const normalized = []
|
||||||
|
for (const dir of arr) {
|
||||||
|
normalized.push(typeof dir === 'string' ? dir.replace(fixtureDir, '<rootDir>') : dir)
|
||||||
|
}
|
||||||
|
return normalized
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getNitroImportDirs (overrides?: NuxtConfig) {
|
||||||
|
const importDirs: unknown[] = []
|
||||||
|
const nuxt = await loadNuxt({
|
||||||
|
cwd: fixtureDir,
|
||||||
|
ready: true,
|
||||||
|
overrides: {
|
||||||
|
...overrides,
|
||||||
|
hooks: {
|
||||||
|
'nitro:config' (config) {
|
||||||
|
if (config.imports) {
|
||||||
|
importDirs.push(...config.imports.dirs || [])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
await nuxt.close()
|
||||||
|
return importDirs
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user