Commit c1a7b22
ref(sveltekit): Replace recast + @babel/parser with acorn (#19533)
Replaces `recast` and `@babel/parser` with `acorn` and
`@sveltejs/acorn-typescript` for AST parsing in the sveltekit
auto-instrumentation plugin.
- [x] If you've added code that should be tested, please add tests.
- [x] Ensure your code lints and the test suite passes (`yarn lint`) &
(`yarn test`).
- [ ] Link an issue if there is one related to your pull request.
Ref #19447
## What this does
The `autoInstrument` Vite plugin parses SvelteKit `+page.ts` /
`+layout.server.ts` files to detect `export const load` or `export
function load` declarations. It only **reads** the AST — never
transforms or prints it — so recast's source-preserving round-trip
feature is entirely unused.
This replaces the full recast + @babel/parser pipeline with acorn
(Node.js's own parser) and `@sveltejs/acorn-typescript` (the actively
maintained TypeScript plugin used by SvelteKit itself).
### Changes
- **Delete** `recastTypescriptParser.ts` — the 91-line Babel parser
config with 20+ plugin declarations is no longer needed
- **Replace** `recast.parse()` with
`acorn.Parser.extend(tsPlugin()).parse()` in `autoInstrument.ts`
- **Fix** AST node type: `StringLiteral` (Babel-specific) → `Literal`
(ESTree standard) for string literal export detection
- **Fix** AST structure: `ast.program` (recast's File wrapper) → direct
`ast` (acorn returns Program)
- **Improve** error handling: `try/catch` around parse (acorn throws
SyntaxError) replaces dubious null check
- **Improve** type safety: remove unsafe double-cast
`(specifier.exported as unknown as t.StringLiteral)` in favor of proper
discriminant narrowing
### Dependency reduction
**Removed:** `recast`, `ast-types`, `esprima`, `source-map`, `tslib`,
`tiny-invariant`, `@babel/parser`, `@babel/types`,
`@babel/helper-string-parser`, `@babel/helper-validator-identifier`
**Added:** `acorn` (already in dep tree via Vite/Rollup),
`@sveltejs/acorn-typescript` (already in dep tree via `@sveltejs/kit`)
Net effect: ~8 fewer transitive dependencies, zero new packages for end
users.
### Why @sveltejs/acorn-typescript?
The original `acorn-typescript` package by TyrealHu hasn't been updated
since January 2024 and has known unpatched bugs. The Svelte team's fork
(`@sveltejs/acorn-typescript`) is actively maintained and is already a
transitive dependency of every SvelteKit project.
All 69 existing tests pass without modification.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: JPeer264 <jan.peer@sentry.io>
Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>1 parent ddca82a commit c1a7b22
File tree
4 files changed
+37
-135
lines changed- packages/sveltekit
- src/vite
4 files changed
+37
-135
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
55 | 54 | | |
| 55 | + | |
| 56 | + | |
56 | 57 | | |
57 | | - | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | | - | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
133 | 135 | | |
134 | 136 | | |
135 | 137 | | |
136 | 138 | | |
137 | 139 | | |
138 | 140 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
| 141 | + | |
143 | 142 | | |
144 | 143 | | |
145 | 144 | | |
| |||
160 | 159 | | |
161 | 160 | | |
162 | 161 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 162 | + | |
| 163 | + | |
168 | 164 | | |
169 | 165 | | |
170 | 166 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1914 | 1914 | | |
1915 | 1915 | | |
1916 | 1916 | | |
1917 | | - | |
1918 | | - | |
1919 | | - | |
1920 | | - | |
1921 | | - | |
1922 | | - | |
1923 | | - | |
1924 | 1917 | | |
1925 | 1918 | | |
1926 | 1919 | | |
| |||
2981 | 2974 | | |
2982 | 2975 | | |
2983 | 2976 | | |
2984 | | - | |
| 2977 | + | |
2985 | 2978 | | |
2986 | 2979 | | |
2987 | 2980 | | |
| |||
8652 | 8645 | | |
8653 | 8646 | | |
8654 | 8647 | | |
8655 | | - | |
8656 | | - | |
8657 | | - | |
| 8648 | + | |
| 8649 | + | |
| 8650 | + | |
| 8651 | + | |
| 8652 | + | |
| 8653 | + | |
| 8654 | + | |
| 8655 | + | |
8658 | 8656 | | |
8659 | 8657 | | |
8660 | 8658 | | |
| |||
25831 | 25829 | | |
25832 | 25830 | | |
25833 | 25831 | | |
25834 | | - | |
25835 | | - | |
25836 | | - | |
25837 | | - | |
25838 | | - | |
25839 | | - | |
25840 | | - | |
25841 | | - | |
25842 | | - | |
25843 | | - | |
25844 | | - | |
25845 | 25832 | | |
25846 | 25833 | | |
25847 | 25834 | | |
| |||
25862 | 25849 | | |
25863 | 25850 | | |
25864 | 25851 | | |
| 25852 | + | |
| 25853 | + | |
| 25854 | + | |
| 25855 | + | |
| 25856 | + | |
| 25857 | + | |
| 25858 | + | |
| 25859 | + | |
| 25860 | + | |
| 25861 | + | |
| 25862 | + | |
25865 | 25863 | | |
25866 | 25864 | | |
25867 | 25865 | | |
| |||
0 commit comments