百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

swc4j

> 编程语言
开源

swc4j (SWC for Java) 是一个基于 JVM 的超快速 JavaScript 和 TypeScript 编译和打包工具。

191 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

swc4j (SWC for Java) 是一个基于 JVM 的超快速 JavaScript 和 TypeScript 编译和打包工具。

swc4j

swc4j (SWC for Java) is an ultra-fast JavaScript and TypeScript compilation and bundling tool on JVM. It is part of the Javet portfolio serving the processing of JavaScript and TypeScript code before the code is executed in Node.js or V8 on JVM.

Features

  • Android + Linux + MacOS + Windows
  • JavaScript, TypeScript, JSX, TSX, etc.
  • Parse
    • AST
      • Visitor
      • Plugin
    • Comments
    • Tokens
  • Transform
    • Minify
    • Multiple Target ES Version
    • Source Map
  • Transpile
    • TS → JS
    • JSX → JS
    • TSX → JS
    • Source Map
  • Sanitizer
    • Built-in Object Protection
    • Keyword Restriction
    • Function Restriction
    • Identifier Restriction
    • Identifier Deletion
    • Identifier Freeze
    • Identifier Naming Convention
  • TypeScript to JVM Bytecode

Quick Start

Dependency

Maven

…

Gradle Kotlin DSL

…

Gradle Groovy DSL

…

Transpile

  • Run the following Java code to transpile TypeScript to JavaScript.
java
// Prepare a simple TypeScript code snippet.
String code = "function add(a:number, b:number) { return a+b; }";
// Prepare a script name.
URL specifier = URI.create("file:///abc.ts").toURL();
// Prepare an option with script name and media type.
Swc4jTranspileOptions options = new Swc4jTranspileOptions()
        .setSpecifier(specifier)
        .setMediaType(Swc4jMediaType.TypeScript);
// Transpile the code.
Swc4jTranspileOutput output = new Swc4j().transpile(code, options);
// Print the transpiled code.
System.out.println(output.getCode());
  • The transpiled JavaScript code and inline source map are as follows.
javascript
function add(a, b) {
  return a + b;
}
//# sourceMappingURL=data:application/json;base64,...

Sanitize

  • Run the following Java code to sanitize the JavaScript code.
…
  • The output is as follows.
javascript
1. const a = 1; // Valid.
2. var a = 1; // Invalid: Keyword var is not allowed.
3. Object = {}; // Invalid: Identifier Object is not allowed.

TypeScript to JVM Bytecode

  • Run the following Java code to compile TypeScript to JVM bytecode and execute it directly — no JavaScript runtime needed.
java
// Create a compiler targeting JDK 17.
ByteCodeCompiler compiler = ByteCodeCompiler.of(
        ByteCodeCompilerOptions.builder()
                .jdkVersion(JdkVersion.JDK_17)
                .build());
// Compile a TypeScript function to JVM bytecode.
ByteCodeRunner runner = compiler.compile("""
        export function add(a: int, b: int): int {
          return a + b
        }""");
// Invoke the compiled function.
ByteCodeClassRunner classRunner = runner.createStaticRunner("$");
int result = classRunner.invoke("add", 1, 2);
System.out.println("1 + 2 = " + result);
  • The output is as follows.
javascript
1 + 2 = 3

Docs

  • Tutorials
  • Release Notes

Blog

  • Run TypeScript Directly in Java
  • Hello Swc4j, Goodbye Antlr
  • How to Compromise V8 on JVM
  • Write SWC Plugins in Java: Part 1, Part 2, Part 3
  • Compile TypeScript to JVM Bytecode with swc4j

License

APACHE LICENSE, VERSION 2.0

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javacompilerjavajavascriptjavet

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言