gradle-js-plugin icon indicating copy to clipboard operation
gradle-js-plugin copied to clipboard

Using functionality without creating separate task

Open elygre opened this issue 12 years ago • 2 comments

I have a need to run multiple (many) CombineJs and MinifyJs-operations, and would like to not have to create a separate task for each, instead doing something like shown below:

  • I did not find an easier way, but I might be wrong...
  • Would you be interested in adding such functionality (e.g. would you want a pull-request)?
task js2 {
    // This works
    new com.eriwen.gradle.js.JsMinifier().minifyJsFile (
        file("target/build/iknowbase/iknowbase-full.js"),
        [] as Set<File>,
        file("target/build/iknowbase/iknowbase-full.min.js"),
        project.closure.compilerOptions, project.closure.warningLevel, project.closure.compilationLevel
    )
    // Maybe something like this should work?
    com.eriwen.gradle.js.JsMinifier.minifyJs {
        source = file (...)
        dest = file (...)
    }
}

elygre avatar Dec 18 '12 23:12 elygre

@elygre Actually, the plugin used to work in such a way that one could specify multiple sets of inputs, but that was too confusing for users so I removed it.

You aren't the first person to bring this up, so perhaps we should write new tasks that accomplish this behavior. I would be more than happy to accept a pull request with suggestions.

If you're not keen on that, here is how you can create tasks dynamically to do what you ask:

javascript.source {
    custom {
        js {
            srcDir "src/js/etc"
            include "*.js"
        }
    }
}

javascript.source.custom.js.files.eachWithIndex { jsFile, idx ->
    tasks.add(name: "dominify${idx}", type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
        source = jsFile
        dest = "${buildDir}/${jsFile.name}"
    }
}
task individualMinify(dependsOn: tasks.matching { Task task -> task.name.startsWith("dominify")})

eriwen avatar Dec 20 '12 16:12 eriwen

My groovy isn't good enough to really know what is the best way of implementing stuff. But, for a start, what do you think about the code at https://gist.github.com/7fb3bda12a0a196e4fd3?

I'm thinking that another way would be to create a function taking only a closure, but I don't know quite how to do that properly...

elygre avatar Dec 20 '12 18:12 elygre