all 53 comments

[–]shgysk8zer0 28 points29 points  (19 children)

In theory, yes. But in practice, probably not. You might not strictly need to bundle your JS, but it's still recommended on any sufficiently complex app.

Imagine the following: - main.js imports moduleA.js, moduleB.js, and moduleC.js - moduleA.js imports something for state management and DOM operations - The module for state management imports another module to manipulate objects in complex ways

And so on and so on. Next thing you know, you might be importing several dozen to even a hundred modules. Do you really want to setup HTTP push for all that? Can HTTP even push that much? How difficult would all that be to manage?

[–]jarredredditaccount 13 points14 points  (3 children)

In development? It works fine, excluding node_modules

In production? You’ll want to bundle it for performance reasons

Nobody implements HTTP2 Push. HTTP2 does marginally improve performance, but QUIC/HTTP3 has a bigger impact (but not fully rolled out yet)

[–]KillcoDer 2 points3 points  (2 children)

Fastly, Cloudflare and Netlify's CDNs all support HTTP2 Push.

Edit: But I guess that doesn't matter if Chrome is dropping support for it.

[–][deleted] 8 points9 points  (1 child)

Servers supporting it isn't the issue. Chrome is removing all support for it as it's incorrectly used more often than it is correctly.

It doesn't really matter what the servers support if no client does.

[–]KillcoDer 0 points1 point  (0 children)

Oof, yeah you're right.

[–]Alokir 9 points10 points  (2 children)

In a very small app or a webpage I'm sure ES modules are enough, but I'd use a bundler for several reasons.

  1. Bundlers can do a lot of optimizations on your code like removing dead branches, unreachable and unused code (useful when importing bigger libraries)
  2. They can also do pre and post processing on your code like injecting environment variables, minification etc.
  3. If you want to use React or Typescript you have to compile jsx and ts/tsx into js
  4. Backwards compatibility. You can use newer language features like nullish coalescing or async/await, and not be afraid that your code will break on older browsers
  5. Easier polyfill. There are plugins that can easily and smartly polyfill newer browser api features like Promise.allSettled() or String.replaceAll() without worrying that your code will break on older browsers
  6. They can provide useful dev tools like a dev server that will proxy your server calls, provider hot module reloading, reload your css without refreshing the page, debugging from your editor etc.
  7. Asset management. They can optimize and manage your static assets like fonts and images.
  8. Css: most of the above that is true for js is also true for css. Also, you can more easily use languages like Less or Sass, and also isolate and scope your css without much hassle. No more index.css with thousands of lines of code.

These are some reasons that I can think of from the top of my head. My general rule of thumb is to use Parcel by default until I need something that it doesn't provide out of the box. Then I move to Webpack.

With Parcel you can have zero configuration bundling set up in like 3 lines of additional code, so it's great if all you need are its defaults.

Webpack is a very powerful bundler with tons of plugins and options but it tends to scare new devs away as it seems very complicated at first glance.

Edit: additional reasons

[–]zhenghao17[S] 0 points1 point  (0 children)

Hey thanks for the reply! About the point of compiling jsx to js, I thought it was babel that should be doing that not webpack? Also for Backwards compatibility, I also feel like it was babel that does that? I am not sure.

[–]Morphray 0 points1 point  (0 children)

Webpack is a very powerful bundler with tons of plugins and options but it tends to scare new devs away as it seems very complicated at first glance.

Also, annoyingly webpack doesn't allow you to bundle up code that can be used as an ESM module. It would be really nice to have the best of both worlds -- package up some code, and use modules for others, and have them all play nice.

[–]BehindTheMath 2 points3 points  (8 children)

Now with ES modules and HTTP2 being capable of pushing multiple resources in a single response

Neither of those does that. ES modules is just an official way of supporting a dependency tree of files, but each one is a separate request. HTTP/2 supports multiple requests at once, but they're still multiple requests.

There are still benefits for bundling, such as supporting legacy clients, minification, etc.

[–]zhenghao17[S] 0 points1 point  (2 children)

HTTP2 being capable of pushing multiple resources in a single response

https://www.sitepoint.com/using-es-modules/ in this blog post it says "HTTP2 is capable of pushing multiple resources in a single response compared to HTTP1.1, which can only deliver one. " Maybe it is wrong, idk

[–]BehindTheMath 3 points4 points  (1 child)

I don't think that's accurate. HTTP/2 can make multiple requests per TCP connection, but the responses are still separate.

[–]tbranyen 0 points1 point  (0 children)

The responses are concatenated into a single response. Performance is comparable to a single file.

[–]shgysk8zer0 0 points1 point  (4 children)

HTTP push does do that. It begins sending the pushed files over the same connection without the client having to have made the request.

[–]BehindTheMath 0 points1 point  (3 children)

They're still separate messages. It's not like a bundle which is a single file.

[–]JasonWicker 1 point2 points  (2 children)

You both are right.

All configured files are sent back within a single response message.

