Skip to content

Commit

Permalink
fix: correctly check index file exists when resolving an entry (#13354)
Browse files Browse the repository at this point in the history
fixes #13350

#13188 caused a regression where index.js and index.ts files would not be resolved as entries because of the additional isFile() check that was added to prioritise matching files over folders.
  • Loading branch information
eltigerchino authored Jan 21, 2025
1 parent a8bdcf8 commit ab58c22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-dancers-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly resolve index file entrypoints such as `src/service-worker/index.js`
7 changes: 4 additions & 3 deletions packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ export function from_fs(str) {
export function resolve_entry(entry) {
if (fs.existsSync(entry)) {
const stats = fs.statSync(entry);
const index = path.join(entry, 'index');

if (stats.isFile()) {
return entry;
} else if (fs.existsSync(index)) {
}

const index = path.join(entry, 'index');
if (fs.existsSync(index + '.js') || fs.existsSync(index + '.ts')) {
return resolve_entry(index);
}
}
Expand Down
26 changes: 17 additions & 9 deletions packages/kit/src/utils/filesystem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,35 @@ test('replaces strings', () => {
);
});

test('ignores hooks.server folder when resolving hooks', () => {
write(join('hooks.server', 'index.js'), '');
test('resolves index files', () => {
write(join('service-worker', 'index.js'), '');

expect(resolve_entry(source_dir + '/hooks')).null;
expect(resolve_entry(source_dir + '/service-worker')).toBe(
join(source_dir, 'service-worker', 'index.js')
);
});

test('resolves entries that have an extension', () => {
write('hooks.js', '');

expect(resolve_entry(join(source_dir, 'hooks.js'))).toBe(join(source_dir, 'hooks.js'));
});

test('ignores hooks folder that has no index file when resolving hooks', () => {
test('resolves universal hooks file when hooks folder exists', () => {
write(join('hooks', 'not-index.js'), '');
write('hooks.js', '');

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'), '');
test('ignores hooks.server folder when resolving universal hooks file', () => {
write(join('hooks.server', 'index.js'), '');

expect(resolve_entry(source_dir + '/hooks')).null;
});

test('resolves entries that have an extension', () => {
write('hooks.js', '');
test('ignores hooks folder when resolving universal hooks file', () => {
write(join('hooks', 'hooks.server.js'), '');

expect(resolve_entry(join(source_dir, 'hooks.js'))).toBe(join(source_dir, 'hooks.js'));
expect(resolve_entry(source_dir + '/hooks')).null;
});

0 comments on commit ab58c22

Please sign in to comment.