-
April 15th, 2010
Modulr: script concatination via CommonJS
Modulr is a script concatenation tool I’ve been playing with which pre-processes client-side scripts into a single script suitable for deployment. This accomplishes two valuable goals:
- lets you break your scripts out into discreet chunks for functionality
- improves performance by minimizing the number of HTTP requests
Example, assuming this hypothetical directory structure:
- canvas --- tools.js - color.js - lang --- enumerable.js - main.js
The content of main.js:
// Declare your dependencies and assign them to variables. var color = require('color'); var ctools = require('canvas/tools'); var enumerable = require('lang/enumerable'); // Use your imported scripts! var rgb = color.hex2rgb("#C00"); var myCanvas = ctools.attach("some-canvas-id"); enumerable.each(["stuff", "things", "other"], function(item, i) { // ... });As you can see, at the top of the file you simply require the scripts you’d like to use before using them. That’s it, done! Modulr takes care of fetching them and writing them into the file.
Modulr avoids global-scope pollution
Note that each require invocation assigns the result to a variable. The scripts which get concatenated via require are trapped within a closure, ensuring that your scripts do not pollute the global scope.
Modulr makes your code more portable
Another great thing about Modulr is that if follows the CommonJS API, allowing you to declare your script dependencies in plain old JavaScript. No config files or embedded tokens, just JavaScript. This has the added benefit of making your scripts more portable — as long as your environment supports the CommonJS API (node, narwhal, etc) your scripts will know how to resolve their dependencies.
Creating a CommonJS compatible module
In order for a script to work with the CommonJS API it must assign public properties to the exports object.
The content of color.js illustrates this:
// Define your functionality: function hex2rgb(hex) { /* ... */ } function rgb2hex(rgb) { /* ... */ } // Attach your API to the exports object: exports.hex2rgb = hex2rgb; exports.rgb2hex = rgb2hex;A working example
To get in and get your hands dirty, check out my working example up on Github. It’s very tiny, and attempts to get the core ideas through as simply as possible.
http://github.com/dandean/modulr-demo
Suggestions and corrections are always welcome.
-
Installing Node.js on a Dreamhost Shared Server
April 9th, 2010
First, get the latest Node.js source onto your server. I keep my git downloads at ~/downloads/git.
If it doesn’t already exist, create your download directories and cd into it:
$ mkdir ~/downloads/git $ cd ~/downloads/git
Then, pull down the latest Node.js source code from GitHub:
$ git clone git://github.com/ry/node.git
Since we’re on a Dreamhost shared server, we have to specify a personal directory for the node binaries to install to. This is because Dreamhost won’t allow us (for good reason) to install whatever we want into location which is shared by every other user on your server.
So, lets create our OWN /usr/local/bin directory:
$ mkdir ~/usr/local/bin
Now that we’ve got that, let’s get into the node Git repo and configure node to use our new custom bin directory:
$ ./configure --prefix=~/usr/local
If everything went well, make should handle the rest!
$ make > ... a whole bunch of compilation output ... $ make install > ... more output ... > Done!
Now, you need to do one more thing to make node visible to your own scripts: add the new node binaries to your PATH! To do this you need to add the following line to two files in your home directory: .bashrc and .bash_profile.
export PATH=$PATH:/home/[YOUR USER NAME]/usr/local/bin
You’re almost done! Just save both of those files, close your connection, reconnect, and check if you can run node! You should get something like this:
$ node --version > v0.1.33-221-gff56d63
Hot shit! Now go build some killer Node.js apps!