Sitemap
Kumar Harsh

Kumar Harsh

I’m a co-founder of Playlyfe, where we use gamification to solve serious business problems. A coder and web-designer, I love astronomy, football, games & food!

The Pain of Browserifying

2 min readOct 30, 2014

--

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:

  1. First include the aforementioned Codemirror mode file.
  2. 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” } }
    ```
  3. In your main file write this:
    ```
    require(‘../public/components/codemirror/mode/javascript/javascript.js’)
    require(‘angular-codemirror’)
  4. app = angular.module(‘myApp’, [ ‘ui.codemirror’])
    ```
  5. 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. (╯°□°)╯︵ ┻━┻

Now, on to watchify.

--

--

Kumar Harsh
Kumar Harsh

Written by Kumar Harsh

I’m a co-founder of Playlyfe, where we use gamification to solve serious business problems. A coder and web-designer, I love astronomy, football, games & food!