Formatting

This commit is contained in:
Wilson Lin 2022-06-22 12:24:52 +10:00
parent 776539b9ce
commit 78c4592b28
5 changed files with 65 additions and 42 deletions

55
nodejs/index.d.ts vendored
View File

@ -5,29 +5,32 @@
* @param cfg - Configurable minifier settings to use
* @returns Minified HTML code
*/
export function minify (src: Buffer, cfg: {
/** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
do_not_minify_doctype?: boolean,
/** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */
ensure_spec_compliant_unquoted_attribute_values?: boolean;
/** Do not omit closing tags when possible. */
keep_closing_tags?: boolean;
/** Do not omit `<html>` and `<head>` opening tags when they don't have attributes. */
keep_html_and_head_opening_tags?: boolean;
/** Keep spaces between attributes when possible to conform to HTML standards. */
keep_spaces_between_attributes?: boolean;
/** Keep all comments. */
keep_comments?: boolean;
/**
* If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [minify-js](https://github.com/wilsonzlin/minify-js).
*/
minify_js?: boolean;
/**
* If enabled, CSS in `<style>` tags and `style` attributes will be minified.
*/
minify_css?: boolean;
/** Remove all bangs. */
remove_bangs?: boolean;
/** Remove all processing_instructions. */
remove_processing_instructions?: boolean;
}): Buffer;
export function minify(
src: Buffer,
cfg: {
/** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
do_not_minify_doctype?: boolean;
/** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */
ensure_spec_compliant_unquoted_attribute_values?: boolean;
/** Do not omit closing tags when possible. */
keep_closing_tags?: boolean;
/** Do not omit `<html>` and `<head>` opening tags when they don't have attributes. */
keep_html_and_head_opening_tags?: boolean;
/** Keep spaces between attributes when possible to conform to HTML standards. */
keep_spaces_between_attributes?: boolean;
/** Keep all comments. */
keep_comments?: boolean;
/**
* If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [minify-js](https://github.com/wilsonzlin/minify-js).
*/
minify_js?: boolean;
/**
* If enabled, CSS in `<style>` tags and `style` attributes will be minified.
*/
minify_css?: boolean;
/** Remove all bangs. */
remove_bangs?: boolean;
/** Remove all processing_instructions. */
remove_processing_instructions?: boolean;
}
): Buffer;

View File

@ -5,7 +5,15 @@
"minify-html": "./cli.js"
},
"main": "index.js",
"files": ["src/**", "Cargo.toml", "cli.js", "index.d.ts", "index.js", "README.md", "postinstall.js"],
"files": [
"src/**",
"Cargo.toml",
"cli.js",
"index.d.ts",
"index.js",
"README.md",
"postinstall.js"
],
"version": "0.9.1",
"types": "index.d.ts",
"scripts": {

View File

@ -39,7 +39,11 @@ const downloadNativeBinary = async () => {
`https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node`
);
} catch (e) {
if (e instanceof StatusError && e.status !== 404 && attempt < MAX_DOWNLOAD_ATTEMPTS) {
if (
e instanceof StatusError &&
e.status !== 404 &&
attempt < MAX_DOWNLOAD_ATTEMPTS
) {
await wait(Math.random() * 2500 + 500);
continue;
}
@ -58,7 +62,9 @@ if (
downloadNativeBinary().then(
() => console.log(`Downloaded ${pkg.name}`),
(err) => {
console.error(`Failed to download ${pkg.name}, will build from source: ${err}`);
console.error(
`Failed to download ${pkg.name}, will build from source: ${err}`
);
const out = cp.spawnSync("npm", ["run", "build-release"], {
cwd: __dirname,
stdio: ["ignore", "inherit", "inherit"],

View File

@ -1,18 +1,28 @@
use minify_html_onepass::{Cfg, Error, in_place as minify_html_native};
use pyo3::prelude::*;
use minify_html_onepass::{in_place as minify_html_native, Cfg, Error};
use pyo3::exceptions::PySyntaxError;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use std::str::from_utf8_unchecked;
#[pyfunction(py_args="*", minify_js="false", minify_css="false")]
#[pyfunction(py_args = "*", minify_js = "false", minify_css = "false")]
fn minify(code: String, minify_js: bool, minify_css: bool) -> PyResult<String> {
let mut code = code.into_bytes();
match minify_html_native(&mut code, &Cfg {
minify_js,
minify_css,
}) {
match minify_html_native(
&mut code,
&Cfg {
minify_js,
minify_css,
},
) {
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }),
Err(Error { error_type, position }) => Err(PySyntaxError::new_err(format!("{} [Character {}]", error_type.message(), position))),
Err(Error {
error_type,
position,
}) => Err(PySyntaxError::new_err(format!(
"{} [Character {}]",
error_type.message(),
position
))),
}
}

View File

@ -114,11 +114,7 @@ for (const f of [
}
for (const f of ["README.md", "nodejs/Cargo.toml"]) {
replaceInFile(
f,
/^(minify-html = )"\d+\.\d+\.\d+"/m,
`$1"${NEW_VERSION}"`
);
replaceInFile(f, /^(minify-html = )"\d+\.\d+\.\d+"/m, `$1"${NEW_VERSION}"`);
}
for (const f of ["README.md"]) {