So lets say I have some small bit of library code that I develop and test in isolation. I use RequireJS during development and have a root level file that depends on 1 other file. So it's define
looks something like...
// lib/main.js
define(['lib/dep1'] function(dep1) {
...
})
I run r.js on the code which results in dist/myLibrary.js
, which looks something like this:
define('lib/dep1',[], function(){...})
define('lib/main',["lib/dep1"], function(dep1){...})
If I pull myLibrary.js
straight into another project it won't work. Nothing is defining itself as the module for that file. But if I append an actual module definition, it works.
define('lib/dep1',[], function(){...})
define('lib/main',["lib/dep1"], function(dep1){...})
define(['lib/main'], function(lib) {
return lib;
})
And the ['lib/main']
seems to be scoped to the module, because if I have an actual lib/main
in my app, it doesn't get used.
Questions:
- Regarding the scoping, is that the normal behavior? The fact that
lib/main
is recognized as a module id from the same file rather than going to look for it someplace else. If I import 10 such libraries that all have alib/main
, they won't collide? - Is there a better way? I am at least initially unconcerned about supporting the non-AMD use case as this is all internal lib development and we all use RequireJS. So within a fully AMDed environment, is there another, better way to do this? Assuming there's no pitfall to this approach, it seems fairly simple and boilerplate to support.
If you are including a concatenated file of modules into another project, then if you just want 'lib/main' to be executed (you do not need to define a module for that concatenated file), then just doing require(['lib/main']) at the bottom of the file is enough, and the optimizer can do this via insertRequire.
If this is a more involved question about "I want to use a file that is made up internally of many modules concatenated together, and those individual modules should not be visible, just one larger import", then doing a build with wrap and almond or some other small AMD API shim](https://github.com/jrburke/requirejs/wiki/AMD-API-Shims) is suggested.
If you want to continue the discussion, it may be better to open an r.js issue, as we will each get proper notifications of changes on the issue instead of the black hole of a gist. Discussion issues in the r.js or requirejs github issue trackers are completely fine to open.