Coder Social home page Coder Social logo

Comments (9)

jrburke avatar jrburke commented on June 8, 2024

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?

from almond.

damiengenet avatar damiengenet commented on June 8, 2024

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.

from almond.

jrburke avatar jrburke commented on June 8, 2024

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.

from almond.

damiengenet avatar damiengenet commented on June 8, 2024

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

from almond.

nchilada avatar nchilada commented on June 8, 2024

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.

from almond.

jrburke avatar jrburke commented on June 8, 2024

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.

from almond.

jrburke avatar jrburke commented on June 8, 2024

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.

from almond.

nchilada avatar nchilada commented on June 8, 2024

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.

from almond.

nchilada avatar nchilada commented on June 8, 2024

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.

from almond.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.