33
Root
2y

So, I was going to complain about JS being finicky and not making a damned bit of sense, but it turns out that it wasn't JS's fault. Not entirely, anyway. It was the halfassed JS minifier middleware (written by the legendary dev himself) that was breaking the JS while writing it to the page.

The original problem:
My code worked. I removed some comments. Big ol' block of //'s. And suddenly $() isn't a function. But if I call $(); at the top, it all works!

It turns out the "minifier" caused JS to think my code was chaining off the previous JS line in the rendering pipeline instead of being a separate statement. so all it really needed was a `;` at the start. What threw me, though, was the last line of the previous blob of (non-minified) JS was a comment, so it should be a separate statement, right?

But as it turns out...

```
console
// JS really is finicky.
.log('Sigh.');
```

Comments
  • 4
    I opt to say it's a feature. Gotta add more stress to a developer's day somehow!
  • 0
    The words "Big ol'" reminds of a meme I shouldn't say here but I do the newline dot thing
  • 4
    Yes.

    While ; is technically not always required they can still serve a purpose and minifiers are not trivial to do right ;)

    I would not use some inhouse minifier when there are so many existing ones that quite battle hardened.
  • 3
    @Voxera I wouldn’t either.

    All this one does is put all of the “minified” script blocks after one another, strip out comments, and remove newlines. I think it adds a randomized nonce attr, too. It’s pretty low-effort.

    I’ve thought about replacing it with a proper minifier, but I doubt I’d be allowed.
  • 1
    @melezorus34 I also do the newline thing once in awhile, but never with a comment between it and what it chains off.
  • 5
    Talk about reinventing the wheel…
    I mean we’re talking about replacing a luxury carbon fiber wheel with a termite infested wooden wagon wheel.

    And when you offer to upgrade? No we want to stick to our lopsided solution, just because
  • 3
    @black-kite One does not question the legendaries, let alone best them.
  • 0
    @Root hail to the rockstars!
  • 2
    @black-kite Also, replacing the minifier would probably break several things things due to how poorly some features are written.

    For example, there are a bunch of partials (blobs of markup/styling/js that are supposed to be self-contained) that together are responsible for the embedded dialog used internally and on third-party sites. The JS in those partials pass state and data amongst one another through global variables, and the control flow meanders through them and back again. (And no, there is no state machine keeping track of it.) Why? One does not question the legendary. But minified, these globals would likely have different names since they’re not namespaced in any way. This is the case in several other features, too.

    “Lopsided and termite-infested” describes most of the product, honestly.
  • 0
    slightly related question from a web dev noob: is it a good practice to use unnecessary semicolons? i believed that if i wrote my js files in a clean enough format, I don't need to add semicolons on every line. but i am not sure about how minifying would impact it
  • 1
    nvm i just read the comments
  • 0
    @dotenvironment The only time you actually need semicolons in JS is before an IIFE (though a bang works just as well) or if you’re doing multiple statements per line for whatever reason.

    Minifying takes your code and produces a functionally identical, yet tiny and unreadable rewrite of it. They’re basically recompilers that optimize for code size, but often have other optimizations as well. But because of this, the presence or lack of semicolons (apart from the above) is irrelevant. tl;dr don’t worry about it; the minifier is smart enough.

    This rant is about a pretty special case where someone really screwed up and now everyone else suffers for it — but it’s ofc hailed as a good thing because woke company and office politics.
  • 0
    @Root yes that's what i figured out, minifiers are mostly alright, thanks.
    anither thing: do iife present any unique usecase? i never liked their syntax , and rarely ever require a function to be immediately invocable
  • 1
    @dotenvironment private variables before block scopes was only achieveable with functions.

    You could make sure only your code changed your variables when using iifes
  • 1
    @dotenvironment What @melezorus34 said: everything within an IIFE is hidden and inaccessible from the outside unless you specifically allow it via getters/setters. Which is also useful to avoid scope/namespace polluting, and it also allows scope encapsulation.

    They’re useful.
  • 1
    @Root Sure you do, thats one of my mantras, question everyone.

    If they cannot defend their code it probably needs replacing snd I expect my colleagues to do the same to me even if they are fresh out of school and are younger than my kids.
Add Comment