Skip to content

From Rust

The core of korpress is a plain Rust library. It has no dependencies, and it does not need Python. Use it to minify HTML, CSS and JavaScript from a Rust program; the Python package is a thin layer on top of it.

The simplest example

This program minifies one string of each kind and prints the result. (To make it compile, first add korpress to your project.)

use _korpress::{css, html, js};

fn main() {
    // CSS never fails.
    let style = css::minify("a { color : red ; }");
    println!("{style}");

    // HTML never fails either. Options::default() minifies inline CSS and JS too.
    let page = html::minify(
        "<p>  Hello,\n   <b>world</b>  </p>  <!-- note -->",
        &html::Options::default(),
    );
    println!("{page}");

    // JavaScript returns a Result: it fails on an unterminated string, comment,
    // template literal or regular expression.
    match js::minify("const a = 1;\n\n// add\nconst b = a + 2;\n") {
        Ok(code) => println!("{code}"),
        Err(e) => eprintln!("cannot minify: {e}"),
    }
    match js::minify("const s = 'oops") {
        Ok(code) => println!("{code}"),
        Err(e) => println!("error: {e} (line {}, column {})", e.line, e.column),
    }
}

With a path dependency on the repository, cargo run prints:

a{color:red}
<p>Hello, <b>world</b></p>
const a=1;const b=a+2;
error: unterminated string at line 1, column 11 (line 1, column 11)

The {e} text already includes the line and column, so the last line repeats them. Print the fields yourself only when you need them apart, for an editor marker for example.

Add it to your project

Note

korpress is not published on crates.io yet. Depend on it from git, or from a local path.

From git:

[dependencies]
korpress = { git = "https://github.com/3d3n-pyc/korpress" }

From a local copy of the repository:

[dependencies]
korpress = { path = "../korpress" }

The package is called korpress, but the library inside it is called _korpress. This is because the same crate also builds the Python extension korpress._korpress. So in your code you write use _korpress::....

The three functions

Each minifier is a module with a minify function.

Function Returns Can fail
css::minify(src: &str) String No
html::minify(src: &str, opts: &html::Options) String No
js::minify(src: &str) Result<String> Yes
  • html::Options has four switches, all true by default: collapse_whitespace, remove_comments, minify_css and minify_js. Use html::Options::default() for the usual result.
  • For what each minifier changes and leaves alone, read the pages for HTML, CSS and JavaScript.

The error type

js::minify and js::minify_with return _korpress::Result<String>, which is Result<String, _korpress::Error>. They fail on an unterminated string, comment, template literal or regular expression.

Error has three public fields: message, and line and column (both start at 1). It implements Display and std::error::Error, so it works with ? and Box<dyn Error>. Its text reads like unterminated string at line 1, column 11.

In depth

More functions and options

  • css::minify_with takes a css::Options (keep_important_comments), and js::minify_with takes a js::Options (keep_important_comments and drop_final_semicolon).
  • css::minify_declarations minifies the content of a style="" attribute.

Dependency details

  • To get the same code each time, pin a commit with rev = "<commit>" (or a tag, once there is one).
  • The python feature, which pulls in PyO3, is off by default. You do not need it.
  • The git dependency was not tested against a published tag: the example above was built with the path form.

Good to know

  • The functions take &str, so the input must be valid UTF-8.
  • The functions are marked #[must_use]: the result is a new string, the input is not changed.