Martin Spa
Martin Spa

Reputation: 1534

Minify JavaScript code in a JSPX page (in a Spring MVC WebApp)

So, I have a jspx page which has JS code in it, and part of the code is generated dynamically, so I can't minify and paste the minified code.

E.g. Let's imagine I draw a chart with Google Chart API, and the I fill the data with a forEach JSTL tag.

<script type="text/javascript">
    google.load('visualization', '1', {'packages':['corechart']});
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'data');
        data.addRows(${fn:length(chartData)});
        <c:forEach items="${chartData}" var="entry" varStatus="status">
           data.setValue(${status.count-1},0,${entry.value});
        </c:forEach>      
        var chart = new google.visualization.LineChart(document.getElementById('google_chart'));
        chart.draw(data, {width: 620, height: 300});    
    }
</script>  

My ideal solution would be for there to exist a tag e.g. <something:minify><script type="text/javascript"> .... </script></something:minify> which would do the job, but I'm interested in all possible solutions.

Upvotes: 1

Views: 841

Answers (1)

Sani Huttunen
Sani Huttunen

Reputation: 24385

UglifyJS can be used to minify code on-the-fly.
It's loosly based on Google Closure but is a lot faster and succeeds in some cases where Closure doesn't.

Upvotes: 1

Related Questions