andamira / medula

Starter Theme & Plugin for WordPress
MIT License
8 stars 0 forks source link

Create a system for minifying js (and optionally joining files) #14

Closed joseluis closed 10 years ago

joseluis commented 10 years ago

Implement it with a cache system

e.g. 1

<?php
$js = file_get_contents($_GET['f']);
$md = md5($js); // you can use sth faster, such as date comparsion
if (file_exists('cache/'.$md.'.js')) {
  echo file_get_contents('cache/'.$md.'.js');
} else {
  $min = yourJsMinifierFunc($js);
  file_put_contents('cache/'.$md.'.js', $min);
  echo $min;
}

e.g. 2:

require 'JShrink/Minifier.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js') {
  readfile('scripts_template.min.js');
} else {
  $output = \JShrink\Minifier::minify(file_get_contents('scripts_template.js'));
  file_put_contents('scripts_template.min.js', $output);
  echo $output;
}