Transpiler

Bun

Symbol

Transpiler

class Transpiler

Quickly transpile TypeScript, JSX, or JS to modern JavaScript.

const transpiler = new Bun.Transpiler();
transpiler.transformSync(`
  const App = () => <div>Hello World</div>;
export default App;
`);
// This outputs:
const output = `
const App = () => jsx("div", {
  children: "Hello World"
}, undefined, false, undefined, this);
export default App;
`
  • scan(code: StringOrBuffer): { exports: string[]; imports: Import[] }

    Get a list of import paths and paths from a TypeScript, JSX, TSX, or JavaScript file.

    @param code

    The code to scan

    const {imports, exports} = transpiler.scan(`
    import {foo} from "baz";
    export const hello = "hi!";
    `);
    
    console.log(imports); // ["baz"]
    console.log(exports); // ["hello"]
    
  • Get a list of import paths from a TypeScript, JSX, TSX, or JavaScript file.

    @param code

    The code to scan

    const imports = transpiler.scanImports(`
    import {foo} from "baz";
    import type {FooType} from "bar";
    import type {DogeType} from "wolf";
    `);
    
    console.log(imports); // ["baz"]
    

    This is a fast path which performs less work than scan.

  • transform(code: StringOrBuffer, loader?: JavaScriptLoader): Promise<string>

    Transpile code from TypeScript or JSX into valid JavaScript. This function does not resolve imports.

    @param code

    The code to transpile

  • transformSync(code: StringOrBuffer, loader: JavaScriptLoader, ctx: object): string

    Transpile code from TypeScript or JSX into valid JavaScript. This function does not resolve imports.

    @param code

    The code to transpile

    transformSync(code: StringOrBuffer, ctx: object): string

    Transpile code from TypeScript or JSX into valid JavaScript. This function does not resolve imports.

    @param code

    The code to transpile

    @param ctx

    An object to pass to macros

    Transpile code from TypeScript or JSX into valid JavaScript. This function does not resolve imports.

    @param code

    The code to transpile