text
stringlengths
0
1.36k
**Alex Sexton:** But that only makes sense on the server.
**Mikeal Rogers:** \[07:49\] Yes, okay. That is fair. But what people were doing already with Browserify was they were taking a bunch of these modules and bundling them up and using them in their web applications. And everybody in production, even if you're using AMD, ends up doing this bundle step. It's really only in...
**Alex Sexton:** Yeah, and potentially in the HTTP/2 push future, but...
**Mikeal Rogers:** Yeah... I've seen some interesting benchmarks around that that show that it's still not --
**Alex Sexton:** I think we're far away from that, but yeah, it's some theoretical future where it's just as fast to not bundle.
**Mikeal Rogers:** Right. So the AMD spec comes along... It had a lot of good fans, people that really actually...
**Alex Sexton:** It was actually part of CommonJS as well. It was a CommonJS spec.
**Mikeal Rogers:** Yeah, so they went back to CommonJS. When we talk about ServerJS and the CommonJS community, this was a mailing list; it wasn't an official standards body, it was really like a mailing list of people that argued about specs they were running on a wiki. There's wasn't much of a process, and I think AM...
Anyway... Yeah, so that kind of brings us to ES modules. I think ES4 had a modules spec in it, which, you know, ES4 kind of died off. Then there was still though like a spec kind of floating around; it was very much based on Python, very pythonic. That had some anti-patterns in it that we really advocated against in No...
**Alex Sexton:** You can do it in current ES modules, but it doesn't dump it into the namespace. You can import\* as foo, and then use the bucket of things off of foo, which is cool.
**Mikeal Rogers:** Right, so that dumps it into one property that is named at the top... That's totally reasonable! \[laughter\]
**Alex Sexton:** It's totally different, but it's still -- I know in some systems, (like Java) whenever you import\* versus import something specific, your runtime gets a lot bigger, because you have to actually pull in all those things separately; there's separate files, all that kind of stuff. With JavaScript it real...
**Mikeal Rogers:** The main problem with it is that you have three import statements, and they all say "import\* from somewhere." One of them gets the bar property from foo, and then later in the code it's just calling bar, and you're like "Well, where the hell did bar come from? I need to know what this does, I need t...
So anyway, they revised the spec; it's still pretty pythonic, but it definitely started to use some of the new syntax coming down the pipe that was also in ES6, stuff like that. Were either of you involved in the spec process at that point, where it kind of came back on the table, and then Yehuda got involved to try an...
**John-David Dalton:** No, I wasn't. I popped in a little bit later.
**Alex Sexton:** I followed the tweets about it... \[laughter\]
**John-David Dalton:** Usually, when it comes to syntax, I'm not a chromogen, I'm pretty open to new syntax... So I'm like "Let's have the new syntax, let's start hearing on it and using it to see what shakes out." I'm all for -- at the time, I was all for the import, the export, all that stuff.
**Mikeal Rogers:** \[11:58\] Right. I think the spec sat in a weird state for quite a while. This was before a lot of new processes were put into place at TC39. The spec kind of lingered with people poking at it, nobody had really implemented it yet, nobody was using it in the wild, because this was kind of pre-Babel, ...
Anyway, at the time that it got kind of ratified in ES2015, there was a lot of people saying "Oh, well this is gonna be compatible with Node", because Yehuda had done a bunch of work looking at how Node modules look and work to spec, and how ES modules work, and "Let's make sure that they have feature parity."
When Bradley started to really dig into this though (Bradley Meck) and figured out how we might actually implement this support, he started to run into a lot of crazy edge cases and gotchas in how Node's module system not only works today and loads modules, but also how it can be kind of dynamically shifted, and stuff ...
**John-David Dalton:** I got involved last year around May, June. I had seen a lot of the discussion about .mjs pop up, and I didn't really like the idea of a new file extension, so...
**Mikeal Rogers:** Let's unwind that a little bit - why do you need a .mjs extension? In the browser, you have this new script include basically that signals "Hey, this is a new style module, not an old style JavaScript thing", but we don't have that in Node.
**John-David Dalton:** You have type=module in the browser; in Node... Node loads things based off of file extension. A .js file, a .json file, a .node are handled based on their extension, and then it defaults to .js; it falls back if it doesn't recognize the extension.
For Node, because the existing module system is CommonJS, there needs to be a way to distinguish quickly between your parse goals - if it's going to be CommonJS or if it's going to be an ESM or ESN module, because they behave differently and there's different rules in place for them.
One of the things is like your EcmaScript module is going to have implicit strict mode, and there's certain syntaxes that are allowed at one and not in the other, so that's why the extension is there.
**Mikeal Rogers:** Okay, and you didn't like the idea that there would be this new extension .mjs?
**John-David Dalton:** \[15:37\] Yeah, I mean... The problem is that all new-facing proper EcmaScript modules would require this new extension, and it introduced some other things too where it was like "Node is not a vacuum, even versions of Node." Projects don't tend to just support one version of Node; there's usuall...
**Mikeal Rogers:** Yeah. I think there's an additional gotcha in the transpilations, too. As we've been working with TC39 to figure out what parts of the spec maybe need to shift or adjust in order to make our support work, we're finding things in the Babel transpilation today that make the module system behave slightl...
**John-David Dalton:** But my main nitpick was just on the parse detection, or the detection of the goal. I didn't wanna introduce a new file extension, because that also carries over into the browser. I mean, you say the browser doesn't care what extension it is, but there's already blog posts that say "Hey, just writ...
Last year I introduced with Bradley a proposal called "Unambiguous JavaScript Grammar", which is a way that says "If your module has at least an import or an export, then you know that it is an ESM file, instead of a CommonJS file or a script target", because it has import or export.
At the time, Node really wanted that to be mandated by the language, so they took it to the TC39 and said "Hey, TC39, would you be interested in changing the language to mandate this?", basically saying that an ES module must have an import or export to make it unambiguous. The reason is that if you don't have an impor...
**Mikeal Rogers:** Can you dig a bit more into the logic there? Why did they not go with this unambiguous grammar? Why didn't they say that?
**John-David Dalton:** Because they want assurances -- one of the things is when you're refactoring your code, you might remove an import or you might remove an export, and be in a state where you're a side-effect-only module, which means no import or export, and then if you do that, you've unintentionally changed your...
In the browser you say that something is a type=module; it's very explicit. But with unambiguous, it requires it at a syntax level, and just having import or export, it's easy to slip out of that and accidentally go to a different parse goal, which is why there's other proposals now a year later that say "Hey, you can ...
**Mikeal Rogers:** \[20:19\] So that brings us through up to that spec... So what does the landscape look like now? Who has implemented ES modules, how have they implemented them? Node is currently pursuing this both in standards and in implementation, trying to make this work with .mjs. What's the current status of th...
**John-David Dalton:** Edge has experimental, I think Firefox and Chrome are both experimental as well, and then I believe it has shipped in Safari. So basically all the major ones have it, either experimental or shipped.
**Alex Sexton:** That means you have to turn on a flag about some thing or another.
**John-David Dalton:** Yes, right... Or have a preview build of the browser. So it's coming, it's right around the corner; it's super close. It's not something that's like a year out, it's something that is months out.
**Mikeal Rogers:** There's also the loader spec, which is its own kind of thing. Have they also implemented the loader spec, and are they considering that more experimental somehow?
**John-David Dalton:** I honestly don't know anything about the loader spec; it is super fuzzy to me. I don't know who's following that. I'm over here on the syntax side.
**Alex Sexton:** Have you guys seen SystemJS, or checked that out at all?
**John-David Dalton:** Yes.
**Alex Sexton:** SystemJS does a lot of stuff with the loader. It's built with the ES module loader project, it's ES module loader polyfill...
**Mikeal Rogers:** So it's a polyfill of the loader...?
**Alex Sexton:** Well, it uses that in order to do more, but it uses the minimal polyfill for the loader API, and then on top of that, it does other stuff... It's pretty cool; I was actually expecting it to catch on a little more, because it kind of does a lot of what JDD is doing now, with crazy support for all differ...
But it has a lot of loader override-type things that kind of get towards that. That's Guy Bedford's project, SystemJS. It's worth checking out. It has 8,000 stars on GitHub, so it's not exactly hurting, but I've never used it, so I can't really talk too much about it, but I think it's roughly in this space.
**John-David Dalton:** I believe I've seen a couple projects use it. I know he's really into that loader space; I like to find devs that are super passionate about a certain topic and kind of defer to them for it. He's in it and probably is into all of the spec and follows all that stuff, so I would say if you have a q...
**Alex Sexton:** Yeah, it's a cool project. People should check it out. I had a blog post a long time ago (as we all did) about AMD versus CommonJS that I thought I'd find a spot to put in there... And it's not super important, but there was --
**Mikeal Rogers:** I remember that post, yeah.
**Alex Sexton:** I think it was a good post, still... It was like a response, I think, to some terrible Tom Dale post where it's like "Give up, AMD. You've lost. Everyone else is dumb", which is interesting, because I'm pretty sure a lot of Ember uses AMD under the covers.