Support Require.js for packaging #38

chiquitinxx opened this Issue Jan 31, 2015 · 3 comments


None yet

1 participant



As we have talked in issue #16, we are going to add require.js( as package manager. Now, the conversion is done file by file. But, with this improvement we want to give facilities to create bigger projects without a hell of files and dependencies managed manually. Seem it has good integration with Node.js, and it uses closure compiler for optimizations.

You can take a look at Serge idea project

The hardest part is resolve dependencies. For example, if you want to convert a class, what classes are needed by that class? We have to do compiler job, see imports, classes used, inheritance, traits,... The target? When I convert a file, all dependencies are also converted and all that files are require.js modules.

Also we have to improve daemon converter, now has to listen changes in all the dependencies tree, also add more files when necessary. But this will be later.

At the moment I will create a new conversion option, when activated, conversion will be done as require.js modules. Without that conversion option, grooscript will work as it does now.

I'll create a branch called 'require' to work with. The plan: release the conversion process in 1.1 release. Later, the daemon.

If you have something to say, or if you want to contribute, please as soon as possible :). Thank you.



In branch 'require' it starts to work. Just as reminder the idea, you will tell grooscript: ey! I want to convert this file to a require.js module. Grooscript will manage all dependencies, an also will convert all to require.js modules. You will have to include any classPath (or will take current folder) as any other conversion option, and all files has to be in that classpath.

GrooScript.setConversionProperty('classPath', 'src/main/groovy')
GrooScript.convertRequireJs(initialGroovyFile, destinationFolder)

If you want to use any of that modules, you have to add it to your require.js, and of course, you will have to load grooscript(.min).js and any dependency before.

I'll show examples soon, just say something if you miss something for the first version.


I'm using it in books demo (require branch of I had to change require.js file to set destination folder ('js/app') and initial groovy script ('bookDemo'):

    baseUrl: 'js/app',
    paths: {
        lib: '../lib',
        jquery: '../lib/jquery'

requirejs(['jquery', 'lib/grooscript.min', 'lib/grooscript-tools'], function($) {
    requirejs(['Templates', 'lib/sockjs-0.3.4', 'lib/stomp', 'bookDemo']);

Defined require.js conversion task in build.grade

task myRequire(type: org.grooscript.gradle.RequireJsTask) {
    sourceFile = 'src/main/groovy/bookDemo.groovy'
    destinationFolder = 'src/main/resources/static/js/app'

When the task run, all files are converted and saved as require.js modules. bookDemo.groovy is the script where all is loaded, some groovy classes are used. For example BookPresenter.groovy:

package component

import demo.model.Book

class BookPresenter implements Chart {

    List<Book> books = []
    Counter booksCounter
    GQuery gQuery = new GQueryImpl()

The result js file is saved as 'src/main/resources/static/js/app/component/BookPresenter.js'. And the content is:

define(['component/Chart','demo/model/Book','component/Counter'], function (Chart,Book,Counter) {
function BookPresenter() {
  var gSobject = gs.inherit(gs.baseClass,'BookPresenter');
  gSobject.clazz = { name: 'component.BookPresenter', simpleName: 'BookPresenter'};
  gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
  gSobject.clazz.interfaces = [{ name: 'component.Chart', simpleName: 'Chart'}];
return BookPresenter;

This is a preview, I wanted to show it, if you want say something. I have to finish gradle plugin to include a daemon conversion task for this require.js conversions. Very soon 1.1 release :)

And that's all folks!


Merged in master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment