Skip to content

Commit 7b4c213

Browse files
committed
Implementation of a JavaScript compressor
1 parent 2976251 commit 7b4c213

5 files changed

Lines changed: 187 additions & 1 deletion

File tree

‎transform/pom.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
</dependency>
3232

3333
<!-- LESS should be moved to a separate module later -->
34+
<!-- Note: Removed for now because yuicompressor provides this dependency -->
35+
<!--
3436
<dependency>
3537
<groupId>org.mozilla</groupId>
3638
<artifactId>rhino</artifactId>
3739
<version>1.7R3</version>
3840
</dependency>
41+
-->
3942

4043
<dependency>
4144
<groupId>com.yahoo.platform.yui</groupId>

‎transform/src/main/java/org/ocpsoft/rewrite/less/Less.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String render(String less)
4848
try {
4949

5050
context.setOptimizationLevel(-1);
51-
context.setLanguageVersion(Context.VERSION_1_8);
51+
context.setLanguageVersion(Context.VERSION_1_6);
5252

5353
Scriptable scope = context.initStandardObjects();
5454

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.InputStreamReader;
21+
import java.io.OutputStream;
22+
import java.io.OutputStreamWriter;
23+
import java.io.Reader;
24+
import java.nio.charset.Charset;
25+
26+
import org.ocpsoft.rewrite.transform.Transformer;
27+
28+
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
29+
30+
/**
31+
*
32+
* JavaScript compressor implementation based on yuicompressor
33+
*
34+
* @author Christian Kaltepoth
35+
*
36+
*/
37+
public class JsCompress implements Transformer
38+
{
39+
40+
private final Charset charset;
41+
42+
public JsCompress()
43+
{
44+
this(Charset.forName("UTF8"));
45+
}
46+
47+
public JsCompress(Charset charset)
48+
{
49+
this.charset = charset;
50+
}
51+
52+
@Override
53+
public void transform(InputStream input, OutputStream output) throws IOException
54+
{
55+
56+
// prepare input reader
57+
Reader reader = new InputStreamReader(input, charset);
58+
JavaScriptCompressor compressor = new JavaScriptCompressor(reader, null);
59+
60+
// write compressed output
61+
OutputStreamWriter writer = new OutputStreamWriter(output, charset);
62+
compressor.compress(writer, 100, true, false, false, false);
63+
writer.flush();
64+
65+
}
66+
67+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.asset.StringAsset;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
28+
import org.ocpsoft.rewrite.test.HttpAction;
29+
import org.ocpsoft.rewrite.test.RewriteTest;
30+
31+
/**
32+
*
33+
* Integration test for {@link JsCompress}.
34+
*
35+
* @author Christian Kaltepoth
36+
*
37+
*/
38+
@RunWith(Arquillian.class)
39+
public class JsCompressTest extends RewriteTest
40+
{
41+
@Deployment(testable = false)
42+
public static WebArchive getDeployment()
43+
{
44+
return RewriteTest.getDeployment()
45+
.addAsWebResource(new StringAsset("var text = \"hello\";\n\nalert(text);"), "test.js")
46+
.addAsServiceProvider(ConfigurationProvider.class, JsCompressTestProvider.class);
47+
}
48+
49+
@Test
50+
public void testJavaScriptCompression() throws Exception
51+
{
52+
HttpAction<HttpGet> action = get("/test.js");
53+
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
54+
assertEquals("var text=\"hello\";alert(text);", action.getResponseContent());
55+
}
56+
57+
@Test
58+
public void testNotExistingSourceFile() throws Exception
59+
{
60+
HttpAction<HttpGet> action = get("/not-existing.js");
61+
assertEquals(404, action.getResponse().getStatusLine().getStatusCode());
62+
}
63+
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform.yui;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
23+
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
24+
import org.ocpsoft.rewrite.transform.Transform;
25+
import org.ocpsoft.rewrite.transform.resolve.WebResourceResolver;
26+
27+
/**
28+
*
29+
* {@link ConfigurationProvider} for {@link JsCompressTest}.
30+
*
31+
* @author Christian Kaltepoth
32+
*
33+
*/
34+
public class JsCompressTestProvider extends HttpConfigurationProvider
35+
{
36+
@Override
37+
public int priority()
38+
{
39+
return 0;
40+
}
41+
42+
@Override
43+
public Configuration getConfiguration(final ServletContext context)
44+
{
45+
return ConfigurationBuilder
46+
.begin()
47+
.addRule(Transform.request(".js")
48+
.resolvedBy(WebResourceResolver.identity())
49+
.apply(JsCompress.class));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)