Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wro4j -- How do you disable aggregation feature in debug mode?

I'm using wildcards to load my javascript and css files and it works great, but it's a pain to work with the aggregated javascript files during the development phase. Is there a way to have wro4j not aggregate the files, but just include them individually?

My wro.xml File

<groups xmlns="http://www.isdc.ro/wro">
 <group name="external">
    <js>/app/lib/*.js</js>
 </group>

 <group name="application">
    <js>/js/*.js</js>
    <css>/css/normalize.css</css>
    <css>/css/*.css</css>
 </group>
</groups>

Thanks!

like image 397
John Gordon Avatar asked Jan 17 '13 21:01

John Gordon


2 Answers

There is a trick to achieve that in development mode (debug=true). There is an endpoint (/wro/wroAPI/model - this is valid if you are mapping WroFilter to /wro/*) which returns a JSON representation of the model. Using this JSON, you can easily insert scripts and/or css links for a given group by iterating group entries. There is no such widget yet available by default, but if you create one and want to contribute it - you're welcome :).

like image 82
Alex Objelean Avatar answered Oct 24 '22 02:10

Alex Objelean


Thanks for the input, Alex.

I've created a ServletFilter that uses the info you provided above. I post it here for posterity. Note: this isn't ideal code (caches too much, for example), it's just here to give folks an idea of what an implementation would look like. My implementation uses Spring's DelegatingFilterProxy to tell the code what groups to worry about. As far as it goes with creating a plugin, could you point me the way to some documentation for implementing, I'm hazy on that part?

public class WroDebugFilter implements Filter
{
    //http loader is shamelessly stolen from another project.
    SimpleHttpLoader httpLoader = new SimpleHttpLoader();

    private List<String> jscriptFiles;
    private List<String> debugGroups;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        request.setAttribute("jscriptFiles",getJavaScriptFiles((HttpServletRequest) request));

        chain.doFilter(request,response);
    }

    private List<String> getJavaScriptFiles(HttpServletRequest request) {

        if (jscriptFiles == null) {

            List<String> cssFiles = new ArrayList<String>();
            List<String> jsFiles = new ArrayList<String>();

            JsonParser jsonParser = new JsonParser();

            String url = httpLoader.httpGet(getApiUrl(request));

            JsonObject data = jsonParser.parse(url).getAsJsonObject();
            JsonArray groups = data.getAsJsonArray("groups");

            for (JsonElement group : groups) {

                JsonObject groupObject = group.getAsJsonObject();

                String groupName = groupObject.get("name").getAsString();

                if (debugGroups.contains(groupName)) {

                    JsonArray resources = groupObject.getAsJsonArray("resources");

                    for (JsonElement resource : resources) {
                        JsonObject resourceObject = resource.getAsJsonObject();
                        String uri = resourceObject.get("uri").getAsString();

                        if (uri.contains(".css")) {
                            cssFiles.add("\t<link rel=\"stylesheet\" href=\"" + request.getContextPath() + uri +"\">");
                        }
                        else {
                            jsFiles.add("<script type=\"text/javascript\" src=\"" + request.getContextPath() + uri + "\"></script>");
                        }
                    }
                }

            }
            cssFiles.addAll(jsFiles);
            jscriptFiles = cssFiles;
        }

        return jscriptFiles;
    }

    private String getApiUrl(HttpServletRequest request) {

        String serverName = request.getServerName();
        String contextPath = request.getContextPath();
        int port = request.getServerPort();

        return String.format("http://%s:%s%s/%s",serverName,port,contextPath,"wro/wroAPI/model");
    }

    @Override
    public void destroy() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<String> getDebugGroups() {
        return debugGroups;
    }

    public void setDebugGroups(List<String> debugGroups) {
        this.debugGroups = debugGroups;
    }
}

The wro.xml file

<groups xmlns="http://www.isdc.ro/wro">
    <group name="external">
        <js>/app/lib/*.js</js>
    </group>

    <group name="application">
        <js>/js/*.js</js>
        <css>/css/normalize.css</css>
        <css>/css/*.css</css>
    </group>
</groups>

and....

The Spring Configuration

    <bean id="wroDebugFilter" class="gov.mystate.dhw.idalink.web.filter.WroDebugFilter">
        <property name="debugGroups">
                <list>
                    <value>application</value>
                </list>
            </property>
    </bean>
like image 43
John Gordon Avatar answered Oct 24 '22 03:10

John Gordon