swCombinePlugin - 1.3.0

Combine stylesheets and javascripts files, with no computation at runtime.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

swCombinePlugin

This plugin combines javascript and stylesheet files into optimized files.

How it works ?

The plugin read all view.yml files and generates the correct combined files :

  • stylesheets
  • javascript

The plugin can also generate packages from a list of files. This feature allows you to create a common set of files for your website, and include specific assets for a view.

Benefits

  • Combine files and optimizations are performed when the application is deployed
  • The view cache file generates the correct path to the combined files, so there is not computation at runtime.

Available optimizers

  • Global

    • swPassDriver : do nothing, use this if you want to combine your files with no optizations
  • Javascript

    • swDriverGoogleClosureCompilerApi : use google closure algorithm
    • swDriverJSMin : use JSMin algorithm
    • swDriverJSMinPlus : user JSMin+ algorithm
  • Stylesheet

    • swDriverCssmin : use Css min algorithm
    • swDriverMinifyCssCompressor : use Minify Css Compressor algorithm

Install

  • edit ProjectConfiguration.class.php by adding the requirement and enable the plugin

    require_once(dirname(__FILE__).'/../plugins/swCombinePlugin/lib/config/swCombineViewConfigHandler.class.php');
     
    class ProjectConfiguration extends ysfProjectConfiguration
    {
      public function setup()
      {
     
         $this->enablePlugins(array(
            // your plugins
            'swCombinePlugin'
         ));
       }
    }
  • edit your app.yml file and add an optional version value

    swToolbox:
      swCombine:
        # the version number can be used to force load of new files from the media server
        #   you can configure a never ending expired time on your media server
        #   if the version is never updated, media will be never reloaded ...
        version: 3
  • add a config_handlers.yml inside APP/config

    modules/*/config/view.yml:
      # put sfViewConfigHandler to restore the default symfony config handler
      # class: sfViewConfigHandler
      class: swCombineViewConfigHandler 
      param:
       private_path: %SF_WEB_DIR%/sw-combine
       public_path:  /sw-combine
       configuration:
         javascript:
           combine:  swCombineJavascript
           driver:   swDriverJSMinPlus
           filename: %s.js                         # %s will by replace by the combined name
           exclude:
             - /js/jQuery/jquery.1.2.6.js
             - /js/jQuery/ui-1.5.3/jquery.ui.all.js
             - /js/jQuery/offset.js
             - /js/jQuery/jquery.form-defaults.js
             - /js/jQuery/jquery.timers-1.2.js
             - /js/jqModal/jqModal.js
             - /js/scrollTo/jquery.scrollTo-min.js
             - /js/main.js
           packages:
             common:
               auto_include: true
               files:
                 - /js/jQuery/jquery.1.2.6.js
                 - /js/jQuery/ui-1.5.3/jquery.ui.all.js
                 - /js/jQuery/offset.js
                 - /js/jQuery/jquery.form-defaults.js
                 - /js/jQuery/jquery.timers-1.2.js
                 - /js/jqModal/jqModal.js
                 - /js/scrollTo/jquery.scrollTo-min.js
                 - /js/main.js
     
     
         stylesheet:
           combine:  swCombineStylesheet
           driver:   swDriverCssmin
           filename: %s.css                        # %s will by replace by the combined name
           exclude:
             - /css/main.css
             - /css/draft/ui-theme.css
             - /css/mg-theme.css
             - /css/print.css
           packages:
             common:
               auto_include: true
               files:
                 - /css/main.css
                 - /css/draft/ui-theme.css
                 - /css/mg-theme.css
  • clear your cache

    ./symfony cc
    
  • combine css and js with

    ./symfony sw:combine frontend
    ./symfony sw:combine backend
    

Assets version (optional)

This plugin has a hidden gem : asset versioning. When css files are combined, a version number is added to all externals references. This feature must be use with helper functions sw_include_stylesheets() and sw_include_javascripts(), this two helpers add the version number on each declared assets.

The asset version format is : ASSET_FILE?_sw=ASSET_VERSION

What does it mean ?

You can configure a long time expired value on your server by using simple regular expression.

(.*)\?_sw=(.*) => expired in 1 month.

Doing so, the user agent will not do any extras requests to the webserver.

Defining the asset version

  • edit your app.yml file by adding these lines

    swToolbox:
      swCombine:
        version: 42
  • clear your cache

     ./symfony cc
    
  • regenerate the combined css and js files with

     ./symfony sw:combine frontend
     ./symfony sw:combine backend