Question

I want to learn Require.js and I was thinking about implementing it in an existing Django project. However, I'm already using the asset manager django-mediagenerator for bundling JavaScript files and CSS files in my project.

Since I'm not completely sure exactly what Require does (I know it loads scripts async, but that's it), I'm wondering if it would be completely useless to implement it in a project that already uses django-mediagenerator.

Any thoughts?

Was it helpful?

Solution

Summary: it's not one or the other: RequireJS and django-mediagenerator tackle different problems that overlap only in some respects.

RequireJS is designed mainly to modularize your JavaScript code so that when you design your project you can use a divide-and-conquer approach instead of gathering everything into one file, or spend your time hunting obscure errors because it so happens that two unrelated pieces of code living in different files decided that they'd record their state in a global variable named state. (Hours of fun!)

django-mediagenerator tackles the problem of gathering files together for serving them efficiently. It's a different problem.

Now, modularizing a project is helpful for design but it hurts performance in production. So RequireJS includes an optimizer that gathers your modules, and according to your instructions, produces one or more optimized bundles. Some of this operation overlaps with what django-mediagenerator does but I've not seen any indication on django-mediagenerator's web site that it can understand dependencies between modules designed to work with RequireJS. The RequireJS optimizer, on the other hand, understands these dependencies and needs to understand them to do its job, so you cannot just do away with it. I mean, you are not forced to use it, but if you do not, you have to come up with something that will replicate some of the intelligence in RequireJS' optimizer.

There are areas (like CSS optimization) where both tools could do the job. If you use both in the same project, you have to decide how to divide responsibilities.

OTHER TIPS

From the RequireJS documentation:

RequireJS takes a different approach to script loading than traditional <script> tags. While it can also run fast and optimize well, the primary goal is to encourage modular code.

That said, the difference is not in the performance or the number of requests, but in the way JavaScript is organized. If you are going to dive in RequireJS, I would go with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top