The Pain of Browserifying
This is just my first post on medium, and I’ve written this more as a rant… So, kindly excuse me if the post is not very easy to read…
Trying to use Browserify for the first time, and I must admit that for whatever reason it’s heralded as the great build-tool, the learning curve is incredibly steep.
I have a project which uses the following:
- Jquery
- AngularJS
- CodeMirror
- Angular-CodeMirror
- HighlightJS
- Angular-HighlightJS
It starts off simple, but very quickly devolves into a lot of hacks and head-scratching. Simple examples worked OK, but as soon as I jumped into the main project, wrote all the configurations (painstakingly), and setup the gulp tasks and ran it — Boom. Either browserify couldn’t build the thing, or if I do somehow happen to conjure up a configuration which didn’t break browserify, the browser didn’t load the modules properly.
So, finally I had to stash all the changes, and start from scratch… building up the project one library at a time, and slowly I could ensure that atleast a subset of the libraries ran perfectly (angular + jquery + highlightjs), but the codemirror libraries are a pain to browserify if you’re new to it.
Normally, you’d include the main CodeMirror, include the `mode` file (say `mode/javascript/javascript.js`) and the angular ui.codemirror module. Not in browserify world. Apparently, you don’t even need to include the main CodeMirror file. Just include the mode file, and browserify will automatically find the main CodeMirror file and bundle it for you.
Get Kumar Harsh’s stories in your inbox
Join Medium for free to get updates from this writer.
<del>But, the main kicker comes when integrating the ui.codemirror module from angular. It’s nigh-impossible it seems. I’ve not had any success till now, with 2 days gone. Perhaps, I’ll have to rewrite it.</del>
The solution to this problem is this:
- First include the aforementioned Codemirror mode file.
- Then in your browserify-shim config (in package.json), specify this shim for the ui.codemirror to work:
```
“browser”: { “angular-codemirror”: “./src/public/components/angular-ui-codemirror/ui-codemirror.js” }, “browserify-shim”: { “angular-codemirror”: { “depends”: [“./src/public/components/codemirror/lib/codemirror.js:CodeMirror”], “exports”: “angular.module(‘ui.codemirror’).name” } }
``` - In your main file write this:
```
require(‘../public/components/codemirror/mode/javascript/javascript.js’)
require(‘angular-codemirror’) - app = angular.module(‘myApp’, [ ‘ui.codemirror’])
``` - And you’re done!
Also, note that the `debowerify` transformer doesn’t work along with the `browserify-shim`. It took me about half a day, and half of my hairs to find this thing out. (╯°□°)╯︵ ┻━┻
If you’re new to Browserify, it’s going to be a painful ride in the start… So, here are some useful resources to help you along the way:
Now, on to watchify.









