Skip to content

Instantly share code, notes, and snippets.

@khalidx
Last active June 2, 2023 02:36
Show Gist options
  • Save khalidx/6a8fecd5bf2d69534450378e4a2eac80 to your computer and use it in GitHub Desktop.
Save khalidx/6a8fecd5bf2d69534450378e4a2eac80 to your computer and use it in GitHub Desktop.
Programmatically compile with @swc/core.

Utility function

import { transform, Options } from '@swc/core'

export async function compile (params: { source: string, options?: Options }) {
  const { code, map } = await transform(params.source, params.options)
  return { code, map }
}

Usage

const { code, map } = await compile({
  source: await readFile(path, 'utf-8'),
  options: {
    jsc: {
      parser: {
        syntax: 'typescript'
      }
    }
  }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment