@rsvelte/compiler 0.1.0

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.
Files changed (5) hide show
  1. package/README.md +183 -0
  2. package/index.cjs +79 -0
  3. package/index.d.ts +140 -0
  4. package/index.js +133 -0
  5. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # rsvelte
2
+
3
+ A high-performance Rust implementation of the Svelte compiler. Drop-in replacement for `svelte/compiler` with up to 100x performance improvement.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @rsvelte/compiler
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Basic Usage
14
+
15
+ ```js
16
+ import { compile, parse, VERSION } from '@rsvelte/compiler';
17
+
18
+ const result = compile('<h1>Hello {name}!</h1>', {
19
+ filename: 'App.svelte',
20
+ generate: 'client',
21
+ });
22
+
23
+ console.log(result.js.code);
24
+ ```
25
+
26
+ ### With Vite + vite-plugin-svelte
27
+
28
+ Use `resolve.alias` to replace the Svelte compiler:
29
+
30
+ ```js
31
+ // vite.config.js
32
+ import { defineConfig } from 'vite';
33
+ import { svelte } from '@sveltejs/vite-plugin-svelte';
34
+
35
+ export default defineConfig({
36
+ resolve: {
37
+ alias: {
38
+ 'svelte/compiler': '@rsvelte/compiler',
39
+ },
40
+ },
41
+ plugins: [svelte()],
42
+ });
43
+ ```
44
+
45
+ ## API
46
+
47
+ ### `compile(source, options?)`
48
+
49
+ Compile a Svelte component to JavaScript.
50
+
51
+ ```js
52
+ const result = compile(source, {
53
+ dev: false,
54
+ generate: 'client', // 'client' | 'server'
55
+ filename: 'Component.svelte',
56
+ css: 'external', // 'injected' | 'external'
57
+ // ... see CompileOptions for all options
58
+ });
59
+
60
+ // result.js.code - Generated JavaScript
61
+ // result.js.map - Source map
62
+ // result.css?.code - Generated CSS (if css: 'external')
63
+ // result.css?.map - CSS source map
64
+ // result.warnings - Compiler warnings
65
+ // result.metadata.runes - Whether component uses runes
66
+ ```
67
+
68
+ ### `compileModule(source, options?)`
69
+
70
+ Compile a Svelte module (`.svelte.js` / `.svelte.ts`).
71
+
72
+ ```js
73
+ const result = compileModule(source, {
74
+ dev: false,
75
+ generate: 'client',
76
+ filename: 'utils.svelte.js',
77
+ });
78
+ ```
79
+
80
+ ### `parse(source, options?)`
81
+
82
+ Parse a Svelte component into an AST.
83
+
84
+ ```js
85
+ const ast = parse('<h1>Hello</h1>', {
86
+ modern: true, // Use modern AST format
87
+ });
88
+ ```
89
+
90
+ ### `preprocess(source, preprocessors, options?)`
91
+
92
+ Preprocess a Svelte component (async).
93
+
94
+ ```js
95
+ const processed = await preprocess(source, [
96
+ {
97
+ markup({ content, filename }) {
98
+ return { code: transformedContent };
99
+ },
100
+ script({ content, filename }) {
101
+ return { code: transformedContent };
102
+ },
103
+ style({ content, filename }) {
104
+ return { code: transformedContent };
105
+ },
106
+ },
107
+ ], { filename: 'App.svelte' });
108
+ ```
109
+
110
+ ### `print(ast, options?)`
111
+
112
+ Print an AST back to source code.
113
+
114
+ ```js
115
+ const ast = parse(source, { modern: true });
116
+ const { code } = print(ast);
117
+ ```
118
+
119
+ ### `parseCss(source)`
120
+
121
+ Parse CSS into an AST.
122
+
123
+ ### `VERSION`
124
+
125
+ The compiler version string.
126
+
127
+ ```js
128
+ import { VERSION } from '@rsvelte/compiler';
129
+ console.log(VERSION); // "0.1.0"
130
+ ```
131
+
132
+ ## Limitations
133
+
134
+ ### Callback Options Not Supported
135
+
136
+ The following options accept JavaScript callback functions and are **not currently supported** in rsvelte. These options will be silently ignored, and the default behavior will be used instead:
137
+
138
+ | Option | Description | Default Behavior |
139
+ |--------|-------------|-----------------|
140
+ | `cssHash` | Custom function to generate CSS hash for scoping | Uses the built-in hash function (same algorithm as the official Svelte compiler default) |
141
+ | `warningFilter` | Function to filter compiler warnings | All warnings are included in the result |
142
+
143
+ **Why?** rsvelte is implemented in Rust and compiled to a native binary. JavaScript callback functions cannot be directly executed inside the Rust runtime. Supporting these would require crossing the Rust-JavaScript boundary for each invocation, which would negate the performance benefits.
144
+
145
+ **Workaround for `warningFilter`:** Filter warnings from the result after compilation:
146
+
147
+ ```js
148
+ const result = compile(source, options);
149
+ const filteredWarnings = result.warnings.filter(
150
+ (w) => w.code !== 'a11y-missing-attribute'
151
+ );
152
+ ```
153
+
154
+ **Workaround for `cssHash`:** Currently no workaround. The default hash function is used. If you require a custom hash, please open an issue.
155
+
156
+ ### Not Yet Implemented
157
+
158
+ | API | Status |
159
+ |-----|--------|
160
+ | `migrate()` | Not implemented - throws error |
161
+ | `walk()` | Deprecated in Svelte 5 - throws error directing to estree-walker |
162
+
163
+ ## Platform Support
164
+
165
+ rsvelte ships native binaries for the following platforms:
166
+
167
+ | Platform | Architecture | Package |
168
+ |----------|-------------|---------|
169
+ | macOS | ARM64 (Apple Silicon) | `@rsvelte/binding-darwin-arm64` |
170
+ | macOS | x64 (Intel) | `@rsvelte/binding-darwin-x64` |
171
+ | Linux | x64 | `@rsvelte/binding-linux-x64-gnu` |
172
+ | Linux | ARM64 | `@rsvelte/binding-linux-arm64-gnu` |
173
+ | Windows | x64 | `@rsvelte/binding-win32-x64-msvc` |
174
+
175
+ Native binaries are installed automatically via `optionalDependencies`. If no native binary is available for your platform, an error will be thrown at runtime.
176
+
177
+ ## Svelte Compatibility
178
+
179
+ rsvelte targets compatibility with **Svelte 5** (`svelte/compiler`). It passes 100% of the official Svelte compiler test suite (3028/3028 tests).
180
+
181
+ ## License
182
+
183
+ MIT
package/index.cjs ADDED
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ const { createRequire } = require('node:module');
4
+
5
+ let nativeBinding = null;
6
+ let loadError = null;
7
+
8
+ const platform = process.platform;
9
+ const arch = process.arch;
10
+
11
+ const platformPackages = {
12
+ 'darwin-arm64': '@rsvelte/binding-darwin-arm64',
13
+ 'darwin-x64': '@rsvelte/binding-darwin-x64',
14
+ 'linux-x64': '@rsvelte/binding-linux-x64-gnu',
15
+ 'linux-arm64': '@rsvelte/binding-linux-arm64-gnu',
16
+ 'win32-x64': '@rsvelte/binding-win32-x64-msvc',
17
+ };
18
+
19
+ const key = `${platform}-${arch}`;
20
+ const packageName = platformPackages[key];
21
+
22
+ if (packageName) {
23
+ try {
24
+ nativeBinding = require(packageName);
25
+ } catch (e) {
26
+ loadError = e;
27
+ }
28
+ } else {
29
+ loadError = new Error(`Unsupported platform: ${key}`);
30
+ }
31
+
32
+ function getBinding() {
33
+ if (nativeBinding) return nativeBinding;
34
+ throw new Error(
35
+ `rsvelte native binding not available for this platform. ` +
36
+ `Ensure the correct @rsvelte/binding-* package is installed. ` +
37
+ `Error: ${loadError?.message}`
38
+ );
39
+ }
40
+
41
+ module.exports.compile = function compile(source, options = {}) {
42
+ return getBinding().compile(source, options);
43
+ };
44
+
45
+ module.exports.compileModule = function compileModule(source, options = {}) {
46
+ return getBinding().compileModule(source, options);
47
+ };
48
+
49
+ module.exports.parse = function parse(source, options = {}) {
50
+ return getBinding().parse(source, options);
51
+ };
52
+
53
+ module.exports.preprocess = async function preprocess(source, preprocessor, options = {}) {
54
+ return getBinding().preprocess(source, preprocessor, options);
55
+ };
56
+
57
+ module.exports.print = function print(ast, options = {}) {
58
+ return getBinding().print(ast, options);
59
+ };
60
+
61
+ module.exports.parseCss = function parseCss(source) {
62
+ return getBinding().parseCss(source);
63
+ };
64
+
65
+ module.exports.walk = function walk() {
66
+ throw new Error(
67
+ 'walk() is deprecated. Use estree-walker instead: https://github.com/Rich-Harris/estree-walker'
68
+ );
69
+ };
70
+
71
+ module.exports.migrate = function migrate() {
72
+ throw new Error('migrate() is not yet implemented in rsvelte.');
73
+ };
74
+
75
+ try {
76
+ module.exports.VERSION = nativeBinding?.getVersion?.() ?? '0.1.0';
77
+ } catch {
78
+ module.exports.VERSION = '0.1.0';
79
+ }
package/index.d.ts ADDED
@@ -0,0 +1,140 @@
1
+ export interface CompileOptions {
2
+ /** Enable dev mode */
3
+ dev?: boolean;
4
+ /** Generate client or server code */
5
+ generate?: 'client' | 'server' | false;
6
+ /** Component filename */
7
+ filename?: string;
8
+ /** Root directory */
9
+ rootDir?: string;
10
+ /** Component name */
11
+ name?: string;
12
+ /** Enable custom element mode */
13
+ customElement?: boolean;
14
+ /** Enable accessors */
15
+ accessors?: boolean;
16
+ /** HTML namespace */
17
+ namespace?: 'html' | 'svg' | 'mathml';
18
+ /** Enable immutable mode */
19
+ immutable?: boolean;
20
+ /** CSS mode */
21
+ css?: 'injected' | 'external';
22
+ /**
23
+ * Custom CSS hash function.
24
+ *
25
+ * **Note:** Currently not supported in @rsvelte/compiler. The default hash function
26
+ * will always be used. See README for details.
27
+ */
28
+ cssHash?: (args: { hash: (input: string) => string; css: string; name: string; filename: string | undefined }) => string;
29
+ /** Preserve HTML comments */
30
+ preserveComments?: boolean;
31
+ /** Preserve whitespace */
32
+ preserveWhitespace?: boolean;
33
+ /** Enable runes mode */
34
+ runes?: boolean;
35
+ /** Disclose compiler version in output */
36
+ discloseVersion?: boolean;
37
+ /** Source map configuration */
38
+ sourcemap?: object | string;
39
+ /** Output JS filename */
40
+ outputFilename?: string;
41
+ /** Output CSS filename */
42
+ cssOutputFilename?: string;
43
+ /** Enable HMR */
44
+ hmr?: boolean;
45
+ /** Use modern AST */
46
+ modernAst?: boolean;
47
+ /**
48
+ * Warning filter function.
49
+ *
50
+ * **Note:** Currently not supported in @rsvelte/compiler. All warnings will be
51
+ * included. See README for details.
52
+ */
53
+ warningFilter?: (warning: Warning) => boolean;
54
+ /** Experimental options */
55
+ experimental?: {
56
+ async?: boolean;
57
+ };
58
+ /** Compatibility options */
59
+ compatibility?: {
60
+ componentApi?: 4 | 5;
61
+ };
62
+ }
63
+
64
+ export interface ModuleCompileOptions {
65
+ dev?: boolean;
66
+ generate?: 'client' | 'server' | false;
67
+ filename?: string;
68
+ rootDir?: string;
69
+ }
70
+
71
+ export interface Warning {
72
+ code: string;
73
+ message: string;
74
+ start?: { line: number; column: number; character: number };
75
+ end?: { line: number; column: number; character: number };
76
+ }
77
+
78
+ export interface CompileResult {
79
+ js: { code: string; map: any };
80
+ css: { code: string; map: any; hasGlobal: boolean } | null;
81
+ warnings: Warning[];
82
+ metadata: { runes: boolean };
83
+ ast?: any;
84
+ }
85
+
86
+ export interface PrintResult {
87
+ code: string;
88
+ map?: any;
89
+ }
90
+
91
+ export interface Processed {
92
+ code: string;
93
+ map?: any;
94
+ dependencies?: string[];
95
+ }
96
+
97
+ /**
98
+ * Compile a Svelte component.
99
+ */
100
+ export function compile(source: string, options?: CompileOptions): CompileResult;
101
+
102
+ /**
103
+ * Compile a Svelte module (.svelte.js / .svelte.ts).
104
+ */
105
+ export function compileModule(source: string, options?: ModuleCompileOptions): CompileResult;
106
+
107
+ /**
108
+ * Parse a Svelte component into an AST.
109
+ */
110
+ export function parse(source: string, options?: { modern?: boolean; loose?: boolean }): any;
111
+
112
+ /**
113
+ * Parse CSS.
114
+ */
115
+ export function parseCss(source: string): any;
116
+
117
+ /**
118
+ * Preprocess a Svelte component.
119
+ */
120
+ export function preprocess(source: string, preprocessor: any | any[], options?: { filename?: string }): Promise<Processed>;
121
+
122
+ /**
123
+ * Print an AST back to source code.
124
+ */
125
+ export function print(ast: any, options?: any): PrintResult;
126
+
127
+ /**
128
+ * Compiler version.
129
+ */
130
+ export const VERSION: string;
131
+
132
+ /**
133
+ * @deprecated Use estree-walker instead.
134
+ */
135
+ export function walk(): never;
136
+
137
+ /**
138
+ * Not yet implemented in @rsvelte/compiler.
139
+ */
140
+ export function migrate(): never;
package/index.js ADDED
@@ -0,0 +1,133 @@
1
+ // rsvelte - High-performance Svelte compiler
2
+ // Native binding with WASM fallback
3
+
4
+ import { createRequire } from 'node:module';
5
+
6
+ let nativeBinding = null;
7
+ let loadError = null;
8
+
9
+ function loadNativeBinding() {
10
+ const platform = process.platform;
11
+ const arch = process.arch;
12
+
13
+ const platformPackages = {
14
+ 'darwin-arm64': '@rsvelte/binding-darwin-arm64',
15
+ 'darwin-x64': '@rsvelte/binding-darwin-x64',
16
+ 'linux-x64': '@rsvelte/binding-linux-x64-gnu',
17
+ 'linux-arm64': '@rsvelte/binding-linux-arm64-gnu',
18
+ 'win32-x64': '@rsvelte/binding-win32-x64-msvc',
19
+ };
20
+
21
+ const key = `${platform}-${arch}`;
22
+ const packageName = platformPackages[key];
23
+
24
+ if (!packageName) {
25
+ loadError = new Error(`Unsupported platform: ${key}`);
26
+ return null;
27
+ }
28
+
29
+ try {
30
+ const require = createRequire(import.meta.url);
31
+ return require(packageName);
32
+ } catch (e) {
33
+ loadError = e;
34
+ return null;
35
+ }
36
+ }
37
+
38
+ nativeBinding = loadNativeBinding();
39
+
40
+ function getBinding() {
41
+ if (nativeBinding) return nativeBinding;
42
+ throw new Error(
43
+ `rsvelte native binding not available for this platform. ` +
44
+ `Ensure the correct @rsvelte/binding-* package is installed. ` +
45
+ `Error: ${loadError?.message}`
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Compile a Svelte component.
51
+ * @param {string} source - Svelte component source code
52
+ * @param {object} [options] - Compilation options
53
+ * @returns {object} Compilation result with js, css, warnings, and metadata
54
+ */
55
+ export function compile(source, options = {}) {
56
+ return getBinding().compile(source, options);
57
+ }
58
+
59
+ /**
60
+ * Compile a Svelte module (.svelte.js/.svelte.ts).
61
+ * @param {string} source - Module source code
62
+ * @param {object} [options] - Compilation options
63
+ * @returns {object} Compilation result
64
+ */
65
+ export function compileModule(source, options = {}) {
66
+ return getBinding().compileModule(source, options);
67
+ }
68
+
69
+ /**
70
+ * Parse a Svelte component into an AST.
71
+ * @param {string} source - Svelte component source code
72
+ * @param {object} [options] - Parse options
73
+ * @returns {object} AST
74
+ */
75
+ export function parse(source, options = {}) {
76
+ return getBinding().parse(source, options);
77
+ }
78
+
79
+ /**
80
+ * Preprocess a Svelte component.
81
+ * NOTE: This is an async function.
82
+ * @param {string} source - Svelte component source code
83
+ * @param {object|object[]} preprocessor - Preprocessor(s)
84
+ * @param {object} [options] - Preprocess options
85
+ * @returns {Promise<object>} Preprocessed result
86
+ */
87
+ export async function preprocess(source, preprocessor, options = {}) {
88
+ return getBinding().preprocess(source, preprocessor, options);
89
+ }
90
+
91
+ /**
92
+ * Print an AST back to source code.
93
+ * @param {object} ast - Svelte AST
94
+ * @param {object} [options] - Print options
95
+ * @returns {object} Print result with code and map
96
+ */
97
+ export function print(ast, options = {}) {
98
+ return getBinding().print(ast, options);
99
+ }
100
+
101
+ /**
102
+ * Parse CSS.
103
+ * @param {string} source - CSS source code
104
+ * @returns {object} Parsed CSS AST
105
+ */
106
+ export function parseCss(source) {
107
+ return getBinding().parseCss(source);
108
+ }
109
+
110
+ /**
111
+ * @deprecated Use estree-walker instead.
112
+ */
113
+ export function walk() {
114
+ throw new Error(
115
+ 'walk() is deprecated. Use estree-walker instead: https://github.com/Rich-Harris/estree-walker'
116
+ );
117
+ }
118
+
119
+ /**
120
+ * Not yet implemented in rsvelte.
121
+ */
122
+ export function migrate() {
123
+ throw new Error('migrate() is not yet implemented in rsvelte.');
124
+ }
125
+
126
+ /** Compiler version string. */
127
+ let _version;
128
+ try {
129
+ _version = nativeBinding?.getVersion?.() ?? '0.1.0';
130
+ } catch {
131
+ _version = '0.1.0';
132
+ }
133
+ export const VERSION = _version;
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@rsvelte/compiler",
3
+ "version": "0.1.0",
4
+ "description": "A high-performance Rust implementation of the Svelte compiler - drop-in replacement",
5
+ "type": "module",
6
+ "main": "index.cjs",
7
+ "module": "index.js",
8
+ "types": "index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "import": "./index.js",
13
+ "require": "./index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "index.cjs",
19
+ "index.d.ts",
20
+ "README.md"
21
+ ],
22
+ "optionalDependencies": {
23
+ "@rsvelte/binding-darwin-arm64": "0.1.0",
24
+ "@rsvelte/binding-darwin-x64": "0.1.0",
25
+ "@rsvelte/binding-linux-x64-gnu": "0.1.0",
26
+ "@rsvelte/binding-linux-arm64-gnu": "0.1.0",
27
+ "@rsvelte/binding-win32-x64-msvc": "0.1.0"
28
+ },
29
+ "keywords": [
30
+ "svelte",
31
+ "compiler",
32
+ "rust",
33
+ "napi",
34
+ "performance"
35
+ ],
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/baseballyama/svelte-compiler-rust"
40
+ },
41
+ "engines": {
42
+ "node": ">= 18"
43
+ }
44
+ }