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

  • Linux + MacOS + Windows
  • JavaScript, TypeScript, JSX, TSX, etc.
  • Parse
    • AST
    • Comments
    • Tokens
  • Transform
    • Minify
    • Multiple Target ES Version
    • Source Map
  • Transpile
    • TS → JS
    • JSX → JS
    • TSX → JS
    • Source Map

Quick Start

  • Add the following dependency to your project or download a snapshot build from the Actions.
<!-- Maven -->
<dependency>
    <groupId>com.caoccao.javet</groupId>
    <artifactId>swc4j</artifactId>
    <version>0.5.0</version>
</dependency>
// Gradle Kotlin DSL
implementation("com.caoccao.javet:swc4j:0.5.0")
// Gradle Groovy DSL
implementation 'com.caoccao.javet:swc4j:0.5.0'
  • Run the following Java code.
// Prepare a simple TypeScript code snippet.
String code = "function add(a:number, b:number) { return a+b; }";
// Prepare a script name.
URL specifier = new URL("file://abc.ts");
// 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 code and inline source map are as follows.
function add(a, b) {
  return a + b;
}
//# sourceMappingURL=data:application/json;base64,...

Docs

Blog

License

APACHE LICENSE, VERSION 2.0