RequireJS: optimize scripts and include RequireJS in it

I have a complex JS project written in AMD format. It uses RequireJS.

My system should be able to include it in various external sites. I want to compile it into a single js file and include it like this:

<script type="text/javascript" src="http://mysystem.com/core-build.js"></script> 

I don’t know if the external site requires requirejs or not, so it seems that requirejs itself should be included in core-build.js. The compiled file must be completely independent of the environment.

On the other hand, it should not redefine the external environment of the site. My system uses jquery, knockout, and these libraries should be defined locally only for my system purposes. They should not affect the global site namespace.

So my question is how to optimize scripts for a single file, include requirejs in it, and migrate everything to a local namespace?

+4
source share
1 answer

You can use the Grunt concat task :

 concat: { dist: { src: [ "www/assets/js/libs/require.js", "dist/debug/app.js" ], dest: "www/app_build.js", separator: ";" } } 

You can see an example of installing the application on the Backbone Boilerplate (even if you are not using Backbone).

+3
source

All Articles