After linking
river.jsin html,there are two Global functions will be occupied, one isdefinethe other ismain.
define function is for registting modules and waiting for invoked.
1 //module A
2 define('module.A',function(exports,require,module){
3 //exports module.A.sing Api
4 exports.sing = function(song){
5 console.log(song + 'is playing');
6 }
7 });
you also can use
returnto exports api
1 //module A
2 define('module.A',function(exports,require,module){
3 //exports module.A.sing Api
4 return {
5 sing : function(song){
6 console.log(song + 'is playing');
7 }
8 }
9 });
1 //module B
2 define('module.B',function(exports,require,module){
3 //get module.A
4 var A = require('module.A');
5 //call sing api
6 A.sing('Heal the world');
7 });
you also can use
this.needto handle dependence
1 //module A
2 define('module.A',function(exports,require,module){
3 //get module.A
4 var A = this.need('module.A');
5 //call sing api
6 A.sing('Heal the world');
7 });
main function is for registting a anonymous modules and will be executed immediately.
It's the entrance of your programme ,this is influenced by other language's main keyword.
Most of time you don't need to use
mainfunction.you can simplly usescopemarkup inhtmlinsteadof it for executing modules. as It's easy to bind dom and data together.
1 //this anonymous module will be executed immediately
2 main(function(exports,require,module){
3 //run module.B
4 require('module.B');
5 });
returnandthis.needstyle also can be used in main function
person.js wrote like below : 1 //Module dependence
2 var a = require('a')
3 , b = require('b');
4
5 //property
6 var name='peter';
7 exports.getName = function(){
8 return name;
9 }
10 exports.setName = function(value){
11 name = value;
12 }
riverjs build . we will get the below folder structure: . .
└── person.js => ├── build
│ ├── app.js
│ ├── river.js
│ └── river.min.js
├── person.js
└── river.json
<script src="build/river.js">
<script src="build/app.js">
riverjs build tool will recursion the target foler to merge
*.jsintobuild/app.js, the module name is based on the folder structure you definded,for example if you have a module A with 5 level folder structurea/b/c/d/e/moduleA, then you canrequire('a.b.c.d.e.moduleA')to call it in any modules. another thing is that you can minify thebuild/app.jsor add sourceMap support by changingriver.json
{
"version": "1.0.82",//current riverjs version
"dist": "./build", //default dist folder
"alias": {
"river.grammer": "grammar"
},
"sourcemap": false, //true, build with sourceMap
"minify": false //true, build with minified
}