2020-01-10 02:30:49 -05:00
|
|
|
use crate::err::ProcessingResult;
|
2020-01-25 02:04:02 -05:00
|
|
|
use crate::proc::MatchAction::*;
|
|
|
|
use crate::proc::MatchMode::*;
|
2020-01-25 02:07:52 -05:00
|
|
|
use crate::proc::Processor;
|
2020-07-10 11:15:56 -04:00
|
|
|
use crate::cfg::Cfg;
|
2020-07-11 08:52:27 -04:00
|
|
|
#[cfg(feature = "js-esbuild")]
|
|
|
|
use crate::proc::checkpoint::Checkpoint;
|
2020-01-10 02:30:49 -05:00
|
|
|
|
2020-07-10 11:15:56 -04:00
|
|
|
pub fn process_script(proc: &mut Processor, cfg: &Cfg) -> ProcessingResult<()> {
|
2020-07-11 08:20:17 -04:00
|
|
|
#[cfg(feature = "js-esbuild")]
|
2020-07-11 08:52:27 -04:00
|
|
|
let start = Checkpoint::new(proc);
|
|
|
|
loop {
|
|
|
|
proc.require_not_at_end()?;
|
|
|
|
// Use fast memchr. Unfortunately all characters in "</script>" are common in JS code.
|
|
|
|
proc.m(WhileNotChar(b'<'), Keep);
|
|
|
|
// `process_tag` will require closing tag.
|
|
|
|
if proc.m(IsSeq(b"</script"), MatchOnly).nonempty() {
|
|
|
|
#[cfg(feature = "js-esbuild")]
|
|
|
|
if cfg.minify_js {
|
|
|
|
let src_range = start.written_range(proc);
|
|
|
|
let src = unsafe {
|
|
|
|
std::string::String::from_utf8_unchecked(proc[src_range].to_vec())
|
|
|
|
};
|
|
|
|
let min = esbuild_rs::esbuild(&src).trim().as_bytes();
|
|
|
|
// `src.len()` is amount of bytes, so this is guaranteed to not overwrite.
|
|
|
|
if min.len() < src.len() {
|
|
|
|
start.erase_written(proc);
|
|
|
|
proc.write_slice(min);
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
break;
|
2020-07-10 11:15:56 -04:00
|
|
|
};
|
2020-07-11 08:52:27 -04:00
|
|
|
// Consume '<'.
|
|
|
|
proc.accept_expect();
|
2020-07-10 11:15:56 -04:00
|
|
|
};
|
2020-01-10 02:30:49 -05:00
|
|
|
Ok(())
|
|
|
|
}
|