Skip to content

Commit

Permalink
Added support to override the r.js (require optimizer) implementation…
Browse files Browse the repository at this point in the history
… via a new 'impl' option
  • Loading branch information
Martin.Snyder committed Jul 11, 2013
1 parent ddf5e1d commit 37f9679
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ JSDoc 3 options:
- *(Must declare this or `requirejs.options`)* requirejs.buildprofile = File reference for config [example](https://github.com/eriwen/gradle-js-plugin/blob/master/src/test/resources/requirejs/build.js)
- requirejs.options = Map of options [require.js docs](http://requirejs.org/docs/optimization.html#options)
- *(Optional)* ignoreExitCode = Fail build if `false` and require.js did not run successfully. Default is `false`.
- *(Optional)* impl = r.js implementation file. Version 2.1.4 is provided within this plugin. Specifying this option allows users to specify a version of the require optimizer of their own choosing

What, you want more? [Tell me!](https://github.com/eriwen/gradle-js-plugin/issues)

Expand All @@ -184,6 +185,7 @@ This project is made possible due to the efforts of these fine people:
* Martin Ziel - Allowing minifyJs task to accept multiple files as input
* [Joe Fitzgerald](https://github.com/joefitzgerald) - JSHint and RequireJS features
* [levsa](https://github.com/levsa) - JSHint predef and checkstyle reporter
* [Martin Snyder](https://github.com/MartinSnyder) - requireJs impl option

## See Also
The [Gradle CSS Plugin](https://github.com/eriwen/gradle-css-plugin)!
12 changes: 11 additions & 1 deletion src/main/groovy/com/eriwen/gradle/js/tasks/RequireJsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.gradle.api.tasks.OutputFile

class RequireJsTask extends SourceTask {
private static final String REQUIREJS_PATH = 'r.js'
private static final String REQUIREJS_IMPLEMENTATION_OPTION = "impl"
private static final String TMP_DIR = "tmp${File.separator}js"
private static final ResourceUtil RESOURCE_UTIL = new ResourceUtil()
private final RhinoExec rhino = new RhinoExec(project)
Expand All @@ -37,10 +38,19 @@ class RequireJsTask extends SourceTask {

@TaskAction
def run() {
final File requireJsFile = RESOURCE_UTIL.extractFileToDirectory(new File(project.buildDir, TMP_DIR), REQUIREJS_PATH)
LinkedHashMap<String, Object> options = [] // [optimize: "none", logLevel: 2, skipModuleInsertion: false, out: dest]
options.putAll(project.requirejs.options)

final File requireJsFile
if (options.containsKey(REQUIREJS_IMPLEMENTATION_OPTION)) {
requireJsFile = new File(options.get(REQUIREJS_IMPLEMENTATION_OPTION))

// This option applies to the task itself, and does not need to be passed on to Rhino or the r.js script itself
options.remove(REQUIREJS_IMPLEMENTATION_OPTION)
} else {
requireJsFile = RESOURCE_UTIL.extractFileToDirectory(new File(project.buildDir, TMP_DIR), REQUIREJS_PATH)
}

final List<String> args = [requireJsFile.canonicalPath]
args.add("-o")
if (project.requirejs.buildprofile != null && project.requirejs.buildprofile.class == File && project.requirejs.buildprofile.exists()) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/groovy/com/eriwen/gradle/js/RequireJsTaskTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ class RequireJsTaskTest extends Specification {
ExecException e = thrown()
}

def "runWithInvalidRequireImplementation"() {
given:
project.requirejs.options = [baseUrl: ".", "paths.jquery": "jam/jquery/dist/jquery", name: "main", out: "main-built.js", "impl": "bad.r.js"]
task.ignoreExitCode = false
addMainFile()
addJamDir()

when:
task.run()

then:
ExecException e = thrown()
}

def "runWithAlternateRequireImplementation"() {
given:
project.requirejs.options = [baseUrl: ".", "paths.jquery": "jam/jquery/dist/jquery", name: "main", out: "main-built.js", "impl": "src/test/resources/requirejs/r.2.1.4.js"]
task.ignoreExitCode = false
addMainFile()
addJamDir()

when:
task.run()

then:
notThrown ExecException
}

def "runWithBuildProfile"() {
given:
project.requirejs.buildprofile = new File("${project.projectDir.absolutePath}${File.separator}build.js")
Expand Down

0 comments on commit 37f9679

Please sign in to comment.