[][src]Function esbuild_rs::transform_direct

pub fn transform_direct<F>(
    code: Arc<Vec<u8>>,
    options: Arc<TransformOptions>,
    cb: F
) where
    F: FnOnce(TransformResult),
    F: Send + 'static, 

This function transforms a string of source code into JavaScript. It can be used to minify JavaScript, convert TypeScript/JSX to JavaScript, or convert newer JavaScript to older JavaScript. The available options roughly correspond to esbuild's command-line flags.

The equivalent Go function will be called via Cgo, which will run the API from a goroutine. This means that this function will return immediately, and cb will be called sometime in the future once the goroutine completes. Additional concurrency management may be necessary to keep the Rust program alive until all calls to this function actually complete.

Arguments

  • code - Source code to transform. Must be UTF-8. A reference will be held on the Arc until the callback is asynchronously called from Go.
  • options - Built TransformOptions created from a TransformOptionsBuilder. A reference will be held on the Arc until the callback is asynchronously called from Go.
  • cb - Closure to call once the goroutine completes with the TransformResult.

Examples

This example uses the crossbeam crate to prevent Rust from exiting until the transform completes.

use std::sync::Arc;
use crossbeam::sync::WaitGroup;
use esbuild_rs::{TransformOptionsBuilder, transform_direct, TransformResult};

fn main() {
  let src = Arc::new(b"let x = NAME;".to_vec());

  let mut options_builder = TransformOptionsBuilder::new();
  options_builder.define.insert("NAME".to_string(), "world".to_string());
  let options = options_builder.build();

  let wg = WaitGroup::new();
  let task = wg.clone();
  transform_direct(src, options, |TransformResult { code, map, errors, warnings }| {
    assert_eq!(code.as_str(), "let x = world;\n");
    drop(task);
  });
  wg.wait();
}