문서의 이전 판입니다!
float
을 키워드로 인식하는 버그가 있다.float
을 키워드로 지정했기 때문인 것 같다.float
을 키워드로 보지 않는다.style.float = xx
갈은 구문이 있다면 style['float'] = xx
로 변경해야 한다.outputEncoding
을 명시하지 않으면 한글을 Unicode 기호로 바꾼다(\ucXXX 형태). 이 때문에 한글로 된 문자열이 많으면 파일 크기가 오히려 늘어난다. Ant로 할 경우 outputEncoding
옵션이 지정 안 되는 듯하다. CompilerOptionscom.google.javascript.jscomp.CommandLineRunner
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
java -jar compiler.jar --help
WHITESPACE_ONLY
: 공백과 주석 제거등만 실행SIMPLE_OPTIMIZATIONS
: 기본값. 공백제거, 세미콜론 보정등.ADVANCED_OPTIMIZATIONS
: 더 강력한 압축. 불필요한 코드 삭제 등.java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js hello.js
–js VAL
: 컴파일 대상 파일–js_output_file VAL
: 컴파일 결과 파일. 저장하지 않으면 표준출력.–charset VAL
: 입출력 캐릭터셋–compilation-level [WHITESPACE_ONLY | SIMPLE_OPTIMIZATIONS | ADVANCED_OPTIMZATIONS]
: 컴파일 레벨 지정outputEncoding
적용 되는지 확인해볼 것.<?xml version="1.0"?> <project basedir="." default="compile"> <taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask" classpath="../build/compiler.jar"/> <target name="compile"> <jscomp compilationLevel="simple" warning="verbose" debug="false" output="output/file.js"> <externs dir="${basedir}/src"> <file name="extern.js"/> </externs> <sources dir="${basedir}/src"> <file name="simple1.js"/> <file name="simple2.js"/> </sources> <sources dir="${basedir}/other"> <file name="simple3.js"/> </sources> </jscomp> </target> </project>
com.google.javascript.jscomp.CommandLineRunner
참조void minifyJsFile(final Set<File> inputFiles, final Set<File> externsFiles, final File outputFile, final CompilerOptions options, final String warningLevel, final String compilationLevel) { Compiler compiler = new Compiler() CompilationLevel.valueOf(compilationLevel).setOptionsForCompilationLevel(options) options.outputCharset = 'utf-8' // 인코딩을 지정해야 올바르게 한글이 출력된다. WarningLevel level = WarningLevel.valueOf(warningLevel) level.setOptionsForWarningLevel(options) List<JSSourceFile> externs = CommandLineRunner.getDefaultExterns() if (externsFiles.size()) { externs.addAll(externsFiles.collect() { JSSourceFile.fromFile(it) }) } List<JSSourceFile> inputs = new ArrayList<JSSourceFile>() inputFiles.each { inputFile -> inputs.add(JSSourceFile.fromFile(inputFile)) } Result result = compiler.compile(externs, inputs, options) if (result.success) { outputFile.write(compiler.toSource()) } else { result.errors.each { println "${it.sourceName}:${it.lineNumber} - ${it.description}" } } }