Search code examples
requirejsamd

How to concat AMD modules so they work in non-amd environment too?


I wrote a library with requirejs. Now I would like to build a distribute version of it where concat all files into a "mylib.dist.js" file. Problem: My library should also work in non-requirejs environments. jQuery for example goes this way too. Every file in the src directory is a amd-module. In the build-process the code will we restructured somehow. They have wrote an own build.js file for this purpose. Is there a library out there which can handle this for me out of the box? Or do I need to write my own build like jQuery Team?


Solution

  • You can use RequireJS' builder, r.js and combine you code with Almond to create a file that would externally behave like a library that is not made up of AMD modules.

    Almond is a small piece of code that is able to load AMD modules, but with some limitations explained in the documentation. (The limitations should not be an issue with what you are trying to do.) The process for doing exactly what you are trying to do is described in Almond's documentation, but in brief what you do is tell r.js to wrap your code in an IIFE and include Almond in the produced bundle.