Skip to content

Commit

Permalink
Allowing externs to be declared for closure compiler, fixes eriwen#17
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Jun 19, 2012
1 parent aed2215 commit 3e51e27
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
3 changes: 2 additions & 1 deletion plugin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ javascript {
custom {
js {
srcDir "src/test/resources"
include "*.js"
include "file*.js"
}
}
}
Expand All @@ -39,6 +39,7 @@ task minify(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
closure {
warningLevel = 'VERBOSE'
compilationLevel = 'SIMPLE_OPTIMIZATIONS'
externs = files("src/test/resources/externs.js")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package com.eriwen.gradle.js

import com.google.javascript.jscomp.CompilerOptions
import org.gradle.api.file.FileCollection

class ClosureCompilerExtension {
public static final NAME = "closure"
CompilerOptions compilerOptions = new CompilerOptions()
String compilationLevel = 'SIMPLE_OPTIMIZATIONS'
String warningLevel = 'DEFAULT'
FileCollection externs = null
}
34 changes: 19 additions & 15 deletions src/main/groovy/com/eriwen/gradle/js/JsMinifier.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.google.javascript.jscomp.CompilerOptions
import com.google.javascript.jscomp.JSSourceFile
import com.google.javascript.jscomp.Result
import com.google.javascript.jscomp.WarningLevel
import java.nio.charset.Charset
import org.gradle.api.file.FileCollection

/**
* Util to minify JS files with Google Closure Compiler.
Expand All @@ -16,23 +18,25 @@ import com.google.javascript.jscomp.WarningLevel
*/
class JsMinifier {


void minifyJsFile(final File inputFile, final File outputFile, final CompilerOptions options,
void minifyJsFile(final File inputFile, 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)
WarningLevel level = WarningLevel.valueOf(warningLevel)
level.setOptionsForWarningLevel(options)
List<JSSourceFile> externs = CommandLineRunner.getDefaultExterns()
Compiler compiler = new Compiler()
CompilationLevel.valueOf(compilationLevel).setOptionsForCompilationLevel(options)
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>()
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}"
}
}
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}"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class MinifyJsTask extends SourceTask {
throw new GradleException("Only 1 file can be minified. Please run MinifyJs for each file.")
}

MINIFIER.minifyJsFile((source.files.toArray() as File[])[0], dest, project.closure.compilerOptions, project.closure.warningLevel, project.closure.compilationLevel)
Set<File> externsFiles = project.closure.externs ? project.closure.externs.files : [] as Set<File>
MINIFIER.minifyJsFile((source.files.toArray() as File[])[0], externsFiles, dest,
project.closure.compilerOptions, project.closure.warningLevel, project.closure.compilationLevel)
}
}
1 change: 1 addition & 0 deletions src/test/resources/externs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var console = {};
2 changes: 1 addition & 1 deletion src/test/resources/file1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
function test1() {
console.log('test1');
return true;
}
}

0 comments on commit 3e51e27

Please sign in to comment.