Compress javascript at runtime

75 views
Skip to first unread message

stefano...@gmail.com

unread,
Nov 25, 2014, 10:21:39 AM11/25/14
to wr...@googlegroups.com
Hi guys,

  I'm a newbie with wro4j and I've been trying to integrate this library in one of my projects. 

I was especially interested in compressing javascript code at runtime.
I'm currently facing a couple of problems:

  • I would like to configure wro4j with Spring using ConfigurableWroFilter  as stated in the manual, but i didn't undestand how to configure the WroManagerFactory and specify the pre/postprocessors. I tried something like this but of course the factory initialization doesn't work:
<bean id="wroFilter" class="ro.isdc.wro.http.ConfigurableWroFilter">
   
<property name="debug" value="${wrofilter.debug}"/>
   
<property name="cacheUpdatePeriod" value="${wrofilter.cacheUpdatePeriod}"/>
   
<property name="modelUpdatePeriod" value="${wrofilter.modelUpdatePeriod}"/>
   
<property name="disableCache" value="${wrofilter.disableCache}"/>
   
<property name="encoding" value="${wrofilter.encoding}"/>
   
<property name="gzipEnabled" value="${wrofilter.gzipEnabled}"/>
   
<property name="wroManagerFactory" ref="wroManagerFactory"/>
 
</bean>
 
 
<bean id="wroManagerFactory" class="ro.isdc.wro.manager.factory.DefaultWroManagerFactory">
   
<property name="preProcessors" value="${wrofilter.preprocessors}"/>
   
<property name="postProcessors" value="${wrofilter.postprocessors}"/>
 
</bean>

Can you please provide an example about how to specify the processors with Spring? 


  • My biggest dubt at the moment is that a couple of javascript files I'm using are created dynamically in JSPs. I managed to separate completely the javascript from the other content so we can say that one jsp corresponds to one javascript source. 
For example I have a controller method like the following:
 
@RequestMapping(value="/script.js", method = RequestMethod.GET)
public String login(ModelMap model) {
 
return "script";
}


Then in "script.jsp" I have some javascript created dynamically, for example:
 
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
var role = "";
<sec:authorize ifAllGranted="role1">
  role += "role1";
</sec:authorize>
 
<sec:authorize ifAllGranted="role2">
  role += "role2";
</sec:authorize>
alert(role);

Finally in wro.xml i have:

  <group name="g1">
   
<js>/script.js</js>
 
</group>
 
At the moment when i try to access /wro/g1.js I get the following error:
 
Grave: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.WEB_002dINF.pages.js_jsp._jspService(js_jsp.java:150) 
 
 I was wondering:
  1. Is this approach correct?
  2. If not, what's the correct way to process javascript in JSP (if any..)?
  3. If yes, do you see any glitch in my code or maybe you can provide a snippet of working code?
 
Thank you very much 
 
Stefano

                            Stefano Zenny

                            unread,
                            Nov 27, 2014, 4:36:17 AM11/27/14
                            to wr...@googlegroups.com
                            Ok, I managed to solve the 2. item: basically I had to revert to WroFilter and apply it as last filter. 

                            Also in file wro.properties had to set:

                             uriLocators=servletContext.DISPATCHER_FIRST


                            Anyways, I'm still not sure if it possible to obtain the same result using the ConfigurableWroFilter

                            Moandji Ezana

                            unread,
                            Nov 27, 2014, 5:44:42 AM11/27/14
                            to wr...@googlegroups.com

                            I can't help you with wro4j, but what I generally do instead of dynamically generating scripts is to create a namespaced data object in the page, that other functions access. In your case, that might be

                            APP.security = { roles: [...] };

                            That way, static scripts can expect the roles array to be filled. Seems simpler to me than generating whole scripts via wro4j.

                            --
                            You received this message because you are subscribed to the Google Groups "wro4j" group.
                            To unsubscribe from this group and stop receiving emails from it, send an email to wro4j+un...@googlegroups.com.
                            To post to this group, send email to wr...@googlegroups.com.
                            Visit this group at http://groups.google.com/group/wro4j.
                            For more options, visit https://groups.google.com/d/optout.

                            Alex Objelean

                            unread,
                            Nov 27, 2014, 5:36:55 PM11/27/14
                            to wr...@googlegroups.com
                            Hi Stefano,

                            sorry for late reply. 
                            As it is now, the WroFilter is not very spring friendly (there is an open issue and I will definitely take some time to fix it). The existing ConfigurableWroFilter tries to fix this problem. I would suggest using WroFilter and providing your configuration through wro.properties (whose content can be controlled using spring profiles or something similar). This approach should allow you to configure easily almost anything you would need to change. 

                            For more examples of using wro4j, you can check this github project.

                            Thanks,
                            Alex

                            Alex Objelean

                            unread,
                            Nov 27, 2014, 5:39:15 PM11/27/14
                            to wr...@googlegroups.com
                            Glad you have figured this out. The DISPATCHER_FIRST allows fetching the content generated by servlet container (like JSP). For this particular use-case, this is ok. Also, the suggestion provided by Moandji might work as well for you.

                            Cheers,
                            Alex

                            On Thursday, 27 November 2014 11:36:17 UTC+2, Stefano Zenny wrote:
                            Reply all
                            Reply to author
                            Forward
                            0 new messages