Mudi,
Cool, thanks for adding your thoughts!
On your first question: Your myPlugin will need to implement a build-time plugin API as well. Are you using cram.js or r.js to build? I agree more documentation is needed around build-time AMD plugin APIs. One problem is that the build-time API has not really been standardized, afaik. For example, it differs between cram.js and r.js.
And the second question, re:
There are two issues here. First, using the top-level wire module is typically not the right thing to use programmatically unless you are actually at the top level of your application. Wire contexts are intended to be used in a hierarchy, begotten one from another by calling the wire() *method* of an existing context, e.g.: parentContext.wire(...).then(...), or by using an injected wire function, e.g. injecting { $ref: 'wire!' }. Both of those preserve the parent-child relationships between contexts (and all of the advantages that brings, like component visibility, chained lifecycle management, etc.). In contrast, the top-level wire *function* always creates a context that inherits from the (implicit) root context.
Second is, as you mentioned, the interaction with the underlying loader. Unfortunately, the way AMD and CommonJS are designed means you must use the local `require()` function to correctly resolve relative paths. This local require() is injected as a free variable that can only be seen within the module itself. That means that it is, by design, impossible for wire's core library code to get its hands on the local require function of a wire spec that it is asked to wire (since the spec's local require function exists only within the spec module). For that to work, the spec itself would need to hand the local require() function to wire. That's a possibility, but starts to make things more verbose. For example, you must use the expanded AMD form:
// Wire spec that uses local require
define(['require'], function(require) {
// Wire DOES NOT support this currently
// But it might be possible to add support
// Provide the local require function to wire so that
// wire can use it to load modules for this spec
$require: require,
component1: {
create: ...
},
...
});
FYI, it is possible to do the above when using wire programmatically, although it's not documented: wire(spec, { require: require }), or parentContext.wire(spec, { require: require })
It's also possible to *directly use* the local require function inside of wire specs, but again, it gets a little cumbersome, and there are probably some edge cases we haven't tested:
// Wire spec that uses local require
define(['require'], function(require) {
// This works today
component1: {
create: {
module: require('../relative/path/to/my/module')
}
},
// And will even work with plugins
text: {
module: require('text!../a/template.html')
}
...
});
I am not sure, though, whether that last example would work in a build. It would be interesting to try it, and maybe unscriptable knows the answer.