일괄 Minify 예
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import com.google.javascript.jscomp.*;
import com.google.javascript.jscomp.Compiler;
public class BatchJsMinify {
private static final String[] EXCLUDE_PATTERNS = { ".min.js", "-min.js" };
private static final CompilationLevel DEFAULT_COMPILATION_LEVEL = CompilationLevel.SIMPLE_OPTIMIZATIONS;
private String srcDirName;
private final File srcDir;
private String destDirName;
private final File destDir;
private com.google.javascript.jscomp.Compiler compiler = new Compiler();;
private CompilerOptions options = new CompilerOptions();
private WarningLevel warningLevel = WarningLevel.QUIET;
public BatchJsMinify(String srcDirName, String destDirName) {
this.srcDirName = srcDirName;
this.destDirName = destDirName;
srcDir = new File(srcDirName);
destDir = new File(destDirName);
if (!srcDir.exists() || !srcDir.isDirectory()) {
throw new IllegalArgumentException(srcDirName + " must exist and be a directory.");
}
destDir.mkdirs();
}
public void compile() throws IOException {
options.setCodingConvention(CodingConventions.getDefault());
options.setOutputCharset("UTF-8");
warningLevel.setOptionsForWarningLevel(options);
DEFAULT_COMPILATION_LEVEL.setOptionsForCompilationLevel(options);
final List<SourceFile> jsSourceFiles = getSourceFiles();
if (jsSourceFiles == null || jsSourceFiles.size() == 0) {
System.out.println("Nothing to compile.");
return;
}
final List<SourceFile> defaultExterns = CommandLineRunner.getDefaultExterns();
compiler.disableThreads(); // thread가 활성화 돼 있으면 오히려 종료가 늦게 되었다.
final Result result = compiler.compile(defaultExterns, jsSourceFiles, options);
if (result.success) {
writeToFile(jsSourceFiles);
} else {
printErrors(result);
}
}
private void printErrors(Result result) {
for (JSError error : result.errors) {
System.err.println("Error : " + error.sourceName + ":" + error.lineNumber + " - " + error.description);
}
}
private List<SourceFile> getSourceFiles() throws IOException {
final List<SourceFile> jsSourceFiles = new ArrayList<>();
Files.walkFileTree(Paths.get(srcDirName), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String filename = file.toString();
if (filename.endsWith(".js") && !filename.endsWith(".min.js") && !filename.endsWith("-min.js")) {
final SourceFile sourceFile = SourceFile.fromFile(file.toFile(), Charset.forName("UTF-8"));
jsSourceFiles.add(sourceFile);
}
return FileVisitResult.CONTINUE;
}
});
return jsSourceFiles;
}
private void writeToFile(List<SourceFile> jsSourceFiles) {
final String[] minified = compiler.toSourceArray();
for (int i = 0; i < jsSourceFiles.size(); i++) {
final SourceFile sourceFile = jsSourceFiles.get(i);
String fileRestPath = sourceFile.getOriginalPath().replace(srcDirName, "");
final File destFile = new File(destDir, fileRestPath);
destFile.getParentFile().mkdirs();
System.out.println("Writing minified js file : " + destFile.getAbsolutePath());
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"))) {
writer.write(minified[i]);
} catch (IOException e) {
throw new IllegalStateException("minified js file save error.", e);
}
}
}
}