cancel
Showing results for 
Search instead for 
Did you mean: 

General Discussions

Add non-AMD modules before AMD modules

Hi

I'm using requirejs for a project and I have 2 modules:

  • a.js: is a non-AMD module that I cannot touch its code
  • b.js: is a AMD module that I have written with define() function. It requires a.js to work.
  • app.js: is the actual application code that uses both a.js and b.js.

app.js looks like this:

//source code for app.js
require(['a.js', 'b.js'],
function( a, b ) {
    a.x = 2;//this will fail because 'a' is not defined
});

Now the question is: what is the simplest way to require() both modules in app.js? I can't do it like:

//source code for app.js
require(['b.js', 'a.js'],
function( b ) {
    a.x = 2;//it works because module 'a' defines a global variable named 'a'
    b.x = 2;//this will fail because module 'b' is loaded before 'a' so it doesn't work
});
0 Likes
4 Replies

This is standard fare for AMD loaders. Most of the time, a shim will not be needed. Your source for app.js is correct but you didn't show your source for b.js. Since you have control over b.js, it should be written as an AMD module with a dependency on a.js

// source code for b.js
// You have to use define, not require, and the dependency on a.js must be here rather than via a nested or internal require
define(['a.js'], function(a){
    // your code for b.js
});

This will ensure that a.js is loaded before b.js when app.js is ready to execute.

0 Likes

Should this thread be moved to AMD Forums Developer?

@dipak 

0 Likes

Can you clarify what exactly you are talking about?

Does this have to do with something like OpenCL or OpenGl ?

What exactly do you mean by "AMD Modules"?  Is this related to AMD hardware or software or something else?

@VictorJohnsonandroni 

@JamesZimmerman 

0 Likes

Maybe you can help this other User that just opened  a RequireJS thread at AMD Forums: https://community.amd.com/t5/general-discussions/compile-requirejs-to-remove-amd-dependency/m-p/5237...

0 Likes