Skip to content

caoccao/swc4j

Repository files navigation

swc4j

Maven Central Discord

swc4j Build

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.

swc4j and Javet

Features

Quick Start

Dependency

Maven

<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j</artifactId> <!-- Must Have -->
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-android-arm</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-android-arm64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-android-x86</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-android-x86_64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-linux-arm64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-linux-x86_64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-macos-arm64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-macos-x86_64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-windows-arm64</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j-windows-x86_64</artifactId>
    <version>2.1.0</version>
</dependency>

Gradle Kotlin DSL

implementation("com.caoccao.javet:swc4j:2.1.0") // Must Have
implementation("com.caoccao.javet:swc4j-android-arm:2.1.0")
implementation("com.caoccao.javet:swc4j-android-arm64:2.1.0")
implementation("com.caoccao.javet:swc4j-android-x86:2.1.0")
implementation("com.caoccao.javet:swc4j-android-x86_64:2.1.0")
implementation("com.caoccao.javet:swc4j-linux-arm64:2.1.0")
implementation("com.caoccao.javet:swc4j-linux-x86_64:2.1.0")
implementation("com.caoccao.javet:swc4j-macos-arm64:2.1.0")
implementation("com.caoccao.javet:swc4j-macos-x86_64:2.1.0")
implementation("com.caoccao.javet:swc4j-windows-arm64:2.1.0")
implementation("com.caoccao.javet:swc4j-windows-x86_64:2.1.0")

Gradle Groovy DSL

implementation 'com.caoccao.javet:swc4j:2.1.0' // Must Have
implementation 'com.caoccao.javet:swc4j-android-arm:2.1.0'
implementation 'com.caoccao.javet:swc4j-android-arm64:2.1.0'
implementation 'com.caoccao.javet:swc4j-android-x86:2.1.0'
implementation 'com.caoccao.javet:swc4j-android-x86_64:2.1.0'
implementation 'com.caoccao.javet:swc4j-linux-arm64:2.1.0'
implementation 'com.caoccao.javet:swc4j-linux-x86_64:2.1.0'
implementation 'com.caoccao.javet:swc4j-macos-arm64:2.1.0'
implementation 'com.caoccao.javet:swc4j-macos-x86_64:2.1.0'
implementation 'com.caoccao.javet:swc4j-windows-arm64:2.1.0'
implementation 'com.caoccao.javet:swc4j-windows-x86_64:2.1.0'

Transpile

  • Run the following Java code to transpile TypeScript to JavaScript.
// 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.
function add(a, b) {
  return a + b;
}
//# sourceMappingURL=data:application/json;base64,...

Sanitize

  • Run the following Java code to sanitize the JavaScript code.
JavetSanitizerStatementListChecker checker = new JavetSanitizerStatementListChecker();

// 1. Check if keyword const can be used.
String codeString = "const a = 1;";
checker.check(codeString);
System.out.println("1. " + codeString + " // Valid.");

// 2. Check if keyword var can be used.
codeString = "var a = 1;";
try {
    checker.check(codeString);
} catch (JavetSanitizerException e) {
    System.out.println("2. " + codeString + " // Invalid: " + e.getMessage());
}

// 3. Check if Object is mutable.
codeString = "Object = {};";
try {
    checker.check(codeString);
} catch (JavetSanitizerException e) {
    System.out.println("3. " + codeString + " // Invalid: " + e.getMessage());
}
  • The output is as follows.
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.
// 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.
1 + 2 = 3

Docs

Blog

License

APACHE LICENSE, VERSION 2.0