Skip to content

Commit fa80627

Browse files
roli-lpciclaude
andcommitted
chore(sveltekit): replace recast + @babel/parser with acorn
Replaces recast and @babel/parser with acorn and @sveltejs/acorn-typescript for AST parsing in the auto-instrumentation plugin. Since the code only reads the AST (never transforms or prints), the full recast pipeline is unnecessary. Key changes: - Delete recastTypescriptParser.ts (no longer needed) - Use acorn Parser.extend(tsPlugin()) for TypeScript parsing - Use @sveltejs/acorn-typescript (actively maintained) instead of TyrealHu's abandoned acorn-typescript - Fix StringLiteral -> Literal node type check (ESTree compliance) - Fix ast.program -> ast (acorn returns Program directly) - Use try/catch for parse errors instead of null program check Removes ~8 transitive dependencies (recast, ast-types, esprima, source-map, tslib, tiny-invariant, @babel/parser, @babel/types). Ref: #19447 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 17b7532 commit fa80627

File tree

4 files changed

+34
-130
lines changed

4 files changed

+34
-130
lines changed

packages/sveltekit/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@
4747
}
4848
},
4949
"dependencies": {
50-
"@babel/parser": "7.26.9",
5150
"@sentry/cloudflare": "10.40.0",
5251
"@sentry/core": "10.40.0",
5352
"@sentry/node": "10.40.0",
5453
"@sentry/svelte": "10.40.0",
5554
"@sentry/vite-plugin": "^5.1.0",
55+
"@sveltejs/acorn-typescript": "^1.0.9",
56+
"acorn": "^8.14.0",
5657
"magic-string": "0.30.7",
57-
"recast": "0.23.11",
5858
"sorcery": "1.0.0"
5959
},
6060
"devDependencies": {
61-
"@babel/types": "^7.26.3",
6261
"@sveltejs/kit": "^2.52.2",
6362
"@sveltejs/vite-plugin-svelte": "^3.0.0",
6463
"svelte": "^4.2.8",

packages/sveltekit/src/vite/autoInstrument.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import * as acorn from 'acorn';
2+
import { tsPlugin } from '@sveltejs/acorn-typescript';
13
import * as fs from 'fs';
24
import * as path from 'path';
3-
import * as recast from 'recast';
45
import type { Plugin } from 'vite';
56
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
6-
import { parser } from './recastTypescriptParser';
7-
import t = recast.types.namedTypes;
7+
8+
const AcornParser = acorn.Parser.extend(tsPlugin());
89

910
export type AutoInstrumentSelection = {
1011
/**
@@ -123,22 +124,22 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
123124

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

126-
const ast = recast.parse(code, {
127-
parser,
128-
});
129-
130-
const program = (ast as { program?: t.Program }).program;
131-
132-
if (!program) {
127+
let program: acorn.Program;
128+
try {
129+
program = AcornParser.parse(code, {
130+
sourceType: 'module',
131+
ecmaVersion: 'latest',
132+
locations: true,
133+
});
134+
} catch {
133135
// eslint-disable-next-line no-console
134136
debug && console.log(`Skipping wrapping ${id} because it doesn't contain valid JavaScript or TypeScript`);
135137
return false;
136138
}
137139

138140
const hasLoadDeclaration = program.body
139141
.filter(
140-
(statement): statement is recast.types.namedTypes.ExportNamedDeclaration =>
141-
statement.type === 'ExportNamedDeclaration',
142+
(statement): statement is acorn.ExportNamedDeclaration => statement.type === 'ExportNamedDeclaration',
142143
)
143144
.find(exportDecl => {
144145
// find `export const load = ...`
@@ -160,11 +161,8 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
160161
return exportDecl.specifiers.find(specifier => {
161162
return (
162163
(specifier.exported.type === 'Identifier' && specifier.exported.name === 'load') ||
163-
// Type casting here because somehow the 'exportExtensions' plugin isn't reflected in the possible types
164-
// This plugin adds support for exporting something as a string literal (see comment above)
165-
// Doing this to avoid adding another babel plugin dependency
166-
((specifier.exported.type as 'StringLiteral' | '') === 'StringLiteral' &&
167-
(specifier.exported as unknown as t.StringLiteral).value === 'load')
164+
// ESTree/acorn represents `export { x as "load" }` with a Literal node (not Babel's StringLiteral)
165+
(specifier.exported.type === 'Literal' && specifier.exported.value === 'load')
168166
);
169167
});
170168
}

packages/sveltekit/src/vite/recastTypescriptParser.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

yarn.lock

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,13 +1914,6 @@
19141914
"@babel/template" "^7.28.6"
19151915
"@babel/types" "^7.28.6"
19161916

1917-
"@babel/parser@7.26.9":
1918-
version "7.26.9"
1919-
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.26.9.tgz#d9e78bee6dc80f9efd8f2349dcfbbcdace280fd5"
1920-
integrity sha512-81NWa1njQblgZbQHxWHpxxCzNsa3ZwvFqpUg7P+NNUU6f3UU2jBEg4OlF/J6rl8+PQGh1q6/zWScd001YwcA5A==
1921-
dependencies:
1922-
"@babel/types" "^7.26.9"
1923-
19241917
"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.4", "@babel/parser@^7.18.10", "@babel/parser@^7.20.7", "@babel/parser@^7.22.10", "@babel/parser@^7.22.16", "@babel/parser@^7.23.5", "@babel/parser@^7.23.6", "@babel/parser@^7.25.4", "@babel/parser@^7.26.7", "@babel/parser@^7.27.7", "@babel/parser@^7.28.0", "@babel/parser@^7.28.4", "@babel/parser@^7.28.5", "@babel/parser@^7.28.6", "@babel/parser@^7.29.0", "@babel/parser@^7.4.5", "@babel/parser@^7.7.0":
19251918
version "7.29.0"
19261919
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.29.0.tgz#669ef345add7d057e92b7ed15f0bac07611831b6"
@@ -2981,7 +2974,7 @@
29812974
"@babel/types" "^7.29.0"
29822975
debug "^4.3.1"
29832976

2984-
"@babel/types@^7.0.0", "@babel/types@^7.18.10", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.22.10", "@babel/types@^7.22.15", "@babel/types@^7.22.17", "@babel/types@^7.22.19", "@babel/types@^7.23.6", "@babel/types@^7.24.7", "@babel/types@^7.25.4", "@babel/types@^7.26.3", "@babel/types@^7.26.8", "@babel/types@^7.26.9", "@babel/types@^7.27.1", "@babel/types@^7.27.3", "@babel/types@^7.27.7", "@babel/types@^7.28.2", "@babel/types@^7.28.5", "@babel/types@^7.28.6", "@babel/types@^7.29.0", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.7.0", "@babel/types@^7.7.2":
2977+
"@babel/types@^7.0.0", "@babel/types@^7.18.10", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.22.10", "@babel/types@^7.22.15", "@babel/types@^7.22.17", "@babel/types@^7.22.19", "@babel/types@^7.23.6", "@babel/types@^7.24.7", "@babel/types@^7.25.4", "@babel/types@^7.26.8", "@babel/types@^7.27.1", "@babel/types@^7.27.3", "@babel/types@^7.27.7", "@babel/types@^7.28.2", "@babel/types@^7.28.5", "@babel/types@^7.28.6", "@babel/types@^7.29.0", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.7.0", "@babel/types@^7.7.2":
29852978
version "7.29.0"
29862979
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.29.0.tgz#9f5b1e838c446e72cf3cd4b918152b8c605e37c7"
29872980
integrity sha512-LwdZHpScM4Qz8Xw2iKSzS+cfglZzJGvofQICy7W7v4caru4EaAmyUuO6BGrbyQ2mYV11W0U8j5mBhd14dd3B0A==
@@ -8514,6 +8507,11 @@
85148507
resolved "https://registry.yarnpkg.com/@sveltejs/acorn-typescript/-/acorn-typescript-1.0.8.tgz#69c746a7c232094c117c50dedbd1279fc64887b7"
85158508
integrity sha512-esgN+54+q0NjB0Y/4BomT9samII7jGwNy/2a3wNZbT2A2RpmXsXwUt24LvLhx6jUq2gVk4cWEvcRO6MFQbOfNA==
85168509

8510+
"@sveltejs/acorn-typescript@^1.0.9":
8511+
version "1.0.9"
8512+
resolved "https://registry.yarnpkg.com/@sveltejs/acorn-typescript/-/acorn-typescript-1.0.9.tgz#ac0bde368d6623727b0e0bc568cf6b4e5d5c4baa"
8513+
integrity sha512-lVJX6qEgs/4DOcRTpo56tmKzVPtoWAaVbL4hfO7t7NVwl9AAXzQR6cihesW1BmNMPl+bK6dreu2sOKBP2Q9CIA==
8514+
85178515
"@sveltejs/kit@^2.52.2":
85188516
version "2.52.2"
85198517
resolved "https://registry.yarnpkg.com/@sveltejs/kit/-/kit-2.52.2.tgz#8de4a96ef7b54a59ccb2d13f4297da3f22c3ec1d"
@@ -25674,17 +25672,6 @@ real-require@^0.2.0:
2567425672
resolved "https://registry.yarnpkg.com/real-require/-/real-require-0.2.0.tgz#209632dea1810be2ae063a6ac084fee7e33fba78"
2567525673
integrity sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==
2567625674

25677-
recast@0.23.11, recast@^0.23.4:
25678-
version "0.23.11"
25679-
resolved "https://registry.yarnpkg.com/recast/-/recast-0.23.11.tgz#8885570bb28cf773ba1dc600da7f502f7883f73f"
25680-
integrity sha512-YTUo+Flmw4ZXiWfQKGcwwc11KnoRAYgzAE2E7mXKCjSviTKShtxBsN6YUUBB2gtaBzKzeKunxhUwNHQuRryhWA==
25681-
dependencies:
25682-
ast-types "^0.16.1"
25683-
esprima "~4.0.0"
25684-
source-map "~0.6.1"
25685-
tiny-invariant "^1.3.3"
25686-
tslib "^2.0.1"
25687-
2568825675
recast@^0.18.1:
2568925676
version "0.18.10"
2569025677
resolved "https://registry.yarnpkg.com/recast/-/recast-0.18.10.tgz#605ebbe621511eb89b6356a7e224bff66ed91478"
@@ -25705,6 +25692,17 @@ recast@^0.20.5:
2570525692
source-map "~0.6.1"
2570625693
tslib "^2.0.1"
2570725694

25695+
recast@^0.23.4:
25696+
version "0.23.11"
25697+
resolved "https://registry.yarnpkg.com/recast/-/recast-0.23.11.tgz#8885570bb28cf773ba1dc600da7f502f7883f73f"
25698+
integrity sha512-YTUo+Flmw4ZXiWfQKGcwwc11KnoRAYgzAE2E7mXKCjSviTKShtxBsN6YUUBB2gtaBzKzeKunxhUwNHQuRryhWA==
25699+
dependencies:
25700+
ast-types "^0.16.1"
25701+
esprima "~4.0.0"
25702+
source-map "~0.6.1"
25703+
tiny-invariant "^1.3.3"
25704+
tslib "^2.0.1"
25705+
2570825706
rechoir@^0.6.2:
2570925707
version "0.6.2"
2571025708
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"

0 commit comments

Comments
 (0)