025 JSJ Require.js with James Burke
The panelists talk to James Burke about Require.js.
Special Guests:
James Burke
Show Notes
The panelists talk to James Burke about Require.js.
Special Guest: James Burke .
Transcript
TIM:
Iām in Windows today; oh, boy.
CHUCK:
Nooooo!
AJ:
Iād appreciate if you wouldnāt use that kind of language. [Laughter]
TIM:
Well you see, Iām trying to dog food Cloud9 and Linux is just too nice; so if Iām in Windows, Iāll actually use Cloud9.
[This episode is sponsored by ComponentOne, makers of Wijmo. If you need stunning UI elements or awesome graphs and charts, then go to Wijmo.com and check them out.]
[Hosting and bandwidth provided by the Blue Box Group. Check them out at bluebox.net.]
CHUCK:
Hey everybody and welcome to episode 25 of the JavaScript Jabber Show. This week on our panel, we have Jamison Dance.
JAMISON:
Hi guys!
CHUCK:
We also have Tim Caswellā¦ who is muted.
TIM:
Here I am!
CHUCK:
AJ OāNeal.
AJ:
Coming at you live from the ethosphere of Orem, Utah.
CHUCK:
Yeah, we can hear a little bit of interference in the ethosphere. We also have Jamesā¦ or Joe Eames. Sorry. I keep wanting to say āJames Edward Grayā. This is the wrong podcast for that. [Chuckles]
JOE:
Howdy? Coming at you live from the Mars rover.
CHUCK:
Oh, you are going to tweet from Mars?
JOE:
Thereās a 16-minute delay, FYI.
CHUCK:
Oh, is that all?
JAMISON:
ā¦guessing where the pauses are going to be, right?
CHUCK:
All right. Iām Charles Max Wood from devchat.tv and this week we have a special guess and that is James Burke.
JAMES:
Howdy? Coming live from Vancouver, BC.
CHUCK:
Awesome. I love Vancouver; Iāve been here a few times. Do you wanna introduce yourself really quick for the folks that donāt know who you are?
JAMES:
Sure. Iām James Burke; I work at Mozilla in the labs group and I mostly focus on open source web operated tooling. I originally joined Mozillaās part of Mozilla messaging group and I was doing an experiment around web-based messaging systems. And then before that, I worked at AOL and was a core Dojo contributor.
CHUCK:
Awesome. So when you look at RequireJS, it actually says that Dojo foundation; can you explain what that is?
JAMES:
Yes. Itās similar to the Apache foundation; itās just a legal entity thatās set up to hold code; mostly to deal with some of the legal stuff around managing code. And so, I really like it versus something like Apache, where if you were to be on the official Apache project, thereās a lot of stuff involved with that. Whereas the Dojo one is fairly light-weight; the individual projects decide how they wanna run. Itās just that you should get CLAs for larger code contributions and then use the foundation as the copyrightā¦ or whoever gives code contributions gives up the license to the foundation to use them. So itās mostly just for legal stuff.
CHUCK:
Nice. And what is the CLA?
JAMES:
Contributor License Agreement, so if youāve had to deal with any of these larger open source foundations, it normally asks that you sign things saying, āYes, Iām contributing on this stuff; itās my own work; Iām not taking it from someone else and Iām giving you permission to use it.ā So itās mostly to satisfy lawyers.
CHUCK:
Yeah. I think I remember when I talked to a lot of people at the Apache foundation; in fact Iām actually wearing my ApacheCon shirt from last year. And yeah, they had CLAs for I think both the developers and for the corporations they work for so the corporation wouldnāt try and claim the code as copyrighted under their umbrella.
JAMES:
Yeah and Dojo has similar thing too.
JAMISON:
You said itās to satisfy lawyers; I donāt think lawyers are ever satisfied.
JAMES:
Thatās true. I think that this is just aā¦
JAMISON:
They hardly feltā¦
JAMES:
Yeah. It hasnāt really been tested; I donāt think, so it could change if somebody ever try to test it.
CHUCK:
Yeah. Itās kind of like having garlic for vampires; itās not a guarantee, but it works most of the time.
JAMISON:
[Laughs] Or if it doesnāt work, you donāt hear about it.
CHUCK:
[Laughs] Oh, Iām sure somebody would hear about it. Anyway, so we are here to talk about RequireJS. Now, I keep hearing the terms āAMDā and āRequireJSā and sometimes it almost sounds like AMD is a library. Is there an AMD library or is RequireJS just an implementation of AMD, as kind of a concept?
JAMES:
Yeah. Itās an implementation of AMD, which is short of Asynchronous Module Definition; which is a really bad name, but thatās the original proposal had on it and it just sort of stuck. I would have preferred a different name, but naming is hard. So AMD, thereās an AMD implement group that discusses the API specification and then thereās a with the specification and thereās a set of compliance unit tests that you can run to test your order; and so, ā¦ and RequireJS is an implementation about API.
JAMISON:
Are they related to the CommonJS guys at all?
JAMES:
Yeah, a lot of it came out of talking with the CommonJS folks. It grew out of some of our experience in Dojo where we had an XHR eval-based loader and thereās enough sort of rough edges with that, that we felt like it will be nice to have this sort of the format which is what AMD is. And so, in trying to investigate how best to go about that, I triedā¦ because Dojo had an ā¦ order that was basically tgat sort of wrapped format that it would compile theā¦ not ācompileā but just convert the code; the regular Dojo module code into that format. So well, if we can just use that format directly, then thereās less translation layers, thereās less discontinuities when you wanna push code to CDNs. And then thereās some environments that donāt allow eval; I mean the Chrome extension system I think by default, they wonāt allow eval.
But at that time, when the CommonJS people started out a newer contributing module system, they already had something going when I started to interact with them. And Chris, also from Dojo, we talked in this. And so, we were trying to apply what we learned from Dojo and then we picked up the CommonJS concepts as far as like relative modules; the idea of a Require to do a module export. Thereās a lot of those ā¦ we try to match up on there, I think where we disagreed was the CommonJSā¦ it felt that it was not right to author in this sort of wrap format; the prefer to do builds and just treat the browser do delivery as a transport, which from our experience was not optimal. One of the things for Dojo is that you shouldnāt either compile stuff to develop web code. And I
think that should be the idea for standards group thatās trying to make the web better in the browser.
One of my ā¦ is I guess with CSS is that, to really make larger CSS, a lot of people are going to basically compile like the ā¦ just because CSS is lagging so far behind. But ideally, they should be gearing towards like, āHey, itās so easy to make in the browser, you just write some text files then you load it in the browser and it works.ā And so, that was one of our requirements in Dojo. And so, ā¦ we decided to take the pieces of the CommonJS stuff that were really neat; their relative references to modules, be one of the bigger ones and then just apply that system to something that we felt would work better with the browser.
JAMISON:
I just have to say I really love that idea. I think itās a great idea to just be able to hit refresh and see your code changes. It seems like such a small thing to haveā¦ I donāt know, to go type some command to recompile your code, but wears on you if you have to do it every time you make a change, so I think itās a great thing.
CHUCK:
Yeah, you see that in other server-side languages too where when you start up your web server, it loads the whole program into memory. And so thereās not a compile step, but you still have to restart the web server in order to load the code changes into memory. And yeah, itās the same deal; itās so much more convenient just to hit refresh and get the changes there.
JAMISON:
To some extent, you can get around via watchers. You can do that on server-side language too. But I know like Browserify has a file watcher thing and some other compiled like module systems things too. But yeah, itās a good thing over all.
JAMES:
Yeah and I think for me, itās always like stop gap measures. And I think it also increases the barrier like when ā¦ that level, you have toā¦ thereās assumptions now for example maybe what kind of servers that you use. And so somebody coming in to JavaScript, I think to reach a broader audience that doesnātā¦ the fewer sort of dependencies you have, the easier it gets to be adapted.
CHUCK:
Right. So what are the advantages of using an AMD system like Require over just like requiring the JavaScript libraries that you want?
JAMISON:
You mean just putting them in script tags?
CHUCK:
Yeah.
JAMISON:
Like in the HTML?
CHUCK:
Yeah, exactly.
JAMISON:
Oh, man so you have to like order your dependencies correctly in your HTML; which is weird. And you have to put everything in global scope to do that; which is also weird. And itās really hard to deal withā¦ it discourages making your code modular, I guess. You end up with lots of like monolithic, huge, gigantic global objects because if thereās a high barrier of entry to just adding another file, then you are not going to do it; you are just going to stick crap on the same file. Does that makes sense?
JAMES:
Yeah, agreed.
JAMISON:
So Iām just talking about the disadvantage of not doing it and someone should probably talk about the advantages of doing it.
JOE:
Well, one of the biggest frustrations can be my files arenāt in the right order and finding them in the right order; finding the right order for them can just be a huge pain in the butt.
JAMISON:
Yeah. It turns you into a human dependency manager; and thatās something computers should do; for you. And thatās what RequireJS does.
JOE:
So does anybody have any good reasons why itās more advantageous to not do it? Because I canāt think of any.
JAMISON:
What do you mean?
JOE:
To not require have a module definition.
JAMISON:
I think the only advantage is just like straight up least number of steps to get something working at all. So maybe if youāre teaching someone how to program or how to do like the basic minimal for example. But for actually production use, I wouldnāt wanna do JavaScript without some kind of modular system for the browser.
AJ:
And you also have to realize, JavaScript is a community of amateurs; just like PHP is. And so, like this new era of actually designing code rather than just throwing things on the floor is a different paradigm. So a lot of existing code is difficult to work with when you start doing modules because it screws things up. So itās like that ārefactor versus hack and fixā kind of thing. So for ā¦, its great but if youāve got something that is existing maybe it will do weird stuff.
JOE:
Yeah, thatās true. And thereās also the fact that thereās just a lot of friction in JavaScript when you are trying to put this in. You know, how many libraries out there that just donāt support RequireJS.
My fill in the blank here with your favorite library.
JAMISON:
So RequireJS does have some plugins (and James should probably the one to talk about this more) but there are plugins to wrap libraries to just expose global objects and make them into AMD modules. So you can just take like your own JavaScript files that stick stuff on window and I believe it will turn those into just exported modules, wont it?
JAMES:
Well, the config thatās in the RequireJS 2.0, it wonāt do any sort of wrapping itself; it will just be sure to load those legacy scripts in the order that you say. So you what you do is you specify the dependencies in the exports in the RequireJS config and you will load it and it will grab that global and then use that as sort of module export.
JAMISON:
So, it kind of makes it look like their modules, even if they are global, so you donāt have to worry about ordering issues? Is that what happens?
JAMES:
Sure. Yeah. And so the only downside with that approach is thatā¦ I mean there are global still exists but for most apps, thatās fine and then when you do sort of an optimization stuff, you donāt have the flexibility of sayā¦ if you have a shim library that depends on say, underscore; for that to work in the system, you need build an underscore into your combine file. But those are for most projects, those are perfectly fine which will allow you to use library systems that you prefer.
JAMISON:
Is there a better way to use libraries that arenāt like AMD modules or is that best way to just use the shim?
JAMES:
Ideally just ā¦ and then you do an export. But you know, you modify library code and that makes people uncomfortable ā and understandably so. Ideally thereās these UMD patterns, universal module patterns whereā¦ so that if you are a library author, then you can pull up your code so that it can act as a module or just do browser globals; so you get a nice resourcesā¦ developers would go to that that system that would make it easier too.
CHUCK:
So, how do you find out about the universal module definition?
JAMES:
Itās on GitHub; thereās umdjs/umd and thereās a sort of templates for that.
JAMISON:
Itās kind of just some boiler plate where you check basically for the existence of different loaders and if you donāt find any of them; you just stick it on the global pretty much, right?
JAMES:
Right. Or you can detect for different module API; so the common ones will be Node or CommonJS-style AMD-style or browser globals.
CHUCK:
Hmm. Looks like the last committer on that is JR Burke. [Chuckles] You know a little bit about this, huh?
JAMES:
Yeah. Well, I think ā¦ I think he started the group, which since I have experienced on these things, yeahā¦
CHUCK:
Right. That makes sense. So do you ever find or do youā¦ I donāt know, is there a major push out there for people to start writing their libraries in this way? Are there other advantages to the library authors to do it this way?
JAMES:
yeah. Well I think itās theā¦ it definitely makes it an easier for people that now for like MVC systems in particular, it makes a lot sense like once youāve goneā¦ you know what, Iām going partition code models to use controlā¦ thatās just natural to be different files, different modules. And once you start doing that, you are realize, āIf only I have a module system, this will be so much easier to be able to read the code better.ā
And I think as developers get in to that, you know, we do need to take some of these patterns; break this code apart, I think the most library, you would see in the value of having a module system. And so I think the only trick then is what do you think is worth supporting? And so, I think one thing to realize is that Ecmascript is looking at a module supportā¦ Itās not done yet, but the basic mechanics of it, as far as I have this dependency, Iām going to reference it with a string, itās going to give me an export value and Iām going to use that locally and Iām not going to depend it on globals; thatās the model going forward. So, I think the AMD fits into that nicely, it works today and it has the least number of gotchas across the development cycle; and of course Iām biased.
CHUCK:
Right. So does RequireJS, if you author a library in a module, does it name space it for you or does it just load it in to the global space anyway?
JAMES:
So it keeps the module in a space and RequireJS actually has this multiversion contact support so you can create different dots or buckets of modules. But technically, if you go on the RequireJS object, you can be like .s.contexts, youāll be able to find that thing but thatās only becauseā¦ but thatās sort of internal data in the system where I could properly hide like an es5 environment. You know, that itself wouldnāt have to be globally accessible; and most doesnāt access it directly. So I think the idea is by using strings for the modules and by making sure that your module doesnāt name itself, it allows you to load multiple versions of the same thing in same module space, or in RequireJS case, you can also create a different contexts. So for unit tests, this has been useful; you can have a separate sort of bucket that locks one or two dependencies and then runs some tests. And as long as you are not modifying global state like a document or changing the DOM, you will alwaysā¦
CHUCK:
Right. So how widely used is RequireJS? Do you really have a good handle on that?
JAMES:
Yeah. For me, I think itās important toā¦ if I focus on the right things and provide the right support and not worry so much about tracking, then things work out. So, thereās bigger projects likeā¦ I think once you get a sort of webmail class system, youāre gonna wanna module format things. I
think IDEs sort of like Adobe Brackets and then Cloud9, it allow use RequireJS on the front end.
And so Dojo itselfā¦ so as far as like AMD modules in general, if you Require a library author and say, āHey, should I worry about doing AMD or opting into calling that module API,ā Dojo has its own AMD loader; there are some other ones and ā¦ if I remember right, it can store files in local storage. So you can get a lot more options. So going to use RequireJS itself, I think AMD in general, it does feel like its growing and if I gauge by the number support questions I get, Iām getting more like sort of newbie questions that are basic JavaScript questions and so to me, thatās an indication that itās really starting to seep into that sort of mainstream developer thing.
CHUCK:
One other thing that Iāve been looking at RequireJS, you have like a project directory right on the webpage it shows it and then thereās a scripts folder; a lot of times, people have some kind of build process on their system, so basically, all other scripts are going to be compiled and minified into one file. So if thatās the case, so when you try and require something, is there a way to work around that?
JAMES:
I donāt think I quite followed that.
CHUCK:
So, letās say that I have like jQuery and jQueryUI and a bunch of other things that I need on my webpage and you know, I just wanna compile it all into one big file that has all the libraries included in it, can I still use RequireJS and if all of that stuff is required?
JAMES:
Yes. Thereās the RequireJS optimizer which is just r.js and itās goalā¦ so what it can do, it actually runs RequireJS either in Node or in Rhino. And then thereās been a request to have it run in the windows script host, which should be easy to do, I just have to create some modules, but what it does is it will trace your dependency tree, match it up find all the dependencies, combine them into one file. It has JS support, so if you have like has blocks and you will convert those so that when you do the optimization, those disappear.
JAMISON:
Whatās fjs?
JAMES:
Itās something sorted by Pete Higgins and itās similar to a Modernizr; itās just that itās a JavaScript API itās a for running a feature or capability tests. And so, yeah itās just a way for you to select differentā¦
JAMISON:
So it will actually optimize out parts of your code that arenāt available in the browser that youāre going to be running on it? What do you mean it has JS support?
JAMES:
You can tell that by build these set of scripts but for this has test like has array, say that thatās true and so then in your code, if you have like if not has array extras, do this work. What will happen after that is that each block will be aā¦
JAMISON:
Oh, okay. So you can kind of assume an environment in your build step?
JAMES:
Right.
JAMISON:
Okay. Thatās cool. I didnāt know that.
JAMES:
I like that idea as well. And I guess then the other stage of that is, āOkay, great Iāve optimized all of my code into a one js file you know, do I need to deliver Require.js which is a dynamic loader which is does workings because it allows you to load code through network. Well, I donāt need that anymore.ā And so thereās Almond, which is on GitHub, jrburke/almond; itās just an AMD API shim which is a lot smaller, its alike 1KB and that just provides the basic define require API, so that you can put that into your build script so that you donāt need to require JS on a few code.
CHUCK:
Nice.
JAMISON:
Soā¦ oh go ahead, Tim.
TIM:
Sorry. So as you guys know, Iāve been working on backend Node stuff for the last few years, but recently, Iāve been porting a lot of my libraries to the browser. And one thing thatās really spoiled me in Node is I can declare my dependencies in a json file and then give some of my code and then they can just NPM install and get their dependencies. And Iāve been looking for something like that in the browser and recently, Iāve found JamJS, which does that and then uses RequireJS to load all the modules. And so thatās what got me using RequireJS. And it kind of breaks so many rules because the JamJS system is essential repository and thereās only one jQuery there; thereās only one whatever. Itās kind of hard to like drop in Zepto instead. I mean, have you seen a lot of increase in use because of this or have you not really heard much about it?
JAMES:
Yeah. So it does seem like JamJS is getting some traction which is neat because it uses Require and thatās awesome. But Iāve worked on a thing called ā¦ and I think itās like ā¦. GitHub. And so the idea behind that is uses GitHub as the package repository and uses the diversion tags that are stored on GitHub for the git repo to find the zip file for that version and install it. But it will also then allow you to do things like if you say, āOkay, in my package json ā¦ dependency section, you can say for jQuery in this case, I use Zepto,ā and so, if the project that assumes that, if it doesnāt already have a jQuery installed it will go and use Zepto in there. And with the RequireJSā¦ by putting in the RequireJS config to say, when somebody asks for jQuery, load zepto or if somebody rename zepto to jQuery, then you know, you got some options to sort of swap in a different provider for that sort of API that you assume by the dependency name.
TIM:
So, you basically just declare some aliases?
JAMES:
Right. Yes.
TIM:
Thatās cool. I mean the main thing I want is I just want to be able to declare my dependencies and not include them in my repo. And I really like the semver versioning that Node has, but I mean, GitHub tags isnāt that far off either; you just donāt get all the magic from semver.
JAMES:
Well, I think you could still apply that semver stuff to a GitHub repo. I donāt support that now just because Iām lazy and a lot of times, projects are a lot shallower and semver in Node project because in our front end project, you canāt do the same thing with Node where it has this sort of nested Node modules installation for things that makeā¦ Itās a great way to avoid conflicts that you got sort of a very big file tree on disc. So anyway, I think you can apply the same semver stuff to a GitHub repo thing; itās just that you wanna say itās greater than or equal to these version numbers. People properly version their repos, which they should be, I think that can still be worked out.
TIM:
Okay, some seems to adapt logic to the client ā¦ from GitHub?
JAMES:
Right.
JAMISON:
So, would there be a way to write some kind of wrapper that would let you use RequireJS with NPM? Because my the biggest problem with Jam I think is just that itās not NPM; I guess itās another package manager and itās another different place to put JavaScript code and thereās tons of JavaScript code already in NPM and lots of it would work in the browser; some of it is just like utility code that doesnāt touch file system APIs or something. So it seems like it would be awesome you could use RequireJS but also have the ability to do dependency resolution and all the nice stuff that NPM gives you.
JAMES:
Yeah, so I think for me itās actually good that they are not the same because NPM assumes some things that are not true in browser space. So in particular, NPM is not good at saying, āHey, I just have this one JS file and dependency and I want to install that.ā And it wants to do these directoriesā¦ it doesnāt feel natural to me as a frontend developer to have these nestedā¦ these directory on disc; whereas I would put everything in like a scripts folder or a mid-folder. And then I think thereās also the assumption that the code in there works for Node. I think one of the folks, one of the difficulties they had was that sort of assumption try to piggy backing into NPM. It comes with some sort of Node assumptions already.
JAMISON:
So I mean, obviously you wouldnāt be able to use like the file system tools without doing tons of shimming and crazy stuff, but Iām just talking about as more of a code repository. Because there is code that doesnātā¦ I mean it was written for Node but itās just like a utility to parse date/time stamps or something just like, you know, random little things that are already there. So you are talking about the nested directory structures being bad becauseā¦ I mean, why would that be a bad thing to have a nested directory structure like that?
JAMES:
So, I think itās good to go to sort of a convention-based thing; it grows more out of my experience with RequireJS; it has very nice convention where you say, āHey, if it has a base ā¦ and then you should say ask for jQuery and just for jQuery.js inside of a base ā¦ā. So the browser, you only get when I/O lookup for the dependency and so I think itās the best if you also have a convention that doesnāt require sort of that configuration line for each dependency that you install. Because then you have these giant config block, which to me is a little bit more brutal; and you are delivering a lost of code that doesnāt need to be there if you just followā¦ I mean, you are delivering a code block that doesnāt need go to their client if you just follow a convention.
And so, what I would like to see, like if thereās couple things with NPM; thereās the registry and then thereās the tool that downloads the code from that registry. So maybe one option will be if you can use that registry but when it downloads, it knows, āHey, Iām not just going to put this in jQuery folder under like Node modules or something but Iām going to put it in a scripts folder and call it jQuery.js.ā And so that sort of natural ID to path usually works out; you know, that might be an option. so I think thatās the thing for me where I think Node was right to say, āOkay. You know what, weāre just going to work out a system that just works for Node; we are not going to consider browser concerns,ā and so theyāve worked out a great system for them. I think conversely, as folks on the browser side, we should make sure that we work on a system that works best for us and the code in a sort of layout we would expect. And then, hey, if thereās a way to match those things up later, great; but I wouldnāt want to start at first try ā¦ NPM generate in a browser-based project
TIM:
Right. I mean, I recently have been porting a lot of my Node libraries to the browser and the problem I had initially is that you have a lot of dependencies depending on which environment you are in. For example, there is jQuery NPM that runs in Node and it depends on JSDOM or something that provides a DOM; in the browser you have the DOM. Whereas, some of my libraries might depend on Nodeās even emitter which you donāt declare that dependency in your Node package. But if you are in the browser, you need something that provides that interface. So you have very different dependencies depending on where you are running; you canāt really use the same package.
AJ:
So, one of the pities there is that I donāt think there is a general desire in the community to standardize on a way to do that. So Iāve got pack manager and in that, I have a key in package.json called ābrowser dependencies.ā So if you list something in browser dependencies, you can list it there, you can also give it an alias, so you can have like events.Nnode and events.browser and alias both of them as events when you wanna put them in. But Iāve contacted a few of the other people that do some of the things like I think it was ender and one of the ones that Jamison had mentioned before. And they didnāt really want to standardize on some sort of key or some sort of system in doing that.
JAMES:
Yeah. So for me, I think this is the same thing with module systems too; itās better to code up something that works well on the brand that you are targeting and then if two other environments finds something that works together where they could ā¦, then I think it makes sense. But I think at this point, Node is fairly settled in its ways; probably NPM 2 where their main focus is just making sure that they can provide a great server infrastructure. So anything that makesā¦ where they would have to do code changes for the browser, I think they are just because less likely to do because they are ā¦ in either, and thatās totally fine because they built some good server stuff but yeah, I can see where it will be hard to find agreement.
CHUCK:
Yeah. So whatās kind of the future of RequireJS? I mean, have you put in all the features in that you want and you are just going to have it be the tool that it is or do you have other features that you wanna put in?
JAMES:
No. itās mostly a refinement. Thereās a loaded plugin support, so itās great for transpilers. So I think thereās still some rough edges in there when you start to get really complicated about the transpiler stuff. So like thereās a CoffeeScript plugin so that you can run CoffeeScript in these systems. But I think the dependency support in that pathway is not as strong as it could be and so Iāve got an idea on how to get that working.
And then, I also wanna use thisā¦ and I think, hopefully itās already been a trueā¦ as a sort of try out some of the Ecmascript stuff, so that I have these other loader plugin itās require-hm under jrburke on GitHub; where you can code in sort of the existing Ecmascript module spec and it will convert that into a JavaScript that runs in todayās browsers loaded via RequireJS. And so Iām hoping to use that sort of thing to help inform their process because there are some things to be worked at for that. And so yeah, what Iām hoping in the future is just about stability; refining how it executes and then hopefully informing something that will get in to theā¦ because at some point, it would be nice for me not to have to do RequireJS amd stuff.
CHUCK:
Right. So one other question I have since itās an open source technology, whatās the best way for people to get involved? I mean, do they need a CLA and then they are good or is there more to it than that?
JAMES:
No. Like the CLA is for sort of non-trivial patches; so I take a lot of like 1-2 line patches because if itās just like, oh yeah, I was being dumb and forgot to do something. And then thereās also helping out on the RequireJS manual, thatās always appreciated; because it is seems to be getting a lot more popular and so the traffic is pretty steady now so, more people that can help on that, I have more time to actually work on core issues. And so yeah, to get involved, I would say check out the progress list, help on questions and you can do pull requests and simple ones that you donāt require CLA; if you wanna refactor the plugin system, then that will be part of CLA.
CHUCK:
Awesome. And then the other question I have is since you are no longer working at AOL under the Dojo project, does that really affect the amount of time and things you have to put in to RequireJS?
JAMES:
Yeah. I think one of the drivers for me for stepping away for working on Dojo was Require itself seem to beā¦ it was getting enough steam and it needed enough of my attention that I really couldnāt do the other stuff. And so, very fortunately ā¦ in the Dojo community, he stepped up and heās converted their loader over to AMD and heās done a lot on some of the base loader and base system work done that I would have probably done. And so, right, I think itās just more about RequireJS and AMD stuff taking a lot more time; but itās great to see that the Dojo folks has been able to handle it themselves.
CHUCK:
Right.
JOE:
I have a question for people that are just getting started out, want to get into RequireJS, do you have a recommendation on where they should go, what the best resources are for learning and implementing RequireJS in your projects?
JAMES:
well Yeah, there is a getting started page on the RequireJS site. I feel like I need to rework that probably a little bit; itās probably a little old, but I think itās the best place to start. Thereās also now, depending on yourā¦ oh gosh, choice of other tools like Tim from ā¦, heās got a Backbone boiler plate that uses RequireJS, so if you are a backbone person thatās probably a nice way to hop into it. But yeah other than that, hopefully the Requirejs.org is a good way to start. And then thereās list you have questions.
CHUCK:
Awesome.
JAMISON:
I have a question; I was doing some templating stuff; it wasnāt with RequireJS but I remember that RequireJS has a text plugin, so it loads just text as a JavaScript module and I looked at it and, have you ever found something like in the back to your fridge and be like, āOh, this looks pretty good.ā And then you pull off the lid and like Cthulhu like pops out and these spores of like mold and fungus just like overwhelm you? Like Iām looking at the text plugin and it does like Java, I/O stuff in the browser; why do you need to do that? I obviously donāt understand all the complexities ofā¦ I mean, this is in use in production all the time so it definitely works and there are probably all kind of crazy edge cases.
JAMES:
Yeah, soā¦
JAMISON:
I think it uses Java to load textā¦
JAMES:
So that text plugin, so how the RequireJS optimizer works for loaded plugins is it actually runs the loader plugin in the build environment, because otherwise it wouldnāt be able to sort of resolve all the plugin resource ID. And so for that to work, since the optimizer runs in Rhino and in Node, it has code paths for those. So thereās a couple of options. If you didnāt like that you can always just write your own text plugin and when in runtime, you load that one while you are running the build, you point to this sort of single file one that does all of them. And then itās also common once you do the build, if you donāt need to do any dynamic loading with the plugin, you donāt need to include it. So thereās aā¦ you can do an exclude in R.js in the optimizer and then you can do a stub module for text things and so just write a single line things and yeah thereās text module but it doesnāt need to do anything.
JAMISON:
Okay. So that makes sense. Itās running in Rhino, so it builds this Java string to get run in Rhino?
JAMES:
Right. And so, why is that all in one file? Most people just like to deal with one file; as far as to say, you know, so I think thatās sort of trade off.
JAMISON:
I think I phrased my question poorly; I wasnāt trying to say, āOh, this is horrible code,ā as much as like is this really how you have to write RequireJS; like do you need to write some Java code in there.
JAMES:
Oh, yeah. No, no. That one, itās because itās doing the I/O access; so in Rhino, the way you get access to the i/o systems is through Java and Java is horrible just wanting to say, āI wanna load some textā¦ā
JAMISON:
Yeah. [Chuckles]
JAMES:
So yeah, Iām sure if you look at the Node pathway, itās a lot simpler. So yeah, that is just the cost of Java.
JAMISON:
Do lots of people use the optimizer on Rhino? I mean, with all the community plugins work, I mean, is it pretty much the standard for them to work it with Rhino and with Node or do people just kind of pick Node because itās easier if you know it?
JAMES:
I would recommend Node because just the build itself runs so much faster. But there are definitely people who they just canāt take on Node as a dependency.
JAMISON:
Sure.
JAMES:
ā¦particularly in the internet environments where they already have Javaā¦
JAMISON:
Yeah.
JAMES:
ā¦things build up and thatās one of the things thatās been interesting for me is once you work onā¦ if you worry about just the client stuff and not as in certain build tools or build environments, thereās these vast JavaScript communities that are not in Node. And like thereās the Visual Studio crowd, they have a new Git which is a package manager and they deliver some JavaScript code through there. They have their whole other ecosystem; and same with the Java folks. So I think itās just which sort of subculture you see getting used. But I definitely get regular on running on Rhino because one of the issues I just was that the closure compiler used optimizer to have failed but itās just how you construct the class path. Anyway so itās used. I would say though most people and I would encourage most people to use the Node ones because it will be faster and I think if you are a JavaScript person, being able to small server things that are JavaScript base, itās a great thing. But optimize runs a lot quicker in Node.
JAMISON:
You heard it first here; āNode is faster than the JVM.ā Weāre quoting you.
CHUCK:
[Evil laugh]
AJ:
Is there anything that isnāt faster than the JVM?
JAMISON:
Oh, no JVM is fast.
CHUCK:
Yeah. They keep optimizing it itās justā¦.
JAMISON:
JVM is super-fast.
AJ:
It seems like whenever I look at Java code, its 90,000% bloat and 2% what you need to do.
CHUCK:
Well thatās Java code, thatās not the JVM.
AJ:
I guess It canātā¦
JAMES:
Well I think in my use case in particular since Iām using Rhino, thereās extra translation layer going on. you know, Iām sure thatās part of the class. But yeah, Node is so much faster; at least for my use case.
TIM:
Well command line tools; with Rhino, you have to start up JVM every time you launch your CLI and thatās not fast.
JAMES:
Right. Agreed.
TIM:
So, I have one quick question and I mentioned this on Twitter a few weeks ago, one of the first things I noticed when I started using RequireJS is itās 80KB of code and the bulk of that can be minified out. Is there a place in the world for smaller AMD loader that like only supports like maybe one AMD format in just the mainstream browsers? I mean, how much of that 15K of minified code is used in the 90% case?
JAMES:
Yeah, soā¦ CHUCK:
Did you just ask if RequireJS is bloatware?
JAMISON:
[Laughs]
TIM:
No, I did not. I asked how much of it is handling the edge cases; because Iām pretty sure all of it is needed for some edge case.
CHUCK:
Right.
JAMES:
Yeah. And so thereās a couple things; so thereās of course thereās IE, which is always a challenge.
JAMISON:
Itās one giant edge case.
JAMES:
Yeah.
CHUCK:
[Laughs]
JAMES:
Unfortunately, itās a pretty large percentage of browser use. But itās for IE10, I think now itās good enough where I can strip some things out with that, but I try to make sure that it runs even on IE6 although the deal is supporting IE6 through IE9, itās the equivalent. As soon as I can cut IE9, then thatās what would eliminate some of that code. I have a little bit of extra code because I support the multiversion context stuff. And then I thinkā¦ but good portion of it is itās just hard to make a network loaded linker. So you know, you have these dependencies you are going to fetch, you got to wait for them to load, you wanna then wait for those dependencies to load and then tracing that back correctly.
But that said, you know, Iām open for pull requests. Oh you know, the other part of that is support some config options that other may not need. But yeah, so I think thatās the strength of the AMD ecosystem; if you donāt like RequireJS in particular, you are welcome to choose another one. I was just like starting with CurlJS; if you didnāt think that RequireJS was probably you know, the size that you wanted. So I guess gauge on the complexity cause, would be likeā¦. so for me I always measure minified gzip because gzip sort of works out to like the amount of entropy stuff thatās different in the file. So minified gzip RequireJS is probably close to 6K whereas Almond is 1K.
So what is the difference between those two; Almond doesnāt do dynamic loading, so thatās one of the bigger hits. If you have to do dynamic loading or you do synchronous loading, you reduce the size of the loader quite a bit. So I think thatās the where the bulk of the implementation cost comes from. And I think for me, itās also been aboutā¦ s once you get to that level of 1K versus 6K and minified gzipā¦ the reason that you are going to wanna use RequireJS.. well, two things; you think modular code is the way to go and you wanna get in to that pattern now; and the other thing is that once you have a module loader, itās much easier to have larger programs like load on demand as user clicks a button. And once you look at that aggregated cost of all that code loading, the loader does not factor in to the overall cost.
TIM:
Right. I mean, my issue is not the file size at all; I just take a 100 pixels up your JPEG header and there you go. I mean, whatās on the web page. My main concern is just the complexity of theā¦ the mental complexity. I noticed on the website that you support like 12 different signatures for defining a module between the permutations of Require and define and RequireJS and all the various options. Do you want to a port a certain format or just each one has its own strengths?
JAMES:
Yeah, so I think for the define format, so itās really nice for configuration to just be able to say define object literal, and so signatures are about making it easier so that the user typing cost is notā¦ you know, what they are focusing on. So thereās the object literal form or define and thereās theā¦ I just got 2 or 3 dependencies, I wanna load. So passing the array of dependencies and then a function that the factory function that gets called with that array, thatās pretty sweet and it matches to what a lot of web libraries today, because you will see a lot that have the immediately invoked function that has passing jQuery and some other things.
So then, but once you go past maybe 3 or 4 dependencies you wanna align the dependency name with a local variable name. So I think thatās where the simplified CommonJS wrapper where you just say define function require and inside there, you can say while a=require and a is ā¦, that meets the need of those developers. Because there are definitely people that want that alignment and I appreciate that alignment when you have more than 4 dependencies. And new modules should have more than 4 dependencies but there are going to be top level modules that tie a bunch of things together and those are naturally to access more than four. So I think itās all about meeting the developer convenience and hopefully informing them in the eventual module of things.
So yeah, and I think the other sort of complication too is when you do the simplified CommonJS format, or just define function require, in order for the loader to find the dependency, it converts that function to a string and then parses up the require calls. But once you minify the code, that gets hard to do. And so, you need to sort of compile down to just say, hereās the dependency rate, hereās the function and then you donāt have to depend on the functions after build. Because thereās also the possibility that someday a JavaScript engine doesnāt need to requireā¦ isnāt obligated to give you back a sort of source version of function to string; could just return like compiled code or something like that. The string compiled code. So in most situations, we wouldnāt be able to scan for dependency and so itās also sort of against that.
CHUCK:
All right. Cool. Well, we need to start wrapping up. I did email you and tell you about picks, right?
JAMES:
Yes, I listen to the show and Iām aware of that.
CHUCK:
Okay. Because Iāve sprung that as a surprise on a few people. And I know that we probably have a few more questions, what weāll do is weāll do the picks, weāll wrap the show up and then if you guys have some question you wanna ask James and he has time, then we will keep recording and weāll put it up as bonus content. Does that work for everybody?
JAMISON:
Sure. Yeah, if it works for James.
JAMES:
Yeah, works for me.
CHUCK:
All right. Cool. Well, Tim, why donāt you start us of with the picks?
TIM:
All right. So, the every time someone asked me what testing library I use in Node, I just say use Node ā¦ but I recently finally gave in and used Mocha. And itās kind of a balance between the ā¦. BDD stuff and still usable. Itās a fairly simple test runner. And I donāt know, I like it; itās fun. And you can use test library you want and you can use Node or you can go crazy and use something thatās based on ā¦ and JavaScript. And the one neat thing I discovered last night is you can combine that with JSCoverage on your code. So Mocha and JSCoverage are cool for doing your tests.
CHUCK:
Awesome. Jamison, what are your picks?
JAMISON:
All right. My first one is lots of you have probably seen it all around the internet but the Steve Yegge post, I think itās called The Magic Bus or something like that. Notes from the Mystery Machine Bus, where he talks about how thereās a software spectrum from conservative to liberal as far as what style of programing language and what style software development you like. And itās a Steve Yegge post, its super long, I think heās a good writer so I think itās interesting. Thereās a lot of stuff I agree with and a lot of stuff that I disagree with but itās a great read and it made me think about some of the choices that I make as far as language and the way I design programs. So, that was great.
Thereās another article; I think Iāve read this a few weeks ago but I just came across it again. It was actually a Ruby talk by one of the core Ruby developers whoās from Japan and itās all about garbage collection but at really high level. It delves a little bit into some basic C code and some basic Ruby code, but I think it will be pretty readable even if you are not super familiar with those languages. And especially in the beginning, itās just all metaphors that explain garbage collection really well. Itās also kind of cute because it has all these like Japanese cultural metaphors that he sticks in that the translator kind of makes notes about. So that was a good read.
And my last one isā¦ oh, weird, I didnāt plan this, but itās a Japanese movie called āHowlās Moving Castle.ā Itās a cartoon. I feel like Iāve watched all the famous like family entertainment that is in English and so I was just looking on IMDB and I found this movie that was rated super highly and I had dismissed it before out of prejudice because I am a doofus, but I sat down and watched with my wife and itās so good. Itās amazing. Christian Bale was one of the voice actors and he does his batman voice; itās pretty great.
CHUCK:
Awesome. All right, AJ, what are your picks?
AJ:
So, just spinning off of Jamison for a second, I think the name of the guy that makes the movie is Miazaki and heās got a whole bunch of movies.
JAMISON:
Yeah. Iāve watched a couple of those other ones; they are all pretty good.
AJ:
Yeah, they are all extremely weird, but good.
CHUCK:
Yeah. Spirited away, Ponyoā¦ Iām trying to thinkā¦ Kikiās Delivery Serviceā¦
JAMISON:
Itās so cool howā¦ I mean, itās not the normal western arc of like, normal intro and then the conflict builds up in the middle and then it ends with resolving the conflict; like itās a lot different style of plot and things happen that seemed really weird that they just kind of ignore all the time. Like I donāt know, itās very you can tell itās from a very different culture, but its great entertainment.
CHUCK:
Yeah.
AJ:
So anyway, a couple of things Iāll mention; I set up a 120-inch projector screen in my new living room. I switched apartments, so I watched a few movies on it and itās pretty awesome. So Iām going to have to give a big plus to āprojector movie nightsā as a pick. Also in aiding that, I would like to mention Make MKV, Handbreak and Requiem. MakeMKV, I think you only need if you are using windows or if you are trying to do a Blu-ray; but it does the decryption on DVDs and Blu-rayās, so that you can copy to your hard drive.
And then Handbreak on Mac and I think on Linux as well, allows you to just straight convert from your DVD drive to a file that you can use on your android phone or on your iPhone; whereas on Windows, I think you have to use MKV first. And it was a simple enough process that my sister figured it out. And then Requiem is a program that for your legally purchased content on iTunes, makes it so that you can use that content on any device of your choice. You have to be logged with the account that the stuff is purchased with so that you canāt pirate with it; which I think is good because I donāt think people should be pirating, but I do think that people should be exercising their rights and we shouldnāt let the media companies stick it to us.
CHUCK:
Right. Okay, cool. James, what are your picks?
JAMES:
So I think for web stuff, thereās x-tag which is done by David Walsh at Mozilla. Itās really neat; it ties into the web component stuff; be able to create your own tags ā¦ into code, JavaScript code. So just that whole area of web component is neat and the object stuff on the es-discuss list itās also on that. And then thereās the Firefox OS BtG. What I really like about that is Iām hoping that sets the stage for Apple and Google to just build in that ā¦ directly into the browser in that sort of install web apps themselves. Hopefully thatās ā¦ for mobile devices and then desktop devices. So it will be great to see that and then lastly. I just saw last night was Jiro Dreams of Sushi and that was just, yeah, it was just really neat and philosophical thing too. So, thatās it for me.
CHUCK:
Nice. Joe, what are your picks?
JOE:
All right, so my first pick is the book series The Lost Fleet by Jack Campbell. Itās a really awesome sci-fi war series of books and I donāt know; thereās like ten books in there something like that; itās way cool. My second pick is a card game, kind of a card game called āSpotItā; super awesome fun family game
CHUCK:
Thatās a fun one.
JOE:
Yeah, really awesome; played it a ton yesterday. Probably played 15 games over the course of half an hour with ten different people, different groups and really just had a blast. And then my last pick is Lets Code JavaScript Test Driven JavaScript by James Shore; which I think Iāve picked before, but I just really feel like I wanna pick it again. Itās James Shore basically just streaming himself writing an app from scratch, using JavaScript, using test driven development ā and itās really super awesome.
CHUCK:
Nice. All right. Well, I guess itās my turn. In the pre-show I mentioned that my car kind of blew up again. [Chuckles] And so Iām just going to pick some of the things that make life easier when you are working on your car; in this case the water pump on the new engine that we put in the car ceased up and when the pulleys donāt turn, the belt doesnāt turn and then eventually the belt just come off and thatās what happened. And thereās not driving things like your air conditioning or your power stirring or your alternator and so you donāt get very far and since the water pump isnāt turning it anymore, itās not cooling your engine, which bad things happen and could overheat your engine. And so, I locked out because and finally gave up right before I got home so I can pull in to the garage right as it was telling me it was overheating.
Anyway, if you are going to work on cars like that, a good set of wrenches is handy, but what you really want is an air compressor and an impact wrench. And if you have the air compressor and the air-powered impact wrench, then a good set of sockets is all you really need to get most of the work done. And beyond those, you know, a good hydraulic jack and some jack stands will get you the rest of the way. So anyway, Iām just going to throw those out.
One other pick that I wanna put out there (and it was something that Joe reminded me of when he mentioned SpotIT) another fun matching kind of game is Set and itās the same kind of thing; you are looking for different shapes and colors and patterns; and so they can all be the same or they can all be different in the shape, the color and the pattern. And anyway, so you are trying to match 3 cards and so when you pick up the cards and whoever is dealing will put more cards down and whoever gets the most cards win. And so itās a pretty fun game. Anyway weāll get the links to all that in the show notes and just wanna thank James again for coming on the show. It was great discussion.
JAMES:
Yeah, thanks for having me.
JAMISON:
I think RequireJS is really pushing the web forward, which is awesome.
CHUCK:
Yeah. Iām going to have to give it another serious look here. Anyway, weāll wrap this up. Weāll catch you all next week. Thanks for listening!
025 JSJ Require.js with James Burke
0:00
Playback Speed: