https://github.com/sam0x17/html-minifier
A zero-dependency HTML/CSS/Javascript minifier for the Crystal language
https://github.com/sam0x17/html-minifier
crystal-language crystal-shards css-minifier html-minifier javascript-minifier
Last synced: 9 months ago
JSON representation
A zero-dependency HTML/CSS/Javascript minifier for the Crystal language
- Host: GitHub
- URL: https://github.com/sam0x17/html-minifier
- Owner: sam0x17
- License: mit
- Created: 2019-10-01T17:10:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T07:50:30.000Z (over 4 years ago)
- Last Synced: 2025-05-12T22:55:34.939Z (10 months ago)
- Topics: crystal-language, crystal-shards, css-minifier, html-minifier, javascript-minifier
- Language: Crystal
- Homepage:
- Size: 178 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# html-minifier
html-minifier embeds the widely-used [html-minifier](https://www.npmjs.com/package/html-minifier)
NPM package in an easy-to-use Crystal shard via [duktape.cr](https://github.com/svaarala/duktape),
which provides a fast, embedded Javascript execution environment within Crystal.
html-minifier can be used to minify arbitrary HTML content, including Javascript and/or CSS.
Some features:
* minifies HTML and any embedded CSS/Javascript within the HTML
* no non-Crystal dependencies (no Node.js or NPM required)
* all html-minifier Javascript is baked into the shard, so you won't need to package any extra files with your app/tool/library
* doesn't embed an entire Node.js runtime (Javascript is executed via duktape.cr)
* simple, Crystal-based API (`HtmlMinifier.minify!("source code")`
* full support for html-minifier [options](https://github.com/kangax/html-minifier#options-quick-reference) via `HtmlMinifier.set_options`
* sane, one-size-fits-all options are included by default (unlike html-minifier on NPM)
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
html-minifier:
github: sam0x17/html-minifier
```
2. Run `shards install`
## Minification
```crystal
require "html-minifier"
HtmlMinifier.minify!(" minify me!") # => " minify me!"
HtmlMinifier.minify!("body { background-color: black }") # => "body{background-color:#000}"
HtmlMinifier.minify!(" alert('hello world');") # => "alert(\"hello world\")"
```
## Configuration
All options supported by html-minifier on NPM are supported by this shard. Options can be specified
by a `JSON::Any` object or by a JSON string, as shown below.
```crystal
require "html-minifier"
HtmlMinifier.minify!("") # => ""
HtmlMinifier.set_options("{\"removeComments\": false}")
HtmlMinifier.minify!("") # => ""
```
Note that user-specified options will override their respective default values. The default
values for all options are shown below:
```json
{
"caseSensitive": true,
"conservativeCollapse": true,
"minifyCSS": true,
"minifyJS": true,
"useShortDoctype": true,
"removeTagWhitespace": true,
"removeScriptTypeAttributes": true,
"removeComments": true,
"collapseWhitespace": true,
"collapseInlineTagWhitespace": true,
}
```