Skip to content

proctorio/hulk.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hulk.js

npm version npm downloads License Tests Coverage

Lightweight, zero-dependency minification library for HTML, CSS, SVG, JavaScript, and JSON content.

Features

  • πŸš€ 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

Installation

npm install @proctorio/hulk

Usage

import 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>'

API

minify__(content, extension)

Minifies the provided content based on the file extension.

Parameters:

  • content (string): The content to minify
  • extension (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 attributes
  • css - Removes comments, whitespace, and optimizes spacing
  • svg - Removes whitespace and comments
  • js - Removes comments and trailing whitespace
  • json - Uses native JSON.stringify for minification

Minification Features

CSS Optimizations

  • βœ… 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, !important flags, string content

HTML Optimizations

  • βœ… 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

SVG Optimizations

  • βœ… Number precision: Removes .0 from 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:space attributes, path data precision

JavaScript Optimizations

  • βœ… Comment removal: Single-line and multi-line comments
  • βœ… Trailing whitespace: Removed per line
  • βœ… Preserved features: Template literals, strings with comment-like syntax

JSON Optimizations

  • βœ… Native JSON.stringify: Reliable and safe minification
  • βœ… Fallback handling: Returns original if JSON is invalid
  • βœ… Nested structure support: Handles complex objects

Examples

Before and After

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>

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Development

Prerequisites

  • Node.js >= 16.0.0
  • npm >= 7.0.0

Setup

git clone https://github.com/proctorio/hulk.js
cd hulk.js
npm install

Scripts

# 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 prepublishOnly

Testing

The project uses Jest with 100% code coverage requirement. Test results and coverage reports are generated in standardized formats:

  • Test Results: npm test generates test-results.xml in JUnit format (.test_output/test-results.xml)
  • Coverage Reports: npm run coverage generates:
    • Cobertura XML format (cobertura-coverage.xml)
    • LCOV format (lcov.info and HTML report in lcov-report/)

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

Publishing

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 publish

The package includes only necessary files for distribution:

  • lib/ - Compiled source code
  • LICENSE - Apache 2.0 license
  • README.md - Documentation

Development files (tests, configuration, etc.) are excluded via .npmignore.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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)

Author

Proctorio

Support

For issues and questions, please visit the GitHub Issues page.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published