[wp-trac] [WordPress Trac] #3372: Consolidated JavaScript/CSS Plugin API

WordPress Trac wp-trac at lists.automattic.com
Sat Nov 14 08:59:15 UTC 2009


#3372: Consolidated JavaScript/CSS Plugin API
-----------------------------+----------------------------------------------
 Reporter:  robertaccettura  |        Owner:  anonymous     
     Type:  feature request  |       Status:  reopened      
 Priority:  lowest           |    Milestone:  Future Release
Component:  JavaScript       |      Version:  2.1           
 Severity:  normal           |   Resolution:                
 Keywords:  needs-patch      |  
-----------------------------+----------------------------------------------
Changes (by westonruter):

 * cc: westonruter (added)
  * severity:  trivial => normal


Comment:

 I just submitted a plugin to the directory called “Optimize Scripts” which
 accomplishes most of what is discussed in this ticket, namely:

  1. Concatenates enqueued scripts together.
  2. Respects head and footer groups.
  3. Minifies code using Google's Closure Compiler.
  4. Caches the concatenated/minified code and rebuilds it only when one of
 its source scripts expires or changes.
  5. Filename for concatenated/minified script is the md5 of all the
 handles concatenated together (thus if an additional handle is provided, a
 new concatenated script is generated).
  6. Provides a filter to limit which scripts get concatenated (i.e. jQuery
 on ajax.googleapis.com should be left alone); by default, all scripts on
 local host are concatenated, and remote scripts are left alone.
  7. Removes the default 'ver' query parameter which WordPress adds to
 every script src if no version argument is supplied on
 wp_enqueue/register_script: this is important for Web-wide caching of
 scripts loaded from ajax.googleapis.com, for example. When registering new
 scripts, pass the filemtime in as the version so that whenever a file
 changes, the concatenated script will be regenerated.

 And there are more options. The current alpha-version of the plugin can be
 found at: http://gist.github.com/233399

 Once the plugin is approved for the directory, it'll be found at:
 http://wordpress.org/extend/plugins/optimize-scripts/

 For example, consider the following code:

 {{{
 wp_deregister_script('jquery'); //remove locally jQuery in favor of
 Google's
 //since no verion provided, no ver query param will be added to script at src
 wp_enqueue_script('jquery',
 'http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js');
 wp_enqueue_script(
    'myscript1',
    get_template_directory_uri().'/myscript1.js',
    array('jquery'),
    filemtime(TEMPLATEPATH).'/myscript1.js',
    true //in_footer
 );
 …
 wp_enqueue_script(
    'myscriptN',
    get_template_directory_uri().'/myscriptN.js',
    array('jquery'),
    filemtime(TEMPLATEPATH).'/myscriptN.js',
    true //in_footer
 );
 }}}

 Without this plugin, the previous code would be generated by WordPress as:

 {{{
 <script
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js?ver=2.8.5"></script>
 <script src="http://example.com/wp-
 content/themes/foo/script1.js?ver=45678901"></script>
 …
 <script src="http://example.com/wp-
 content/themes/foo/scriptN.js?ver=12345678"></script>
 }}}

 But with this plugin enabled, the output would be as follows:

 {{{
 <script
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
 <script src="http://example.com/wp-
 content/js/9e107d9d372bb6826bd81d3542a419d6.js?ver=7659098223"></script>
 }}}

 I'd love to get your feedback, suggestions, and contributions to make the
 code better so that we can boost WordPress frontend performance.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/3372#comment:29>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list