Live data from Hacker News

CommonJS is hurting JavaScript

deno.com

81–90 of 134 posts

Re: CommonJS is hurting JavaScript

#81

I think this is funny, since esm being "native" on browsers doesn't really matter until you can convince devs they don't actually need to use a bundler. So long as you're using a bundler, the browser's runtime doesn't really matter - you're using the runtime the bundler presents and emulates on the browser. Native ESM has proven to be quite painful in ecosystems that _don't_ rely on the presence of a bundler to patch…

I think it's still largely prudent to use bundler tools. I think the biggest issue with not going to ESM syntax comes down to static analysis and tree shaking. It's so much better with proper ESM, and will reduce overhead for the browsers. The bundlers definitely paper over various issues. Being able to import non-js resources like styles, json and other references is useful, to say the least. I don't think this will ever be really practical for direct browser use short of some generational compute and network improvements.

That said, we aren't that far off. Many site are spewing several MB of JS on load, and it's relatively well performing even on modest phones these days. At least relative to 90's dialup where the rule on loads was measured close to 15s. Things are absolutely snappy (mostly). I think the biggest hurdle today is shear entropy. React+MUI+Redux is imo pretty great, and getting to something similar in pure JS would take a lot of effort. Not insurmountable, but significant. There's still a new framework of the month nearly every month in the JS space.

Getting movement is hard. It'll take time and persistence.

Re: CommonJS is hurting JavaScript

#82

Earlier quoted context omitted.

If you are same origin, you do not require CORS to use type=module. Also type=module works on HTML pages served from file:// (file:///.../index.html).

It is not possible to do same origin from a file system. Everything served from a file system is considered to be different origins. This drives me bonkers.

Because browser makers only have respect for the bourgeoisie and clear contempt for the proletariat.

Re: CommonJS is hurting JavaScript

#83

Does anyone have a detailed understanding of why CommonJS (and its async incarnation, AMD) were not adopted by browsers? I do much like the `import` syntax personally and its a little cleaner to read, but CommonJS and AMD were the undisputed winners of the module format until ES Modules were born. Not that I have a problem with ES Modules, I don't, however I am interested in what was so insufficient about the precedi…

> That isn't per se an issue I don't think, esp. because AMD built on top of CommonJS primitives, and with minimal refactoring CommonJS code could be used in the browser when defined this way if asynchronicity is a must.

This existed: the UMD module format was the turducken you got if you built modules to work both as AMD and CommonJS at the same time. AMD wrappers, async require, and a bunch of boilerplate to determine if the module was being loaded by an AMD loader or in a CommonJS environment (or worst of all, a CommonJS environment with AMD loader primitives).

It was a lot of ugly boilerplate. I don't think I ever saw a project intentionally write UMD modules by hand. I do recall some Typescript projects that distributed as UMD modules for a while, because that was boilerplate Typescript was always good at streamlining.

> I do much like the `import` syntax personally and its a little cleaner to read, but CommonJS and AMD were the undisputed winners of the module format until ES Modules were born. Not that I have a problem with ES Modules, I don't, however I am interested in what was so insufficient about the preceding formats that we couldn't have standardized on them

I think it is absolutely the syntax that needed standardizing. AMD was always a hack for module loading using available browser tech as best as it could and screaming for better syntax. There was so much pain every time working with AMD in making sure that define() wrappers were correct and the list of dependencies correctly matched the names and order of those as parameters of the module's function wrapper. AMD was always in desperate need of an import syntax. (One of the reasons Typescript was built was to provide such an import syntax ahead of ESM standardization. It's why I started using Typescript in the 0.x wilds.)

In many ways ESM were always the natural improvement of the AMD format. One of the things that hung browser standardization in various stages was debates about how compatible to be with AMD. There were multiple attempts and a lot of debate at "Loader APIs" that could be extension points to directly interface classic AMD loaders such as Require.js and the Browser's. Had one of those Loader APIs made the final cut it likely would have been possible to "natively" import legacy AMD directly from ESM.

Loader APIs lost to a number of factors including complexity and I think also the irony that CommonJS won the "bundler war" while those debates were going on. I think it must have seemed that the writing was on the wall that AMD compatibility was no longer that useful and Loader APIs were never going to be great for CommonJS compatibility (again, because of those synchronous assumptions that doomed CommonJS to always be the nemesis of browser modules).

(The dying compromises of the "Loader APIs" tangents is what eventually delivered importmaps.)

AMD compatibility without "Loader APIs" is basically impossible. Even though Require.JS was quite dominant, it was never the only loader, and part of its dominance was it was an extremely configurable loader with tons of plugins. There wasn't an "AMD loader standard" that browsers could emulate.

I generally do think that ESM is what we got trying to fix the syntax needs of AMD and clean up and actually standardize the AMD loader situation. In the end it didn't end up backwards compatible with AMD like it tried to do, but from my impression it certainly tried and that was unfortunately part of why ESM standardization was so slow and what led to such a larger mess of CommonJS modules in the wild in the time that took.

Re: CommonJS is hurting JavaScript

#84

Earlier quoted context omitted.

Is CommonJS even a thing for javascript running in web browsers? Moving from CommonJS to modules shouldn't affect any running in a browser.

Sure it is. I've used it via require.js. There were other ways too.

