I'm trying to make an external library using Require.js. Thanks to Require.js not compiling single js file correctly and Require.js (almond.js) Timing Off I've figured out how to get everything to "compile" in to a single optimized/built file, and that single file works. There's just one problem: I can't figure out how to set a variable for my library.
Let's say I want my library to create window.Foo
. I tried using a main.js
file with:
window.Foo = require([], function() {
window.Foo = {someValue: 1};
return {someValue: 2};
});
and a wrapper end fragment of:
return require('main');
}));
As you can see, I tried to expose Foo to the global space both by explicitly setting window.Foo
from inside the require call, and setting it explicitly from outside via the return value of the end fragment. But neither one works; if I add a console.log(window.foo)
right after I load the built file, it tells me that window.Foo
is undefined.
If I do a window.setTimeout window.Foo
eventually does get set (to {someValue: 1}
), but I can't very well expect my users to have to wrap all their code with a timeout. Can anyone please explain how I can get window.Foo
to be defined as soon as my optimized/built file is loaded?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…