Class ResourceBundleProcessor
- All Implemented Interfaces:
org.broadleafcommerce.presentation.dialect.BroadleafProcessor,org.broadleafcommerce.presentation.dialect.BroadleafTagReplacementProcessor
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 unlessincludeAsyncDeferUnbundledis 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 unlessincludeAsyncDeferUnbundledis 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
bundlepreloadmay 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 Summary
FieldsModifier and TypeFieldDescriptionprotected booleanFields inherited from class org.broadleafcommerce.common.web.processor.AbstractResourceProcessor
bundlingService, environment, resourcesRequestFields inherited from interface org.broadleafcommerce.presentation.dialect.BroadleafProcessor
DEFAULT_PRECEDENCE, DEFAULT_PREFIX -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddCssToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Adds the CSS to the model in a <link> tagprotected voidaddDependencyRestrictionToModel(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 firedprotected voidaddDependentBundleRestrictionToModel(String src, boolean async, boolean defer, String dependencyEvent, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Deprecated.protected voidaddElementToModel(String src, boolean async, boolean defer, String dependencyEvent, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Deprecated.protected voidaddElementToModel(String src, boolean async, boolean defer, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Deprecated.protected voidaddElementToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Adds the bundle to the model.protected voidaddJavaScriptToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model) Adds JavaScript to the model in a <script> tagprotected org.broadleafcommerce.presentation.model.BroadleafTemplateModelbuildModelBundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context) Builds the model that contains the bundled resources the tag should be replaced withprotected org.broadleafcommerce.presentation.model.BroadleafTemplateModelbuildModelUnbundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context) Builds the model that contains the unbundled resources the tag should be replaced withprotected org.broadleafcommerce.presentation.model.BroadleafTemplateElementbuildUnbundledSyncCompletedEventElement(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context) Builds a script element that fires the bundle complete event only when supportedprotected StringcleanUpJavaScriptName(String original) Cleans up the name that will be used in the dependency handling JavaScriptprotected 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 CSSgetLinkAttributes(String src) Deprecated.UsegetNormalCssAttributes(ResourceTagAttributes)insteadgetName()getNormalCssAttributes(ResourceTagAttributes tagAttributes) Builds a map of normal (non-deferred) attributes to put on a CSS <link> tagintgetScriptAttributes(String src, boolean async, boolean defer) Deprecated.UsegetScriptAttributes(ResourceTagAttributes)insteadgetScriptAttributes(ResourceTagAttributes tagAttributes) Gets the attributes to put on the <script> tagprotected booleanuseAsyncJavaScript(ResourceTagAttributes attributes) Tells if the JavaScript added to the page should be asynchronousprotected voidvalidateTagAttributes(ResourceTagAttributes attributes) Validates the requested tag attributesMethods inherited from class org.broadleafcommerce.common.web.processor.AbstractResourceProcessor
buildBundledFilesList, buildResourceTagAttributes, getBundleAppendText, getBundleCompleteEventJavaScript, getBundleEnabled, getBundlePath, getBundleUrl, getFullUnbundledFileName, getReplacementModel, getRequestedFileNames, postProcessUnbundledFileListMethods inherited from class org.broadleafcommerce.presentation.dialect.AbstractBroadleafTagReplacementProcessor
getPrefix, replacementNeedsProcessing
-
Field Details
-
deferredCssAttributes
-
normalCssAttributes
-
resourceVersioningEnabled
@Value("${resource.versioning.enabled:true}") protected boolean resourceVersioningEnabled
-
-
Constructor Details
-
ResourceBundleProcessor
public ResourceBundleProcessor()
-
-
Method Details
-
getName
-
getPrecedence
public int getPrecedence()- Specified by:
getPrecedencein interfaceorg.broadleafcommerce.presentation.dialect.BroadleafProcessor- Overrides:
getPrecedencein classorg.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:AbstractResourceProcessorBuilds the model that contains the unbundled resources the tag should be replaced with- Specified by:
buildModelUnbundledin classAbstractResourceProcessor- Parameters:
attributeFiles- list of files that are to be includedattributes- the attributes of the original tag this processor replacescontext- 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:AbstractResourceProcessorBuilds the model that contains the bundled resources the tag should be replaced with- Specified by:
buildModelBundledin classAbstractResourceProcessor- Parameters:
attributeFiles- list of files that are to be bundledattributes- the attributes of the original tag this processor replacescontext- 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) Deprecated. -
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) Deprecated. -
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 addcontext- the context of the original bundle tagmodel- 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 addcontext- the context of the original bundle tagmodel- 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 addcontext- the context of the original bundle tagmodel- 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 includecontext- 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 addcontext- the context of the original bundle tagmodel- the model to add the script to
-
cleanUpJavaScriptName
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) Deprecated.UsegetScriptAttributes(ResourceTagAttributes)instead -
getScriptAttributes
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.UsegetNormalCssAttributes(ResourceTagAttributes)instead -
getNormalCssAttributes
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
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
Description copied from class:AbstractResourceProcessorValidates the requested tag attributes- Overrides:
validateTagAttributesin classAbstractResourceProcessor- 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 tagcontext- the context of the bundle tag- Returns:
- the script element or null if not supported
-
addDependencyRestrictionToModel(List, ResourceTagAttributes, BroadleafTemplateContext, BroadleafTemplateModel)instead