Click here to Skip to main content
15,920,633 members
Articles / Programming Languages / Javascript

#javascript Require.js vs CommonJS / AMD modules and #angularjs

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Mar 2014CPOL2 min read 12.9K   2   6
#javascript Require.js vs CommonJS / AMD modules and #angularjs

I recently replied to the following twitter conversation about require.js vs browserify.


Angular.js and AMD

Recently, I tried to integrate some AMD modules into an angularjs application. I did not spent much time on it but I was under the impression that the integration was not straightforward.

On this twitter thread, @tbranyen pointed to me that using the angular dependency injection annotations, I can easily reuse my existing require.js modules. I must give it a try on my next project.

AMD vs CommonJS

First of all, I must say that I can understand the arguments from the CommonJS supporters, but the require.js configuration file gives me extra powers...

An awesome feature of requirejs is that you can create different execution context. This means that you can, for instance, load different versions of the same module into two different context executions. This can be very powerful.

I'm using this on my js-utils project to make it just a repository of modules without hard dependencies. This means that js-utils node_modules folder will have just a few references.

JavaScript
var requirejs = require('requirejs');

requirejs = requirejs.config({

    /// isolate this context
    context: 'context1',

    /// use local packages, located on node_modules
    nodeRequire: require,

    /// modules baseurl
    baseUrl: __dirname,

    /// the lookup paths
    paths: [ ... ]

});

I do not consider myself a fanboy of require.js because:

  • I know that its complexity demands a bigger learning curve that it's not always required.
  • I know that browserify is more easy and compatible with node modules.
  • I know that its syntax can be very verbose.
  • I know that r.js needs a lot of improvements.
  • I know that r.js source maps are almost useless (the variables and functions are already minified).

But I also know that:

  • I can create complex dependencies on require.js config file
  • I can create friendly alias on require.js config file
  • I can easily change version of the dependencies just by changing the configuration file
  • I can use different execution contexts

If I said anything wrong, please let me know because I really want to hear your thoughts on this matter!

@aetheon

Visit www.divhide.com for more information, contacts and news about Web Development.
See other blog posts at blog.divhide.com.

Divhide purpose is to follow the HTML5 movement and contribute with applications which prove the quality of technology.
Feel free to contact divhide.

This article was originally posted at http://blog.divhide.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionJS Including Engine Pin
Alexandru Lungu24-Mar-14 2:50
professionalAlexandru Lungu24-Mar-14 2:50 
Many years ago I came up with 2 solutions for the including problem:

Here (http://s.codeproject.com/Articles/17277/Challenge-Me-Javascript-Including-Engine-Part-II[^]) is a solution similar to C apps - with a header file and a body file - in header you write only what you want to include - headers are loaded first, then it is generated the order for the body files to be included.

The Second solution (http://s.codeproject.com/Articles/17278/Challenge-Me-Javascript-Including-Engine-Part-III[^]) doesn't use header file, just type $include("FileA.js", "Folder/FileB" /*more files*/); however it uses some tricks (like a frame, throwing and cathing an error to stop parsing the file after the $include, etc)

Of course if you have hundreds - thousands of files it will be more efficient as time to load (for Release) to use a tool to generate a smaller amount of files by combining some of them.

I haven't been working with java script for many years (so I don't know if they still work), but then, for debugging purposes the solution with header and body files was extremely useful to me.

QuestionRequireJS / AngularJS Pin
Kenneth Kasajian18-Mar-14 16:27
Kenneth Kasajian18-Mar-14 16:27 
AnswerRe: RequireJS / AngularJS Pin
aetheon18-Mar-14 22:38
aetheon18-Mar-14 22:38 
GeneralRe: RequireJS / AngularJS Pin
Kenneth Kasajian19-Mar-14 15:40
Kenneth Kasajian19-Mar-14 15:40 
GeneralRe: RequireJS / AngularJS Pin
aetheon19-Mar-14 22:12
aetheon19-Mar-14 22:12 
GeneralRe: RequireJS / AngularJS Pin
Kenneth Kasajian27-May-14 11:07
Kenneth Kasajian27-May-14 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.