@rsvelte/compiler 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -51
- package/package.json +1 -1
- package/svelte_compiler_rust_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -1,39 +1,52 @@
|
|
|
1
1
|
# rsvelte
|
|
2
2
|
|
|
3
|
-
> **⚠️ Early Stage Project** —
|
|
3
|
+
> **⚠️ Early Stage Project** — rsvelte already passes the official Svelte 5 compiler test suite end-to-end, but it's still pre-1.0. APIs, output, and behaviour may change without notice. Use it in production at your own risk.
|
|
4
4
|
|
|
5
|
-
A Rust port of the official Svelte 5 compiler
|
|
5
|
+
**A Rust port of the official Svelte 5 compiler, built to slot natively into the [OXC](https://oxc.rs/) ecosystem.**
|
|
6
|
+
|
|
7
|
+
## Why rsvelte exists
|
|
8
|
+
|
|
9
|
+
The end goal isn't "another Svelte compiler" — it's making Svelte a first-class citizen of OXC's Rust-native JavaScript/TypeScript toolchain.
|
|
10
|
+
|
|
11
|
+
Today, the native JS toolchain that has grown up around OXC — `oxlint`, `oxfmt`, [Rolldown](https://rolldown.rs/), and [`tsgo`](https://github.com/microsoft/typescript-go) (wired into `oxlint` via [`tsgolint`](https://github.com/oxc-project/tsgolint)) — can only see `.js` / `.ts` / `.jsx` / `.tsx` files. `.svelte` files are invisible to them because parsing Svelte requires running the JavaScript-based Svelte compiler, which native tools can't and won't link against. The result: Svelte developers don't get the order-of-magnitude speed-ups that the rest of the JS ecosystem is starting to take for granted.
|
|
12
|
+
|
|
13
|
+
rsvelte fixes that at the source. By porting the compiler — **and** the surrounding ecosystem hot paths (`svelte2tsx`, `svelte-check`, `vite-plugin-svelte`) — to Rust on top of OXC's own parser, codegen, and semantic stack, rsvelte gives OXC a Svelte surface it can call into directly. Once upstreamed, that surface unlocks:
|
|
14
|
+
|
|
15
|
+
- **`oxlint`** — lint `<script>` blocks and Svelte-specific patterns at OXC speed (a Rust path forward for `eslint-plugin-svelte`).
|
|
16
|
+
- **`oxfmt`** — format `.svelte` files alongside the rest of the project (a Rust path forward for `prettier-plugin-svelte`).
|
|
17
|
+
- **Rolldown** — native bundling of Svelte projects through OXC's parser stack, without a JS-side compiler hop.
|
|
18
|
+
- **`tsgo` + `tsgolint`** — type-checking and type-aware linting over `.svelte` files. Already wired into `@rsvelte/svelte-check` today as the correctness bridge.
|
|
19
|
+
|
|
20
|
+
Until we get there, the drop-in replacement story — `@rsvelte/compiler`, `@rsvelte/svelte-check`, `@rsvelte/vite-plugin-svelte` — lets you use rsvelte today and acts as the correctness bridge that proves the Rust port is byte-identical to upstream Svelte.
|
|
6
21
|
|
|
7
22
|
## Packages
|
|
8
23
|
|
|
9
|
-
|
|
24
|
+
All packages ship under the `@rsvelte` scope on npm.
|
|
10
25
|
|
|
11
26
|
| Package | Drop-in for | Status |
|
|
12
27
|
|---|---|---|
|
|
13
|
-
| [`@rsvelte/compiler`](npm/compiler) | [`svelte/compiler`](https://svelte.dev/docs/svelte-compiler) | ✅ 100% test compat ([details](#compatibility)) |
|
|
28
|
+
| [`@rsvelte/compiler`](npm/compiler) | [`svelte/compiler`](https://svelte.dev/docs/svelte-compiler) (wasm) | ✅ 100% test compat ([details](#compatibility)) |
|
|
14
29
|
| [`@rsvelte/svelte2tsx`](npm/svelte2tsx) | [`svelte2tsx`](https://github.com/sveltejs/language-tools/tree/master/packages/svelte2tsx) | ✅ 245 / 245 fixtures |
|
|
15
|
-
| [`@rsvelte/svelte-check`](npm/svelte-check) | [`svelte-check`](https://github.com/sveltejs/language-tools/tree/master/packages/svelte-check) CLI |
|
|
16
|
-
| [`@rsvelte/vite-plugin-svelte`](https://github.com/baseballyama/vite-plugin-svelte/tree/rsvelte) | [`@sveltejs/vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte) |
|
|
17
|
-
| [`@rsvelte/vite-plugin-svelte-native`](npm/vite-plugin-svelte-native) | — | NAPI bindings
|
|
30
|
+
| [`@rsvelte/svelte-check`](npm/svelte-check) | [`svelte-check`](https://github.com/sveltejs/language-tools/tree/master/packages/svelte-check) CLI | ✅ v1.0 — walker + overlay + tsgo backend + incremental + watch |
|
|
31
|
+
| [`@rsvelte/vite-plugin-svelte`](https://github.com/baseballyama/vite-plugin-svelte/tree/rsvelte) | [`@sveltejs/vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte) | ✅ v1.0 — fork that routes through the NAPI compiler |
|
|
32
|
+
| [`@rsvelte/vite-plugin-svelte-native`](npm/vite-plugin-svelte-native) | — | NAPI bindings the Vite plugin and other Node tools consume |
|
|
18
33
|
|
|
19
|
-
See [`docs/ecosystem-implementation-plan.md`](docs/ecosystem-implementation-plan.md) for the full ecosystem port plan.
|
|
34
|
+
See [`docs/ecosystem-implementation-plan.md`](docs/ecosystem-implementation-plan.md) for the full ecosystem port plan, including which upstream tools are intentionally **out of scope** (and where they're being routed instead — usually back to OXC).
|
|
20
35
|
|
|
21
|
-
## Quick
|
|
36
|
+
## Quick start
|
|
22
37
|
|
|
23
|
-
###
|
|
38
|
+
### Use as `svelte/compiler` (wasm)
|
|
24
39
|
|
|
25
40
|
```bash
|
|
26
41
|
npm install @rsvelte/compiler
|
|
27
42
|
```
|
|
28
43
|
|
|
29
|
-
Use it as a drop-in replacement for `svelte/compiler`:
|
|
30
|
-
|
|
31
44
|
```js
|
|
32
|
-
import { compile, compileModule, parse } from '@rsvelte/compiler';
|
|
45
|
+
import { compile, compileModule, parse, VERSION } from '@rsvelte/compiler';
|
|
33
46
|
|
|
34
47
|
const result = compile('<h1>Hello, {name}!</h1>', {
|
|
35
|
-
generate: 'client',
|
|
36
|
-
filename: 'App.svelte'
|
|
48
|
+
generate: 'client', // or 'server'
|
|
49
|
+
filename: 'App.svelte',
|
|
37
50
|
});
|
|
38
51
|
|
|
39
52
|
console.log(result.js.code);
|
|
@@ -41,18 +54,22 @@ console.log(result.css?.code);
|
|
|
41
54
|
|
|
42
55
|
// Compile a Svelte module (.svelte.js / .svelte.ts)
|
|
43
56
|
const moduleResult = compileModule('export const count = $state(0);', {
|
|
44
|
-
filename: 'counter.svelte.js'
|
|
57
|
+
filename: 'counter.svelte.js',
|
|
45
58
|
});
|
|
46
59
|
|
|
47
|
-
// Parse
|
|
60
|
+
// Parse to AST
|
|
48
61
|
const ast = parse('<h1>Hello</h1>', { modern: true });
|
|
62
|
+
|
|
63
|
+
console.log(VERSION); // upstream Svelte version this build targets
|
|
49
64
|
```
|
|
50
65
|
|
|
51
|
-
The
|
|
66
|
+
The public surface mirrors [`svelte/compiler`](https://svelte.dev/docs/svelte-compiler) — `compile`, `compileModule`, `parse`, and `VERSION` are all available. Output is byte-identical to the official compiler on every in-scope fixture (see [Compatibility](#compatibility)).
|
|
67
|
+
|
|
68
|
+
> **Heads-up:** a few function-valued options can't cross the wasm / NAPI boundary. See [Compiler option compatibility](#compiler-option-compatibility) before passing `cssHash` or `warningFilter`.
|
|
52
69
|
|
|
53
|
-
### Vite
|
|
70
|
+
### Use with Vite
|
|
54
71
|
|
|
55
|
-
|
|
72
|
+
[`@rsvelte/vite-plugin-svelte`](https://github.com/baseballyama/vite-plugin-svelte/tree/rsvelte) is a fork of `@sveltejs/vite-plugin-svelte` that swaps in the rsvelte compiler. The public API matches upstream exactly — your `vite.config.js` doesn't need to change.
|
|
56
73
|
|
|
57
74
|
```bash
|
|
58
75
|
npm install -D @rsvelte/vite-plugin-svelte
|
|
@@ -64,19 +81,19 @@ import { svelte } from '@rsvelte/vite-plugin-svelte';
|
|
|
64
81
|
import { defineConfig } from 'vite';
|
|
65
82
|
|
|
66
83
|
export default defineConfig({
|
|
67
|
-
plugins: [svelte()]
|
|
84
|
+
plugins: [svelte()],
|
|
68
85
|
});
|
|
69
86
|
```
|
|
70
87
|
|
|
71
|
-
### SvelteKit
|
|
88
|
+
### Use with SvelteKit
|
|
72
89
|
|
|
73
|
-
SvelteKit
|
|
90
|
+
SvelteKit pulls in `@sveltejs/vite-plugin-svelte` internally, so the cleanest swap is a package-manager override that redirects the upstream plugin to the rsvelte fork. With pnpm:
|
|
74
91
|
|
|
75
92
|
```bash
|
|
76
93
|
pnpm add -D @rsvelte/vite-plugin-svelte
|
|
77
94
|
```
|
|
78
95
|
|
|
79
|
-
```
|
|
96
|
+
```jsonc
|
|
80
97
|
// package.json
|
|
81
98
|
{
|
|
82
99
|
"pnpm": {
|
|
@@ -87,42 +104,106 @@ pnpm add -D @rsvelte/vite-plugin-svelte
|
|
|
87
104
|
}
|
|
88
105
|
```
|
|
89
106
|
|
|
90
|
-
Then
|
|
107
|
+
Then `pnpm install`. No changes to `vite.config.js` or `svelte.config.js` are needed. (npm and yarn ship equivalent `overrides` / `resolutions` fields if you prefer those.)
|
|
91
108
|
|
|
92
|
-
###
|
|
109
|
+
### Type-check with `svelte-check`
|
|
110
|
+
|
|
111
|
+
`@rsvelte/svelte-check` is a drop-in CLI replacement for `svelte-check`, backed by a Rust walker plus a tsgo overlay for `<script lang="ts">` diagnostics.
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
npm install -D @rsvelte/svelte-check
|
|
115
|
+
npx svelte-check
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Common flags:
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
npx svelte-check --workspace . # type-check the current workspace
|
|
122
|
+
npx svelte-check --tsgo # run tsgo against the .svelte overlay (recommended)
|
|
123
|
+
npx svelte-check --watch # re-check on file changes
|
|
124
|
+
npx svelte-check --incremental # reuse cached overlay between runs
|
|
125
|
+
npx svelte-check --output machine # JSON-friendly output for CI
|
|
126
|
+
npx svelte-check --fail-on-warnings # treat warnings as errors
|
|
127
|
+
npx svelte-check --compiler-warnings "css-unused-selector:ignore"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
See `npx svelte-check --help` for the full list. The CLI flag set is a superset of upstream's — every upstream flag works, plus a few rsvelte-specific ones (`--tsgo`, `--emit-overlay`).
|
|
131
|
+
|
|
132
|
+
### Convert `.svelte` to `.tsx` (`svelte2tsx`)
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install @rsvelte/svelte2tsx
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
import { svelte2tsx } from '@rsvelte/svelte2tsx';
|
|
140
|
+
|
|
141
|
+
const result = await svelte2tsx('<h1>Hello, {name}!</h1>', {
|
|
142
|
+
filename: 'App.svelte',
|
|
143
|
+
isTsFile: true,
|
|
144
|
+
mode: 'ts', // or 'dts' to emit a declaration file
|
|
145
|
+
version: '5',
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
console.log(result.code); // the synthesised .tsx
|
|
149
|
+
console.log(result.exportedNames); // { props, all }
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Useful if you're building your own language tooling on top of the same surface `svelte-check`, the Svelte language server, and `tsc` all rely on.
|
|
153
|
+
|
|
154
|
+
### Embed in a Rust crate
|
|
155
|
+
|
|
156
|
+
```toml
|
|
157
|
+
[dependencies]
|
|
158
|
+
svelte-compiler-rust = { git = "https://github.com/baseballyama/rsvelte" }
|
|
159
|
+
```
|
|
93
160
|
|
|
94
161
|
```rust
|
|
95
|
-
use
|
|
162
|
+
use svelte_compiler_rust::{compile, CompileOptions};
|
|
96
163
|
|
|
97
164
|
let source = r#"<h1>Hello, {name}!</h1>"#;
|
|
98
165
|
let result = compile(source, CompileOptions::default()).unwrap();
|
|
99
166
|
println!("{}", result.js.code);
|
|
100
167
|
```
|
|
101
168
|
|
|
102
|
-
|
|
169
|
+
The Rust API is the same surface OXC will eventually wire `oxlint` / `oxfmt` into. Unlike the JS surface, the Rust `CompileOptions` honours **every** field — including `css_hash` and `warning_filter` as real Rust closures.
|
|
170
|
+
|
|
171
|
+
## Compiler option compatibility
|
|
172
|
+
|
|
173
|
+
The JS-facing surfaces (`@rsvelte/compiler` wasm bundle, `@rsvelte/vite-plugin-svelte-native` NAPI bindings) accept the full `svelte/compiler#CompileOptions` shape, but **function-valued** options can't currently cross the language boundary. The Rust core has no way to call back into JavaScript, so callback-shaped fields are accepted (so the TypeScript types stay drop-in compatible with upstream Svelte) and then **silently ignored**.
|
|
174
|
+
|
|
175
|
+
If your build relies on any of these, the value won't take effect. Use the workarounds below.
|
|
176
|
+
|
|
177
|
+
| Option | Behaviour in rsvelte (JS surface) | Workaround |
|
|
178
|
+
|---|---|---|
|
|
179
|
+
| `cssHash({ hash, name, filename, css }) => string` | Ignored. CSS scope classes fall back to the default `svelte-<base36hash>` scheme — identical to upstream Svelte's default `cssHash`. | Pre-compute the hash on the JS side and pass it as `cssHashOverride: '<hash>'` — an rsvelte-specific extension that injects a deterministic string. |
|
|
180
|
+
| `warningFilter(warning) => boolean` | Ignored. All compiler warnings are returned unfiltered. | Filter `result.warnings` yourself after compilation. |
|
|
181
|
+
|
|
182
|
+
Everything else (`generate`, `css`, `dev`, `hmr`, `sourcemap`, `runes`, `compatibility`, `experimental.async`, `preserveComments`, `preserveWhitespace`, `customElement`, `accessors`, `namespace`, `immutable`, `modernAst`, `discloseVersion`, `outputFilename`, `cssOutputFilename`, …) matches upstream exactly. The full list of accepted fields is mirrored in [`npm/vite-plugin-svelte-native/index.d.ts`](npm/vite-plugin-svelte-native/index.d.ts).
|
|
103
183
|
|
|
104
|
-
|
|
105
|
-
- **2.1x faster single-threaded, 15.8x faster multi-threaded** vs the official JS compiler
|
|
106
|
-
- **Drop-in replacement** — N-API bindings for seamless use with existing tools (Vite, SvelteKit, …)
|
|
107
|
-
- **WASM build** — runs in the browser (used by the docs playground)
|
|
108
|
-
- **Ecosystem port underway** — `svelte2tsx` already at 100%; `svelte-check` and `vite-plugin-svelte` shim in progress
|
|
184
|
+
The Rust API (`svelte_compiler_rust::compile`) has no such restriction — `css_hash: Option<CssHashFn>` and `warning_filter: Option<WarningFilterFn>` work as real `Arc<dyn Fn>` closures.
|
|
109
185
|
|
|
110
186
|
## Performance
|
|
111
187
|
|
|
112
|
-
|
|
188
|
+
Per-task benchmark across 3,637 real `.svelte` files, 10 iterations (3 warmup), against the official `svelte/compiler`:
|
|
113
189
|
|
|
114
|
-
|
|
|
115
|
-
|
|
116
|
-
| **
|
|
117
|
-
| **
|
|
118
|
-
|
|
|
190
|
+
| Task | JS (`svelte/compiler`) | Rust (single-threaded) | Rust (multi-threaded) | Multi vs JS |
|
|
191
|
+
|---|---:|---:|---:|---:|
|
|
192
|
+
| **Full pipeline** — parse / analyze / codegen | 864.8 ms | 381.1 ms | 50.1 ms | **17.3×** |
|
|
193
|
+
| **Parser only** — phase 1, isolated | 187.5 ms | 8.7 ms | 1.9 ms | **99.5×** |
|
|
194
|
+
| **`svelte2tsx`** — `.svelte` → `.tsx` generation | 306.1 ms | 115.3 ms | 16.0 ms | **19.1×** |
|
|
195
|
+
| **`svelte-check`** — CLI, 500-file workspace | 2,088.0 ms | 46.9 ms | 13.8 ms | **151.5×** |
|
|
119
196
|
|
|
120
|
-
> Apple M1
|
|
197
|
+
> Apple M1 Pro · 10-core arm64 · 3,637 `.svelte` files · 10 iterations (3 warmup). Recorded 2026-05-24 at commit `da6b3c8`. Live numbers, charts, and reproduction steps live on the [benchmark page](https://baseballyama.github.io/rsvelte/benchmark) (or run `node scripts/run-benchmark.mjs > docs/static/benchmark-results.json` locally).
|
|
121
198
|
|
|
122
|
-
A single-threaded **100× speedup** over the JS compiler is one of this project's explicit goals —
|
|
199
|
+
A single-threaded **100× speedup** over the JS compiler is one of this project's explicit goals — the parser is already at multi-threaded `99.5×` and `svelte-check` at `151.5×`, but the full pipeline is still climbing. Current numbers are a snapshot, not a ceiling.
|
|
123
200
|
|
|
124
201
|
## Compatibility
|
|
125
202
|
|
|
203
|
+
<!-- svelte-target-version -->
|
|
204
|
+
**Targeting Svelte `v5.51.5`** ([`8ea33bf7fe86`](https://github.com/sveltejs/svelte/commit/8ea33bf7fe86)) — automatically maintained by `pnpm run update-docs`.
|
|
205
|
+
<!-- /svelte-target-version -->
|
|
206
|
+
|
|
126
207
|
Current compatibility with the official Svelte compiler test suite:
|
|
127
208
|
|
|
128
209
|
| Test Suite | Pass | Total | Status | Notes |
|
|
@@ -149,11 +230,11 @@ Re-run `pnpm run test-and-update` to refresh these numbers.
|
|
|
149
230
|
|
|
150
231
|
## Goals
|
|
151
232
|
|
|
152
|
-
1. **
|
|
153
|
-
2. **100
|
|
154
|
-
3. **
|
|
155
|
-
4. **
|
|
156
|
-
5. **
|
|
233
|
+
1. **OXC ecosystem integration** — be the Svelte surface that `oxlint`, `oxfmt`, Rolldown, and `tsgo` (via `tsgolint`) all link against. This is the project's reason for existing; everything else is a step toward it.
|
|
234
|
+
2. **100% test compatibility** with the official `svelte/compiler` test suite — keeps the Rust port provably equivalent to upstream while OXC integration lands.
|
|
235
|
+
3. **100× single-threaded speedup** over the JS compiler via Rust + OXC.
|
|
236
|
+
4. **Drop-in replacements** for the ecosystem hot paths (`svelte/compiler`, `svelte-check`, `vite-plugin-svelte`, `svelte2tsx`) so you can adopt rsvelte today without touching the rest of your build.
|
|
237
|
+
5. **Ecosystem port** — see [`docs/ecosystem-implementation-plan.md`](docs/ecosystem-implementation-plan.md) for the multi-wave plan.
|
|
157
238
|
|
|
158
239
|
## Architecture
|
|
159
240
|
|
|
@@ -168,11 +249,11 @@ src/compiler/phases/
|
|
|
168
249
|
|
|
169
250
|
Key design decisions:
|
|
170
251
|
|
|
171
|
-
-
|
|
172
|
-
-
|
|
173
|
-
- Direct AST passing between phases — no re-parsing
|
|
174
|
-
- Parallel processing with `rayon
|
|
175
|
-
- No backward-compat shims for internal APIs — refactor freely
|
|
252
|
+
- JavaScript parsing, semantic analysis, and codegen all run on OXC — the same crates `oxlint` / `oxfmt` use, so the OXC integration target stays cheap.
|
|
253
|
+
- Memory-efficient AST (u32 positions, `compact_str`, `bumpalo`-arena allocation on hot paths).
|
|
254
|
+
- Direct AST passing between phases — no re-parsing.
|
|
255
|
+
- Parallel processing with `rayon`.
|
|
256
|
+
- No backward-compat shims for internal APIs — refactor freely.
|
|
176
257
|
|
|
177
258
|
## Development
|
|
178
259
|
|
|
@@ -233,6 +314,10 @@ Tests an error-mode option not yet wired through rsvelte's diagnostic pipeline.
|
|
|
233
314
|
|
|
234
315
|
Two svelte2tsx fixtures shaped around `expected.error.json` (error-path assertions) are skipped pending a structured error-fixture runner.
|
|
235
316
|
|
|
317
|
+
### Function-valued compiler options (JS surface)
|
|
318
|
+
|
|
319
|
+
See [Compiler option compatibility](#compiler-option-compatibility). The Rust API is unaffected.
|
|
320
|
+
|
|
236
321
|
## License
|
|
237
322
|
|
|
238
323
|
MIT
|
package/package.json
CHANGED
|
Binary file
|