0
# SWC WebAssembly
1
2
SWC WebAssembly provides a complete JavaScript/TypeScript compilation toolchain in WebAssembly format, enabling fast parsing, transformation, minification, and code generation in both browser and Node.js environments where native bindings are unavailable.
3
4
## Package Information
5
6
- **Package Name**: @swc/wasm
7
- **Package Type**: npm
8
- **Language**: TypeScript (compiled from Rust)
9
- **Installation**: `npm install @swc/wasm`
10
11
## Core Imports
12
13
```typescript
14
import * as swc from "@swc/wasm";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const swc = require("@swc/wasm");
21
```
22
23
## Basic Usage
24
25
```typescript
26
import * as swc from "@swc/wasm";
27
28
// Transform TypeScript code
29
const result = await swc.transform(`
30
class Example {
31
private value: number = 42;
32
33
getValue(): number {
34
return this.value;
35
}
36
}
37
`, {
38
jsc: {
39
target: "es2015",
40
parser: {
41
syntax: "typescript"
42
}
43
}
44
});
45
46
console.log(result.code);
47
// Output: ES2015 JavaScript code
48
49
// Parse code into AST
50
const ast = await swc.parse(`const x = 1;`, {
51
syntax: "ecmascript",
52
target: "es2020"
53
});
54
55
// Minify JavaScript
56
const minified = await swc.minify(`
57
function example() {
58
const message = "Hello World";
59
console.log(message);
60
}
61
`, {
62
module: false
63
});
64
65
console.log(minified.code);
66
// Output: minified code
67
```
68
69
## Architecture
70
71
SWC WebAssembly is built around several key components:
72
73
- **Parser Engine**: Converts JavaScript/TypeScript source code into Abstract Syntax Trees (AST)
74
- **Transform Pipeline**: Applies configurable transformations (TypeScript compilation, module conversion, etc.)
75
- **Code Generator**: Converts AST back to JavaScript with optional source maps
76
- **Minifier**: Optimizes JavaScript code for production deployment
77
- **WebAssembly Interface**: Provides both synchronous and asynchronous APIs via wasm-bindgen
78
79
## Capabilities
80
81
### Code Parsing
82
83
Parse JavaScript and TypeScript code into Abstract Syntax Trees with comprehensive syntax support and error reporting.
84
85
```typescript { .api }
86
function parse(src: string, options?: ParseOptions): Promise<Module>;
87
function parse(src: string, options: ParseOptions & { isModule: false | "commonjs" }): Promise<Script>;
88
function parseSync(src: string, options?: ParseOptions): Module;
89
function parseSync(src: string, options: ParseOptions & { isModule: false | "commonjs" }): Script;
90
91
interface ParseOptions extends ParserConfig {
92
comments?: boolean;
93
script?: boolean;
94
target?: JscTarget;
95
}
96
```
97
98
[Code Parsing](./parsing.md)
99
100
### Code Transformation
101
102
Transform JavaScript and TypeScript code with configurable compilation targets, module systems, and optimization settings.
103
104
```typescript { .api }
105
function transform(
106
code: string | Program,
107
options?: Options,
108
experimental_plugin_bytes_resolver?: any
109
): Promise<Output>;
110
111
function transformSync(
112
code: string | Program,
113
opts?: Options,
114
experimental_plugin_bytes_resolver?: any
115
): Output;
116
117
interface Output {
118
code: string;
119
map?: string;
120
diagnostics: string[];
121
}
122
```
123
124
[Code Transformation](./transformation.md)
125
126
### Code Generation
127
128
Convert Abstract Syntax Trees back to JavaScript source code with optional source map generation.
129
130
```typescript { .api }
131
function print(m: Program, options?: Options): Promise<Output>;
132
function printSync(m: Program, options?: Options): Output;
133
```
134
135
[Code Generation](./code-generation.md)
136
137
### Code Minification
138
139
Optimize JavaScript code for production with configurable compression and mangling options.
140
141
```typescript { .api }
142
function minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
143
function minifySync(code: string, opts?: JsMinifyOptions): Output;
144
```
145
146
[Code Minification](./minification.md)
147
148
## Core Types
149
150
```typescript { .api }
151
type Program = Module | Script;
152
153
interface Module extends Node, HasSpan {
154
type: "Module";
155
body: ModuleItem[];
156
interpreter: string;
157
}
158
159
interface Script extends Node, HasSpan {
160
type: "Script";
161
body: Statement[];
162
interpreter: string;
163
}
164
165
interface Options extends Config {
166
script?: boolean;
167
cwd?: string;
168
caller?: CallerOptions;
169
filename?: string;
170
root?: string;
171
rootMode?: "root" | "upward" | "upward-optional";
172
envName?: string;
173
configFile?: string | boolean;
174
swcrc?: boolean;
175
swcrcRoots?: boolean | MatchPattern | MatchPattern[];
176
inputSourceMap?: boolean | string;
177
sourceFileName?: string;
178
sourceRoot?: string;
179
plugin?: Plugin;
180
isModule?: boolean | "unknown" | "commonjs";
181
outputPath?: string;
182
}
183
184
interface Config {
185
test?: string | string[];
186
exclude?: string | string[];
187
env?: EnvConfig;
188
jsc?: JscConfig;
189
module?: ModuleConfig;
190
minify?: boolean;
191
sourceMaps?: boolean | "inline";
192
inlineSourcesContent?: boolean;
193
}
194
195
type JscTarget = "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022";
196
197
type ParserConfig = TsParserConfig | EsParserConfig;
198
199
interface TsParserConfig {
200
syntax: "typescript";
201
tsx?: boolean;
202
decorators?: boolean;
203
dynamicImport?: boolean;
204
}
205
206
interface EsParserConfig {
207
syntax: "ecmascript";
208
jsx?: boolean;
209
functionBind?: boolean;
210
decorators?: boolean;
211
decoratorsBeforeExport?: boolean;
212
exportDefaultFrom?: boolean;
213
importAssertions?: boolean;
214
}
215
216
interface MatchPattern {
217
// Pattern matching interface for file selection
218
}
219
220
interface Span {
221
start: number;
222
end: number;
223
ctxt: number;
224
}
225
```