I'm hoping to take some JavaScript and run a minifier on it. Uglify (said to be the standard) is itself written in Javascript and the only packages I've seen involve a Node installation or a Ruby installation. At the moment, I'd settle for just a website that minifies/compresses Javascript (i.e., paste your code in the website, click 'compress' and then download the result).

Are there no tools written in CLI or in PHP or otherwise easily found without polluting one's workstation with npm or ruby?

    dalecosp;11065583 wrote:

    Are you looking for something more in-depth than, for example, https://javascript-minifier.com/?

    Yes. I've tried a few online ones so I guess at the very least I'm looking for endorsements from respected peers.

    I tried your link and it chokes on this line of Javascript:

    const VLNT_VALIDATION_URL = "/ajax/validate"; // url of validation endpoint

    This, unfortunately, boggles the entire script:

    // Error : Unexpected token: keyword (const)
    // Line  : 8
    // Col   : 0

    I also tried the pretty nice looking one suggested by google:
    https://closure-compiler.appspot.com/home

    But the Simple option is not very compressed at all (many many lines of code instead of one super-compressed line) and the 'advanced' option barfs on even the simplest stuff, yielding no output at all. For example, using the advanced option on this Javascript:

    var myclass = function(p1, p2) {
      this.publicProperty = null;
    };
    

    Yields no output but a warning:

    JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 2 character 2
      this.publicProperty = null;
      ^
    

    Now I wouldn't claim to be Javascript master, but my code has been working fine. If there's some better way to approach the creation of public properties, I'd be psyched to do the orthodox thing.

      I guess Mr. Chilton suffers from the same issue that most of the rest of us do ... too busy to stay ahead of the curve.

        dalecosp;11065587 wrote:

        I guess Mr. Chilton suffers from the same issue that most of the rest of us do ... too busy to stay ahead of the curve.

        UGH it's so true! I guess I might actually bother to install npm and then install node-uglify.

          I remember using Yahoo's YUI compressor; they may have kept that around after shutting down YUI generally a few years back.

          Query: the reason for having minification/compression. Is it an obfuscation thing? If it's to reduce over-the-wire bandwidth you could instead have the server deliver it gzip-compressed. If the resource is compressed then minification doesn't produce any further reduction (the minification would only strip out redundancies that zipping would strip out anyway).

          sneakyimp wrote:

          Now I wouldn't claim to be Javascript master, but my code has been working fine. If there's some better way to approach the creation of public properties, I'd be psyched to do the orthodox thing.

          You could refer to the object by name ("window", I guess) rather than using [font=monospace]this[/font]. Or you could create an object to serve as a namespace and declare all your stuff inside it; then the only "global" object you create is the namespace.

            Weedpacket;11065589 wrote:

            Query: the reason for having minification/compression. Is it an obfuscation thing? If it's to reduce over-the-wire bandwidth you could instead have the server deliver it gzip-compressed. If the resource is compressed then minification doesn't produce any further reduction (the minification would only strip out redundancies that zipping would strip out anyway).

            I have to admit I do like the idea of some of this code being obfuscated as it contains a couple of payment gateway api keys in it. It is presumably safe to put these in one's Javascript as that's what the payment gateway recommends. But the primary motive is page load speed. The client has made it clear that they want pages to load quickly under the belief that our search engine ranking would be adversely impacted for slow page load times.

            I'm not familiar with gzip delivery options -- although I vaguely recall that this is possible. As a developer, I'm mostly interested in having compressed delivery be as easy as possible. Our process for reducing these Javascript files currently involves manually minifying them with some tool and we have a single PHP controller that aggregates all our minified JS into a single JS file and then delivers the aggregated, minified JS. Our server caches this JS server-side to enhance performance. I believe we also send a header with the JS to cache it on the browser side. Having loaded it just now, the file, which comprises nearly all our site's JavaScript, is about 225KB. Here's the request header:

            GET /minify/script.js HTTP/1.1
            Host: example.com
            User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0
            Accept: */*
            Accept-Language: en-US,en;q=0.5
            Accept-Encoding: gzip, deflate, br
            Referer: https://example.com/my-account/payment
            Cookie: csrf=0c17676b225ab12e1cd495bd9517b695; mysite_session=nnslsd5a7vfdhde0h4ovkdebbkabskmr
            DNT: 1
            Connection: keep-alive
            Pragma: no-cache
            Cache-Control: no-cache
            

            The no-cache stuff worries me a bit. I wonder if perhaps it's because I did a shift-click on refresh to force page reload.

            Here's the response header:

            HTTP/1.1 200 OK
            Date: Fri, 02 Mar 2018 18:45:16 GMT
            Server: Apache/2.4.7 (Ubuntu)
            X-Powered-By: PHP/5.5.9-1ubuntu4.23
            Set-Cookie: csrf=0c17676b225ab12e1cd495bd9517b695; expires=Fri, 02-Mar-2018 20:45:16 GMT; Max-Age=7200; path=/; domain=.example.com
            Expires: Sun, 01 Apr 2018 18:45:17 GMT
            Cache-Control: max-age=2592000
            Pragma: cache
            Keep-Alive: timeout=5, max=100
            Connection: Keep-Alive
            Transfer-Encoding: chunked
            Content-Type: Content-Type: application/javascript; charset=UTF-8
            

            If I'm not mistaken, that Content-Type header says we are clearly not sending the content gzipped. And yes I know php 5.5.9 is old. This is my workstation running Ubuntu 14 LTS. I'll be upgrading to Ubuntu 18 LTS in April or so.

            Weedpacket;11065589 wrote:

            You could refer to the object by name ("window", I guess) rather than using [font=monospace]this[/font]. Or you could create an object to serve as a namespace and declare all your stuff inside it; then the only "global" object you create is the namespace.

            I learned that the Closure Compiler was eliminating critical constructors from the compressed code because no code had called this constructors. This seems like a ridiculous "feature" to me. I installed node-uglify using apt and this command is rocking:

            uglifyjs -v --max-line-len 200000 -nc --no-dead-code -o ./output-file.js ./input-file.js
              Write a Reply...