Class ResourceBundleProcessor

java.lang.Object
org.broadleafcommerce.presentation.dialect.AbstractBroadleafTagReplacementProcessor
org.broadleafcommerce.common.web.processor.AbstractResourceProcessor
org.broadleafcommerce.common.web.processor.ResourceBundleProcessor
All Implemented Interfaces:
org.broadleafcommerce.presentation.dialect.BroadleafProcessor, org.broadleafcommerce.presentation.dialect.BroadleafTagReplacementProcessor

@Component("blResourceBundleProcessor") @ConditionalOnTemplating public class ResourceBundleProcessor extends AbstractResourceProcessor

Works with the blc:bundle tag.

This processor does not do the actual bundling. It merely changes the URL which causes the other bundling components to be invoked through the normal static resource handling processes.

This processor relies bundle.enabled. If this property is false (typical for dev) then the list of resources will be output as individual SCRIPT or LINK elements for each JavaScript or CSS file respectively.

To use this processor, supply a name, mapping prefix, and list of files.

 
 <blc:bundle name="lib.js"
             mapping-prefix="/js/"
             files="plugins.js,
                    libs/jquery.MetaData.js,
                    libs/jquery.rating.pack.js,
                    libs/jquery.dotdotdot-1.5.1.js" />
  
 

With bundling enabled this will turn into:


 
  <script type="text/javascript" src="/js/lib-blbundle12345.js" />
 
 

Where the -blbundle12345 is used by the BundleUrlResourceResolver to determine the actual bundle name.

With bundling disabled this turns into:

 
  <script type="text/javascript" src="/js/plugins.js" />
  <script type="text/javascript" src="/js/jquery.MetaData.js" />
  <script type="text/javascript" src="/js/jquery.rating.pack.js.js" />
  <script type="text/javascript" src="/js/jquery.dotdotdot-1.5.1.js" />
 
 

This processor also supports producing the 'async' and 'defer' attributes for Javascript files. For instance:

 
 <blc:bundle name="lib.js"
             async="true"
             defer="true"
             mapping-prefix="/js/"
             files="plugins.js,
                    libs/jquery.MetaData.js,
                    libs/jquery.rating.pack.js,
                    libs/jquery.dotdotdot-1.5.1.js" />
  
 

If bundling is turned on, the single output file contains the 'async' and 'defer' name-only attributes. When bundling is turned off, then those name-only attributes are applied to each individual file reference.

This processor only supports files that end in .js and .css

Tag attributes:

  • name - (required) the final name prefix of the bundle
  • mapping-prefix - (required) the prefix appended to the final tag output whether that be
  • files - (required) a comma-separated list of files that should be bundled together. May be excluded in some cases. See note at bottom.
  • async - (optional) true to set async="true" on the resulting tags. Note, this will be ignored when unbundled for JavaScript unless includeAsyncDeferUnbundled is specified.
  • defer - (optional) true to set defer="true" on JS or to add non-render-blocking CSS. Note, this will be ignored when unbundled for JavaScript unless includeAsyncDeferUnbundled is specified.
  • includeAsyncDeferUnbundled - (optional) true to include async and defer (if enabled) on the unbundled replacement tags. They are not included by default when unbundled due to race conditions between JavaScript dependencies.
  • bundle-dependency-event - (optional) name of a JavaScript event to wait for before adding this bundle to the DOM. The JavaScript event must be named the value of this attribute. The event must also be added as a global variable of the value of this attribute with "Event" on the end (bundleDependencyEventValueEvent). Lastly, the event must be dispatched to the body.
    For example, if the value of this attribute was "test",
    var testEvent = new CustomEvent("test"); document.body.dispatchEvent(testEvent);
  • bundle-completed-event - (optional) name of JS event to fire when this bundle has completed. See bundle-dependency-event for specifics. Use this together with bundle-dependency-event to chain load dependencies. Note that if using this along with a <blc:bundlepreload>, both the <blc:bundle> and <blc:bundlepreload> tags need to have this event, otherwise the bundlepreload may generate the bundle without the JS that fires the event.

This processor has the ability to retrieve a bundle that has already been requested earlier in the template looking it up with the bundle name. See ResourcesRequest for more information. This helps with not having to duplicate the bundle information across the <blc:bundlepreload> and <blc:bundle> tags.

