Comments (9)
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.
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.
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.
Thank you for your answer. Other requirements were perfectly clear but indeed I missed this one.
from almond.
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.
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.
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.
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.
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)
- normalize fails for relative imports with prefixes HOT 7
- Return almond.js as string when required from node.js HOT 2
- How to handle anonymous modules HOT 2
- unsafe String split HOT 3
- can't get requirejs, angularjs and almond to work together HOT 2
- Uncaught Error: No fs HOT 2
- Build problem HOT 2
- AlmondJS + CDN module with UMD HOT 4
- Any way to listen the DOMContentLoaded event HOT 1
- Your js script is crashing Firefox HOT 3
- Issue with relative path when they match such path: "../../vendor/some-lib/./some-file" HOT 1
- Issue with relative path when they match such path: "../../vendor/some-lib/./some-file" HOT 1
- Creating reusable libraries with RequireJS & AlmondJS HOT 1
- Almond + Electron HOT 5
- Loading CSS files HOT 3
- Can almond be forced to require synchronously?
- require.config(...) is being executed too late
- Calander Start With Monday rather than Sunday HOT 1
- Location of almond.js is critical to the build process?
- TypeError: Cannot read property 'splice' of undefined HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from almond.