chiquitinxx / grooscript

Converts your Groovy code to Javascript
https://www.grooscript.org
Other
221 stars 18 forks source link

Add support for CommonJs, es6 or UMD modules #63

Open luisvt opened 6 years ago

luisvt commented 6 years ago

Please add support for converting the groovy files into js using commonJs modules. For example having next code:

// Person.groovy
package com.example.grooscript_sample.models

class Person {
    String firstName
    String lastName
}

and:

// PersonPresenter.groovy
package com.example.grooscript_sample.presenters

import com.example.grooscript_sample.models.Person

class PersonPresenter {
    Person person
    def buttonClick() {
        if (person.firstName) {
            $('#salutes').append("<p>Hello ${person.firstName}!</p>")
        }
    }
}

should be transpiled to:

// Person.js

module.exports.Person = function Person() {
  var gSobject = gs.init('Person');
  gSobject.clazz = { name: 'com.example.grooscript_sample.models.Person', simpleName: 'Person'};
  gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
  gSobject.firstName = null;
  gSobject.lastName = null;
  if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};

  return gSobject;
 };

and:

// PersonPresenter.js

var Person = require('./Person');

module.export.PersonPresenter = function PersonPresenter() {
  var gSobject = gs.init('PersonPresenter');
  gSobject.clazz = { name: 'com.example.grooscript_sample.presenters.PersonPresenter', simpleName: 'PersonPresenter'};
  gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
  gSobject.person = null;
  gSobject['buttonClick'] = function(it) {
    if (gs.bool(gs.gp(gSobject.person,"firstName"))) {
      return gs.mc(gs.mc(this,"$",["#salutes"], gSobject),"append",["<p>Hello " + (gs.gp(gSobject.person,"firstName")) + "!</p>"]);
     };
   }
  if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};

  return gSobject;
 };
chiquitinxx commented 6 years ago

A few days ago, I created a new branch for the next release of grooscript. The idea is support Groovy 3, and generate ES 6 code. I think support of ES6 modules is a good choice. I haven't update roadmap because the development is going slow, maybe if more people want to contribute :) Please feel free to add your ideas, feedback, ... in this thread.