NOTE: This library is being Open-Sourced after more than 2 years of in house development
File Size: Just 4KB
(145 Lines of Code)
Develop or use common js (CJS) modules, bundle them in your favourite server language and require
them in browsers.
// file: modules/items/cheese.js
module.exports = {
name: 'Cheese',
// ...
};
// file: modules/recipe/index.js
var cheese = require('../items/cheese');
// ...
module.exports = function(){
return [
cheese,
// ...
];
}
<script src="bundled.js.php"></script>
<script>
var recipe = require('recipe/index');
console.log(recipe());
</script>
With the help of the server side language you are using, you will be creating an output similar to the following and serve it as a javascript file. Please have a look at examples
directory for more details.
// file:bundled.js.php
// Send Header Content-Type: application/javascript
// PRINT contents of submarine-bundler.js
// e.g, with PHP
// echo file_get_contents('submarine-bundler.js');
Submarine.register('items/cheese', function(require, module, exports){
// PRINT contents of file:modules/items/cheese.js
// e.g, with PHP
// echo file_get_contents('modules/items/cheese.js');
});
//...
Submarine.register('recipe/index', function(require, module, exports){
// PRINT contents of file:modules/recipe/index.js
// e.g, with PHP
// echo file_get_contents('modules/recipe/index.js');
});