6

I have a javascript project were most modules are using some third party libs as 'global' dependencies (in particular jquery and jquery plugins)

I would like to "clean" the project (to express all libraries as requirejs dependencies.), however this is in my case a large task (lots of files, lots of third-party libs).

How could I make the migration easier / quicker ?

  • Can I "fake" using amd dependencies by wrapping the third-party libs in modules that just load them (with the order! plugin ?)
  • Is it safe to mix modules that load third-party libs as modulesand modules that directly use the global ?
  • If I wanted to automate things, are there any tools could I use to 'parse' a requirejs module to tell me if a particular symbol is used ?

Edit : What I mean by my last question is "Would it be possible to automatically rewrite my js files so that hey explicitely import dependencies instead of relying on browser globals ?"

2
  • Can you explain your last question. For what reason do you wanna parse the modules? Commented Sep 7, 2012 at 6:32
  • @AndreasKöberle I edited my question, hoping it clarifies things. Commented Sep 7, 2012 at 7:12

2 Answers 2

1

Can I "fake" using amd dependencies by wrapping the third-party libs in modules that just load them

Yes you can, RequireJS has a shim config that is designed just for that.

Take a look at this article it will help you organize your JavaScript code with RequireJS http://www.stefanprodan.eu/2012/09/intro-requirejs-for-asp-net-mvc/

Sign up to request clarification or add additional context in comments.

1 Comment

From what I understood, the 'shim' option was there to set up the dependencies between third party libs (the canonical example being Backbone, that needs jquery to be loaded), without using the 'order!'plugin. I would like to use shim in the end, but for the moment I can not, since I can not express that my components depends on a lib (I would have no define(["backbone"], function(Backbone), for example). I'm still using 1.0 with the 'order!' plugin, but I'll like to fix that.
0

I had a similar question about the need of wrapping third party code in AMD modules. My conclusion was, that there are no benefits in my case (large Backbone app). So you should ask yourself if you really need to import jquery for example per AMD. This will end in modules where you import jquery every time, which is a lot of error prone boilerplate code.

So in short it makes no sense to use AMD for code that you will use in any case.

1 Comment

Not true. Including jQuery into every definition of a module is useful, especially when it comes to unit testing. Also, if you are using Backbone, the only files you should be using jQuery are the views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.