Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@
}
},
"dependencies": {
"@babel/parser": "7.26.9",
"@sentry/cloudflare": "10.40.0",
"@sentry/core": "10.40.0",
"@sentry/node": "10.40.0",
"@sentry/svelte": "10.40.0",
"@sentry/vite-plugin": "^5.1.0",
"@sveltejs/acorn-typescript": "^1.0.9",
"acorn": "^8.14.0",
"magic-string": "~0.30.0",
"recast": "0.23.11",
"sorcery": "1.0.0"
},
"devDependencies": {
"@babel/types": "^7.26.3",
"@sveltejs/kit": "^2.52.2",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"svelte": "^4.2.8",
Expand Down
34 changes: 15 additions & 19 deletions packages/sveltekit/src/vite/autoInstrument.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as acorn from 'acorn';
import { tsPlugin } from '@sveltejs/acorn-typescript';
import * as fs from 'fs';
import * as path from 'path';
import * as recast from 'recast';
import type { Plugin } from 'vite';
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
import { parser } from './recastTypescriptParser';
import t = recast.types.namedTypes;

const AcornParser = acorn.Parser.extend(tsPlugin());

export type AutoInstrumentSelection = {
/**
Expand Down Expand Up @@ -123,23 +124,21 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>

const code = (await fs.promises.readFile(id, 'utf8')).toString();

const ast = recast.parse(code, {
parser,
});

const program = (ast as { program?: t.Program }).program;

if (!program) {
let program: acorn.Program;
try {
program = AcornParser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
});
} catch {
// eslint-disable-next-line no-console
debug && console.log(`Skipping wrapping ${id} because it doesn't contain valid JavaScript or TypeScript`);
return false;
}

const hasLoadDeclaration = program.body
.filter(
(statement): statement is recast.types.namedTypes.ExportNamedDeclaration =>
statement.type === 'ExportNamedDeclaration',
)
.filter((statement): statement is acorn.ExportNamedDeclaration => statement.type === 'ExportNamedDeclaration')
.find(exportDecl => {
// find `export const load = ...`
if (exportDecl.declaration?.type === 'VariableDeclaration') {
Expand All @@ -160,11 +159,8 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
return exportDecl.specifiers.find(specifier => {
return (
(specifier.exported.type === 'Identifier' && specifier.exported.name === 'load') ||
// Type casting here because somehow the 'exportExtensions' plugin isn't reflected in the possible types
// This plugin adds support for exporting something as a string literal (see comment above)
// Doing this to avoid adding another babel plugin dependency
((specifier.exported.type as 'StringLiteral' | '') === 'StringLiteral' &&
(specifier.exported as unknown as t.StringLiteral).value === 'load')
// ESTree/acorn represents `export { x as "load" }` with a Literal node (not Babel's StringLiteral)
(specifier.exported.type === 'Literal' && specifier.exported.value === 'load')
);
});
}
Expand Down
91 changes: 0 additions & 91 deletions packages/sveltekit/src/vite/recastTypescriptParser.ts

This file was deleted.

Loading
Loading