@Component(value="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:
async="true"
on the resulting tags. Note, this will be ignored
when unbundled for JavaScript unless includeAsyncDeferUnbundled
is specified.
defer="true"
on JS or to add non-render-blocking CSS. Note,
this will be ignored when unbundled for JavaScript unless includeAsyncDeferUnbundled
is
specified.
bundleDependencyEventValue
Event). Lastly, the event must be dispatched to the body.
var testEvent = new CustomEvent("test"); document.body.dispatchEvent(testEvent);
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.
ResourceBundlingService
Modifier and Type | Field and Description |
---|---|
protected Map<String,String> |
deferredCssAttributes |
protected Map<String,String> |
normalCssAttributes |
protected boolean |
resourceVersioningEnabled |
bundlingService, environment, resourcesRequest
Constructor and Description |
---|
ResourceBundleProcessor() |
Modifier and Type | Method and Description |
---|---|
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
|
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
|
protected void |
addDependentBundleRestrictionToModel(String src,
boolean async,
boolean defer,
String dependencyEvent,
org.broadleafcommerce.presentation.model.BroadleafTemplateContext context,
org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
|
protected void |
addElementToModel(ResourceTagAttributes attributes,
org.broadleafcommerce.presentation.model.BroadleafTemplateContext context,
org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
Adds the bundle to the model.
|
protected void |
addElementToModel(String src,
boolean async,
boolean defer,
org.broadleafcommerce.presentation.model.BroadleafTemplateContext context,
org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
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.
|
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
|
protected org.broadleafcommerce.presentation.model.BroadleafTemplateModel |
buildModelBundled(List<String> attributeFiles,
ResourceTagAttributes attributes,
org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
Builds the model that contains the bundled resources the tag should be replaced with
|
protected org.broadleafcommerce.presentation.model.BroadleafTemplateModel |
buildModelUnbundled(List<String> attributeFiles,
ResourceTagAttributes attributes,
org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
Builds the model that contains the unbundled resources the tag should be replaced with
|
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
|
protected String |
cleanUpJavaScriptName(String original)
Cleans up the name that will be used in the dependency handling JavaScript
|
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
|
protected Map<String,String> |
getLinkAttributes(String src)
Deprecated.
Use
getNormalCssAttributes(ResourceTagAttributes) instead |
String |
getName() |
protected Map<String,String> |
getNormalCssAttributes(ResourceTagAttributes tagAttributes)
Builds a map of normal (non-deferred) attributes to put on a CSS <link> tag
|
int |
getPrecedence() |
protected Map<String,String> |
getScriptAttributes(ResourceTagAttributes tagAttributes)
Gets the attributes to put on the <script> tag
|
protected Map<String,String> |
getScriptAttributes(String src,
boolean async,
boolean defer)
Deprecated.
Use
getScriptAttributes(ResourceTagAttributes) instead |
protected boolean |
useAsyncJavaScript(ResourceTagAttributes attributes)
Tells if the JavaScript added to the page should be asynchronous
|
protected void |
validateTagAttributes(ResourceTagAttributes attributes)
Validates the requested tag attributes
|
buildBundledFilesList, buildResourceTagAttributes, getBundleAppendText, getBundleCompleteEventJavaScript, getBundleEnabled, getBundlePath, getBundleUrl, getFullUnbundledFileName, getReplacementModel, getRequestedFileNames, postProcessUnbundledFileList
@Value(value="${resource.versioning.enabled:true}") protected boolean resourceVersioningEnabled
public String getName()
public int getPrecedence()
getPrecedence
in interface org.broadleafcommerce.presentation.dialect.BroadleafProcessor
getPrecedence
in class org.broadleafcommerce.presentation.dialect.AbstractBroadleafTagReplacementProcessor
protected org.broadleafcommerce.presentation.model.BroadleafTemplateModel buildModelUnbundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
AbstractResourceProcessor
buildModelUnbundled
in class AbstractResourceProcessor
attributeFiles
- list of files that are to be includedattributes
- the attributes of the original tag this processor replacescontext
- the context of the original tagprotected org.broadleafcommerce.presentation.model.BroadleafTemplateModel buildModelBundled(List<String> attributeFiles, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
AbstractResourceProcessor
buildModelBundled
in class AbstractResourceProcessor
attributeFiles
- list of files that are to be bundledattributes
- the attributes of the original tag this processor replacescontext
- the context of the original tag@Deprecated protected void addElementToModel(String src, boolean async, boolean defer, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
addElementToModel(ResourceTagAttributes, BroadleafTemplateContext, BroadleafTemplateModel)
instead@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(ResourceTagAttributes, BroadleafTemplateContext, BroadleafTemplateModel)
insteadprotected void addElementToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
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 toprotected void addJavaScriptToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
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 toprotected void addCssToModel(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
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 toprotected List<org.broadleafcommerce.presentation.model.BroadleafTemplateElement> getDeferredCssElements(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
attributes
- the attributes of the original resource tag and the src of the CSS to includecontext
- the context of the original resource tag@Deprecated protected void addDependentBundleRestrictionToModel(String src, boolean async, boolean defer, String dependencyEvent, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
protected void addDependencyRestrictionToModel(List<String> files, ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context, org.broadleafcommerce.presentation.model.BroadleafTemplateModel model)
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 toprotected String cleanUpJavaScriptName(String original)
original
- the original name@Deprecated protected Map<String,String> getScriptAttributes(String src, boolean async, boolean defer)
getScriptAttributes(ResourceTagAttributes)
insteadprotected Map<String,String> getScriptAttributes(ResourceTagAttributes tagAttributes)
tagAttributes
- the attributes of the original bundle tag and src of this script@Deprecated protected Map<String,String> getLinkAttributes(String src)
getNormalCssAttributes(ResourceTagAttributes)
insteadprotected Map<String,String> getNormalCssAttributes(ResourceTagAttributes tagAttributes)
tagAttributes
- the attributes on the original bundle tagprotected boolean useAsyncJavaScript(ResourceTagAttributes attributes)
attributes
- the attributes on the original bundle tagprotected void validateTagAttributes(ResourceTagAttributes attributes)
AbstractResourceProcessor
validateTagAttributes
in class AbstractResourceProcessor
attributes
- the tag attributes from the original resource tagprotected org.broadleafcommerce.presentation.model.BroadleafTemplateElement buildUnbundledSyncCompletedEventElement(ResourceTagAttributes attributes, org.broadleafcommerce.presentation.model.BroadleafTemplateContext context)
attributes
- the attributes of the bundle tagcontext
- the context of the bundle tagCopyright © 2022. All rights reserved.