While working on Careplane recently, I ran into a problem that was made easier by JSONPath. I was already enjoying the ability to test my JavaScript with Node.js, Jasmine, and other npm packages. It would be nice if I could get the same package system I have with npm and use it with my client-side JavaScript.
With browserify, now I can!
There are three major benefits to using browserify, which I’ll detail below.
Using npm Modules in Your Client-Side Scripts
In Careplane, I wanted to use the JSONPath npm package. With browserify, I can write the following into my client-side code:
1 2 3 4 5 6 |
|
From the command-line, I can create a single JavaScript file (I’ll call it application.js) that contains my code and includes the JSONPath package. I can even bake jQuery into application.js!
1 2 3 4 |
|
Now that I have a nicely wrapped application.js, I can include it from a web page, or in this case, the browser plugin I’m developing.
Here’s an example of use from a web page:
1
|
|
N.B. Not all npm packages will work client-side, especially ones that depend on core Node.js modules like fs
or http
.
Organize Your Code
Careplane has a ton of JavaScript split into a separate file for each class. Previously, I was using simple concatenation and a hand-crafted file list to construct an application.js and to determine script load order. Now it can be managed automatically by browserify because each of my class files can require other class files.
For instance, the Kayak class in src/drivers/Kayak.js
depends on the Driver class in src/Driver.js
. I can now do this:
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 |
|
1
|
|
Super simple!
Uniformity With Testing Environment
I like to run my Jasmine JavaScript tests from the command-line and so does our Continuous Integration system. With jasmine-node, I had to maintain a list of files in my src directory that were loaded in a certain order in order to run my tests. Now, each spec file can use Node.js’ CommonJS require
statement to require files from my src directory, and all dependencies are automatically managed.
1 2 3 4 5 6 |
|
1 2 3 |
|
When I run my specs from the command-line with Node.js (I have an examples
rake task that runs node and jasmine), Node’s built-in CommonJS require
is used and it recognizes the require
statements I wrote into src/drivers/Kayak.js.
I can also run my Jasmine specs from the standard Jasmine web server. All I have to do is create a browserified script that is included before Jasmine runs its tests in my browser.
1 2 |
|
1 2 3 |
|
1 2 3 4 5 6 7 |
|
When I visit http://localhost:8888 in my browser, my Jasmine tests all run!
The only drawback is that my code has to be re-browserified whenever it changes. This only adds about a second of overhead while I wait for my watchr script to run the browserification.
More
If you’d like to take a look at the environment I set up for Careplane, you can check out the source.
I hope you enjoy browserify as much as I have!