Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Add documentation and little fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chiquitinxx committed May 23, 2015
1 parent 65a3fb1 commit c0bc19a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
48 changes: 47 additions & 1 deletion doc/requirejs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,43 @@ dependency will be added in `*Initial.js*` module.
Then, you don't have to take care about all the files and dependencies, each time that you convert `*Initial.groovy*`, all
groovy files will be converted to modules, and all dependencies will be include in that modules.

=== Add javascript dependencies

If you want to include dependencies to other external require.js modules, you can use AST @RequireJsModule. For example,
in this class we are going to use data from a require.js module in `*lib/data.js*`.

[source,groovy]
--
include::../src/test/src/files/Require.groovy[]
--

The javascript result of this conversion is:

[source,javascript]
--
define(['lib/data'], function (data) {

function Require() {
//..
gSobject.data = data;
//..

return gSobject;
};

return Require;
});
--

In groovy you can use and test `*data*` field as a normal field.

=== Using it

You have all that javascript modules, now you can use in your require.js application. You have to be sure that
you include `*grooscript.js*` in your require.js dependencies. All require.js modules that you have generated are converted
groovy code that needs `*grooscript.js*` to run.

This is an example require.js initial file:
Some examples require.js initial file:

[source,javascript]
--
Expand All @@ -47,6 +77,22 @@ requirejs(['lib/grooscript.min'], function() {
});
--

[source,javascript]
--
requirejs.config({
baseUrl: 'js/app',
paths: {
lib: '../lib',
jquery: '../lib/jquery'
},
shim: {
'bookDemo': ['lib/grooscript.min', 'lib/grooscript-tools']
}
});

requirejs(['jquery', 'bookDemo']);
--

And that's all, you don't have to worry about more groovy files, just add javascript dependencies in your project.

For more info about this, can take a look at this https://github.com/chiquitinxx/grooscript/issues/38[github improvement]. Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class RequireJsFileGenerator {
''
} else {
if (classes.size() == 1) {
"return ${classes[0]};${LINE_SEPARATOR}"
" return ${classes[0]};${LINE_SEPARATOR}"
} else {
def mapClasses = classes.collect { "${it}:${it}" }.join(',')
"return {${mapClasses}};${LINE_SEPARATOR}"
" return {${mapClasses}};${LINE_SEPARATOR}"
}
}
}
Expand Down
30 changes: 6 additions & 24 deletions src/test/groovy/org/grooscript/convert/GsConverterSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.grooscript.convert.util.RequireJsDependency
import org.grooscript.test.ConversionMixin
import spock.lang.Specification

import static org.grooscript.util.Util.LINE_SEPARATOR

/**
* User: jorgefrancoleza
* Date: 23/05/15
Expand All @@ -13,43 +15,23 @@ class GsConverterSpec extends Specification {

void 'test basic conversion'() {
expect:
converter.toJs(BASIC_CLASS).startsWith '''function A() {
var gSobject = gs.inherit(gs.baseClass,'A');
gSobject.clazz = { name: 'A', simpleName: 'A'};
gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};'''
converter.toJs(BASIC_CLASS).startsWith '''function A() {'''
}

void 'convert as a require.js module add spaces'() {
expect:
converter.toJs(BASIC_CLASS, conversionOptionsWithRequireJs()).startsWith(' ' + '''
function A() {
var gSobject = gs.inherit(gs.baseClass,'A');
gSobject.clazz = { name: 'A', simpleName: 'A'};
gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};''')
converter.toJs(BASIC_CLASS, conversionOptionsWithRequireJs()).startsWith(" ${LINE_SEPARATOR} function A() {")
}

void 'initialize require.js modules (ast) converting as require.js module'() {
expect:
converter.toJs(CLASS_WITH_REQUIRE_MODULE, conversionOptionsWithRequireJs()).startsWith(' ' + '''
function A() {
var gSobject = gs.inherit(gs.baseClass,'A');
gSobject.clazz = { name: 'A', simpleName: 'A'};
gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
gSobject.module = module;
if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};''')
converter.toJs(CLASS_WITH_REQUIRE_MODULE, conversionOptionsWithRequireJs()).contains('gSobject.module = module;')
converter.requireJsDependencies == [new RequireJsDependency(path: 'any/path', name: 'module')]
}
void 'ignore require.js modules (ast) converting normal'() {
expect:
converter.toJs(CLASS_WITH_REQUIRE_MODULE).startsWith('''function A() {
var gSobject = gs.inherit(gs.baseClass,'A');
gSobject.clazz = { name: 'A', simpleName: 'A'};
gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
gSobject.module = null;
if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};''')
converter.toJs(CLASS_WITH_REQUIRE_MODULE).contains('gSobject.module = null;')
converter.requireJsDependencies == []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RequireJsFileGeneratorSpec extends Specification {
require.generate(requireTemplate)

then:
1 * fileSolver.saveFile(destFileName, "define(function () {${LS}jsCode${LS}return A;${LS}});")
1 * fileSolver.saveFile(destFileName, "define(function () {${LS}jsCode${LS} return A;${LS}});")
}

void 'returns more than one class'() {
Expand All @@ -64,7 +64,7 @@ class RequireJsFileGeneratorSpec extends Specification {
require.generate(requireTemplate)

then:
1 * fileSolver.saveFile(destFileName, "define(function () {${LS}jsCode${LS}return {A:A,B:B};${LS}});")
1 * fileSolver.saveFile(destFileName, "define(function () {${LS}jsCode${LS} return {A:A,B:B};${LS}});")
}

private basicModuleResult = "define(function () {${LS}jsCode${LS}});"
Expand Down
5 changes: 0 additions & 5 deletions src/test/src/files/Require.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package files

import org.grooscript.asts.RequireJsModule

/**
* User: jorgefrancoleza
* Date: 23/05/15
*/
class Require {
@RequireJsModule(path = 'lib/data')
def data
def nombre
}

0 comments on commit c0bc19a

Please sign in to comment.