2025-01-03 18:52:00
github.com
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.
- Android + Linux + MacOS + Windows
- JavaScript, TypeScript, JSX, TSX, etc.
- Parse
- Transform
- Minify
- Multiple Target ES Version
- Source Map
- Transpile
- TS → JS
- JSX → JS
- TSX → JS
- Source Map
- Sanitizer
- Add the following dependency to your project or download a snapshot build from the Actions.
dependency>
groupId>com.caoccao.javetgroupId>
artifactId>swc4jartifactId>
version>1.3.0version>
dependency>
// Gradle Kotlin DSL
// Linux + MacOS + Windows
implementation("com.caoccao.javet:swc4j:1.3.0")
// Android
implementation("com.caoccao.javet:swc4j-android:1.3.0")
// Gradle Groovy DSL
// Linux + MacOS + Windows
implementation 'com.caoccao.javet:swc4j:1.3.0'
// Android
implementation 'com.caoccao.javet:swc4j-android:1.3.0'
- 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 = 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 JavaScript code and inline source map are as follows.
function add(a, b) {
return a + b;
}
//# sourceMappingURL=data:application/json;base64,...
- 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.
Keep your files stored safely and securely with the SanDisk 2TB Extreme Portable SSD. With over 69,505 ratings and an impressive 4.6 out of 5 stars, this product has been purchased over 8K+ times in the past month. At only $129.99, this Amazon’s Choice product is a must-have for secure file storage.
Help keep private content private with the included password protection featuring 256-bit AES hardware encryption. Order now for just $129.99 on Amazon!
Support Techcratic
If you find value in Techcratic’s insights and articles, consider supporting us with Bitcoin. Your support helps me, as a solo operator, continue delivering high-quality content while managing all the technical aspects, from server maintenance to blog writing, future updates, and improvements. Support Innovation! Thank you.
Bitcoin Address:
bc1qlszw7elx2qahjwvaryh0tkgg8y68enw30gpvge
Please verify this address before sending funds.
Bitcoin QR Code
Simply scan the QR code below to support Techcratic.
Please read the Privacy and Security Disclaimer on how Techcratic handles your support.
Disclaimer: As an Amazon Associate, Techcratic may earn from qualifying purchases.