Add support for configuration and JS minification using esbuild
This commit is contained in:
parent
0787bb83f1
commit
ced0e82515
19 changed files with 174 additions and 62 deletions
|
|
@ -44,6 +44,7 @@ else
|
|||
fi
|
||||
cargo build $rust_build_arg
|
||||
mv Cargo.toml.orig Cargo.toml
|
||||
mkdir -p src/main/resources/
|
||||
cp target/rust/$rust_build_dir/libhyperbuild_java.$ext src/main/resources/$os_name-x86_64.nativelib
|
||||
|
||||
mvn clean package
|
||||
|
|
|
|||
|
|
@ -53,9 +53,10 @@ public class Hyperbuild {
|
|||
* If the code fails to be minified, a {@link SyntaxException} will be thrown with a descriptive English message and position in code where the error occurred.
|
||||
*
|
||||
* @param code {@link ByteBuffer} containing HTML code to minify
|
||||
* @param cfg {@link Configuration} minification settings to use
|
||||
* @return length of the written minified code in the {@link ByteBuffer}
|
||||
*/
|
||||
public static native int minifyInPlace(ByteBuffer code);
|
||||
public static native int minifyInPlace(ByteBuffer code, Configuration cfg);
|
||||
|
||||
/**
|
||||
* Minify HTML code represented as a {@link String}.
|
||||
|
|
@ -63,9 +64,10 @@ public class Hyperbuild {
|
|||
* If the code fails to be minified, a {@link SyntaxException} will be thrown with a descriptive English message and position in code where the error occurred.
|
||||
*
|
||||
* @param code HTML code to minify
|
||||
* @param cfg {@link Configuration} minification settings to use
|
||||
* @return minified HTML code
|
||||
*/
|
||||
public static native String minify(String code);
|
||||
public static native String minify(String code, Configuration cfg);
|
||||
|
||||
/**
|
||||
* Basic exception class representing minification errors.
|
||||
|
|
@ -75,4 +77,31 @@ public class Hyperbuild {
|
|||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class representing minification configuration.
|
||||
*/
|
||||
public static class Configuration {
|
||||
private final boolean minifyJs;
|
||||
|
||||
public Configuration(boolean minifyJs) {
|
||||
this.minifyJs = minifyJs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder to help create configuration.
|
||||
*/
|
||||
public static class Builder {
|
||||
private boolean minifyJs = false;
|
||||
|
||||
public Builder setMinifyJs(boolean minifyJs) {
|
||||
this.minifyJs = minifyJs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Configuration build() {
|
||||
return new Configuration(this.minifyJs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,24 @@
|
|||
use hyperbuild::hyperbuild;
|
||||
use hyperbuild::{hyperbuild, Cfg};
|
||||
use jni::JNIEnv;
|
||||
use jni::objects::{JByteBuffer, JClass, JObject, JString};
|
||||
use jni::sys::{jint, jstring};
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
fn build_cfg(
|
||||
env: &JNIEnv,
|
||||
obj: &JObject,
|
||||
) -> Cfg {
|
||||
Cfg {
|
||||
minify_js: env.get_field(*obj, "minifyJs", "Z").unwrap().z().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minifyInPlace(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
input: JByteBuffer,
|
||||
cfg: JObject,
|
||||
)
|
||||
-> jint {
|
||||
let source = match env.get_direct_buffer_address(input) {
|
||||
|
|
@ -19,7 +29,7 @@ pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minifyInPlace(
|
|||
}
|
||||
};
|
||||
|
||||
(match hyperbuild(source) {
|
||||
(match hyperbuild(source, &build_cfg(&env, &cfg)) {
|
||||
Ok(out_len) => out_len,
|
||||
Err((err, pos)) => {
|
||||
env.throw_new(
|
||||
|
|
@ -36,12 +46,13 @@ pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minify(
|
|||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
input: JString,
|
||||
cfg: JObject,
|
||||
)
|
||||
-> jstring {
|
||||
let source: String = env.get_string(input).unwrap().into();
|
||||
let mut code = source.into_bytes();
|
||||
|
||||
match hyperbuild(&mut code) {
|
||||
match hyperbuild(&mut code, &build_cfg(&env, &cfg)) {
|
||||
Ok(out_len) => env.new_string(unsafe { from_utf8_unchecked(&code[0..out_len]) }).unwrap().into_inner(),
|
||||
Err((err, pos)) => {
|
||||
env.throw_new(
|
||||
|
|
|
|||
Reference in a new issue