Create onepass variant for Python

This commit is contained in:
Wilson Lin 2022-06-22 12:24:15 +10:00
parent fde8354461
commit 776539b9ce
18 changed files with 85 additions and 10 deletions

View File

@ -11,6 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
variant: [main, onepass]
os: [macos-11.0, ubuntu-18.04, windows-2019, self-hosted-linux-arm64, self-hosted-macos-arm64]
python: [3.8, 3.9, '3.10']
steps:
@ -49,7 +50,7 @@ jobs:
run: bash ./prebuild.sh
- name: Build native module
working-directory: ./python
working-directory: ./python/${{ matrix.feature }}
run: cargo build --release
- name: Install Python build tools (macOS x64)
@ -71,7 +72,7 @@ jobs:
- name: Pack and publish package
shell: bash
working-directory: ./python
working-directory: ./python/${{ matrix.feature }}
run: |
cat << 'EOF' > "$HOME/.pypirc"
[pypi]

View File

@ -3,6 +3,7 @@
## 0.9.2
- Fix Node.js dependency version.
- Create onepass variant for Python.
## 0.9.1

5
format
View File

@ -4,7 +4,7 @@ set -Eeuxo pipefail
pushd "$(dirname "$0")" >/dev/null
npx prettier@2.3.2 -w 'version' 'bench/*.{js,json}' 'bench/runners/*.{js,json}' 'bench/runners/*/*.{js,json}' 'gen/*.{ts,json}'
npx prettier@2.3.2 -w 'version' 'bench/*.{js,json}' 'bench/runners/*.{js,json}' 'bench/runners/*/*.{js,json}' 'gen/*.{ts,json}' 'nodejs/*.{js,json,ts}'
for dir in \
bench/runners/minify-html \
@ -16,7 +16,8 @@ for dir in \
fuzz/process \
java \
nodejs \
python \
python/main \
python/onepass \
ruby \
rust/main \
rust/onepass \

View File

@ -1 +0,0 @@
../README.md

View File

@ -15,7 +15,7 @@ name = "minify_html"
crate-type = ["cdylib"]
[dependencies]
minify-html = { path = "../rust/main" }
minify-html = { path = "../../rust/main" }
[dependencies.pyo3]
version = "0.13.0"
features = ["extension-module"]

1
python/main/README.md Symbolic link
View File

@ -0,0 +1 @@
../../README.md

View File

@ -0,0 +1,11 @@
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

10
python/onepass/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
/Cargo.lock
/target/
__pycache__/
/venv/
# Used by Python setuptools.
/build/
/dist/
/*.egg-info/

21
python/onepass/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
publish = false
name = "minify_html_onepass"
description = "Even faster version of minify-html"
license = "MIT"
homepage = "https://github.com/wilsonzlin/minify-html"
readme = "README.md"
repository = "https://github.com/wilsonzlin/minify-html.git"
version = "0.9.1"
authors = ["Wilson Lin <code@wilsonl.in>"]
edition = "2018"
[lib]
name = "minify_html_onepass"
crate-type = ["cdylib"]
[dependencies]
minify-html-onepass = { path = "../../rust/onepass" }
[dependencies.pyo3]
version = "0.13.0"
features = ["extension-module"]

1
python/onepass/README.md Symbolic link
View File

@ -0,0 +1 @@
../../rust/onepass/README.md

View File

@ -0,0 +1,5 @@
def minify(
code: str,
minify_css: bool = False,
minify_js: bool = False,
) -> str: ...

24
python/onepass/src/lib.rs Normal file
View File

@ -0,0 +1,24 @@
use minify_html_onepass::{Cfg, Error, in_place as minify_html_native};
use pyo3::prelude::*;
use pyo3::exceptions::PySyntaxError;
use pyo3::wrap_pyfunction;
use std::str::from_utf8_unchecked;
#[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,
}) {
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))),
}
}
#[pymodule]
fn minify_html_onepass(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(minify))?;
Ok(())
}

View File

@ -14,12 +14,11 @@ An HTML minifier that provides the functionality of [minify-html](https://github
The API is different compared to minify-html; refer to per-package documentation for more details.
- <img width="24" src="https://wilsonl.in/minify-html/icon/java.png"> [in.wilsonl.minifyhtmlonepass](https://search.maven.org/artifact/in.wilsonl.minifyhtmlonepass/minify-html-onepass)
- <img width="24" src="https://wilsonl.in/minify-html/icon/nodejs.png"> [@minify-html/onepass](https://www.npmjs.com/package/@minify-html/onepass)
- <img width="24" src="https://wilsonl.in/minify-html/icon/python.png"> [minify-html-onepass](https://pypi.org/project/minify-html-onepass)
- <img width="24" src="https://wilsonl.in/minify-html/icon/ruby.png"> [minify_html_onepass](https://rubygems.org/gems/minify_html_onepass)
- <img width="24" src="https://wilsonl.in/minify-html/icon/rust.png"> [minify-html-onepass](https://crates.io/crates/minify-html-onepass)
If you don't see your preferred language here and the main library supports it, raise an issue.
## Parsing
In addition to the [minify-html rules](https://github.com/wilsonzlin/minify-html/blob/master/notes/Parsing.md), the onepass variant has additional requirements:

View File

@ -101,7 +101,8 @@ for (const f of [
"cli/Cargo.toml",
"nodejs/Cargo.toml",
"java/Cargo.toml",
"python/Cargo.toml",
"python/main/Cargo.toml",
"python/onepass/Cargo.toml",
"ruby/Cargo.toml",
"wasm/Cargo.toml",
]) {