Gradle JavaScript Plugin

Gradle plugin for working with JS


Project maintained by eriwen Hosted on GitHub Pages — Theme by mattgraham

Wrangling your JS in a Gradle build is easy! Just add this to your build.gradle file:

// Pull the plugin from a Maven Repo
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.eriwen:gradle-js-plugin:1.4.0'
    }
}
// Invoke the plugin
apply plugin: 'js'

// Declare your sources
javascript.source {
    dev {
        js {
            srcDir jsSrcDir
            include "*.js"
            exclude "*.min.js"
        }
    }
    prod {
        js {
            srcDir jsSrcDir
            include "*.min.js"
        }
    }
}

// Specify a collection of files to be combined, then minified and finally GZip compressed.
task combinejs(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
    if (env == 'prod') {
        source = javascript.source.prod.js.files
    } else {
        source = javascript.source.dev.js.files
    }
    dest = file("${buildDir}/all.js")
}

task minifyjs(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
    source = combinejs
    dest = file("${buildDir}/all-min.js")
    closure {
        warningLevel = 'QUIET'
    }
}

task gzipjs(type: com.eriwen.gradle.js.tasks.GzipJsTask) {
    source = minifyjs
    dest = file("${buildDir}/all-min.js")
}

Need more than 1 set of files generated? We'll have to declare our tasks a bit differently:

task jsDev(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
    source = ["${projectDir}/js/file1.js", "${projectDir}/js/file2.js"]
    dest = file("${buildDir}/all-debug.js")
}

task jsProd(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
    source = ["${projectDir}/js/file1.js", "${projectDir}/js/file2.js"]
    dest = file("${buildDir}/all.js")
}

JSHint support

task jshintjs(type: com.eriwen.gradle.js.tasks.JsHintTask) {
    source = javascript.source.dev.js.files
    dest = file("${buildDir}/jshint.out")
    jshint.options = [expr: "true", unused: "true"]
}

JSDoc 3 support

task jsdocjs(type: com.eriwen.gradle.js.tasks.JsDocTask) {
    source = ["${projectDir}/js/file1.js", "${projectDir}/js/file2.js"]
    destinationDir = file("${buildDir}/jsdoc")
}

props2Js support

task processProps(type: com.eriwen.gradle.js.tasks.Props2JsTask) {
    source = file("${projectDir}/src/test/resources/test.properties")
    dest = file("${buildDir}/props.jsonp")
    props {
        type = 'jsonp'
        functionName = 'fn'
    }
}

require.js support

task require(type: com.eriwen.gradle.js.tasks.RequireJsTask) {
    source = javascript.source.dev.js.files
    dest = "${buildDir}/out.js"
    requirejs.buildprofile = new File("src/main/resources/requirejs-config.js")
}

Built-in Tasks and Options

combineJs

minifyJs (Uses the Google Closure Compiler)

gzipJs

jshint

jsdoc

JSDoc 3 options:
-t or --template <value> The name of the template to use. Default: the "default" template
-c or --configure <value> The path to the configuration file. Default: jsdoc __dirname + /conf.json
-e or --encoding <value> Assume this encoding when reading all source files. Default: utf-8
-T or --test Run all tests and quit.
-d or --destination <value> The path to the output folder. Use "console" to dump data to the console. Default: console
-p or --private Display symbols marked with the @private tag. Default: false.
-r or --recurse Recurse into subdirectories when scanning for source code files.
-h or --help Print this message and quit.
-X or --explain Dump all found doclet internals to console and quit.
-q or --query <value> Provide a querystring to define custom variable names/values to add to the options hash.
-u or --tutorials <value> Directory in which JSDoc should search for tutorials.

props2js

requireJs

What, you want more? Tell me!

Contributors

This project is made possible due to the efforts of these fine people:

See Also

The Gradle CSS Plugin!

Brought to you by @eriwen