From 19823dd716568140011eaa86110bb8868f0e2513 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Thu, 19 Dec 2024 05:28:31 +0800 Subject: [PATCH] fix: correctly resolve no hooks file when a similarly named directory exists (#13188) fixes #13184 --- .changeset/empty-mugs-press.md | 5 +++++ packages/kit/src/utils/filesystem.js | 24 ++++++++++++----------- packages/kit/src/utils/filesystem.spec.js | 18 ++++++++++++++--- 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 .changeset/empty-mugs-press.md diff --git a/.changeset/empty-mugs-press.md b/.changeset/empty-mugs-press.md new file mode 100644 index 000000000000..c26949bf5bb0 --- /dev/null +++ b/.changeset/empty-mugs-press.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly resolve no hooks file when a similarly named directory exists diff --git a/packages/kit/src/utils/filesystem.js b/packages/kit/src/utils/filesystem.js index 58bcce22c7e3..4a2c5e502cb0 100644 --- a/packages/kit/src/utils/filesystem.js +++ b/packages/kit/src/utils/filesystem.js @@ -170,22 +170,24 @@ export function resolve_entry(entry) { if (fs.existsSync(entry)) { const stats = fs.statSync(entry); const index = path.join(entry, 'index'); - if (stats.isDirectory() && fs.existsSync(index)) { + + if (stats.isFile()) { + return entry; + } else if (fs.existsSync(index)) { return resolve_entry(index); } + } - return entry; - } else { - const dir = path.dirname(entry); - - if (fs.existsSync(dir)) { - const base = path.basename(entry); - const files = fs.readdirSync(dir); + const dir = path.dirname(entry); - const found = files.find((file) => file.replace(/\.(js|ts)$/, '') === base); + if (fs.existsSync(dir)) { + const base = path.basename(entry); + const files = fs.readdirSync(dir); + const found = files.find((file) => { + return file.replace(/\.(js|ts)$/, '') === base && fs.statSync(path.join(dir, file)).isFile(); + }); - if (found) return path.join(dir, found); - } + if (found) return path.join(dir, found); } return null; diff --git a/packages/kit/src/utils/filesystem.spec.js b/packages/kit/src/utils/filesystem.spec.js index e9ccf8d832a9..84d58d406097 100644 --- a/packages/kit/src/utils/filesystem.spec.js +++ b/packages/kit/src/utils/filesystem.spec.js @@ -101,14 +101,26 @@ test('replaces strings', () => { }); test('ignores hooks.server folder when resolving hooks', () => { - write('hooks.server/index.js', ''); + write(join('hooks.server', 'index.js'), ''); expect(resolve_entry(source_dir + '/hooks')).null; }); test('ignores hooks folder that has no index file when resolving hooks', () => { - write('hooks/not-index.js', ''); + write(join('hooks', 'not-index.js'), ''); write('hooks.js', ''); - expect(resolve_entry(source_dir + '/hooks')).toBe(source_dir + '/hooks'); + expect(resolve_entry(source_dir + '/hooks')).toBe(join(source_dir, 'hooks.js')); +}); + +test('ignores hooks folder when resolving universal hooks', () => { + write(join('hooks', 'hooks.server.js'), ''); + + expect(resolve_entry(source_dir + '/hooks')).null; +}); + +test('resolves entries that have an extension', () => { + write('hooks.js', ''); + + expect(resolve_entry(join(source_dir, 'hooks.js'))).toBe(join(source_dir, 'hooks.js')); });