Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Compress Vite output by default in production #12308

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ Map<String, String> getDefaultDevDependencies() {

if (featureFlags.isEnabled(FeatureFlags.VITE)) {
defaults.put("vite", "v2.7.0-beta.3");
defaults.put("rollup-plugin-brotli", "3.1.0");
defaults.put("mkdirp", "1.0.4"); // for application-theme-plugin
} else {
// Webpack plugins and helpers
Expand Down
7 changes: 6 additions & 1 deletion flow-server/src/main/resources/vite.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { processThemeResources } from '@vaadin/application-theme-plugin/theme-ha
import settings from '#settingsImport#';
import { UserConfigFn, defineConfig, HtmlTagDescriptor, mergeConfig } from 'vite';

import brotli from 'rollup-plugin-brotli';

const frontendFolder = path.resolve(__dirname, settings.frontendFolder);
const themeFolder = path.resolve(frontendFolder, settings.themeFolder);
const buildFolder = path.resolve(__dirname, settings.frontendBundleOutput);
Expand Down Expand Up @@ -68,7 +70,9 @@ function runWatchDog(watchDogPort) {
}

export const vaadinConfig: UserConfigFn = (env) => {
if (env.mode === 'development' && process.env.watchDogPort) {
const devMode = env.mode === 'development';

if (devMode && process.env.watchDogPort) {
// Open a connection with the Java dev-mode handler in order to finish
// vite when it exits or crashes.
runWatchDog(process.env.watchDogPort);
Expand All @@ -92,6 +96,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
}
},
plugins: [
!devMode && brotli(),
{
name: 'custom-theme',
config() {
Expand Down
5 changes: 5 additions & 0 deletions flow-tests/test-frontend/vite-production/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<classifier>tests</classifier>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>0.1.2</version>
</dependency>
</dependencies>

<build>
Expand Down
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);
}

}