8000 Add configuration to support use of CommentOptions::disabled() by dougtc1 · Pull Request #286 · wilsonzlin/minify-html · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading < 8000 /div>
Diff view
Diff view
6 changes: 6 additions & 0 deletions minhtml/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ struct Cli {
#[structopt(long)]
minify_js: bool,

/// Remove all comments from minified JavaScript (only used when minify_js = True) using
/// [CommentOptions::disabled()]()https://docs.rs/oxc_codegen/latest/oxc_codegen/struct.CommentOptions.html#method.disabled)
#[structopt(long)]
remove_js_comments: bool,

/// When `{{`, `{#`, or `{%` are seen in content, all source code until the subsequent matching closing `}}`, `#}`, or `%}` respectively gets piped through untouched.
#[structopt(long)]
preserve_brace_template_syntax: bool,
Expand Down Expand Up @@ -124,6 +129,7 @@ fn main() {
minify_css: args.minify_css,
minify_doctype: args.minify_doctype,
minify_js: args.minify_js,
remove_js_comments: args.remove_js_comments,
preserve_brace_template_syntax: args.preserve_brace_template_syntax,
preserve_chevron_percent_template_syntax: args.preserve_chevron_percent_template_syntax,
remove_bangs: args.remove_bangs,
Expand Down
1 change: 1 addition & 0 deletions minify-html-java/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn build_cfg(env: &mut JNIEnv, obj: &JObject) -> Cfg {
minify_css: env.get_field(obj, "minify_css", "Z").unwrap().z().unwrap(),
minify_doctype: env.get_field(obj, "minify_doctype", "Z").unwrap().z().unwrap(),
minify_js: env.get_field(obj, "minify_js", "Z").unwrap().z().unwrap(),
remove_js_comments: env.get_field(obj, "remove_js_comments", "Z").unwrap().z().unwrap(),
preserve_brace_template_syntax: env.get_field(obj, "preserve_brace_template_syntax", "Z").unwrap().z().unwrap(),
preserve_chevron_percent_template_syntax: env.get_field(obj, "preserve_chevron_percent_template_syntax", "Z").unwrap().z().unwrap(),
remove_bangs: env.get_field(obj, "remove_bangs", "Z").unwrap().z().unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions minify-html-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function minify(
minify_doctype?: boolean;
/** Minify JavaScript in `<script>` tags using [minify-js](https://github.com/wilsonzlin/minify-js). */
minify_js?: boolean;
/** Remove all comments from minified JavaScript (only used when minify_js = True). */
remove_js_comments?: boolean;
/** When `{{`, `{#`, or `{%` are seen in content, all source code until the subsequent matching closing `}}`, `#}`, or `%}` respectively gets piped through untouched. */
preserve_brace_template_syntax?: boolean;
/** When `<%` is seen in content, all source code until the subsequent matching closing `%>` gets piped through untouched. */
Expand Down
1 change: 1 addition & 0 deletions minify-html-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn minify(mut cx: FunctionContext) -> JsResult<JsBuffer> {
minify_css: get_bool!(cx, opt, "minify_css"),
minify_doctype: get_bool!(cx, opt, "minify_doctype"),
minify_js: get_bool!(cx, opt, "minify_js"),
remove_js_comments: get_bool!(cx, opt, "remove_js_comments"),
preserve_brace_template_syntax: get_bool!(cx, opt, "preserve_brace_template_syntax"),
preserve_chevron_percent_template_syntax: get_bool!(cx, opt, "preserve_chevron_percent_template_syntax"),
remove_bangs: get_bool!(cx, opt, "remove_bangs"),
Expand Down
1 change: 1 addition & 0 deletions minify-html-python/minify_html.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def minify(
minify_css: bool = False,
minify_doctype: bool = False,
minify_js: bool = False,
remove_js_comments: bool = False,
preserve_brace_template_syntax: bool = False,
preserve_chevron_percent_template_syntax: bool = False,
remove_bangs: bool = False,
Expand Down
3 changes: 3 additions & 0 deletions minify-html-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::string::String;
minify_css = false,
minify_doctype = false,
minify_js = false,
remove_js_comments = false,
preserve_brace_template_syntax = false,
preserve_chevron_percent_template_syntax = false,
remove_bangs = false,
Expand All @@ -40,6 +41,7 @@ fn minify(
minify_css: bool,
minify_doctype: bool,
minify_js: bool,
remove_js_comments: bool,
preserve_brace_template_syntax: bool,
preserve_chevron_percent_template_syntax: bool,
remove_bangs: bool,
Expand All @@ -58,6 +60,7 @@ fn minify(
minify_css,
minify_doctype,
minify_js,
remove_js_comments,
preserve_brace_template_syntax,
preserve_chevron_percent_template_syntax,
remove_bangs,
Expand Down
1 change: 1 addition & 0 deletions minify-html-ruby/ext/minify_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn minify_html(source: String, cfg: RHash) -> String {
minify_css: cfg.aref(StaticSymbol::new("minify_css")).unwrap_or_default(),
minify_doctype: cfg.aref(StaticSymbol::new("minify_doctype")).unwrap_or_default(),
minify_js: cfg.aref(StaticSymbol::new("minify_js")).unwrap_or_default(),
remove_js_comments: cfg.aref(StaticSymbol::new("remove_js_comments")).unwrap_or_default(),
preserve_brace_template_syntax: cfg.aref(StaticSymbol::new("preserve_brace_template_syntax")).unwrap_or_default(),
preserve_chevron_percent_template_syntax: cfg.aref(StaticSymbol::new("preserve_chevron_percent_template_syntax")).unwrap_or_default(),
remove_bangs: cfg.aref(StaticSymbol::new("remove_bangs")).unwrap_or_default(),
Expand Down
1 change: 1 addition & 0 deletions minify-html-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn minify(code: &[u8], cfg: &JsValue) -> Vec<u8> {
minify_css: get_prop!(cfg, "minify_css"),
minify_doctype: get_prop!(cfg, "minify_doctype"),
minify_js: get_prop!(cfg, "minify_js"),
remove_js_comments: get_prop!(cfg, "remove_js_comments"),
preserve_brace_template_syntax: get_prop!(cfg, "preserve_brace_template_syntax"),
preserve_chevron_percent_template_syntax: get_prop!(cfg, "preserve_chevron_percent_template_syntax"),
remove_bangs: get_prop!(cfg, "remove_bangs"),
Expand Down
2 changes: 2 additions & 0 deletions minify-html/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Cfg {
pub minify_doctype: bool,
/// Minify JavaScript in `<script>` tags using [minify-js](https://github.com/wilsonzlin/minify-js).
pub minify_js: bool,
/// Remove all comments from minified JavaScript (only used when minify_js = True)
pub remove_js_comments: bool,
/// When `{{`, `{#`, or `{%` are seen in content, all source code until the subsequent matching closing `}}`, `#}`, or `%}` respectively gets piped through untouched.
pub preserve_brace_template_syntax: bool,
/// When `<%` is seen in content, all source code until the subsequent matching closing `%>` gets piped through untouched.
Expand Down
17 changes: 11 additions & 6 deletions minify-html/src/minify/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ pub fn minify_js(cfg: &Cfg, mode: TopLevelMode, out: &mut Vec<u8>, code: &[u8])
}).minify(&allocator, &mut program);

// Generate minified code
// Disable treeshake annotations (e.g., /*#__PURE__*/, /*@__PURE__*/)
// These are only useful for bundlers, not inline scripts
let comments = if cfg.remove_js_comments {
CommentOptions::disabled()
} else {
// Disable treeshake annotations (e.g., /*#__PURE__*/, /*@__PURE__*/)
// These are only useful for bundlers, not inline scripts
CommentOptions {
annotation: false,
..CommentOptions::default()
}
};
let codegen_options = CodegenOptions {
minify: true,
comments: CommentOptions {
annotation: false,
..CommentOptions::default()
},
comments,
..CodegenOptions::default()
};
let minified = Codegen::new()
Expand Down
24 changes: 24 additions & 0 deletions minify-html/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,27 @@ fn test_style_attr_minification() {
// `style` attributes are removed if fully minified away.
eval_with_css_min(br#"<div style=" /* */ "></div>"#, br#"<div></div>"#);
}

#[test]
fn test_remove_js_comments() {
let mut cfg = Cfg::default();
cfg.minify_js = true;
cfg.remove_js_comments = true;
eval_with_cfg(
br#"
<script>
// first comment to be removed
cp('create', id);
// second comment to be removed
cp('render', {
onFullActivation: function () {
console.log(1);
// Last comment to be removed
}
});
</script>
"#,
b"<script>cp(`create`,id),cp(`render`,{onFullActivation:function(){console.log(1)}});</script>",
&cfg
);
}
0