Skip to content

unsonet/minify-text

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@unsonet/minify-text

Universal text and code minification utility for Node.js and browser environments.

Supports:

  • JavaScript / TypeScript (via Terser)
  • JSON / JSONC
  • HTML / XML
  • CSS
  • YAML / SQL / Shell
  • Plain text fallback

Features

  • Single API for all file types
  • Automatic syntax detection by filename
  • Terser-powered JS/TS minification
  • Safe fallback minifiers for non-code formats
  • Comment stripping + whitespace compression
  • Browser-compatible core (no fs dependency)
  • Deterministic output (useful for CI / diffing)

Installation

npm install @unsonet/minify-text

Usage

Basic

import { minifyText } from '@unsonet/minify-text';

const result = await minifyText(`
  function test() {
    console.log("hello");
  }
`, {
  filename: 'test.ts'
});

console.log(result);

API

minifyText(input, options)

function minifyText(
  input: string,
  options?: MinifyTextOptions
): Promise<string>

Options

type MinifyTextOptions = {
  filename?: string;
  syntax?: 'auto' | 'js' | 'ts' | 'json' | 'jsonc' | 'html' | 'xml' | 'css' | 'yaml' | 'sql' | 'shell' | 'text';

  // terser options (only for js/ts)
  ecma?: number;
  module?: boolean;
  mangle?: boolean;
  compress?: boolean | object;
  keep_classnames?: boolean;
  keep_fnames?: boolean;
};

How it works

1. Syntax detection

If syntax: 'auto', detection is based on file extension:

file.ts  ts
file.js  js
file.json  json
file.html  html

2. JS / TS path (Terser)

For JavaScript / TypeScript:

  • full AST-based minification
  • compression enabled (passes: 2)
  • optional mangling
  • removes comments
  • preserves function/class names (by default)

Example:

const result = await minifyText(code, {
  filename: 'index.ts',
  mangle: false,
});

3. Non-JS path (safe minifiers)

For all other formats:

JSON / JSONC

  • parsed and re-stringified (minified)

HTML / XML

  • collapses whitespace between tags
  • removes redundant spaces

CSS

  • removes whitespace around selectors and rules

YAML / SQL / SHELL

  • compact whitespace normalization

TEXT fallback

  • safe whitespace collapsing only

Behavior guarantees

  • No AST parsing except JS/TS (Terser)
  • No regex-based JS manipulation
  • Output is always valid for its format
  • Safe for browser usage (no Node APIs required in core logic)

Examples

JavaScript

await minifyText(`
  function sum(a, b) {
    return a + b;
  }
`, { filename: 'file.js' });

JSON

await minifyText(`
{
  "a": 1,
  "b": 2
}
`, { filename: 'data.json' });

HTML

await minifyText(`
<div>
  <span> hello </span>
</div>
`, { filename: 'index.html' });

CSS

await minifyText(`
body {
  margin: 0;
  padding: 0;
}
`, { filename: 'style.css' });

Design principles

  • predictable output
  • no unsafe transformations for non-code files
  • JS handled only by real parser (Terser)
  • everything else is conservative compression

License

MIT

About

Text minification and optimization tools.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors