Giter Club home page Giter Club logo

dotopstory / minify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matthiasmullie/minify

0.0 2.0 0.0 2.25 MB

CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.

Home Page: https://www.minifier.org

License: MIT License

Dockerfile 0.17% PHP 96.60% Makefile 0.27% Shell 0.53% CSS 0.95% JavaScript 1.49%

minify's Introduction

Minify - minifier.org

Build status Code coverage Code quality Latest version Downloads total License

Donate/Support: Support

Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns, such as:

JavaScript

  • object['property'] -> object.property
  • true, false -> !0, !1
  • while(true) -> for(;;)

CSS

  • @import url("http://path") -> @import "http://path"
  • #ff0000, #ff00ff -> red, #f0f
  • -0px, 50.00px -> 0, 50px
  • bold -> 700
  • p {} -> removed

And it comes with a huge test suite.

Usage

CSS

use MatthiasMullie\Minify;

$sourcePath = '/path/to/source/css/file.css';
$minifier = new Minify\CSS($sourcePath);

// we can even add another file, they'll then be
// joined in 1 output file
$sourcePath2 = '/path/to/second/source/css/file.css';
$minifier->add($sourcePath2);

// or we can just add plain CSS
$css = 'body { color: #000000; }';
$minifier->add($css);

// save minified file to disk
$minifiedPath = '/path/to/minified/css/file.css';
$minifier->minify($minifiedPath);

// or just output the content
echo $minifier->minify();

JS

// just look at the CSS example; it's exactly the same, but with the JS class & JS files :)

Methods

Available methods, for both CSS & JS minifier, are:

__construct(/* overload paths */)

The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified. All CSS/JS passed along, will be combined into 1 minified file.

use MatthiasMullie\Minify;
$minifier = new Minify\JS($path1, $path2);

add($path, /* overload paths */)

This is roughly equivalent to the constructor.

$minifier->add($path3);
$minifier->add($js);

minify($path)

This will minify the files' content, save the result to $path and return the resulting content. If the $path parameter is omitted, the result will not be written anywhere.

CAUTION: If you have CSS with relative paths (to imports, images, ...), you should always specify a target path! Then those relative paths will be adjusted in accordance with the new path.

$minifier->minify('/target/path.js');

gzip($path, $level)

Minifies and optionally saves to a file, just like minify(), but it also gzencode()s the minified content.

$minifier->gzip('/target/path.js');

setMaxImportSize($size) (CSS only)

The CSS minifier will automatically embed referenced files (like images, fonts, ...) into the minified CSS, so they don't have to be fetched over multiple connections.

However, for really large files, it's likely better to load them separately (as it would increase the CSS load time if they were included.)

This method allows the max size of files to import into the minified CSS to be set (in kB). The default size is 5.

$minifier->setMaxImportSize(10);

setImportExtensions($extensions) (CSS only)

The CSS minifier will automatically embed referenced files (like images, fonts, ...) into minified CSS, so they don't have to be fetched over multiple connections.

This methods allows the type of files to be specified, along with their data:mime type.

The default embedded file types are gif, png, jpg, jpeg, svg & woff.

$extensions = array(
    'gif' => 'data:image/gif',
    'png' => 'data:image/png',
);

$minifier->setImportExtensions($extensions);

Installation

Simply add a dependency on matthiasmullie/minify to your composer.json file if you use Composer to manage the dependencies of your project:

composer require matthiasmullie/minify

Although it's recommended to use Composer, you can actually include these files anyway you want.

Try it

Simply try it out online at https://www.minifier.org.

License

Minify is MIT licensed.

Challenges

If you're interested in learning some of the harder technical challenges I've encountered building this, you probably want to take a look at what I wrote about it on my blog.

minify's People

Contributors

alexcorvi avatar ginopane avatar gordonlesti avatar igll avatar k3min avatar kaittodesk avatar matthiasmullie avatar mattleff avatar merty avatar roggeo avatar rozinko avatar smxsm avatar throup avatar vladimmi avatar woutervanvliet avatar zebratrois avatar

Watchers

 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.