Today, I've had a bit of a hard time figuring out how to properly shim Angular, AngularUI and their dependencies, jQuery and jQuery UI, for Browserify.
Admittedly, shiming is something I usually try to avoid by finding a CommonJS version of whatever library or framework I'm looking to use. With AngularUI, I didn't really have an option.
In this post, I'm going to show you what I did, to use AngularUI.Sortable within my Browserify-project. A very similiar process should work for using the entire AngularUI suite or different sub-modules.
Create a bower.json
with the following contents:
{"name": "my-project-name"}
Then, install everything we need:
bower install jquery#1.x jquery-ui-sortable angular angular-ui-sortable --save
First, we need to install browserify-shim
:
npm install browserify-shim --save
Now, open your package.json
and make sure it includes all of the following blocks of data.
We're telling Browserify to use the browserify-shim
transform that we just installed:
"browserify": {"transform": ["browserify-shim"]}
Next, we have to setup some aliases:
"browser": {"jquery": "./bower_components/jquery/dist/jquery.js","jquery-ui-sortable": "./bower_components/jquery-ui-sortable/jquery-ui-sortable.js","angular": "./bower_components/angular/angular.js","angular-ui-sortable": "./bower_components/angular-ui-sortable/sortable.js"}
And now we can specify the actual shim configuration for browserify-shim
:
"browserify-shim": {"jquery": {"exports": "$"},"jquery-ui-sortable": {"depends": "jquery","exports": null},"angular": {"exports": "angular","depends": "jquery"},"angular-ui-sortable": {"depends": ["angular", "jquery-ui-sortable"],"exports": "null"}}
Now, go to the part of your code where you want to use the angular-ui-sortable
module and make sure to require('angular-ui-sortable')
before specifying it as a dependency for your angular module.
This could look something like this:
var angular = require('angular')require('angular-ui-sortable')var app = angular.module('myApp', ['ui.sortable'])
Now you will want to bundle your scripts with Browserify. You might be using Browserify from the command line directly or a build system like Grunt or gulp.js.
If you want to use gulp, check out my video tutorial on using gulp with Browserify.
Follow these steps and you should be able to use AngularUI with Angular and Browserify without problems. If you have any questions, feel free to leave me a comment below and I'll try to help out.
Thanks for reading, see you soon! :)
Hi, I’m Max! I'm a fullstack JavaScript developer living in Berlin.
When I’m not working on one of my personal projects, writing blog posts or making YouTube videos, I help my clients bring their ideas to life as a freelance web developer.
If you need help on a project, please reach out and let's work together.
To stay updated with new blog posts, follow me on Twitter or subscribe to my RSS feed.