From b429419b78f7894cc5e579abc534af605b55b766 Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Wed, 4 Dec 2024 17:32:54 +0800 Subject: [PATCH] fix: auto import the multiple levels of nested directories in 'utils' --- src/core/config/resolvers/imports.ts | 2 +- test/fixture/routes/imports.ts | 1 + test/fixture/utils/foo/bar/test.ts | 1 + test/fixture/utils/foo/test.ts | 1 + test/tests.ts | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 test/fixture/utils/foo/bar/test.ts create mode 100644 test/fixture/utils/foo/test.ts diff --git a/src/core/config/resolvers/imports.ts b/src/core/config/resolvers/imports.ts index 2a1a901290..d3e82102da 100644 --- a/src/core/config/resolvers/imports.ts +++ b/src/core/config/resolvers/imports.ts @@ -27,7 +27,7 @@ export async function resolveImportsOptions(options: NitroOptions) { // Auto imports from utils dirs options.imports.dirs ??= []; options.imports.dirs.push( - ...options.scanDirs.map((dir) => join(dir, "utils/*")) + ...options.scanDirs.map((dir) => join(dir, "utils/**/*")) ); // Normalize exclude diff --git a/test/fixture/routes/imports.ts b/test/fixture/routes/imports.ts index 6cdf5b54b6..be04c44283 100644 --- a/test/fixture/routes/imports.ts +++ b/test/fixture/routes/imports.ts @@ -1,5 +1,6 @@ export default defineEventHandler(() => { return { testUtil: testUtil(), + testNestedUtil: testFooUtil() + testBarUtil(), }; }); diff --git a/test/fixture/utils/foo/bar/test.ts b/test/fixture/utils/foo/bar/test.ts new file mode 100644 index 0000000000..191b70b1e0 --- /dev/null +++ b/test/fixture/utils/foo/bar/test.ts @@ -0,0 +1 @@ +export const testBarUtil = () => 12_345; diff --git a/test/fixture/utils/foo/test.ts b/test/fixture/utils/foo/test.ts new file mode 100644 index 0000000000..1e7e145661 --- /dev/null +++ b/test/fixture/utils/foo/test.ts @@ -0,0 +1 @@ +export const testFooUtil = () => 1234; diff --git a/test/tests.ts b/test/tests.ts index 122e06c735..d45bc80211 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -457,6 +457,7 @@ export function testNitro( const res = await callHandler({ url: "/imports" }); expect(res.data).toMatchObject({ testUtil: 123, + testNestedUtil: 1234 + 12_345, }); });