[TYPO3-core] RFC: minify JavaScript
Martin Kutschker
Martin.Kutschker at n0spam-blackbox.net
Tue Sep 18 16:31:02 CEST 2007
Dmitry Dulepov [typo3] schrieb:
> Hi
>
> This is SVN patch request.
>
> Branch: trunk
>
> Problem: we can save lots of web traffic if we generate less spacey
> (spicy? ;)) JavaScript. For example, just a combination of blur and
> spam-protect scripts gives 400 bytes savings for each request. Yesterday
> I had approximately 600000 visits on one site yesterday, which gives 228
> megabytes of useless traffic! Of course it will be less dramatic with
> gzip enabled but still...
>
> Solution: use JSMin library (Expat (MIT) license, compatible with GPL)
> to minify javascript. Catch errors and return unminified script if error
> happens. It is safe because JSMin was around for ages and used by many
> products nowadays. Result is cached, so no real impact on performance.
>
> There are two patches: full (with JSMin in typo3/contrib/jsmin) and
> short (with only typo3 code). You can look at short only but you need to
> apply full patch. Add "config.minifyJS=1" to TS template and see the
> effect. This works also with
If you add an optional error message parameter like this ...
function minifyJavaScript($script, &$error='') {
require_once(PATH_typo3 . 'contrib/jsmin/jsmin-1.1.0.php');
try {
$script = trim(JSMin::minify(str_replace(chr(13), '', $script)));
}
catch(JSMinException $e) {
t3lib_div::devLog('Error while minifying JavaScript: ' .
$e->getMessage(), 't3lib_div', 2,
array('JavaScript' => $script, 'Stack trace' => $e->getTrace()));
if (isset($error)) {
$error = $e->getMessage();
}
}
return $script;
}
...you can add a TS-error here:
if ($GLOBALS['TSFE']->config['config']['minifyJS']) {
$minifyError = '';
$_scriptCode = t3lib_div::minifyJavaScript($_scriptCode,$minifyError);
if ($minifyError) {
$GLOBALS['TT']->setTSlogMessage($minifyError,3);
}
}
And I suggest to rename the config option to config.compactJS or
config.minimizeJS just in case we choose a different method in the future.
AFAIK "minify" is not an English word and both "compact" and "minimize"
signify what is done.
So, nearly a +1 from me :-)
Masi
More information about the TYPO3-team-core
mailing list