Using require.js is your choice. CommonJS often isn't a choice.

Re: CommonJS is hurting JavaScript

#85
post #2

[flagged]

Every time this is mentioned, the person is heavily downvoted. But if you were here in the 2000, that's exactly what happened. In fact, this very article starts with "JavaScript, the undisputed king of web development, is being sabotaged — not by a rival language or a revolutionary new technology, but by its own baggage from the past.". That's the first sentence. Nobody wanted to work with JS, it was used to make sno…

The most telling part of this comment is that you omitted PHP.

And the parent comment mentioning VBScript is the other half. VBScript was equally terrible.

Imagine a world where every interpreted runtime used Lisp. Or UCSD Pascal.

People would be here saying "this was forced on us!" And wishing that browsers ran MIPS assembler they could target with their new language of choice.

Re: CommonJS is hurting JavaScript

#86

Does anyone have a detailed understanding of why CommonJS (and its async incarnation, AMD) were not adopted by browsers? I do much like the `import` syntax personally and its a little cleaner to read, but CommonJS and AMD were the undisputed winners of the module format until ES Modules were born. Not that I have a problem with ES Modules, I don't, however I am interested in what was so insufficient about the precedi…

From what I remember, reading the conversations over the years...the issue was twofold: 1. Because `require()` is "just a magic function", it can't be statically analyzed by a JS runtime prior to actually running the code. This leads to limitations with regards to tree-shaking and other optimizations. 2. The last point leads to the even bigger (and probably "deal-breaker") reason for the change, the desire to fetch p…

Your first point is absolutely spot-on but I am curious as to how much treeshaking was on the minds of masses at the time. The tooling of that era didn't really have any good support for tree shaking even for non-AMD includes and it was quite experimental tech (as in, I don't think it was a decision making factor for the majority of the tools on the scene).

The second point actually isn't strictly valid. I've written my own "all-in-one" async custom loader [0] that can require() CommonJS/AMD includes, regular "add a script tag" includes w/out any exports, or even css stylesheets all asynchronously, with asynchronous dependency trees for each async dependency in turn. You can define in the HTML source code a "source map" that maps each dependency name to a specific URL, so that you don't need knowledge of the filesystem tree to load dependencies.

Ideally, this source map can be generated via the tooling you use to compile the code (e.g. `tsc` is aware of the path to each dependency) but I haven't written my own tool to generate the require path to url map.

[0]: https://github.com/mqudsi/loader

Re: CommonJS is hurting JavaScript

#87
post #79
post #58

Shrug I think there should be more praise on these guys for what they accomplished given the state of JavaScript when they started. They saw a problem and came up with a solution. Was it perfect? No, but it's not this abominable creation. Much like John Resig's work on jQuery nudged JavaScript forward, so did the work on CommonJS/Node.

[deleted]

[deleted]

Re: CommonJS is hurting JavaScript

#88

Earlier quoted context omitted.

> and its async incarnation, AMD A bare-bones implementation of AMD could be put together with less than a kilobyte of JavaScript (this is what we used at Mozilla for a minute circa 2012). Meanwhile, the ECMAScript folks were working on ES6, which was going to have a module system. Why would the browser build in support for a highly-opinionated system that you could implement yourself so trivially, all while a TC39-b…

FWIW they did it[0]. Alameda is promise based AMD from the same folks that were key in AMD being successful when it was so successful [0]: https://github.com/requirejs/alameda

Alameda does a great job of explaining why you can't just slap promises on something: it breaks the semantics of what `require()` returns. `require('foo')` returns foo, maybe. `require(['foo'])` returns `Promise`.

Re: CommonJS is hurting JavaScript

#89
post #40

I wouldn't go as far as saying CommonJS is hurting JavaScript, but it's current status in the node ecosystem is definitely painful. I mean the default remains CommonJS, so using esmodules is awkward on 'native' node, but if you use the most popular bundling systems, the default becomes esmodules. I won't pretend I know which is better between the two, but the decision should be made to either make it optional or depr…

[deleted]

Re: CommonJS is hurting JavaScript

#90

Earlier quoted context omitted.

Thinking back to when CommonJS was implemented, absolutely. I don't think you'd want to call Spring or ASP the gold standards.

You know, now that I think about it, ASP might actually be the first example of a server side JS programming environment, kinda sorta. Nobody really did it, but you could use JScript instead of VBScript.

There was the Netscape JS stuff, but I don't think it was nearly as popular as Classic ASP by 2000. I wrote a lot of ASP in JScript, was able to reuse validation libraries, etc for common inputs/forms which was nice at the time. I think the difficult points, were COM iterators were kind of alien feeling in JScript, and developing COM controls at the time were awkward and a real pain to debug/diagnose even in VS at the time.

Not to mention, very little in the space of Classic ASP was open-source, free or even anything not insanely expensive from what I recall at the time. What was in the box was pretty much just JS and VBS, and most of what you could piece together was sluggish as all hell. Cool, you can use the spell checker from MS-Word... damn, three users tried to use it at the same time on the server. etc.

I have some fond and nightmare memories from those days. I think in terms of before jQuery (though scriptaculous, prototype and others were cool), after jQuery and after Browserify and 6to5/Babel. The ESM transition is much, much slower going.

Post reply on HN