2

I configured JS and CSS compressor for a project but I'm not noticing real minification of the Javascript files.

In my production.rb I have:

config.assets.css_compressor = :sass
config.assets.js_compressor = :uglifier

CSS seems to be all minified (not only bundled together).

I'm deploying to Heroku and I notice that it precompile my assets and it's probably got some GZip compression too.

But somehow JS files still got tons of spaces, variables all kept their default name, so I guess it's only bundling all files in application.js.

Am I getting something wrong or that's the expected output? Should I configure something apart if I want real minification of those files?

What I want is something similar to what is produced in http://jscompress.com/ or what is found on the minified jQuery version.

Thanks!

lucasarruda
  • 1,462
  • 1
  • 25
  • 45
  • You are saying that your browser on a production url shows un-minified js and css files right? Because if you are just talking about your development environment, it is expected that you would see un-minified assets. – MilesStanfield Mar 01 '16 at 20:59
  • Yes, I'm not talking about my local (development). I'm talking about my production environment. On Heroku, with Production settings, together with Staging environment. They both got un-minified JS. CSS looks fine (minified). – lucasarruda Mar 01 '16 at 21:07
  • @MilesStanfield, I discovered I was modifying the wrong JS files to trigger the compression. That is a necessary step to trigger the compression, otherwise Rails will use the cache and it won't trigger it. – lucasarruda Mar 02 '16 at 21:35

1 Answers1

1

I discovered I was modifying a Javascript that was not on the assets pipeline, therefore the compression/minification wouldn't trigger.

When I modified the correct file (being loaded on application.js, the minification triggered correctly [0].

So, always remember to modify a Javascript file [that is being loaded on the assets pipeline] so you trigger the compressor, otherwise Rails will use the cached javascript files and they won't be compressed.

[0] Watch also for invalid chars: UglifyJS ended up failing because of a comment in the first line of a .js file. But after I removed the comment (<!-- (comment) ... ->), it worked properly and I could see the application.js minified in staging. More about it here https://stackoverflow.com/a/35751343/135767.

Community
  • 1
  • 1
lucasarruda
  • 1,462
  • 1
  • 25
  • 45
  • This is totally wrong. You do not have to modify a file to "trigger" asset compilation, whatever "trigger" means in this context. – user229044 Mar 02 '16 at 22:03
  • 1
    I didn't said compilation. Compilation is always triggered. I said **compression**. Compression or minification is only triggered when an asset is modified (and therefore it's mtime). See http://stackoverflow.com/a/7988689/135767 for more details. – lucasarruda Mar 02 '16 at 22:51
  • Please correct me if I'm wrong and I'll happily edit or delete my answer. – lucasarruda Mar 10 '16 at 20:55
  • As far as I am concerned, your entire question and answer is wrong and has no value. Assets are **precompiled** via `rake assets:precompile`, and compressions happens at that time. The idea of "triggering" asset compilation by modifying a file makes no sense. Even if a file's mtime was somehow relevant, **use `touch `**. Adding and removing garbage lines from a file just to update its mtime is insane. – user229044 Mar 10 '16 at 21:07
  • 1
    I didn't add lines, I just edited a file with `touch`. Heroku would `precompile` files but they weren't being compressed with Uglifier until I _touched_ a file in the assets pipeline. I believe it is due to cache. The problem I had with the comment in the file is just a side node, because `<` fails in UglifyJS if it's included in the beginning of the fil. It has nothing to do with the main solution to the problem, it's just a common issue with compression. – lucasarruda Mar 10 '16 at 21:15