Author:
apazzolini, bpolster, Jacob Mitash (jmitash)
See Also:
  • Field Details

    • deferredCssAttributes

      protected final Map<String,String> deferredCssAttributes
    • normalCssAttributes

      protected final Map<String,String> normalCssAttributes
    • resourceVersioningEnabled

      @Value("${resource.versioning.enabled:true}") protected boolean resourceVersioningEnabled
  • Constructor Details

    • ResourceBundleProcessor

      public ResourceBundleProcessor()
  • Method Details

    • getName

      public String getName()
    • getPrecedence

      public int getPrecedence()
      Specified by:
      getPrecedence in interface org.broadleafcommerce.presentation.dialect.BroadleafProcessor
      Overrides:
      getPrecedence in class org.broadleafcommerce.presentation.dialect.AbstractBroadleafTagReplacementProcessor
    • buildModelUnbundled

      protected org.broadleafcommerce.presentation.model.BroadleafTemplateModel buildModelUnbundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
      Description copied from class: AbstractResourceProcessor
      Builds the model that contains the unbundled resources the tag should be replaced with
      Specified by:
      buildModelUnbundled in class AbstractResourceProcessor
      Parameters:
      attributeFiles - list of files that are to be included
      attributes - the attributes of the original tag this processor replaces
      context - the context of the original tag
      Returns:
      model containing resources the tag should be replaced with
    • buildModelBundled

      protected org.broadleafcommerce.presentation.model.BroadleafTemplateModel buildModelBundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
      Description copied from class: AbstractResourceProcessor
      Builds the model that contains the bundled resources the tag should be replaced with
      Specified by:
      buildModelBundled in class AbstractResourceProcessor
      Parameters:
      attributeFiles - list of files that are to be bundled
      attributes - the attributes of the original tag this processor replaces
      context - the context of the original tag
      Returns:
      model containing resources the tag should be replaced with
    • addElementToModel

      @Deprecated protected void addElementToModel(String src, boolean async, boolean defer, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
    • addElementToModel

      @Deprecated protected void addElementToModel(String src, boolean async, boolean defer, String dependencyEvent, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
    • addElementToModel

      protected void addElementToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
      Adds the bundle to the model.
      Parameters:
      attributes - the original bundle tag attributes and the src of the bundle to add
      context - the context of the original bundle tag
      model - the model to add the script to
    • addJavaScriptToModel

      protected void addJavaScriptToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
      Adds JavaScript to the model in a <script> tag
      Parameters:
      attributes - the original bundle tag attributes and the src of the JavaScript to add
      context - the context of the original bundle tag
      model - the model to add the script to
    • addCssToModel

      protected void addCssToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
      Adds the CSS to the model in a <link> tag
      Parameters:
      attributes - the original bundle tag attributes and the src of the CSS to add
      context - the context of the original bundle tag
      model - the model to add the link to
    • getDeferredCssElements

      protected List<org.broadleafcommerce.presentation.model.BroadleafTemplateElement> getDeferredCssElements(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
      Gets a list of elements to add to the model for deferred CSS
      Parameters:
      attributes - the attributes of the original resource tag and the src of the CSS to include
      context - the context of the original resource tag
      Returns:
      list of elements needed for deferred CSS
    • addDependentBundleRestrictionToModel

      @Deprecated protected void addDependentBundleRestrictionToModel(String src, boolean async, boolean defer, String dependencyEvent, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
    • addDependencyRestrictionToModel

      protected void addDependencyRestrictionToModel(List<String> files, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
      Adds JavaScript to the model that will insert a given script tag when the dependency event is fired
      Parameters:
      attributes - the original bundle tag attributes and src of the script to add
      context - the context of the original bundle tag
      model - the model to add the script to
    • cleanUpJavaScriptName

      protected String cleanUpJavaScriptName(String original)
      Cleans up the name that will be used in the dependency handling JavaScript
      Parameters:
      original - the original name
      Returns:
      clean name
    • getScriptAttributes

      @Deprecated protected Map<String,String> getScriptAttributes(String src, boolean async, boolean defer)
    • getScriptAttributes

      protected Map<String,String> getScriptAttributes(ResourceTagAttributes tagAttributes)
      Gets the attributes to put on the <script> tag
      Parameters:
      tagAttributes - the attributes of the original bundle tag and src of this script
      Returns:
      attributes to put on the replacement script tag
    • getLinkAttributes

      @Deprecated protected Map<String,String> getLinkAttributes(String src)
    • getNormalCssAttributes

      protected Map<String,String> getNormalCssAttributes(ResourceTagAttributes tagAttributes)
      Builds a map of normal (non-deferred) attributes to put on a CSS <link> tag
      Parameters:
      tagAttributes - the attributes on the original bundle tag
      Returns:
      map of attributes to put on the link tag
    • useAsyncJavaScript

      protected boolean useAsyncJavaScript(ResourceTagAttributes attributes)
      Tells if the JavaScript added to the page should be asynchronous
      Parameters:
      attributes - the attributes on the original bundle tag
      Returns:
      true if the JavaScript should be async and false otherwise
    • validateTagAttributes

      protected void validateTagAttributes(ResourceTagAttributes attributes)
      Description copied from class: AbstractResourceProcessor
      Validates the requested tag attributes
      Overrides:
      validateTagAttributes in class AbstractResourceProcessor
      Parameters:
      attributes - the tag attributes from the original resource tag
    • buildUnbundledSyncCompletedEventElement

      protected org.broadleafcommerce.presentation.model.BroadleafTemplateElement buildUnbundledSyncCompletedEventElement(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
      Builds a script element that fires the bundle complete event only when supported
      Parameters:
      attributes - the attributes of the bundle tag
      context - the context of the bundle tag
      Returns:
      the script element or null if not supported