cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Asset Manager - minify

Open niden opened this issue 13 years ago • 29 comments

Add the ability to minify resources using the Asset Manager

niden avatar Apr 16 '13 14:04 niden

And join in one file (by groups maybe)...

lantian avatar Apr 16 '13 20:04 lantian

My minifier has the following API:

$minifier->addScript($url, $bMinify = true);

That way if you do the following:

$minifier->addScript('myJS.js')->addScript('myOtherJs.js', false)->addScript('myThirdJs.js')->addScript('AnotherJs.js');

and then do

$minifier->renderScripts();

you get this:

<script src="89432ut983h493q9v98.js"></script>
<script src="myOtherJs.js"></script>
<script src="943u7890g3h983qn54.js"></script>

Because this was the order in which they were defined, and because the second one had a minify flag set to false (for example, CKeditor cannot be minified - it breaks - at least it still did back when I was using it a couple years back). The last two are again merged and minified into one file.

I suggest this approach as it's very practical.

Swader avatar Apr 17 '13 01:04 Swader

+1

touhonoob avatar May 09 '13 05:05 touhonoob

+1

DragosMocrii avatar May 09 '13 08:05 DragosMocrii

+1

philippgerard avatar May 09 '13 16:05 philippgerard

+1

So Asset Manager would create these minified resources transparently? Meaning the output file is a hash generated from the files included. Phalcon would then be smart enough to know when to recreate the asset files.

A flag to turn this on and off would be great for development and debugging these compressed assets. Support for CSS would also be a feature I assume.

nesbert avatar May 09 '13 19:05 nesbert

@nesbert Yes, CSS too. Phalcon could natively figure out when to regenerate the hashfiles (according to last_modified or whichever way is best) and would then rebuild and potentially delete the old files.

This might be wishful thinking and somewhat outside the scope of this NFR, but instead of (or in addition to?) having an on/off switch, perhaps add source maps support. This would let us use minified resources in development as well, without having to resort to outside tools for this type of work (http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/)

Swader avatar May 09 '13 19:05 Swader

+1

hlasso avatar May 09 '13 21:05 hlasso

:+1: I'd like Asset Manager to return normal resource name and support versions as well. Doable? ;)

roman-kulish avatar May 10 '13 01:05 roman-kulish

+1

ghost avatar May 13 '13 05:05 ghost

:+1:

iforp avatar May 13 '13 08:05 iforp

+1

hugoduraes avatar May 13 '13 09:05 hugoduraes

+1 Minify and join Deeply needed this feature. And it would be nice if we can define script dependency like this

    shim: {
        "backbone": {
            "deps": [ "underscore", "jquery" ],
        },
        "bbqparams" : ["backbone"],
        "bootstrap":  ['jquery'],
        "jqvalidate" : ['jquery'],
        "jqvalidate_id" : ['jqvalidate'],
        "jqdeparam" : ['jquery'],
        "flexslider" : ['jquery'],
        "dtp" : ['jquery'],
        "dtp-id" : ['dtp'],
        "jquery.ui.widget" :['jquery'],
        "jqfupload" :  ["jquery.ui.widget", "jqiframe-transport"],
        'jcrop' : ['jquery']
    } // end Shim Configuration

jeffreycahyono avatar May 14 '13 13:05 jeffreycahyono

+1

tasmaniski avatar May 21 '13 23:05 tasmaniski

+1

altapo avatar May 22 '13 11:05 altapo

We have already integrated JSMin (in pure c) into Phalcon (1.2.0), this filter is very fast, it returns the filtered javascript as a string that we can easily concatenate with other filtered/unfiltered scripts.

To make the component extensible we plan to support a common interface to integrate filters like: YUI, Closure, Sass, Dart, Coffee Script, TypeScript (insert your favorite minifier/compiler), etc.

However most of these tools require execute binaries such as java and have write permissions to the file system. In some cases, if there is more than one filter, the generated files need to be read again by Phalcon to produce the final compilation.

So, our plan for this is:

  • Keep Jsmin (in C) as part of the phalcon's core
  • Implement an interface to use external minification/compression tools (some of them can be added to the Incubator)

