Skip to content

Commit

Permalink
fix: prevent hooks.server code being imported into client during bu…
Browse files Browse the repository at this point in the history
…ild (#12195)

* fix hooks.server matching hooks

* changeset

* unit test

* Update .changeset/slimy-eyes-pull.md

---------

Co-authored-by: Tee Ming <chewteeming01@gmail.com>
  • Loading branch information
CaptainCodeman and eltigerchino authored Jun 21, 2024
1 parent 97d1085 commit 5645614
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-eyes-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: prevent client import error when a `hooks.server` file imports a private environment variable
2 changes: 1 addition & 1 deletion packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function resolve_entry(entry) {
const base = path.basename(entry);
const files = fs.readdirSync(dir);

const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
const found = files.find((file) => file.replace(/\.(js|ts)$/, '') === base);

if (found) return path.join(dir, found);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/src/utils/filesystem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mkdtempSync, writeFileSync, readdirSync, mkdirSync, readFileSync } from
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { assert, expect, beforeEach, test } from 'vitest';
import { copy, mkdirp } from './filesystem.js';
import { copy, mkdirp, resolve_entry } from './filesystem.js';

/** @type {string} */
let source_dir;
Expand Down Expand Up @@ -99,3 +99,9 @@ test('replaces strings', () => {
'the quick brown fox jumps over the lazy dog'
);
});

test('ignores hooks.server folder when resolving hooks', () => {
write('hooks.server/index.js', '');

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

0 comments on commit 5645614

Please sign in to comment.