Bun.Transpiler
class. To create an instance of Bun’s transpiler:
.transformSync()
Transpile code synchronously with the .transformSync()
method. Modules are not resolved and the code is not executed. The result is a string of vanilla JavaScript code.
new Bun.Transpiler()
constructor, pass a second argument to .transformSync()
.
Nitty gritty
Nitty gritty
When
.transformSync
is called, the transpiler is run in the same thread as the currently executed code.If a macro is used, it will be run in the same thread as the transpiler, but in a separate event loop from the rest of your application. Currently, globals between macros and regular code are shared, which means it is possible (but not recommended) to share states between macros and regular code. Attempting to use AST nodes outside of a macro is undefined behavior..transform()
The transform()
method is an async version of .transformSync()
that returns a Promise<string>
.
Bun.Transpiler.transformSync
. The cost of the threadpool will often take longer than actually transpiling code.
Nitty gritty
Nitty gritty
The
.transform()
method runs the transpiler in Bun’s worker threadpool, so if you run it 100 times, it will run it across Math.floor($cpu_count * 0.8)
threads, without blocking the main JavaScript thread.If your code uses a macro, it will potentially spawn a new copy of Bun’s JavaScript runtime environment in that new thread..scan()
The Transpiler
instance can also scan some source code and return a list of its imports and exports, plus additional metadata about each one. Type-only imports and exports are ignored.
imports
array has a path
and kind
. Bun categories imports into the following kinds:
import-statement
:import React from 'react'
require-call
:const val = require('./cjs.js')
require-resolve
:require.resolve('./cjs.js')
dynamic-import
:import('./loader')
import-rule
:@import 'foo.css'
url-token
:url('./foo.png')
.scanImports()
For performance-sensitive code, you can use the .scanImports()
method to get a list of imports. It’s faster than .scan()
(especially for large files) but marginally less accurate due to some performance optimizations.
Reference
See Typescript Definitions