rah/rah_minify

Assets minifier for Textpattern CMS

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 4

Forks: 0

Open Issues: 0

Type:textpattern-plugin

dev-master 2013-08-01 09:33 UTC

This package is auto-updated.

Last update: 2024-10-15 13:03:53 UTC


README

Packagist | GitHub | Twitter | Donate

Rah_minify is an asset compressor plugin for Textpattern CMS. It minifies and preprocesses flat JavaScript and CSS files. Rah_minify removes comments, unneeded white-space and compressed property values and variables to its shortest format, while keeping the code’s behavior intact.

Installing

Using Composer. The plugin uses relies to some development packages, so you will have to add some repository link rules to your composer.json file:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/gocom/YUI-CSS-compressor-PHP-port.git"
        }
    ],
    "minimum-stability": "dev"
}

The above lower your minimum-stability to development versions and adds link to our fork of YUI CSS compressor PHP port by Túbal Martín. After done, you can run the installation:

$ composer.phar require rah/rah_minify

Requirements

Rah_minify’s minimum requirements:

  • Textpattern 4.5 or newer.

Preferences

Rah_minify offers few preferences that can be used to do all the needed configuration. All settings can be found from Advanced Preferences, organized under a Minify section. The following settings will be present.

Security key for the public callback

A key that is used to restrict unwanted access to the callback URL. This should be set as a long, strong value, containing combination of numbers, characters and symbols.

If set, assets can be processed and minified with a public callback URL. This removes any need to have the site to set to debugging mode or having to login to Textpattern. Each time the callback URL is accessed, processed files are created from your assets.

This callback URL can be used with a cron job to do scheduled processing job, or added into automated deployment task. The callback could be executed on post-hook after each commit, or code change. This way you never have to worry about compressing and compiling your JavaScript or CSS code manually, or polluting your repository with unneeded files.

A callback URL would look like this:

http://example.com/?rah_minify=key

Where key is the value set in Security key for the public callback field, and http://example.com/ is path to your Textpattern site.

List of compressed files

This setting sets the JavaScript and CSS files that are being compressed by rah_minify. The setting takes an list of absolute file system paths, formatted in source and target pairs, pairs separated by line breaks.

source target
source target [\n...]

Where the source is the file that is being compressed and the target is the location where the compressed file is written to. The target and the source paths are separated by a space (U+0020). Compressed files are separated by line breaks (a single UNIX style linefeed). All paths will be relative to the Textpattern’s installation directory (e.g. ./textpattern).

The following would compress files named main.js, extra.js and default.css, located in assets/src/, and write the results to the parent assets directory. The files would be located in a assets directory placed to the site’s root, the same directory containing the textpattern directory and your site’s index.php file.

../assets/src/main.js ../assets/main.js
../assets/src/extra.js ../assets/extra.js
../assets/src/default.css ../assets/default.css

Create snapshots?

If set to Yes, additional timestamped files are created each time an asset file is compressed. These files act as versions of the created files. These files offer an way to optimize caching of static resources.

These timestamped versions are written to the same target directory as the compressed file. Versioned files follow the naming pattern:

{targetName}.{timestamp}.{targetExtension}

Where in the {targetName} is the name of the target file (e.g. main.min), the {timestamp} is a Unix timestamp (e.g. 1345269546) and the {targetExtension} is the target files extension (e.g. css or js). The timestamp included in the filename is same as the target file’s modification date.

Consider the following file pair:

../assets/src/main.js ../assets/main.js

The created versioned file would be named as main.1345269546.js, written to the same parent assets directory as the minified main.js file.

Setting processed files

Todo.

Optionally from PHP

Todo.

Extension decides

Processing action is decided from the source asset file’s extension. Files that end with a .css are processed as a CSS and .js as a JavaScript. If the optional LESS PHP compiler is installed, rah_minify can also process LESS code used in .less files. Files with an unrecognized extension fill be ignored.

Joining files

Rah_minify also supports file joining. All the source assets files are and can be processed as stacks, where contents are written to a single compressed target file. How this works is very simple. Any file pair that has the same target path will be joined and compressed output written to the same target file.

With the following file configuration setting only two compressed files are written while there are total of five source files:

../js/jquery.plugin.js ../js/main.min.js
../js/main.js ../js/main.min.js
../css/bootstrap.css ../css/main.min.css
../css/jquery.plugin.css ../css/main.min.css
../css/main.css ../css/main.min.css

The above will compress two files named as main.min.css and main.min.js. The files are joined and written to the target files in the order they are listed. The sources of bootstrap.css will be at the top of the main.min.css while the styles from main.css are at the end of the file.

LESS support

To enable LESS syntax support in your stylesheets, you would first need to install and import LESS PHP class to Textpattern. First download LESSPHP and upload lessc.inc.php to your website’s server. After you have uploaded the file open up your Textpattern’s configuration file, config.php and add the following line to it:

include "/absolute/path/to/lessc.inc.php";

Where the /absolute/path/to/less.inc.php is an absolute path to the LESS PHP’s class file you uploaded.

Debugging and the activity log

Rah_minify writes a log and a debugging lines to Textpattern’s tag trace when the site is in a debugging mode. Looking at the trace can give an idea why a file isn’t processed correctly. Lines added by rah_minify look something like the following:

[rah_minify: main.min.css is up to date]

Every line added by rah_minify is prefixed appropriately with rah_minify:.

Thanks to

Changelog

Version 0.1 – upcoming

  • Initial release.