Lightweight, zero-dependency minification library for HTML, CSS, SVG, JavaScript, and JSON content.
- π Zero runtime dependencies - Pure JavaScript implementation
- β‘ Fast and efficient - Regex-based transformations
- π― Simple API - Single function with extension-based routing
- π¦ Tiny footprint - Minimal bundle size
- β Well-tested - 100% code coverage with 41 comprehensive tests
- π§ ES Modules - Modern module system support
- πͺ Advanced optimizations - Aggressive minification with safe defaults
npm install @proctorio/hulkimport minify__ from '@proctorio/hulk';
// Minify HTML
const html = '<div> <p> Hello </p> </div>';
const minifiedHtml = minify__(html, 'html');
// Result: '<div><p>Hello</p></div>'
// Minify CSS with advanced optimizations
const css = `
.class {
color: #ffffff;
margin: 0px;
padding: 0.5em;
}
`;
const minifiedCss = minify__(css, 'css');
// Result: '.class{color:#fff;margin:0;padding:.5em}'
// Minify JavaScript
const js = `
function test() {
// This is a comment
return 42;
}
`;
const minifiedJs = minify__(js, 'js');
// Result: 'function test() {\n return 42;\n}'
// Minify JSON
const json = '{\n "key": "value"\n}';
const minifiedJson = minify__(json, 'json');
// Result: '{"key":"value"}'
// Minify SVG
const svg = '<svg> <circle cx="50" cy="50" r="40"/> </svg>';
const minifiedSvg = minify__(svg, 'svg');
// Result: '<svg><circle cx="50" cy="50" r="40"/></svg>'Minifies the provided content based on the file extension.
Parameters:
content(string): The content to minifyextension(string): File type -'html','css','svg','js', or'json'
Returns:
- (string): Minified content, or empty string if input is not a string
Supported Extensions:
html- Removes whitespace, comments, and optimizes attributescss- Removes comments, whitespace, and optimizes spacingsvg- Removes whitespace and commentsjs- Removes comments and trailing whitespacejson- Uses native JSON.stringify for minification
- β
Zero unit removal:
0pxβ0,0remβ0(all CSS units) - β
Leading zero removal:
0.5emβ.5em - β
Trailing semicolon removal:
color:red;}βcolor:red} - β
Hex color compression:
#ffffffβ#fff,#112233β#123 - β
Whitespace optimization: Removes spaces around
{}:;and after colons - β
Preserved features:
calc()function spaces,!importantflags, string content
- β
Attribute spacing:
class = "foo"βclass="foo" - β
Tag spacing:
<div >β<div> - β
Default type removal: Strips
type="text/javascript"from<script>tags - β
Boolean attributes:
checked="checked"βchecked - β Whitespace collapsing: Multiple spaces reduced to single space
- β Comment removal: HTML comments are stripped
- β
Number precision: Removes
.0from whole numbers (10.0β10) - β
Trailing zeros:
1.500β1.5 - β
Leading zeros:
0.5β.5 - β Attribute spacing: Optimizes spaces around equals signs
- β
Preserved features:
xml:spaceattributes, path data precision
- β Comment removal: Single-line and multi-line comments
- β Trailing whitespace: Removed per line
- β Preserved features: Template literals, strings with comment-like syntax
- β Native JSON.stringify: Reliable and safe minification
- β Fallback handling: Returns original if JSON is invalid
- β Nested structure support: Handles complex objects
CSS Example:
/* Before */
.button {
color: #ffffff;
margin: 0px;
padding: 0.5rem 10px 0em;
border-radius: 0.25em;
}
/* After */
.button{color:#fff;margin:0;padding:.5rem 10px 0;border-radius:.25em}HTML Example:
<!-- Before -->
<script type="text/javascript">
<input type="checkbox" checked="checked" disabled="disabled" />
</script>
<!-- After -->
<script><input type="checkbox" checked disabled/></script>SVG Example:
<!-- Before -->
<svg width = "100.0" height = "50.0">
<circle cx="10.500" cy="0.5" r="5"/>
</svg>
<!-- After -->
<svg width="100" height="50"><circle cx="10.5" cy=".5" r="5"/></svg>This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Node.js >= 16.0.0
- npm >= 7.0.0
git clone https://github.com/proctorio/hulk.js
cd hulk.js
npm install# Build (copies src/ to lib/)
npm run build
# Run tests
npm test
# Run tests with coverage
npm run coverage
# Lint code
npm run lint
# Check linting without fixing
npm run lint:check
# Pre-publish checks (build + test)
npm run prepublishOnlyThe project uses Jest with 100% code coverage requirement. Test results and coverage reports are generated in standardized formats:
- Test Results:
npm testgeneratestest-results.xmlin JUnit format (.test_output/test-results.xml) - Coverage Reports:
npm run coveragegenerates:- Cobertura XML format (
cobertura-coverage.xml) - LCOV format (
lcov.infoand HTML report inlcov-report/)
- Cobertura XML format (
All 41 tests cover:
- β All minification functions (HTML, CSS, SVG, JS, JSON)
- β Advanced CSS optimizations (zero units, hex colors, leading zeros)
- β HTML attribute optimizations (boolean attributes, default types)
- β SVG number precision optimizations
- β Edge cases (empty content, invalid input, special characters)
- β Type validation and error handling
- β Comment preservation in strings
- β
CSS
calc()function handling - β Template literals in JavaScript
This package is published to npm as @proctorio/hulk.
To publish a new version:
# Update version in package.json
npm version patch # or minor, or major
# Build and test
npm run prepublishOnly
# Publish to npm
npm publishThe package includes only necessary files for distribution:
lib/- Compiled source codeLICENSE- Apache 2.0 licenseREADME.md- Documentation
Development files (tests, configuration, etc.) are excluded via .npmignore.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure:
- All tests pass (
npm test) - Code coverage remains at 100% (
npm run coverage) - Code follows the linting rules (
npm run lint)
Proctorio
For issues and questions, please visit the GitHub Issues page.