Stitch your JavaScript files as CommonJS for the browser
This is a port of @sstephenson's Stitch npm module.
Suppose you have two JavaScript files, under app/webroot/js/
named a.js
and b.js
.
Content of a.js
:
var A = 'I am A';
module.exports = A;
Now in b.js
, we can require()
A by doing this:
var A = require('A');
var B = 'I am B, btw';
var C = require('another_folder_in_webroot_js/c');
module.exports = B; // exporting is optional
Compatible with CakePHP 2.x
Clone/Download this repository to /app/Plugin/Stitch
, and load the plugin from /app/Config/bootstrap.php
:
CakePlugin::load('Stitch', array('bootstrap' => true));
See configuration values in app/Config/Stitch/Config/bootstrap.php
file.
There would be cases when you have multiple paths, and end up having two or more same module IDs. In those cases, you can consider prefixing your module IDs.
For example, your paths include /app/webroot/js/
and /app/Plugin/MyPlugin/webroot/js
, and they both have foo.js
under their respective js directories. Then you can prefix your plugin's path like this:
Configure::write('Stitch.paths', array(
'/app/webroot/js/',
'/app/Plugin/MyPlugin/webroot/js/' => array(
'prefix' => 'my_plugin',
),
));
Now in the browser, you can require() the files separately:
require('foo');
require('my_plugin/foo');
At the moment, Stitch can perform the compiling via shell based on the configuration found in bootstrap:
$ ./Console/cake Stitch.stitch run
The shell basically runs the compiler found in a separate Stitch class which is available as a library, and you are free to implement its functionality in various ways. For example, a new StitchHelper utilizing this library eliminating the need of running it via shell.
See unit tests for understanding more on how the library works.
Stitch was written for handling JavaScript files only, files that end with .js
as extensions. But you can extend it for supporting various extensions, like .underscore
for Underscore.js templates and .coffee
for CoffeeScript files. All you need to do is add new classes under /app/Plugin/Stitch/Lib/Compiler/
and it will automatically map them based of file extensions.
Compiler class names begin with StitchCompiler and the suffix is the camelized version of file extension.