module.declare(["increment"], function(require, exports, module) {
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
// module.id === "program"
})
or (AMD like, taken from RequireJS website)
I would prefer something involving the word "import" rather than just the array, and maybe give an "as" option as well for shortcut or easy refactoring purpose.//my/shirt.js now has some dependencies, a cart and inventory //module in the same directory as shirt.js define(["./cart", "./inventory"], function(cart, inventory) { //return an object to define the "my/shirt" module. return { color: "blue", size: "large" addToCart: function() { inventory.decrement(this); cart.add(this); } } } );
module
.import("increments")
.declare(function(require, exports, module) {
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
// module.id === "program"
})
(Wrappings style, with "as" alias)module
.import("increments").as("inc")
.declare(function(require, exports, module) {
var inc = require('inc').increment;
var a = 1;
inc(a); // 2
// module.id === "program"
})
(AMD Style)module
.import("./cart
")
.import("./inventory
")
.define(function(cart, inventory) { //return an object to define the "my/shirt" module. return { color: "blue", size: "large" addToCart: function() { inventory.decrement(this); cart.add(this); } } } );
--
You received this message because you are subscribed to the Google Groups "CommonJS" group.
To post to this group, send email to comm...@googlegroups.com.
To unsubscribe from this group, send email to commonjs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/commonjs?hl=en.
To unsubscribe from this group, send email to commonjs+u...@googlegroups.com.
> If you want to use toString in a standard spec, at least inject "require" inHmmm... I understand the issue, but it's not really solvable in a
> the function scope 'either through a global or a closure if you can) so the
> wrapping function does not provide a way to change function(require){var
> foo=require("foo")} for something else like function(load){var
> foo=load("foo"}, but only function(){var foo=require("foo")} IMHO it's at
> least less confusing on what you can or can't do as a programmer (and you
> could even want to do a function(){var r=require, foo=r("foo")}" and it
> should work, but toString will have some problems handling such a use case
satisfactory way. I suppose an implementation could have a global
require function that threw a helpful error. That might not be too
bad of an idea, actually.
I believe that in practice it's a minor issue though. These are
judgement calls, of course, but I believe the convenience and
scalability of implicit dependency resolution (scanning) outweighs the
unambiguousness of explicit (the dependency array). Think about big
libraries here. What would it be like to explicitly specify the
dependencies for this:
https://github.com/khs4473/Codera/blob/master/lib/text-panel.js#L3-29
That would be an array of 19 elements or 19 calls to import/as or
something similar. Having to remember the boilerplate rules seems
like a small price to pay not to have to maintain such a monster.
So you can do (I alias this as I don't like telling the path explicitly multiple time for refactoring concern)//brushes.js
module
.import("./text-brush")
.import("./gutter-brush")
.declare(function(require) {
return {
Text : require("./text-brush"),
Gutter: require("./gutter-brush")
} });
It sounds quite neat to me, you can do this multiple time for this module, have at least half of the dependencies grouped in a logical way, have less requires in you code, and finally have a more concise module by removing some require boilerplate by trading things like "GutterBrush" to "Brush.Gutter" , no ?module
.import("./brushes").as("brushes")
.declare(function(require) {
...
var Brushes = require("brushes);
...
}
I totally understand your impatience! I think we need to have one
> Apart from that, I have added a vote for choosing the best candidate between
> AMD and Wrappings on Bettermeans for Module 2.0, I consider this as a high
> priority for us to build further on CommonJS, I know more and more people
> watching at this everyday and waiting for a "winner" to go on with CommonJS
> for the browser. I'm quite neutral here, I do not find either one or the
> other perfect, but I prefer one that is not perfect for me choice rather
> than a battle between to pretty good candidates.
more knock-down-drag-out debate before we're ready though. We need to
put all of the technical aspects on the table so that everyone can see
what it really comes down to. I need to get my transport spec out
there first though...
--
How much do you want to consider recent developments? For me, there
are two that should impact something like this:
a) Simple Module modules strawman is gaining more prominence.
b) AMD has some real world adoption.
So for me, I would rather see AMD as a base because it has some
adoption and real world usage, the most of any other wrapped format or
transport alternative. It has been proven in the market.
The arguments against using AMD are bikesheds except for two:
1) Specifying a transport and a wrapped format in one API
2) Reliance on Function toString().
#1: This is necessary to bootstrap module usage on the web. Most
single file web libraries need to specify their ID today because they
can still be used in projects that may not know how to add module IDs
when combining scripts. In order to make sure those libraries work
also when used in other environments, it is best to specify a base
transport format that all implementations understand. Implementations
can provide alternatives but having a commonly understood is
important.
#2: Not using toString creates more typing for the developer, and in
practice toString works well enough. It seems reasonable to expect
better parsing APIs will come to JS environments over time, and asking
the ES committee to formalize the expectations of toString seem
possible, particular given how environments work today.
To me, forcing an ES5 baseline is worse than toString usage since ES5
cuts out older IE browsers which are still in use today. At a minimum
the ES5 baseline should not be in the proposal.
Usage of "module" should be avoided given the possible confusion or
conflict with Simple Modules. For the "module" free variable that may
be used inside a factory function, maybe something more like "self" is
more appropriate. Using AMD as a base avoids the other "module" uses.
I would prefer to see constraints on the use of require to only be for
static dependencies that should be expected to run at the top of the
factory function, and any use of computed require uses a callback
style require. This will also match Simple Modules behavior better.
Those require rules, using AMD as a base, and picking up some of the
Modules 1.1 wording is my preferred approach.
James