Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define can't be called with only 1 argument #32

Closed
damiengenet opened this issue Jul 12, 2012 · 9 comments
Closed

define can't be called with only 1 argument #32

damiengenet opened this issue Jul 12, 2012 · 9 comments

Comments

@damiengenet
Copy link

AMD specifications (https://github.com/amdjs/amdjs-api/wiki/AMD) states that the define function can be called with 1, 2 or 3 parameters, almond only supports 2 or 3 arguments.

@jrburke
Copy link
Member

jrburke commented Jul 12, 2012

Almond is for built code, where modules are combined together. In that scenario, modules need to be named, which implies at least two arguments.

Put another way -- in what way would supporting an anonymous module be useful to do in almond? If it does not have a name, how could another module refer to it? If another module cannot refer to it, then why is it included in a built file?

@damiengenet
Copy link
Author

es5-shim (https://github.com/kriskowal/es5-shim/) is using that technique because it doesn't provide any new namespace on its own, it only overloads missing standard js api without any dependencies. That's the only case I can think of atm though.

In any case, even if it might be abused, being able to have only 1 argument seems to be a mandatory part of the AMD standard. Not supporting it will most certainly cause incompatibilities problems, as already experienced with es5-shim.

@jrburke
Copy link
Member

jrburke commented Jul 12, 2012

Ah, the expectation when using almond (the second bullet point under Restrictions mentions that it should be used only with named modules. Normally the AMD-aware build tool, like the requirejs optimizer, takes care of that.

Modules in source form should not have IDs in them, to allow the most portability, as the es5-shim does. However, when it comes time to combine modules together into a built file, which is when almond would be used, it is expected that the tool building the file that contains multiple modules will inject names into the define() calls at that time. Otherwise, there is no way for the loader to know how to satisfy the dependency if a module does a require('es5-shim'), and the factory function for es5-shim will never get run.

So it does not matter if es5-shim does or does not export a module value, it is just that the loader will not know what functions to call to execute module code without names, when in a built file.

If you think there is a way that can be made clearer in the README, I'm open to clarifying it. The main thing though is to use an optimization tool that knows how to insert names for anonymous modules when combining modules together. Built files with a combination of modules is when almond would be used.

@damiengenet
Copy link
Author

Thank you for your answer. Other requirements were perfectly clear but indeed I missed this one.

@nchilada
Copy link

nchilada commented Sep 2, 2013

Can I propose that define(callback) and define(deps, callback) at least fail gracefully?

Let's say we optimize all our JavaScript into one file, a.js. But our page has to be loaded with external libraries like b.js, which sees that define exists and calls it with only one argument. I understand we can't expect almond to load this library, but it would be preferable to simply bail out of define rather than misinterpret the arguments and crash.

@jrburke
Copy link
Member

jrburke commented Sep 6, 2013

I would not want to silently fail. That will just make the problem harder to find. What I would consider is throwing an error with a specific message about the likely cause. I was hoping to avoid that as it increases the size of the library and is not needed for properly optimized code.

@jrburke
Copy link
Member

jrburke commented Sep 6, 2013

The other option too is that you can fork the code and provide your own version of the AMD API provider that you prefer. For these smaller AMD providers I encourage that as it allows fine grained control over the type of provider you want.

@nchilada
Copy link

I would not want to silently fail. That will just make the problem harder to find.

My reasoning was that if the call is expected and fails, a little time with the developer tools would easily identify the issue. But yes, it would make the common case a little harder and an uncommon case a lot easier.

@nchilada
Copy link

Anyway, thanks for the suggestions. We managed to fix it on our end in the way you describe, just wanted to let you know that this requirement (define needing all 3 arguments) can cause some problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants