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

9
nodejs/index.d.ts vendored
View File

@ -5,9 +5,11 @@
* @param cfg - Configurable minifier settings to use * @param cfg - Configurable minifier settings to use
* @returns Minified HTML code * @returns Minified HTML code
*/ */
export function minify (src: Buffer, cfg: { export function minify(
src: Buffer,
cfg: {
/** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */ /** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
do_not_minify_doctype?: boolean, do_not_minify_doctype?: boolean;
/** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */ /** 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; ensure_spec_compliant_unquoted_attribute_values?: boolean;
/** Do not omit closing tags when possible. */ /** Do not omit closing tags when possible. */
@ -30,4 +32,5 @@ export function minify (src: Buffer, cfg: {
remove_bangs?: boolean; remove_bangs?: boolean;
/** Remove all processing_instructions. */ /** Remove all processing_instructions. */
remove_processing_instructions?: boolean; remove_processing_instructions?: boolean;
}): Buffer; }
): Buffer;

View File

@ -5,7 +5,15 @@
"minify-html": "./cli.js" "minify-html": "./cli.js"
}, },
"main": "index.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", "version": "0.9.1",
"types": "index.d.ts", "types": "index.d.ts",
"scripts": { "scripts": {

View File

@ -39,7 +39,11 @@ const downloadNativeBinary = async () => {
`https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node` `https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node`
); );
} catch (e) { } 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); await wait(Math.random() * 2500 + 500);
continue; continue;
} }
@ -58,7 +62,9 @@ if (
downloadNativeBinary().then( downloadNativeBinary().then(
() => console.log(`Downloaded ${pkg.name}`), () => console.log(`Downloaded ${pkg.name}`),
(err) => { (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"], { const out = cp.spawnSync("npm", ["run", "build-release"], {
cwd: __dirname, cwd: __dirname,
stdio: ["ignore", "inherit", "inherit"], stdio: ["ignore", "inherit", "inherit"],

View File

@ -1,18 +1,28 @@
use minify_html_onepass::{Cfg, Error, in_place as minify_html_native}; use minify_html_onepass::{in_place as minify_html_native, Cfg, Error};
use pyo3::prelude::*;
use pyo3::exceptions::PySyntaxError; use pyo3::exceptions::PySyntaxError;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction; use pyo3::wrap_pyfunction;
use std::str::from_utf8_unchecked; 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> { fn minify(code: String, minify_js: bool, minify_css: bool) -> PyResult<String> {
let mut code = code.into_bytes(); let mut code = code.into_bytes();
match minify_html_native(&mut code, &Cfg { match minify_html_native(
&mut code,
&Cfg {
minify_js, minify_js,
minify_css, minify_css,
}) { },
) {
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }), 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"]) { for (const f of ["README.md", "nodejs/Cargo.toml"]) {
replaceInFile( replaceInFile(f, /^(minify-html = )"\d+\.\d+\.\d+"/m, `$1"${NEW_VERSION}"`);
f,
/^(minify-html = )"\d+\.\d+\.\d+"/m,
`$1"${NEW_VERSION}"`
);
} }
for (const f of ["README.md"]) { for (const f of ["README.md"]) {