Git Product home page Git Product logo

lz-mini-js's Introduction

lz-mini-js

Compress source js code to no-dependencies self-decompressable js code.
lz-mini-js aims to provide as small output js code as possible and don't rely on external libraries for self-decompress.

Usage

CLI

npx lz-mini-js file.js file.lz.js

This command creates compressed js from file.js and write content to file.lz.js.
The result is fully standalone and does not require any dependencies to decompress itself. So it can be used as is in browsers or nodejs if you want.

API

Module is pretty simple:

const lzJs = require('lz-mini-js');
// ...
const compressedMinifiedCode = await lzJs('function decompressed() { return 42; }');
// write compressedMinifiedCode to file or pass anywhere else

lz-mini-js will minify only self-decompressor code using uglifyjs, source code to be compressed never minified by lz-mini-js.

If you want minify self-decompressor code by youself, pass false to second parameter:

const compressedCode = await lzJs(..., false);
// false means do not minify self-decompressor code
// here we can pass compressedCode to preferred minifier

How it works?

It uses lz-string lib for compressing and decompressing code, but with some tricks to reduce space usage in js code context. For compressing used lz-string itself, for decompressing - inlined version of decompressor.

Because of inlined decompressor lz-mini-js is not situable for very small scripts (~1k bytes and less), in this case decompressor itself is too big and compression profit is not notable. That's because we need to minify it by maximum.

To make self-decompressor extremely lightweight (~344 bytes using uglifyjs) it was almost fully rewritten with many assumptions. The main is compressed code string is always correct and we don't need to make any checks.

You can see inlined decompressor code in file src/lz-decompress.js, it's rewriten version of lz-string function _decompress.

lz-mini-js's People

Contributors

iassasin avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

bittorf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.