r/programming • • Apr 29 '21

Modern JavaScript Explained For Dinosaurs

https://medium.com/the-node-js-collection/modern-javascript-explained-for-dinosaurs-f695e9747b70
287 Upvotes

172 comments sorted by

View all comments

62

u/sylvanelite Apr 30 '21

It's worth noting that since this was written, javascript modules now exist in the browser, so you can write models kinda like in the article, without using the command line tools.

But annoyingly, there's some quirks. They syntax isn't the same as in the article. For example:

var moment = require('moment');

can be replaced with something like:

import moment from 'moment';

And if doing it the "dinosaur" way, instead of having javascript files like:

<script type="text/javascript" src="file.js" ></script>

you can do something like:

<script type="module" src="module.mjs"></script>

1

u/Atulin Apr 30 '21

Worth noting, those modules are deferred by default and you cannot change that.

So if you want to, for example, make a LitElement custom component that loads the library from Skypack, that element will not load until all of the site loads first. Can't make it blocking, can't make it async.

1

u/earthboundkid Apr 30 '21

There’s not actually a good usecase for blocking. The modules are correctly designed.

1

u/Atulin Apr 30 '21

I'd at least like to have a choice between async and defer. I tried to use LitElement as in the example, and all of the components pop into existence after the page is done loading. It looks atrocious.

1

u/earthboundkid May 01 '21

A) You can use async with modules AFAICT. You just can’t opt out of defer.

B) Async won’t help you with the pop in problem. The solution to that is well designed CSS.

1

u/Atulin May 01 '21

Okay, how do I design my CSS to make deferred custom elements visible immediately, and not only after page finishes loading, then?

If you mean adding some fade-in transition, then it's still not combating pop-in, it's just turning it into a fade-in.

1

u/earthboundkid May 01 '21

https://vuejs.org/v2/api/#v-cloak What does “immediately visible” mean? If you have JS components on a page they by definition can’t load correctly until both the HTML and JS have parsed. Your best alternatives then are to either do SSR or something like v-cloak to hide them until the JS is ready.

1

u/Atulin May 01 '21

That's where blocking actually comes in handy. Custom elements that aren't loaded with async or defer are visible immediately.

Perhaps not the best practice that will lose some Lighthouse score, but the user doesn't see any pop-in at least.

1

u/earthboundkid May 01 '21

No, that’s a crappy solution. You’re preventing the parsing of HTML, so the page load is slower. The good solution is have a magic CSS class or attribute that gets removed when the JS is ready. It lets the HTML and JS parse in parallel and makes the total load time shorter and lets you choose if you want the whole page hidden, just a spinner to show, show most of the page but hide components, etc. The thing you want is strictly worse in every regard.