The suggested syntax is:


//Get some Javascript collection
$js = $this->assets->get('footer');

//Add/Enable the JsMin filter in the collection
$js->addFilter(new Phalcon\Assets\Filters\Jsmin());

//Add a remote resource / no need to be minified again (second false)
$js->addJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, false);

//Local resource (must be minified)
$js->addJs('public/js/my-script-1.js');

//Local resource already minified
$js->addJs('public/js/my-script-2.js', true, false);

//This will open every remote/local resource added to the collection
//passing every filter to every file (if applicable)
//generate a unique file with all the files joined
//print a <script src="/to/compiled-file.js"></script>
$js->output();

Outstanding issues

  • If the user does not have write permissions, output the file(s) inlined into the HTML: ?
  • Partially inline some of the resources added, write the remaining to a file ?
  • Generate the file name with automatic/manual versioning #639
  • Implement a "not filter anymore" feature when the application goes to a production stage

Thoughts?

ghost avatar May 28 '13 16:05 ghost

Good job! But I thinks that 'storeMinifiedFileTo' (or other name, irrelevant) method is required or maybe production/development switcher for disabling/enabling the filter

lantian avatar May 28 '13 16:05 lantian

@phalcon this applies to CSS too, I assume? As for the API, sounds good to me.

About those outstanding issues:

  1. I don't think that's necessary. Using this would hurt performance significantly, one would be better off uploading it manually to a custom server somewhere and including it from such a CDN.
  2. Which scenarios does this apply to?
  3. I don't think this is necessary - minified files are meant to be discarded, but if it were an option and if there was a flag we could set as to how many versions to keep and give Phalcon the naming pattern, this would be interesting. I think it's a bit of an overkill though.
  4. Sounds good.

How about automatic re-minification? Have you considered adding the auto-read from last_modified to automatically rebuild the resources on every request if necessary? Also, source maps - yay or nay?

Swader avatar May 28 '13 17:05 Swader

this applies to CSS too, I assume?

CSS doesn't need complex minify, only space removing, backslashes, etc =) (aka filter)

  1. I don't think that's necessary. Using this would hurt performance significantly, one would be better off uploading it manually to a custom server somewhere and including it from such a CDN.

Agree and CDN is important thing

Also output can be cached...

lantian avatar May 28 '13 17:05 lantian

For CSS, the same syntax could be used, but first I would like to discuss the external filters too.

This is a possible example of how a filter for YUI/CSS compressor can be implemented:

<?php

use Phalcon\Assets\FilterInterface;

class CssYUICompressor implements FilterInterface
{

    protected $_options;

    public function __construct($options)
    {
        $this->_options = $options;
    }

    public function filter($contents)
    {       

        file_put_contents('temp/my-temp-1.css', $contents);

        system(
            $this->_options['java-bin'] . 
            ' -jar ' . 
            $this->_options['yui'] .
            ' --type css '.
            'temp/my-temp-file-1.css ' .
            $this->_options['extra-options'] .
            ' -o temp/my-temp-file-2.css'
        );

        return file_get_contents("temp/my-temp-file-2.css");
    }
}

Usage:

//Get some CSS collection
$css = $this->assets->get('footer');

//Add/Enable the YUI compressor filter in the collection
$css->addFilter(new CssYUICompressor(array(
     'java-bin' => '/usr/local/bin/java',
     'yui' => '/some/path/yuicompressor-x.y.z.jar',
     'extra-options' => '--charset utf8'
)));

While Phalcon does respect the openbase_dir directive allowing the extension to be executed in shared hostings or secured/restricted environments (http://php.net/manual/en/ini.core.php#ini.open-basedir).

Also functions that aren't allowed via php.ini are also restricted in Phalcon but I think that include calls to "system" or similar would increase the possibility to be black-listed by anti-viruses that implement heuristic analysis of viruses. So, Initially, I would not be in favor of including things like these in the core of the framework.

I think that filters that require execute arbitrary binaries must be placed in the Incubator.

ghost avatar May 28 '13 17:05 ghost