GithubHelp home page GithubHelp logo

isabella232 / minifyjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from minifyjs/minifyjs

0.0 0.0 0.0 772 KB

JavaScript minifier written in PHP, seeking to provide ultimate compression for all those looking for it. Currently fairly stable.

PHP 100.00%

minifyjs's Introduction

MinifyJS - A JavaScript minifier written in PHP

Because there was no JS minifier written in PHP available that could do optimisation of variables, and other (sometimes extreme and complex) transformations, I decided to write my own. Using the parser and tokenizer ported from Narcissus by Tino from tweakers.net, I created a simple system based on an AST which could easily handle all transformations.

How do I use it?

In the root folder there is a PHP file called min.php. It has the execute bit set and contains the shebang neccessary. ./min.php jquery-1.7.2.js does the trick.

There are various flags you can use, which will impact MinifyJS in some way:

  • --no-mangle or -nm: Do not optimise variables

  • --no-crush-bool or -ncb: Do not transform true to !0 or false to !1

  • --unsafe or -us: Perform possibly unsafe transformations. Almost always safe

  • --beautify or -b: Instead of minifying, beautify the code. Comments are still lost, except for license blocks

  • --no-copyright or -nc: Also remove license blocks (/*! ... */)

  • --strip-debug or -sd: Strip calls using the console variable, and remove debugger statements

  • --no-inlining or -ni: Do not inline constant variables (var a = 5;alert(a); will not transform to alert(5))

  • --timer or -t: Instead of showing the minified version, show various timing statistics

What kind of optimisations are done?

A lot. A few things:

  • Variables are minified when not global.
  • Constant folding: alert(5 + 5 / 2) will become alert(7.5). Only when the result is smaller than the original.
  • Inlining
  • Removal of useless statements.
  • if (foo) bar => foo&&bar and other similar stuff.
  • Constant if statements are replaced by either the then or else (depending on the condition)
  • Same with while, for statements and alike.
  • Unused variables and named functions are removed

Why?

The initial payload sent by a website is often on a clean cache. This basically means that people will have to download all content you send for your page. By minifying as much as you can, the page will be at the client faster. MinifyJS attempts to create not only smaller, but also faster JavaScript.

minifyjs's People

Contributors

rgustbardon avatar rvanvelzen 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.