The first argument, id, is a string literal. It specifies the id of the module being defined. This argument is optional, and if it is not present, the module id should default to the id of the module that the loader was requesting for the given response script. When present, the module id MUST be a "top-level" or absolute id (relative ids are not allowed).
- Give the module an unreachable id (for example "^anon-1" which won't match any require syntax).
- Assign the module id as "null" and resolve all relative modules against an empty string.
- Scan for the newest script tag and infer the module based on that.
- Encourage libraries only to set define.amd when they are in the middle of a require() call so that define() calls from within other script tags are ignored
You received this message because you are subscribed to a topic in the Google Groups "amd-implement" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/amd-implement/c9EcRZyKmvA/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to amd-implemen...@googlegroups.com.
John, James, the mixed environment is a challenge here at LinkedIn, as there's just no way to flip a switch and be running 100% module system.Trying to solve it on the loader side:===For Inject, we can actually enable the define.amd flag within the local execution scope due to the XHR design. This would be the most like #2 in the original proposal.
For script tag loaders, unless we pull a huge amount of lab.js' logic into everyone's code,
there's no good way to manage toggling the amd flag. Maybe it's just a documentation solution then about how the define.amd flag works, and a simple "use at your own risk" pattern for people who are locked in a mixed environment:
define.disableAmd = define.amd;delete define['amd'];// include your script tags, thendefine.amd = define.disableAmd;delete define['disableAmd'];Not that I want to encourage bad code, as the goal is to ultimately get to a module loader.Trying to solve it on the AMD module developer's side:===Maybe we just need to update the pattern for detecting AMD and calling define, with a note specifically about try/catching if you're using an anonymous define. Since everyone throws in one form or another, the following code is a lot more bulletproof. Note the try/catch added to the anonymous define call.if (define && define.amd) {try {define([], myExports);} catch (e) {(module && module.exports) ? module.exports = myExports : window.myName = myExports;}}else if (module && module.exports) {module.exports = myExports;}else {window.myName = myExports;}
If this was part of the original document can't you do document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src?
Thanks John.It sounds like the consensus is:
- users: "Don't mix <script> and require(), it's unsupported."
- library authors: "Either create a named define (like jQuery), or do NOT use an anonymous define.
Declaring an anonymous define() will break users who use script loaders and would like to concatenate your file to another AMD module."
I would like to get some agreement on:
- if a user does: <script>define([], function() { console.log('a'); })</script> what should happen?
- if a user does: <script>define(['foo'], function(foo) { console.log(foo); })</script> what should happen?
Personally, I think at the time define() is invoked, if the ID cannot be determined, we should probably throw an error. The spec would read (emphasis mine): "The first argument, id, is a string literal. It specifies the id of the module being defined. This argument is optional, and if it is not present, the module id should default to the id of the module that the loader was requesting for the given response script. If no module ID can be determined (for example due to inclusion via a script tag), a loader should throw an error to the end-user. When present, the module id MUST be a "top-level" or absolute id (relative ids are not allowed)."
I still have some concerns about libraries like fastClick https://github.com/ftlabs/fastclick/blob/master/lib/fastclick.js This library cannot be concatenated since it forces an anonymous define. Do tools such as r.js fix these anonymous defines and map them to a proper module path?--Jakob