[TYPO3-dev] Javascript Minify-Manager

Christopher Lörken christopher at loerken.net
Mon Mar 9 12:22:55 CET 2009


Hello,

I actually had the same thoughts a couple of months ago and also wanted 
something similar.

I came up with a first step towards a solution that I am using myself 
which basically does what you suggested for self-written plugins. I've 
attached the code to the end of this post if you're interested.

Here the short list of features and shortcomings of the solution:

Features:
   - minifies Javascript files using the unbuild 
t3lib_div::minifyJavascript function.
   - Stores the result in a temporary file in typo3temp folder.
   - Minified file is only being generated if it does not yet exist.

Shortcomings:
   - It is no proper extension that could be enabled/disabled/configured 
from TS.
   - The temp file is only being generated based on the filename of the 
original file. Changing the content of the Javascript file does not 
automatically lead to a new minified version, the temp file has to be 
deleted manually.
   - No check for possible parallel generation of the file. (There are 
some suggestions in this thread...)
   - Does not work with scriptaculous.

@scriptaculous: I think the actual minification with t3lib_div worked 
well, but the problem with scriptaculous is its feature to only include 
the actually needed versions during runtime. That means, its different 
.js files are actually accessed by their file names from within the JS 
itself. Obviously something like that does not allow to be put in 
temporary files with md5ed file names.
For the contrib folder I thus ended up minifying the JS using JSmin by 
hand and replacing the content of the files. That worked for our setup.


I brought up this topic in the german typo3.net forum:
http://www.typo3.net/index.php?id=13&action=list_post&tid=78786&page=1

And I think some of you have already answered there. Anyway. Here you 
have it again ;)


Cheers,
Christopher



Code:

	/**
	 * Minifies a JavaScript File, stores it as temporary file and returns 
a HTML <script> tag reference to
	 * it. The file will be based on a md5 hash of the original file. It 
will only be minified and written if
	 * it did not exist in the temp folder.
	 *
	 * @param String $file
	 * @param tslib_piBase $plugin - Reference to the calling plugin
	 * @param bool $skipForDebug if set true, the script will return a 
reference to the original file (no min/tmp). Use this if you are 
debugging the JS file since it will not be replaced if only the content 
changes.
	 * @return string <script> tag wrapped HTML ready reference to the JS file.
	 */
	function minifyJS2TempFile($file, &$ext, $skipForDebug = false) {
		if ($skipForDebug) return '<script type="text/javascript" 
src="'.htmlspecialchars($GLOBALS['TSFE']->absRefPrefix.$file).'"></script>';
		// Create filename / tags:
		$hashedName = 'typo3temp/javascript_'.substr(md5($file),0,10).'.js';
		$output = '<script type="text/javascript" 
src="'.htmlspecialchars($GLOBALS['TSFE']->absRefPrefix.$hashedName).'"></script>';
		// Write minifies file if not exists:
		if (!@is_file(PATH_site.$hashedName))	{
			//read original
			$str = file_get_contents($file);
			//minify
			$str = t3lib_div::minifyJavaScript($str, $error);
			t3lib_div::writeFile(PATH_site.$hashedName,$str);
		}
		return $output;
	}





More information about the TYPO3-dev mailing list