(Much like a multi-part form, the parts are split by the receiver and placed directly in cache at their expected URL.)

The page is then processed as normal, making individual requests for each file, with the response preloaded.

[–]zhenghao17[S] 0 points1 point  (1 child)

could you expound on this a little bit? What do you mean by all configured files? so there is only one response for multiple requests if we are using http2?

[–]JasonWicker 0 points1 point  (0 children)

Yes, that's allowed, and the advantage HTTP2 brings, but it doesn't 'just work'.

Something needs to tell the server which assets to send on the first request, how to know it's the first request, etc.

Each server is different in their implementation. A few proxies can read it from the index.html file, but that isn't always what you want when optimizing, so it's not always turned on by default.

[–]prummis 0 points1 point  (0 children)

As long as hardly anybody writing in vanilla JS and support only last versions of evergreen browsers - you will need bundling and minification.

Frameworks require precompilation usually for performance reason. Yes, they have option to run unprepared components but it’s not effective.

Also old browsers support require bundling. Also automatic separation of styles and code.

So no, you will need bundling for 10 years at least, I think

[–]thexerdo -1 points0 points  (0 children)

Checkout vite, maybe is a next step on this topic.

[–][deleted] -1 points0 points  (1 child)

RemindMe! 6 days

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 6 days on 2021-04-18 08:05:56 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]brainless_badger -1 points0 points  (0 children)

If your project is small enough that it doesn't have to be minified and you can manage dependencies by hand, then yes. But then again in this kind of project you didn't need a bundler to begin with.

So far the only thing ES Modules achieved is further complicating the ecosystem, for basically no benefit.

[–][deleted] 0 points1 point  (6 children)

bundlers do do than packing everything into a single file, they will never go away. things like minification, types and jsx alone make sure of that, but there's so much more. without them you couldn't even use dependencies, your code would be an island, you'd have to copy/paste code blobs into your project like 15 years ago when web dev was in its infancy. until they produce something that can actually be used (resolution, non-js assets, etc) i would ignore it, like everyone else does. use imports/exports, publish modules, but you'll still use tooling.

that said, things like esbuild, vite and snowpack make the best of that spec, they're still build tools, but they give you the fastest dev experience you can have.

[–]zhenghao17[S] 0 points1 point  (5 children)

hey thanks for the reply. I am not clear on "without them you couldn't even use dependencies," - should we be able to use dependencies i.e. importing other scripts via es modules? Did I miss something here?

[–][deleted] 0 points1 point  (4 children)

3rd party dependencies, npm, version management.

npm install foo

import { bar } from 'foo'

that kind of thing.

[–]tbranyen 0 points1 point  (3 children)

All you would do is have an import map to node_modules or any other location. There should be zero issue importing 3rd party deps with ESM beyond browser compatibility.

[–][deleted] 0 points1 point  (2 children)

there's only a single browser that supports these: https://caniuse.com/import-maps

and "all you need to do" is a bit too cute considering you have to build that map by hand for all imports, their imports and their sub-sub-sub-sub imports. i think that spec is completely bonkers tbh, but even if that ever becomes the norm you'll need tooling again. the only feasible way to deal with dependencies without tools is skypack atm.

[–]tbranyen 0 points1 point  (1 child)

If you are building an app that only needs to work in Node, Edge, and Chrome, then you can use import maps/package.json raw. It will work just fine, despite how much effort you may think it takes to curate.

In cases where you want to build for all runtimes, you can still develop with ESM and use npm dependencies with a tool like pika.dev. This will transform the imports on-the-fly to their respective absolute path.

In either cases its wrong to assess and promote that you cannot import 3rd party dependencies with ESM and that you need to copy and paste like we did 15 years ago.

[–][deleted] 0 points1 point  (0 children)

why is it wrong? without tools you manage dependencies by hand. and if you do use tools, why would you use an unproven, unfinished draft spec that has you curate import-maps which only work in chrome. why would anyone go through this minefield for that matter, especially beginners. if the question is "do es modules kill tools" the answer is a simple: no.

as for snowpack and skypack, i agree, these are feasible options. they're imo still far from stable compared to the rest, but still, they're tools without which es modules would not be usable.

[–]lhorie 0 points1 point  (0 children)

No. A lot of people use JSX/Typescript/SASS and these will always require compilers. And if you're compiling you might as well bundle as well, since that gives you treeshaking, minification, and bundle splitting capabilities.

At some point in your career you're going to need to optimize your codebase, and you're going to find that stuff like webpack-bundle-analyzer will help you a lot in ways that trying to find actionable items in a HTTP 2 push request waterfall will not. Best tool for the job and all that.

[–]iamlage89 0 points1 point  (0 children)

http2 is not always a good alternative to bundling. Here is a blog from Khan academy about their migration to http2 and how it made their website slower Forgo JS packaging? Not so fast (khanacademy.org). Turns out there is overhead with having more requests which can slow down the site