Avoid cloning script data before passing to esbuild

This commit is contained in:
Wilson Lin 2020-07-25 14:18:23 +10:00
parent cae66f3e2a
commit e76c12a9db
3 changed files with 15 additions and 14 deletions

View File

@ -21,6 +21,6 @@ js-esbuild = ["crossbeam", "esbuild-rs"]
[dependencies]
crossbeam = { version = "0.7", optional = true }
esbuild-rs = { version = "0.1.3", optional = true }
esbuild-rs = { version = "0.2.1", optional = true }
lazy_static = "1.4"
memchr = "2"

View File

@ -339,7 +339,7 @@ impl<'d> Processor<'d> {
// Resulting minified JS to write.
// TODO Verify.
// TODO Rewrite these in esbuild fork so we don't have to do a memcpy and search+replace.
let min_js = result.js.trim().replace("</script", "<\\/script");
let min_js = result.js.as_str().trim().replace("</script", "<\\/script");
let js_len = if min_js.len() < src.len() {
self.code[write_next..write_next + min_js.len()].copy_from_slice(min_js.as_bytes());
min_js.len()

View File

@ -38,19 +38,20 @@ pub fn process_script(proc: &mut Processor, cfg: &Cfg, js: bool) -> ProcessingRe
if js && cfg.minify_js {
let (wg, results) = proc.new_script_section();
let src = start.written_range(proc);
// TODO Optimise: Avoid copying to new Vec.
esbuild_rs::transform(Arc::new(proc[src].to_vec()), TRANSFORM_OPTIONS.clone(), move |result| {
let mut guard = results.lock().unwrap();
guard.push(JsMinSection {
src,
result,
unsafe {
esbuild_rs::transform_direct_unmanaged(&proc[src], &TRANSFORM_OPTIONS.clone(), move |result| {
let mut guard = results.lock().unwrap();
guard.push(JsMinSection {
src,
result,
});
// Drop Arc reference and Mutex guard before marking task as complete as it's possible proc::finish
// waiting on WaitGroup will resume before Arc/Mutex is dropped after exiting this function.
drop(guard);
drop(results);
drop(wg);
});
// Drop Arc reference and Mutex guard before marking task as complete as it's possible proc::finish
// waiting on WaitGroup will resume before Arc/Mutex is dropped after exiting this function.
drop(guard);
drop(results);
drop(wg);
});
};
return Ok(());
};
break;