swc4j (SWC for Java) 是一个基于 JVM 的超快速 JavaScript 和 TypeScript 编译和打包工具。
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.
………// 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());function add(a, b) {
return a + b;
}
//# sourceMappingURL=data:application/json;base64,...…1. const a = 1; // Valid.
2. var a = 1; // Invalid: Keyword var is not allowed.
3. Object = {}; // Invalid: Identifier Object is not allowed.// 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);1 + 2 = 3暂无开放 Issues,或尚未同步最近议题。