Coder Social home page Coder Social logo

jsqueeze's Introduction

JSqueeze: Efficient JavaScript minification in PHP

Latest Stable Version Total Downloads Build Status

JSqueeze shrinks / compresses / minifies / mangles Javascript code.

It's a single PHP class that has been developed, maintained and thoroughly tested since 2003 on major JavaScript frameworks (e.g. jQuery).

JSqueeze operates on any parse error free JavaScript code, even when semi-colons are missing.

In term of compression ratio, it compares to YUI Compressor and UglifyJS.

Installation

Through composer:

{
    "require": {
        "patchwork/jsqueeze": "~2.0"
    }
}

Usage

use Patchwork\JSqueeze;

$jz = new JSqueeze();

$minifiedJs = $jz->squeeze(
    $fatJs,
    true,   // $singleLine
    true,   // $keepImportantComments
    false   // $specialVarRx
);

Features

  • Removes comments and white spaces.
  • Renames every local vars, typically to a single character.
  • Keep Microsoft's conditional comments.
  • In order to maximise later HTTP compression (deflate, gzip), new variables names are choosen by considering closures, variables' frequency and characters' frequency.
  • Can rename also global vars, methods and properties, but only if they are marked special by some naming convention. Use JSqueeze::SPECIAL_VAR_PACKER to rename vars whose name begins with one or more $ or with a single _.
  • Renames also local/global vars found in strings, but only if they are marked special.
  • If you use with/eval then be careful.

Bonus

  • Replaces false/true by !1/!0
  • Replaces new Array/Object by []/{}
  • Merges consecutive var declarations with commas
  • Merges consecutive concatened strings
  • Can replace optional semi-colons by line feeds, thus facilitating output debugging.
  • Keep important comments marked with /*!...
  • Treats three semi-colons ;;; like single-line comments.
  • Fix special catch scope across browsers
  • Work around buggy-handling of named function expressions in IE<=8

To do?

  • foo['bar'] => foo.bar
  • {'foo':'bar'} => {foo:'bar'}
  • Dead code removal (never used function)
  • Munge primitives: var WINDOW=window, etc.

License

This library is free software; you can redistribute it and/or modify it under the terms of the (at your option): Apache License v2.0 (see provided LICENCE.ASL20 file), or GNU General Public License v2.0 (see provided LICENCE.GPLv2 file).

jsqueeze's People

Contributors

0b10011 avatar adamwill avatar callmemagnus avatar nicolas-grekas avatar stchr avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.