Note that there are some explanatory texts on larger screens.

plurals
  1. POConfigure RequireJS to not reload scripts that were already included on page
    text
    copied!<p>I am looking at integrating RequireJS into an existing web application which does not use AMD. The page I will be including my scripts on will already include a number of scripts, including jQuery and some common in-house scripts.</p> <p>I was curious what would happen if I loaded some of these scripts with Require after they had already been loaded in a traditional <code>&lt;script&gt;</code> tag. Here is what I tried:</p> <p>index.html</p> <pre><code>&lt;script src="js/lib/jquery.js"&gt;&lt;/script&gt; &lt;script src="js/lib/require.js" data-main="js/init.js"&gt;&lt;/script&gt; </code></pre> <p>init.js</p> <pre><code>require.config({ paths: { jquery: "js/lib/jquery" }); require(["jquery"], function($) { //Do stuff }); </code></pre> <p>My hope was that Require would be smart enough to see that <code>js/lib/jquery.js</code> was already loaded on the page, and use it as a module, rather than reloading the script. I then inspected the page contents and found that Require had in fact reloaded the script:</p> <pre><code>&lt;script type="text/javascript" src="/js/libs/jquery.js"&gt;&lt;/script&gt; ... &lt;script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="jquery" src="/js/libs/jquery.js"&gt;&lt;/script&gt; </code></pre> <p>I understand that this is probably a bit of a reach to expect Require to be smart enough to do what I wish it would, but it would be really handy for devs integrating Require with an existing site if it worked this way. That way you could move legacy scripts into AMD modules at your leisure, without having to even change your require code. Maybe I'm just dreaming, though. :)</p> <p>Anyone have any ideas how to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload