diff --git a/packages/kit/src/nitro.ts b/packages/kit/src/nitro.ts index 43375ea113..9f46e41241 100644 --- a/packages/kit/src/nitro.ts +++ b/packages/kit/src/nitro.ts @@ -1,4 +1,5 @@ import type { Nitro, NitroDevEventHandler, NitroEventHandler } from 'nitropack' +import type { Import } from 'unimport' import { normalize } from 'pathe' import { useNuxt } from './context' @@ -82,3 +83,33 @@ export function useNitro (): Nitro { } return (nuxt as any)._nitro } + +/** + * Add server imports to be auto-imported by Nitro + */ +export function addServerImports (imports: Import[]) { + const nuxt = useNuxt() + nuxt.hook('nitro:config', (config) => { + config.imports = config.imports || {} + config.imports.autoImport = true + if (Array.isArray(config.imports.imports)) { + config.imports.imports = [...config.imports.imports, ...imports] + } else { + config.imports.imports = [config.imports.imports, ...imports] + } + }) +} + +/** + * Add directories to be scanned by Nitro + */ +export function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean } = {}) { + const nuxt = useNuxt() + nuxt.hook('nitro:config', (config) => { + config.scanDirs = config.scanDirs || [] + + for (const dir of (Array.isArray(dirs) ? dirs : [dirs])) { + config.scanDirs[opts.prepend ? 'unshift' : 'push'](dir) + } + }) +} diff --git a/test/basic.test.ts b/test/basic.test.ts index 659c8bd665..d46ab83892 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -41,6 +41,17 @@ describe('server api', () => { expect(await $fetch('/api/counter')).toEqual({ count: 2 }) expect(await $fetch('/api/counter')).toEqual({ count: 3 }) }) + + it('should auto-import', async () => { + const res = await $fetch('/api/auto-imports') + expect(res).toMatchInlineSnapshot(` + { + "autoImported": "utils", + "fromServerDir": "test-utils", + "thisIs": "serverAutoImported", + } + `) + }) }) describe('route rules', () => { diff --git a/test/fixtures/basic/modules/auto-registered/index.ts b/test/fixtures/basic/modules/auto-registered/index.ts index 60406e1126..a254abaad1 100644 --- a/test/fixtures/basic/modules/auto-registered/index.ts +++ b/test/fixtures/basic/modules/auto-registered/index.ts @@ -1,4 +1,4 @@ -import { addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit' +import { addServerHandler, addServerImports, addServerImportsDir, createResolver, defineNuxtModule } from 'nuxt/kit' export default defineNuxtModule({ meta: { @@ -11,5 +11,16 @@ export default defineNuxtModule({ handler: resolver.resolve('./runtime/handler'), route: '/auto-registered-module' }) + + addServerImports([{ + from: resolver.resolve('./runtime/some-server-import'), + name: 'serverAutoImported', + as: 'autoimportedFunction' + }, { + from: resolver.resolve('./runtime/some-server-import'), + name: 'someUtils' + }]) + + addServerImportsDir(resolver.resolve('./runtime/server')) } }) diff --git a/test/fixtures/basic/modules/auto-registered/runtime/server/utils/some-utils.ts b/test/fixtures/basic/modules/auto-registered/runtime/server/utils/some-utils.ts new file mode 100644 index 0000000000..e31094e9e1 --- /dev/null +++ b/test/fixtures/basic/modules/auto-registered/runtime/server/utils/some-utils.ts @@ -0,0 +1 @@ +export const testUtils = 'test-utils' diff --git a/test/fixtures/basic/modules/auto-registered/runtime/some-server-import.ts b/test/fixtures/basic/modules/auto-registered/runtime/some-server-import.ts new file mode 100644 index 0000000000..00e7322c37 --- /dev/null +++ b/test/fixtures/basic/modules/auto-registered/runtime/some-server-import.ts @@ -0,0 +1,5 @@ +export function serverAutoImported () { + return 'serverAutoImported' +} + +export const someUtils = 'utils' diff --git a/test/fixtures/basic/server/api/auto-imports.ts b/test/fixtures/basic/server/api/auto-imports.ts new file mode 100644 index 0000000000..58ec0f85d0 --- /dev/null +++ b/test/fixtures/basic/server/api/auto-imports.ts @@ -0,0 +1,7 @@ +export default defineEventHandler(() => { + return { + thisIs: autoimportedFunction(), + autoImported: someUtils, + fromServerDir: testUtils + } +})