Bun

Symbol

Transpiler.constructor

constructor (options?: TranspilerOptions): Transpiler

Referenced types

interface TranspilerOptions

  • allowBunRuntime?: boolean
  • autoImportJSX?: boolean
  • deadCodeElimination?: boolean

    Experimental

    Enabled by default, use this to disable dead code elimination.

    Some other transpiler options may still do some specific dead code elimination.

  • define?: Record<string, string>

    Replace key with value. Value must be a JSON string.

     { "process.env.NODE_ENV": "\"production\"" }
    
  • exports?: { eliminate: string[]; replace: Record<string, string> }
  • inline?: boolean

    This does two things (and possibly more in the future):

    1. const declarations to primitive types (excluding Object/Array) at the top of a scope before any let or var declarations will be inlined into their usages.
    2. let and const declarations only used once are inlined into their usages.

    JavaScript engines typically do these optimizations internally, however it might only happen much later in the compilation pipeline, after code has been executed many many times.

    This will typically shrink the output size of code, but it might increase it in some cases. Do your own benchmarks!

  • loader?: JavaScriptLoader

    What is the default loader used for this transpiler?

  • logLevel?: 'error' | 'verbose' | 'debug' | 'info' | 'warn'
  • macro?: MacroMap

    Replace an import statement with a macro.

    This will remove the import statement from the final output and replace any function calls or template strings with the result returned by the macro

       {
           "react-relay": {
               "graphql": "bun-macro-relay"
           }
       }
    

    Code that calls graphql will be replaced with the result of the macro.

    import {graphql} from "react-relay";
    
    // Input:
    const query = graphql`
        query {
            ... on User {
                id
            }
        }
    }`;
    

    Will be replaced with:

    import UserQuery from "./UserQuery.graphql";
    const query = UserQuery;
    
  • minifyWhitespace?: boolean

    Experimental

    Minify whitespace and comments from the output.

  • target?: Target
    "browser"
    
  • treeShaking?: boolean
  • tsconfig?: string | TSConfig

    TSConfig.json file as stringified JSON or an object Use this to set a custom JSX factory, fragment, or import source For example, if you want to use Preact instead of React. Or if you want to use Emotion.

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