Skip to content

Commit

Permalink
Include minify_css and minify_js in the Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayman2000 committed Jan 21, 2022
1 parent 7c5aa71 commit 6c2f04d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/Cargo.core.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ edition = "2018"
name = "minify_html_core"
crate-type = ["cdylib"]

[features]
js-esbuild = []

[dependencies]
minify-html = { path = "../rust/main", features = [] }
[dependencies.pyo3]
Expand Down
4 changes: 4 additions & 0 deletions python/Cargo.js.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ edition = "2018"
name = "minify_html"
crate-type = ["cdylib"]

[features]
default = [ "js-esbuild" ]
js-esbuild = []

[dependencies]
minify-html = { path = "../rust/main", features = ["js-esbuild"] }
[dependencies.pyo3]
Expand Down
84 changes: 84 additions & 0 deletions python/src/lib.template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use minify_html::{Cfg, minify as minify_html_native};
#[cfg(feature = "js-esbuild")]
use minify_html::{minify_css as minify_css_native, minify_js as minify_js_native};
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use std::string::String;
Expand Down Expand Up @@ -45,9 +47,91 @@ fn minify(
Ok(String::from_utf8(out_code).unwrap())
}

#[cfg(feature = "js-esbuild")]
#[pyfunction(
py_args = "*",
do_not_minify_doctype = "false",
ensure_spec_compliant_unquoted_attribute_values = "false",
keep_closing_tags = "false",
keep_comments = "false",
keep_html_and_head_opening_tags = "false",
keep_spaces_between_attributes = "false",
remove_bangs = "false",
remove_processing_instructions = "false",
)]
fn minify_css(
code: String,
do_not_minify_doctype: bool,
ensure_spec_compliant_unquoted_attribute_values: bool,
keep_closing_tags: bool,
keep_comments: bool,
keep_html_and_head_opening_tags: bool,
keep_spaces_between_attributes: bool,
remove_bangs: bool,
remove_processing_instructions: bool,
) -> PyResult<String> {
let code = code.into_bytes();
let out_code = minify_css_native(&code, &Cfg {
do_not_minify_doctype,
ensure_spec_compliant_unquoted_attribute_values,
keep_closing_tags,
keep_comments,
keep_html_and_head_opening_tags,
keep_spaces_between_attributes,
minify_css: true,
minify_js: false,
remove_bangs,
remove_processing_instructions,
});
Ok(String::from_utf8(out_code).unwrap())
}

#[cfg(feature = "js-esbuild")]
#[pyfunction(
py_args = "*",
do_not_minify_doctype = "false",
ensure_spec_compliant_unquoted_attribute_values = "false",
keep_closing_tags = "false",
keep_comments = "false",
keep_html_and_head_opening_tags = "false",
keep_spaces_between_attributes = "false",
remove_bangs = "false",
remove_processing_instructions = "false",
)]
fn minify_js(
code: String,
do_not_minify_doctype: bool,
ensure_spec_compliant_unquoted_attribute_values: bool,
keep_closing_tags: bool,
keep_comments: bool,
keep_html_and_head_opening_tags: bool,
keep_spaces_between_attributes: bool,
remove_bangs: bool,
remove_processing_instructions: bool,
) -> PyResult<String> {
let code = code.into_bytes();
let out_code = minify_js_native(&code, &Cfg {
do_not_minify_doctype,
ensure_spec_compliant_unquoted_attribute_values,
keep_closing_tags,
keep_comments,
keep_html_and_head_opening_tags,
keep_spaces_between_attributes,
minify_css: false,
minify_js: true,
remove_bangs,
remove_processing_instructions,
});
Ok(String::from_utf8(out_code).unwrap())
}

#[pymodule]
fn REPLACE_WITH_MODULE_NAME(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(minify))?;
#[cfg(feature = "js-esbuild")]
m.add_wrapped(wrap_pyfunction!(minify_css))?;
#[cfg(feature = "js-esbuild")]
m.add_wrapped(wrap_pyfunction!(minify_js))?;

Ok(())
}

0 comments on commit 6c2f04d

Please sign in to comment.