I have a requirejs project that has been optimized into a single file, which I am loading synchronously. The code loads as expected, but the modules are still loading asynchronous (they aren’t available until later in the loading process) and it’s causing problems for some legacy code I’m hoping it will work with.
Given the code below, is there any way of making main-build‘s methods available immediately after loading, before legacy_code.js is loaded?
<script type="text/javascript" src="/scripts/vendor/require.js"></script>
<script type="text/javascript" src="/scripts/main-build.js"></script>
<!-- At this point, the code is set up and it will be invoked later -->
<!-- ...but the next file requires to access methods in the modules.-->
<script type="text/javascript" src="/script/legacy_code.js"></script>
If worst comes to worst, I can write my own versions of define() and require() (which would also free up a lot of bandwidth since all it would need to do is catalogue and invoke the modules), but I’m hoping there’s a built-in way to do this.
Answering my own question here. After looking around a long while, I can’t see any way to load Requirejs code synchronously or invoke modules immediately after loading. So I ended up writing some code to temporarily overwrite
requireanddeclaremethods. It works for my use case, but may require changes if anyone else needs it. I’m sure it can be optimized more than it is right now, but it works.You can find the code on GitHub, which I will try to maintain.
The code at this point looks like this: