6
votes

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?Andreas Köberle
@AndreasKöberle I edited my question, hoping it clarifies things.phtrivier

2 Answers

1
votes

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/

0
votes

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.