Skip to content

Commit 194130e

Browse files
authored
fix(sveltekit): Wrap load when typed explicitly (#8049)
When `load` is explicitly typed (using `export const load: LayoutLoad = ...`), auto-instrumentation failed. This patch extends the regex to handle this case and adds a test for it.
1 parent 2cc7708 commit 194130e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/sveltekit/src/vite/autoInstrument.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
9898
debug && console.log(`Skipping wrapping ${id} because it already contains Sentry code`);
9999
}
100100

101-
const hasLoadDeclaration = /((const|let|var|function)\s+load\s*(=|\())|as\s+load\s*(,|})/gm.test(codeWithoutComments);
101+
const hasLoadDeclaration = /((const|let|var|function)\s+load\s*(=|\(|:))|as\s+load\s*(,|})/gm.test(
102+
codeWithoutComments,
103+
);
102104
if (!hasLoadDeclaration) {
103105
// eslint-disable-next-line no-console
104106
debug && console.log(`Skipping wrapping ${id} because it doesn't declare a \`load\` function`);

packages/sveltekit/test/vite/autoInstrument.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ describe('canWrapLoad', () => {
176176
async function somethingElse(){};
177177
export { somethingElse as load, foo }`,
178178
],
179+
180+
[
181+
'export variable declaration - inline function with assigned type',
182+
`import type { LayoutLoad } from './$types';
183+
export const load : LayoutLoad = async () => { return { props: { msg: "hi" } } }`,
184+
],
179185
])('returns `true` if a load declaration (%s) exists and no Sentry code was found', async (_, code) => {
180186
fileContent = code;
181187
expect(await canWrapLoad('+page.ts', false)).toEqual(true);

0 commit comments

Comments
 (0)