1. Javascript压缩
1.1 closure-compiler
Maven: https://mvnrepository.com/artifact/com.google.javascript/closure-compiler
<!–jdk8最多支持到这个版本,在以上版本就是需要jdk11才行–>
<!– https://mvnrepository.com/artifact/com.google.javascript/closure-compiler –>
<dependency>
<groupId>com.google.javascript</groupId>
<artifactId>closure-compiler</artifactId>
<version>v20220502</version>
</dependency>
public class ContentConverUtil {
private static File tempJavascriptDir = FileUtil.mkdir("temp/generateCode/javascript");
/**
* Javascript压缩
*
* @param javascriptCotnent 待压缩的javascript内容
* @return
*/
@SneakyThrows
public static String javascriptCompressor(String javascriptCotnent) {
String result = javascriptCotnent;
if (StrUtil.isNotBlank(javascriptCotnent)) {
File rowjavascriptCotnentTempFile = FileUtil.createTempFile(tempJavascriptDir);
File compressorjavascriptCotnentTempFile = FileUtil.file(tempJavascriptDir, StrUtil.format("{}.min.js", IdUtil.getSnowflakeNextIdStr()));
try {
FileUtil.writeUtf8String(javascriptCotnent,rowjavascriptCotnentTempFile);
String[] args = new String[]{"–js", FileUtil.getAbsolutePath(rowjavascriptCotnentTempFile), "–js_output_file", FileUtil.getAbsolutePath(compressorjavascriptCotnentTempFile), "–warning_level", "QUIET"};
Constructor<CommandLineRunner> constructor = ReflectUtil.getConstructor(CommandLineRunner.class, String[].class);
CommandLineRunner commandLineRunner = constructor.newInstance((Object)args);
ReflectUtil.invoke(commandLineRunner, "doRun", new Object[0]);
result = FileUtil.readString(compressorjavascriptCotnentTempFile, StandardCharsets.UTF_8);
} finally {
FileUtil.del(rowjavascriptCotnentTempFile);
FileUtil.del(compressorjavascriptCotnentTempFile);
}
}
return result;
}
}
2. Html压缩
2.1 htmlcompressor
Maven: https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor/1.5.2
<!– https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor –>
<dependency>
<groupId>com.googlecode.htmlcompressor</groupId>
<artifactId>htmlcompressor</artifactId>
<version>1.5.2</version>
</dependency>
ContentConverUtil.java
public class ContentConverUtil {
/**
* HTML压缩
* @param htmlCotnent
* @return
*/
@SneakyThrows
public static String htmlCompressor(String htmlCotnent) {
if(StrUtil.isNotBlank(htmlCotnent)) {
HtmlCompressor htmlCompressor = new HtmlCompressor();
return htmlCompressor.compress(htmlCotnent);
}
return htmlCotnent;
}
}
3. Css压缩
3.1 yuicompressor
Maven: https://mvnrepository.com/artifact/com.yahoo.platform.yui/yuicompressor
pom.xml
<!– 当前仅用于css压缩,里面的js压缩工具已经没啥作用 https://mvnrepository.com/artifact/com.yahoo.platform.yui/yuicompressor –>
<dependency>
<groupId>com.yahoo.platform.yui</groupId>
<artifactId>yuicompressor</artifactId>
<version>2.4.8</version>
</dependency>
ContentConverUtil.java
public class ContentConverUtil {
/**
* CSS压缩
*
* @param cssCotnent 待压缩的css内容
* @return
*/
@SneakyThrows
public static String cssCompressor(String cssCotnent) {
if (StrUtil.isNotBlank(cssCotnent)) {
BufferedReader reader = null;
ByteArrayOutputStream resultOutputStream = null;
Writer writer = null;
try {
reader = IoUtil.getReader(new ByteArrayInputStream(cssCotnent.getBytes()), StandardCharsets.UTF_8);
CssCompressor cssCompressor = new CssCompressor(reader);
resultOutputStream = new ByteArrayOutputStream();
writer = new OutputStreamWriter(resultOutputStream, StandardCharsets.UTF_8);
cssCompressor.compress(writer, -1);
} finally {
IoUtil.close(reader);
IoUtil.close(writer); // 必须先关闭流,字符数组才有数据
}
cssCotnent = IoUtil.toStr(resultOutputStream, StandardCharsets.UTF_8);
}
return cssCotnent;
}
}
————————————————
版权声明:本文为CSDN博主「嗯嗯**」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39651356/article/details/128072720