Jeff-Lewis / minify

Automatically exported from code.google.com/p/minify
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

PHP pre-processing in JS files #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It's not weird to see PHP used inside js files, for global vars (defined 
in PHP), translations strings, and so on. 

Right now minify only reads the file as lines, append them, minify them 
and output them. For the PHP embedded in JS files we need to preprocess 
the JS file as a PHP file, before we minify and all the stuff.

For this, in the function "combine($minify = true)":

    foreach($this->files as $file) {
      if ($this->type === self::TYPE_CSS && MINIFY_REWRITE_CSS_URLS) {
        // Rewrite relative CSS URLs.
        $combined[] = self::rewriteCSSUrls(file_get_contents($file),
            dirname($file));
      } else if ($this->type === self::TYPE_JS) {
        ob_start();                    // Start output buffering
        include($file);                // Include the file
        $contents = ob_get_contents(); // Get the contents of the buffer
        ob_end_clean();                // End buffering and discard
        $combined[] = $contents;
      } else {
        $combined[] = file_get_contents($file);
      }
    }

Cheers,
Victor
http://beer2beer.com

Original issue reported on code.google.com by espiga...@gmail.com on 22 Jul 2007 at 11:37

GoogleCodeExporter commented 9 years ago
If you're preprocessing your JS files with PHP, then the best solution would be 
to
turn on output buffering at the top of the file, then use JSMin to minify the
contents of the processed buffer and send that to the browser. I don't plan to 
add
PHP preprocessing functionality to Minify.

Original comment by rgr...@gmail.com on 28 Aug 2007 at 5:21

GoogleCodeExporter commented 9 years ago
This can be done in 2.x verions by using a custom Minify_Source object instead 
of a 
file path, e.g. in /min/groupsConfig.php. For performance you'd need to provide 
a 
last modified time (or emulate one).

Ask the google group how to get this working.

Original comment by mrclay....@gmail.com on 27 Nov 2008 at 3:23