rwalter
rwalter

Reputation: 891

RequireJS shim being ignored in simple example

As I understand it, you can use a shim with Requirejs to dynamically load scripts that are not defined as modules in their own files. So why can I not get a simple script like this to work?

Here's my non-working example:

/libs/test.js:

var a = 'Hello from TestJS';

main.js

require.config({
    shim: {
        './libs/test': { exports: 'test'}
    }
}

Then trying this in the console:

require(['./libs/test'], function(t) { console.log(t); })

Which produces undefined.

So how do I get hold of a?

I can see from the network tab that test.js is loaded from the server. I have a feeling my shim config is being ignored and requirejs is just loading it without the shim. Hence if I just do console.log(window.a), I see 'Hello from TestJS'.

Upvotes: 0

Views: 217

Answers (1)

Louis
Louis

Reputation: 151380

With the code you show for test.js, you have to specify that the exported symbol is a, not test because the latter is never defined in test.js:

shim: {
    './libs/test': { exports: 'a'}
}

Upvotes: 2

Related Questions