by crazypython · 5y ago · 0 comments
You also don't need a transpiler like Babel if you drop support for IE11. Almost all ES6 features– such as classes, proxies, arrow functions, and async are supported by modern browsers and their older versions. As long as you avoid features like static, private, and ??=, you get instant compile times.
It's not 2015. You don't need a transpiler to write modern JavaScript. As of writing, the latest version of Safari is 14 and the latest version of Chrome is 88. These features fully work in Safari 13 and Chrome 70, which have less than 1% of marketshare.
I suggest using a single-pass transpiler like Surcase[0] that doesn't translate to IE11-compatible syntax, and just loops through the string once, avoiding generating an AST– making it much faster– removing TypeScript annotations and desugaring JSX.
If you additionally need to support IE11, you can use a development build with Surcase and a production build with a bundler like Snowpack. C/C++ developers have been doing things like this for ages: compiling files as objects during development, and compiling them into one big binary for production.
2. How do you handle the performance impact of your browser doing multiple sequantial requests because it doesn't know your whole dependecy tree?
See my comment above.
> 2. How do you handle the performance impact of your browser doing multiple sequantial requests because it doesn't know your whole dependecy tree?
Put all your dependencies in the HTML file, so it fetches them all in parallel. So instead of just loading the root application file and forcing the browser to resolve dependencies:
<script type="module" src="/scripts/app.js" defer></script>
Load the dependencies like this: <script type="module" src="/scripts/lib/gui.js" defer></script>
<script type="module" src="/scripts/lib/guielements.js" defer></script>
<script type="module" src="/scripts/lib/network.js" defer></script>
<script type="module" src="/scripts/lib/input.js" defer></script>
<script type="module" src="/scripts/lib/rendering.js" defer></script>
<script type="module" src="/scripts/lib/sockets.js" defer></script>
<script type="module" src="/scripts/lib/world.js" defer></script>
<script type="module" src="/scripts/lib/ui/classupgrades.js" defer></script>
<script type="module" src="/scripts/lib/ui/toasts.js" defer></script>
<script type="module" src="/scripts/lib/netstore/minimap.js" defer></script>
<script type="module" src="/scripts/data/configuration.js" defer></script>
<script type="module" src="/scripts/app.js" defer></script>
You can see a demonstration of this technique on my production site, a complex JavaScript SPA that loads everything (including dynamic content) in less than one second: http://vnav.ioFor a small performance boost, you can load smaller files first via <link rel=preload>.
> Put all your dependencies in the HTML file, so it fetches them all in parallel.
> ...
> You can see a demonstration of this technique on my production site, a complex JavaScript SPA that loads everything (including dynamic content) in less than one second
But it doesn't work like this in practice! There is a limit to the number of concurrent connections a browser will make, Chrome for example is <=10 IIRC. You can see this in the waterfall:
`rendering.js`, the last `.js` in the queue, stalled for 300ms.
Additionally, each round trip is dependent on:
- The user's latency
- Your server's response time
So with each connection there is overhead. You would be much better served concatenating these files.
`util.js` took 510ms, of which 288ms was spent stalled (i.e. waiting for a connection) after which spent 220ms waiting (time-to-first-byte).
Furthermore, Lighthouse gives your page a performance score of 60, which isn't great. Key metrics:
- 1.7 seconds to paint (which is okay)
- 7.7 seconds to interactive (which is terrible)
Finally, why on earth are you redirecting to HTTP after serving an HTTPS response? This makes your page load even slower:
if (window.location.protocol === "https:") {
window.location.href = 'http:' + window.location.href.slice().slice(6);
}In HTTP/2, which our site will switch to soon, the concurrent limit is 100. The world's changed, change with it, and throw away those bundlers.
Either way, you can compare to our direct competitors: https://diep.io https://arras.io
> Finally, why on earth are you redirecting to HTTP after serving an HTTPS response?
This isn't really related to the argument at hand. My app is currently on HTTP for reasons downstream.
Also, there are tools that only bundle npm deps, like Snowpack or Vite. I think those would be perfect for your use case.
I'm on Ubuntu 20.04 using the latest Firefox, if that helps.
EDIT: Seems to load fine in Chromium.
In development, use an index.html that loads it directly from the filesystem, using a single-pass annotation remover such as https://github.com/alangpierce/sucrase A single-pass transpiler doesn't build an AST, instead just looping over everything once, which will surely make it faster than esbuild.
In production, use a bundler like snowpack.
And saying it quite a lot in this thread.
1: https://www.typescriptlang.org/docs/handbook/intro-to-js-ts....
When I do, I use a script tag, and put it before my ESM modules:
<script src="/scripts/data/contractor.js"></script>
Use them via global variables.I generally avoid third-party JavaScript modules for performance and maintainability reasons. When I do use them, I'll frequently vendorize them (put them in my source tree) and make ESM modules for them.
I love using plain ESM and I prefer to pull in stuff that only relies on that. But when I need graphing on a single page I'm not going to try to vendor Vega and I'm not going to pull it in on all pages. When I need xls parsing I don't want to vendor xls.js and maintain the diff myself.
So I have to use something like snowpack to make it work with my ESM system and dynamic imports.
If you don't have to require heavy libraries for certain pages then that's great and I encourage to use pure ESM without build systems for that, but you should also recognize that not all use-cases are ready for it.
Currently I use snowpack to handle dependencies but still run all my own code unbundled and unminified.
Add a <script src=> tag.
Access it by object destructuring a global variable. You can access ES5 stuff from ES6, just let ES5 run first.
That position isn't without good reason. Refer, for example, to the linked post, explaining the engineering decisions behind esbuild. ("It's Golang, not JS" is only part of the answer to "Why is esbuild fast?" The implications seem to be lost on many of esbuild's users, given the nature of esbuild itself and the job it's supposed to do.)
I mean there's a lot of garbage out there as with any ecosystem but saying "we all agree the JS ecosystem sucks" reeks of immaturity.
I love that TypeScript and esbuild both have 0 dependencies. For the first time I can have a build pipeline without transient dependencies.
Also your website is serving dozens of cascading files instead of a single minified one.
Feels like 2010 to me.
Bundlers aren't the "wheels" here. The real "wheels" here are ESM modules, which have been available in every modern browser for a while. Bundlers are just pre-wheel, prehistoric, stopgap technology from the 2010s that we should be moving away from.
And there's no problem serving multiple files in 2020 because we have HTTP2 multiplexing now. In fact, it's probably more efficient than using bundling, because you can cache much better. Minification is also virtually unnecessary with Brotli, and not minifying has the added benefit of making the debugging experience much better.
And bundlers haven't been "perfected". Not even close. Webpack is without a doubt the worst piece of technology in my stack right now, together with Babel. Those two are terrible by themselves, but they also manage to "infect" other things elsewhere in my stack: for example, ESLint and Jest need Webpack/Babel plugins to work.
I really dislike this line of thought: Babel isn't just a polyfill tool. It's an incredible way to enhance your code. Further, the future of javascript changes isn't static and shouldn't be.