Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Compress Vite output by default in production (#12308)
As Brotli is disabled by default, you need to enable it to use this feature. #4740 will enable Brotli by default once server issues are sorted out Fixes #12055
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
flow-tests/test-frontend/vite-production/src/test/java/com/vaadin/viteapp/CompressionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.vaadin.viteapp; | ||
|
||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.brotli.dec.BrotliInputStream; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class CompressionIT { | ||
|
||
private String getRootURL() { | ||
return "http://localhost:8888"; | ||
} | ||
|
||
@Test | ||
public void resourcesAvailableAsUncompressed() throws Exception { | ||
String bundleName = getJsBundleName(); | ||
|
||
String file = IOUtils.toString(new URL(getRootURL() + bundleName), | ||
StandardCharsets.UTF_8); | ||
Assert.assertTrue(file.contains("generated-flow-imports")); | ||
} | ||
|
||
@Test | ||
public void resourcesAvailableAsBrotli() throws Exception { | ||
String bundleName = getJsBundleName(); | ||
|
||
URL compressedUrl = new URL(getRootURL() + bundleName + ".br"); | ||
BrotliInputStream stream = new BrotliInputStream( | ||
compressedUrl.openStream()); | ||
|
||
String file = IOUtils.toString(stream, StandardCharsets.UTF_8); | ||
Assert.assertTrue(file.contains("generated-flow-imports")); | ||
} | ||
|
||
private String getJsBundleName() throws Exception { | ||
String indexHtml = IOUtils.toString( | ||
new URL(getRootURL() + "/index.html"), StandardCharsets.UTF_8); | ||
Pattern p = Pattern.compile(".* src=\"VAADIN/build/([^\"]*).*", | ||
Pattern.DOTALL); | ||
|
||
Matcher m = p.matcher(indexHtml); | ||
if (!m.matches()) { | ||
throw new IllegalStateException("No script found"); | ||
} | ||
return "/VAADIN/build/" + m.group(1); | ||
} | ||
|
||
} |