ServerJS/Modules/GlobalFileLoading: Difference between revisions

no edit summary
(New page: The following is a simple system based on loading files by filename. This is quite Ruby-like. // // Evaluate the content of the fully specified file // in the global scope. //...)
 
No edit summary
Line 51: Line 51:


Remark: I think chdir() is not a good option here because if one module does chdir to include other modules which also do chdir(), then chdir() is required before every individual "load". Otherwise the current directory is basically unknown to any particular script. -- Peter Michaux
Remark: I think chdir() is not a good option here because if one module does chdir to include other modules which also do chdir(), then chdir() is required before every individual "load". Otherwise the current directory is basically unknown to any particular script. -- Peter Michaux
== sample code ==
// ==================================================================
// source code ------------------------------------------------------
//
// math.js
//
LIB.add = function() {
  var sum = arguments[0];
  for (var i=1; i<arguments.length; i++) {
    sum += arguments[i];
  }
  return sum;
};
//
// increment.js
//
require('math');
LIB.increment = function(val) {
  return LIB.add(val, 1);
};
//
// program.js
//
require('increment');
var a = 1;
LIB.inc(a); // 2
// ==================================================================
// browser code in development
//
// *Can* manually include all dependencies like the following.
// Other more complex techniques for manual dependency resolution
// can be used.
//
<script type="text/javascript">require=function(){}</script>
<script src="/js/src/math.js" type="text/javascript"></script>
<script src="/js/src/increment.js" type="text/javascript"></script>
<script src="/js/src/program.js" type="text/javascript"></script>
// ===================================================================
// browser code in production
//
// Can just concatenate the source files and perhaps minify them.
// Other more complex techniques can be used.
//
<script type="text/javascript">require=function(){}</script>
<script src="/js/lib.js" type="text/javascript"></script>
<script src="/js/program.js" type="text/javascript"></script>
39

edits