SWC jvm binding by kotlin.
implementation("dev.yidafu.swc:swc-binding:0.6.0")
val swc = SwcNative()
val res = swc.transformSync(
"""
import x from './test.js';
class Foo {
bar: string
}
""".trimIndent(),
false,
Options().apply {
jsc = jscConfig {
parser = ParserConfig().apply {
syntax = "ecmascript"
}
}
}
)
val ast = SwcNative().parseSync(
"""
import x from './test.js';
class Foo {
bar: string
}
""".trimIndent(),
ParseOptions().apply { syntax = "typescript" },
"temp.js"
)
see swc#parseSync
@Throws(RuntimeException::class)
fun parseSync(code: String, options: ParserConfig, filename: String?): Program
Native method
@Throws(RuntimeException::class)
fun parseSync(code: String, options: String, filename: String?): String
Kotlin DSL Wrapper method
Kotlin DSL Wrapper method
@Throws(RuntimeException::class)
fun parseFileSync(filepath: String, options: ParserConfig): Program
Native method
@Throws(RuntimeException::class)
fun parseFileSync(filepath: String, options: String): String
Kotlin DSL Wrapper method
@Throws(RuntimeException::class)
fun transformSync(code: String, isModule: Boolean, options: Options): TransformOutput
Native method
@Throws(RuntimeException::class)
fun transformSync(code: String, isModule: Boolean, options: String): String
Kotlin DSL Wrapper method
@Throws(RuntimeException::class)
fun transformFileSync(filepath: String, isModule: Boolean, options: Options): TransformOutput
Native method
@Throws(RuntimeException::class)
fun transformFileSync(filepath: String, isModule: Boolean, options: String): String
Kotlin DSL Wrapper method
@Throws(RuntimeException::class)
fun printSync(program: Program, options: Options): TransformOutput
Native method
@Throws(RuntimeException::class)
fun printSync(program: String, options: String): String
Kotlin DSL Wrapper method
@Throws(RuntimeException::class)
fun minifySync(program: Program, options: Options): TransformOutput
Native method
@Throws(RuntimeException::class)
fun minifySync(program: String, options: String): String
import x from './test.js';
class Foo {
bar: string
}
Js code above, equal to kotlin ast dsl below.
module {
body = arrayOf(
importDeclaration {
specifiers = arrayOf(
importDefaultSpecifier {
local = createIdentifier {
span = emptySpan()
value = "x"
}
}
)
source = stringLiteral {
value= "./test.js"
raw = "./test.js"
span = emptySpan()
}
typeOnly = false
span = emptySpan()
},
classDeclaration {
identifier = createIdentifier { }
span = emptySpan()
body = arrayOf(
classProperty {
span = emptySpan()
typeAnnotation = tsTypeAnnotation {
span = emptySpan()
typeAnnotation = tsKeywordType {
span = emptySpan()
kind = TsKeywordTypeKind.STRING
}
}
}
)
}
)
}
If you want create ast segment, call createXxx
function to create segment.
createVariableDeclaration {
span = span(0, 17, 0)
kind = 'const'
declare = false
declarations = arrayOf(
variableDeclaratorImpl {
span = span(6, 17, 0)
id = identifier {
span = span(5, 9, 0)
value = "foo"
}
init = stringLiteral {
span = span(12,17, 0)
value = "bar"
raw = "'bar'"
}
}
)
}
SWC configuration maybe boolean | T
.
Such as
export interface Config {
// ...
sourceMaps?: boolean | "inline";
// ...
}
So, you should using Booleanable<T>
options {
// ...
sourceMaps = BooleanableString.ofValue("inline")
// or
sourceMaps = BooleanableString.ofFalse()
}
Booleanable typealias
- BooleanableString
- BooleanableFloat
- BooleanableInt
- BooleanableArrayString
- BooleanableTerserCompressOptions
- BooleanableTerserMangleOptions
- BooleanableArrayMatchPattern
How to implement SWC JVM binding -- English translation -- 中文原文