diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 8c2b746d5c..ef2a6c9c6c 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -46,6 +46,8 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { .map(m => m.entryPath!), ) + const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4 + const nitroConfig: NitroConfig = defu(nuxt.options.nitro, { debug: nuxt.options.debug, rootDir: nuxt.options.rootDir, @@ -63,6 +65,12 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { }, imports: { autoImport: nuxt.options.imports.autoImport as boolean, + dirs: isNuxtV4 + ? [ + resolve(nuxt.options.rootDir, 'shared', 'utils'), + resolve(nuxt.options.rootDir, 'shared', 'types'), + ] + : [], imports: [ { as: '__buildAssetsURL', diff --git a/packages/nuxt/src/imports/module.ts b/packages/nuxt/src/imports/module.ts index af728fb6db..33aad9a885 100644 --- a/packages/nuxt/src/imports/module.ts +++ b/packages/nuxt/src/imports/module.ts @@ -54,6 +54,8 @@ export default defineNuxtModule>({ await nuxt.callHook('imports:context', ctx) + const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4 + // composables/ dirs from all layers let composablesDirs: string[] = [] if (options.scan) { @@ -64,6 +66,12 @@ export default defineNuxtModule>({ } composablesDirs.push(resolve(layer.config.srcDir, 'composables')) composablesDirs.push(resolve(layer.config.srcDir, 'utils')) + + if (isNuxtV4) { + composablesDirs.push(resolve(layer.config.rootDir, 'shared', 'utils')) + composablesDirs.push(resolve(layer.config.rootDir, 'shared', 'types')) + } + for (const dir of (layer.config.imports?.dirs ?? [])) { if (!dir) { continue diff --git a/packages/schema/src/config/common.ts b/packages/schema/src/config/common.ts index 61fcd1f2a5..f50ec19da4 100644 --- a/packages/schema/src/config/common.ts +++ b/packages/schema/src/config/common.ts @@ -355,6 +355,11 @@ export default defineUntypedSchema({ */ plugins: 'plugins', + /** + * The shared directory. This directory is shared between the app and the server. + */ + shared: 'shared', + /** * The directory containing your static files, which will be directly accessible via the Nuxt server * and copied across into your `dist` folder when your app is generated. @@ -424,12 +429,13 @@ export default defineUntypedSchema({ */ alias: { $resolve: async (val: Record, get): Promise> => { - const [srcDir, rootDir, assetsDir, publicDir] = await Promise.all([get('srcDir'), get('rootDir'), get('dir.assets'), get('dir.public')]) as [string, string, string, string] + const [srcDir, rootDir, assetsDir, publicDir, sharedDir] = await Promise.all([get('srcDir'), get('rootDir'), get('dir.assets'), get('dir.public'), get('dir.shared')]) as [string, string, string, string, string] return { '~': srcDir, '@': srcDir, '~~': rootDir, '@@': rootDir, + '#shared': resolve(rootDir, sharedDir), [basename(assetsDir)]: resolve(srcDir, assetsDir), [basename(publicDir)]: resolve(srcDir, publicDir), ...val,