Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "disable" require.js at runtime?

My team and I have an AngularJS app that does not use RequireJS but is loaded within the context of an existing site which uses RequireJS. During the build process I am minifying/concatenating all our scripts into a single JS file which includes certain vendor libs like Lodash, Moment, etc.

Running the site isolated from the parent is fine, but within the context of the parent application I get the well-documented error:

Uncaught Error: Mismatched anonymous define() module: function () { return _; }

I don't have control of the parent application, is there a way at runtime to "disable" RequireJS or configure it to ignore my scripts?

The current solution we have is to modify the vendor libs and remove the AMD code, but this is undesired because we use Bower (with grunt-bower-install) and would like to just update dependencies automatically rather than rely on manual processes and custom hacks.

like image 452
Terry Avatar asked Jul 14 '14 21:07

Terry


People also ask

What is RequireJS used for?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

What is module in RequireJS?

A module in RequireJS is a scoped object and is not available in the global namespace. Hence, global namespace will not be polluted. RequireJS syntax allows to load modules faster without worrying about keeping track of the order of dependencies. You can load multiple versions of the same module in the same page.


1 Answers

An approach that could be considered if you are confident you have all the relevant script dependencies under control is to hide the define function at the start of your minified/concatenated scripts.

For example, do something like the following at the start of the minified scripts:

var __origDefine = define;
define = null;

And at the end of the minified scripts:

define = __origDefine;
like image 83
chrisg Avatar answered Nov 01 '22 23:11

chrisg