Shepherd is a polyfill to manage Javascript modules and dependencies with an ECMAScript:Harmony-compliant syntax.
Stop wrapping your code with functions to build a module, your files are your modules. Period.
Shepherd allows you to take advantage of the features of the upcoming ECMAScript:Harmony within today's engines.
var _min = 0; var _max = 1000; var _lte = function (a, b) { return a <= b;}; var setMin = function (newMin) { if (_lte(newMin, max)) { min = newMin; } }; var setMax = function (newMax) { if(_lte(min, newMax)) { max = newMax; } }; var random = function () { return Math.round(Math.random() * (max - min)) + min; };
//s6d module randomRange { export setMin; export setMax; export random; } //-s6d var _min = 0; var _max = 1000; var _lte = function (a, b) { return a <= b;}; var setMin = function (newMin) { if (_lte(newMin, max)) { min = newMin; } }; var setMax = function (newMax) { if(_lte(min, newMax)) { max = newMax; } }; var random = function () { return Math.round(Math.random() * (max - min)) + min; };
Just include the following line to your header:
<script type="text/javascript" src="lib/shepherd.dev.js"></script>
Then include all the files you want Shepherd to handle like the following:
<script type="harmony" data-src="path/to/my/file.js"></script>
An in-depth documentation on the syntax will very soon (I promise) be made available.
Once you're ready for production, Shepherd comes with an optimizer that will concatenate all your Javascript files into a single file. Obviously, as bandwidth is precious, the generated file is minified with UglifyJS.
//s6d module myModule at 'path/to/my/awesome/module.js' //-s6d console.log(myModule); // Displays myModule to the console
//s6d module myNamedModule { export doSomething; } //-s6d var doSomething = function () {/* ... */};Using the named module (myApp.js):
//s6d module myNamedModule; //-s6d // The above declaration specifies we want to use the module 'myNamedModule' myNamedModule.doSomething();
//s6d export myExportedVar; export myExportedFn; //-s6d var a = 'This variable is private'; var b = function () { return 'This function is private too'; }; var myExportedVar = 'This variable is publicly available'; var myExportedFn = function () { return 'This function is publicly available'; }File2.js : Importing a portion of the module
//s6d import myExportedVar from 'File1.js'; //-s6d console.log(a); //undefined console.log(b); //undefined console.log(myExportedFn); //undefined console.log(myExportedVar); //"This variable is publicly available"File2a.js : Importing the whole module
//s6d module 'File1.js'; //-s6d console.log(a); //undefined console.log(b); //undefined console.log(myExportedFn()); //"This function is publicly available" console.log(myExportedVar); //"This variable is publicly available"
//s6d module myModule { import imp1 from '/path/to/import.js'; import imp2 from namedImportedModule; module mod1 from '/path/to/module.js'; module mod2 from myImportedModule; export expA; export expB; } //-s6d /** Here after goes the code of your module **/ var expA = "This is an exported string"; var expB = ['This', 'is', 'an', 'exported', 'array'];
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>If you want to remove jQuery from the global space and make it available as a module in Shepherd, just add the following in your header:
<script type="text/shepherd-js"> { modularize: '$', noGlobal: ['jQuery', '$'] } </script>Using jQuery in a module (myModule.js):
//s6d module $ from jQuery; //-s6d $.ajax(/*...*/);
require('../../build/shepherd.server.js').call(this, 'app.js');(where app.js is the main entry point of your app)
Of course, you can leave me a message here at GitHub or on Twitter @xcambar.
And if you want to send me an E-mail, surely you will find my address with ease.
Encapsulation through modules is a key factor to efficient/beautiful Javascript code.
Both AMD and CommonJS have proven very efficient, but they are hardly compatible.
ECMA made a proposal for the future versions of Javascript for native modules.
⇒ Waiting for the day the proposal will be integrated into the runtimes, a library allows you to use them: Shepherd.