Mecha CMS

Add extra functionality to the core engine.

Minify

Author: Taufik Nurrohman · 133 Views

Tweet

Remove white-spaces and unused comments in HTML, CSS and JavaScript files.

Table of Content

Download Latest Version

There should be a config.php file in the lot\state folder of this extension. It contains some configuration data about the minifier:

// `Minify::{$any}($input, $comment, $quote)`
[
    'css' => [2, 2], // `[$comment, $quote]`
    'html' => [2, 1],
    'js' => [2, 2]
]
CodeDescription
0Remove
1Keep
2Remove if/but/when…

Usage 

HTML 

Minify::html($input, $comment = 2, $quote = 1);

Minify HTML string:

echo Minify::html('<!DOCTYPE html>
<html dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <!-- remove -->
  </head>
  <body>
    <!--[if IE]> keep <![endif]-->
    <h1>Test</h1>
    <p>
      <button>Button 1</button> <button>Button 2</button>
    </p>
  </body>
</html>');

CSS 

Minify::css($input, $comment = 2, $quote = 0);

Minify CSS string:

echo Minify::css('* {
  margin: 0;
  padding: 0;
  font: inherit;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

/* remove */

/*! keep */

/** keep */

/* @licence keep */

/* @license keep */

/* @preserve keep */

html {
  background: #fff;
  font: normal normal 13px/1.5 Helmet, FreeSans, sans-serif;
  color: #000;
}');

JS 

Minify::js($input, $comment = 2);

Minify JavaScript string:

echo Minify::js('var i, j, k = Date.now();

for (i = 0, j = k; i < k; ++i) {
    console.log("bark: " + i);
}');

Options 

ParameterDescription
$inputThe string input to be minified.
$commentThe $comment parameter value consists of 0, 1 and 2. 0 means remove all comments, 1 means preserve all comments, 2 means remove all comments except special comments like IE conditional comments or license comments.
$quoteThe $quote parameter value consists of 0 and 1. 0 means remove quotes where possible, 1 means preserve all quotes.