Search code examples
pythondjangorequirejsbundling-and-minificationdjango-mediagenerator

Is Require.js unneccesary when using django-mediagenerator


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?


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.