Live data from Hacker News

The state of JavaScript modules

medium.com

71–80 of 106 posts

Re: The state of JavaScript modules

#71

Earlier quoted context omitted.

I know you're trying to be helpful, but I'd like to make clear that if you're a noobie, you don't need any of these tools. Plain Javascript is just fine, especially with the improvements of ES6. Tools are here to solve problems. Don't bother with them until you have that problem yourself. If you one day decide you want to set up things like auto-minification, image compression, CSS-preprocessing, then look into build…

You don't need any of those if you are doing a node project, yeah, but if you want to do any work on the browser, you're gonna need to transpile anyway, so it's something people should be aware of. Plus it's literally a 20 line config file to do so. module.exports = { entry: './src/app.js', output: { path: './bin', filename: 'app.bundle.js', }, module: { loaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'ba…

Soon with the new webpack-cli, you'll just be able to 'webpack init':

https://medium.com/webpack/announcing-the-new-webpack-cli-75...

Re: The state of JavaScript modules

#72
post #69
post #45

Serious question: what benefit does this (.mjs) have over the current way of doing things?

It enables you to not package modules that your app's not using, preventing the dreaded "shipping x MB to your users".

But can't you just do some "tree shaking" with something like Rollup, so it only sends the code you use?

Re: The state of JavaScript modules

#73

“Most frontend developers still remember the dark days of JavaScript dependency management.” As opposed to now, where even the smallest “app” has hundreds if not thousands of dependencies for the most basic of functionality (‘need to iterate an array, better bring lodash’), often there being multiple copies of the same dependency due to conflicting versions?

You beat me to the punch: I really don't remember "you would copy and paste a library into a vendor folder, rely on global variables, try to concat everything in the right order and still would had to deal with namespace issues" as being all that big of a problem! It was (is!) simple enough, and it mostly worked; if you were trying to deploy something to a customer site where they already accidentally had two differe…

I do remember that being a huge pain in the back, as in:

- You have to maintain a full list of your files, in order, to include them in your html page (in dev mode) and concat (for prod mode)

- If you want to know on what other module a given module depends on, you have to hunt every call in the source code

- You can never remove a dependency on an external lib as you can never be sure it's not used somewhere

- Upgrading a lib was a pain

Compared to every other language, it was a huge pain point. It's not by chance that so many people started to work on implementing modules for JS, there was a real need.

Re: The state of JavaScript modules

#74
post #72
post #69

Earlier quoted context omitted.

It enables you to not package modules that your app's not using, preventing the dreaded "shipping x MB to your users".

But can't you just do some "tree shaking" with something like Rollup, so it only sends the code you use?

You can only do so _thanks to_ the new module system. From the Rollup website [1]:

> Since this approach is based on explicit import and export statements, it is vastly more effective than simply running an automated minifier to detect unused variables in the compiled output code.

[1] https://rollupjs.org/#tree-shaking

Re: The state of JavaScript modules

#75

Just like when Meteor was the hottest kid on the block, ES6 came along and pulled the rug out from under me. Now that I'm finally used to ES6, here comes some other frankenstein with more flesh bolted on. At what point will Javascript relax and stop reinventing itself and what works? When will stability become a core goal for the Javascript thought-leaders? I haven't worked with .NET and C# for years but I bet I coul…

Before ~2015: "JavaScript is a terrible language!" After ~2016: "Stop improving JavaScript, I can't keep up!" (Not a personal attack, just general sentiment I've observed)

I've been using JS lightly for a couple of years, in earnest for a couple of months. From the perspective of someone who's comfortable in C, Lisp, Python I think the semantics are fine (given a certain subset).

One major issue I have with JS is that the standard library is severely lacking in the basics. We have Promises but Math.round only rounds to the nearest integer. We have generators but no string formatting (the template syntax is not a replacement).

Re: The state of JavaScript modules

#76
post #70
post #66

Earlier quoted context omitted.

Can you give some examples (or better, a writeup) about how static analysis is hard without classes? It seems like Haskell gets along fine without it.

I meant higher level constructs. Classes are just an example. Even in Haskell you have typeclasses and modules. The point is that even though in JavaScript (and other Turing complete languages) you can emulate anything with what is available (for JS that is functions) you cannot easily build tooling around your ad hoc constructs.

Again, maybe a concrete example would help?

Re: The state of JavaScript modules

#77
post #76
post #70

Earlier quoted context omitted.

I meant higher level constructs. Classes are just an example. Even in Haskell you have typeclasses and modules. The point is that even though in JavaScript (and other Turing complete languages) you can emulate anything with what is available (for JS that is functions) you cannot easily build tooling around your ad hoc constructs.

Again, maybe a concrete example would help?

Import/export statements vs require() calls?

  const x = require;
  const m = x('crypto');
Here I'm requiring a module but it's hard to follow for a tool that Crypto is being imported.

The proliferation of class systems in JS before ES6 classes. Building a tool (IDE for example) to support all of them would be a complex problem.

See also interviews with Anders Hejlsberg (e.g. [0]) where he explicitly mentions that TypeScript type system's primary focus is allowing better tooling support for developers (like press dot and see what's there, safe refactorings).

[0]: https://devchat.tv/js-jabber/209-jsj-typescript-with-anders-...

Re: The state of JavaScript modules

#78
post #42

Earlier quoted context omitted.

JS is backward compatible. So it got that too from Java other than the funky name. So it's stable. jQuery? Works. Good old document.getElementById? Works. And thanks to the standardization efforts of vendors (and countless volunteers, who created feature matrices and feature tests and polyfills, and so on), now using those is easy, because you can look up what's supported, what will be supported, and what is deprecat…

I generally agree, just a small nitpick: > Are you making sites for blind people? Use/support tools that are thinking about WAI-ARIA/semantic markup. I think developers should always have accessibility in their mind. People (or organizations they work for) may choose to use IE8 and yes, it makes sense to draw a line somewhere regards to supporting browsers. They can decide if an upgrade effort is worth to use your ap…

Absolutely. But that's still a trade off (between using a tool that might be better in some other sense, but then you might not have time to add ARIA tags, or reshape the markup a bit). And usually it's not an explicit requirement. So it's up to the developer/team.

And I agree with doing the minimum. There was a great site that shows sites as they "look" like to a screen reader, but I can't find it now. (But it was very much like WAVE http://wave.webaim.org )

Re: The state of JavaScript modules

#79
post #47
post #42

Earlier quoted context omitted.

JS is backward compatible. So it got that too from Java other than the funky name. So it's stable. jQuery? Works. Good old document.getElementById? Works. And thanks to the standardization efforts of vendors (and countless volunteers, who created feature matrices and feature tests and polyfills, and so on), now using those is easy, because you can look up what's supported, what will be supported, and what is deprecat…

> JS is backward compatible. So it got that too from Java other than the funky name. You mean forward compatible. Which is unlike in Java. New code (e.g. with ES6 features) doesn't work in older JS engines / browsers -- that's why we have transpilers.

Java 8 lambas don't work on Java 6 JRE/JVMs either.

But your code is "futureproof". The spec/language/VM is backward compatible with old code. (In the sense that you can drop in the new version and old code runs.)

Re: The state of JavaScript modules

#80

Earlier quoted context omitted.

There's not much to it. 1) Pick a build tool (gulp, grunt, webpack - if you don't want an option, use webpack). You don't need a build tool but it helps with transpilation and it's a good base for the future. 2) Make or copy/adapt your build tool's config file to use Babel so you can use the latest ES version but compile down to work in browsers. You'll have to Google but these exist all over the place and they aren'…

I appreciate your help, but it's somewhat comical to note how much your comment resembles this satire: https://hackernoon.com/how-it-feels-to-learn-javascript-in-2... > It's not more difficult than setting up a Python, C, or any other environment, you simply have options It's decidedly more complicated than Python. Being on par with C is hardly appropriate for a dynamic language. To be clear, I really do appreciate t…

[deleted]
Post reply on HN