Skip to content

Instantly share code, notes, and snippets.

@dmitriid
Last active November 12, 2016 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriid/7bd6f2c10d263bae40e0addc7edd5281 to your computer and use it in GitHub Desktop.
Save dmitriid/7bd6f2c10d263bae40e0addc7edd5281 to your computer and use it in GitHub Desktop.
Why I'm not using Google's closure compiler

The question

tl;dr: Closure compiler makes little to no sense outside of Google's ecosystem.

Nolan Lawson conducted a very nice research on how various Javasctip tools bundle/compile Javascript. I highly recommend it: The cost of small modules.

This article has made several rounds on Twitter and many people have asked: Why aren't more people using Closure? There are many reasons for that.

Considering the dire state of Javascript tools today, Google's Closure compiler is yet another cryptic, badly configured, half-baked tool to throw into the ever-groing pile of twigs and sticks called Javascript infrastructure.

Documentation

There's basically none.

The Quickstart shows how to create a page that contains basically exactly one line of Javascript code. Instead of showing you how to do more, the next step is Advanced compilation which is basically Javascript one-liners interspersed with Python code to invoke the compiler service.

  • How do I set up a project to compile my application?
  • How do I properly process the multiple modules my application has?
  • How do I include/process third-party modules?
  • How do I bundle stuff?
  • etc.

All these are unanswered.

Let's download the app then. There could me more info.

To run this you need a library you don't need

The README for the compiler has this nugget:

If you're using globs or many files, you may start to run into problems with managing dependencies between scripts. In this case, you should use the Closure Library. It contains functions for enforcing dependencies between scripts, and Closure Compiler will re-order the inputs automatically.

If you follow the link, this is the page you land on:

The Closure Library is a broad, well-tested, modular, and cross-browser JavaScript library. You can pull just what you need from a large set of reusable UI widgets and controls, and from lower-level utilities for DOM manipulation, server communication, animation, data structures, unit testing, rich-text editing, and more.

WAT?

I want to handle my dependencies, not have a UI library. Maybe they refer to ClosureBuilder? I have no idea.

Let's maybe run it?

I have some code in my app that consists of multiple modules, relies on some third-library code (node_modules) etc. The entry point is index.js

How do I run it so that it generates code I need to run my app in the browser?

However you run it, it only produces some minified code that:

  • Doesn't include any dependencies (except those directly in the folder)
  • Generates code that throws a require is not defined error in the browser

At this point I'm ready to give up because, well, I already have my sweet set up that handles everything, transpiles, and compiles, and minifies, and bundles all code.

Switching to ADVANCED compilation modules may pollute your console output with mutiple warnings or errors that are also not helpful in the least.

In (small) conclusion

Google's Closure compiler makes no sense outside Google infrastructure. When you have set up everything the Google way and there are people to help you along as you run into problems, you're ok.

When you're alone (as most devs are), you will either spend an unknown number of hours going through error-messages, disassembling the setups of other projects (ClojureScript, Angular 2)...

Or just use the tools which you kinda sorta can setup without going insane

@martinklepsch
Copy link

Agree with most of your observations but feel like you jumped to a conclusion "Closure is useless outside of Google" too quickly.

The closurescript community has (by now, not always been that way of course) very solid tools wrapping Closure and I've rarely ran into issues with it that couldn't be solved by a few minutes of googling. Of course when you approach Closure Compiler alone you won't have a community that solved these issues for you but that's the same with babel and ES6 and whatever; it's easy because someone else figured out a way to package it up nicely.

Once someone has done that using Closure might be just as easy as using your current thing (which apparently is also close to driving you mad so maybe it's not to hard to reach that point after all?)

@statonjr
Copy link

statonjr commented Nov 1, 2016

Someone recommend that I read "Closure: The Definitive Guide". Closure made more sense after spending a weekend with the book. https://www.amazon.com/Closure-Definitive-Guide-Michael-Bolin/dp/1449381871

@dmitriid
Copy link
Author

dmitriid commented Nov 3, 2016

@statonjr Thanks!

@martinklepsch
Copy link

There also seems to be some work by @roman01la to make Closure easier to use from Webpack: https://github.com/roman01la/webpack-closure-compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment