How can I help Google's Closure Compiler optimize my JavaScript code? - javascript

I've been tinkering with Google's Closure Compiler and was able to get everything compiling perfectly using the advanced compilation option. How can I get the best compilation results from my code?

Read about Advanced compilation.

Related

Compiling Dart into minifier friendly javascript: From dartdevc into google-closure-compiler

What compiler options are best to ensure that dartdevc generates minifier friendly javascript code which can be compressed by google closure compiler in ADVANCED mode.
Please show a tested example that specifies options for 1. dartdevc, and 2. java -jar goolge-closure-compiler.jar as a simple bash script, without pub.
Module type should be 'common' if possible, dart_sdk.js should be included, the final result should be es3 or es5 for compatibility with all browsers, and all output goes into one compressed .js file.
The dartdevc compiler is not meant for production usage at this time, and does not support any sort of "advanced" optimizations (such as those done by the Google Closure Compiler). Our only supported optimization path is using dart2js, our optimizing compiler which in many cases is as good as or better the Google Closure Compiler.
See "When should I use dartdevc" on our FAQ page:
Use dartdevc whenever you’re actively working on your code.
Keep using dart2js to build your deployed, production application. With dart2js you get advanced optimizations such as tree shaking to minimize downloaded code size.
I'm excited you'd like to see dartdevc work for more use cases, but we are concentrating on a great developer experience and keeping optimization usage in dart2js at this time.

Javascript Intellisense in IDE

How does Intellisense work in IDEs for JavaScript such as Webstorm or Eclipse?
Where do the suggestions come from?
Can we write the code to make the suggestions more accurate?
Please see http://blog.jetbrains.com/webstorm/2014/07/how-webstorm-works-completion-for-javascript-libraries/ for some hints on improving JavaScript code completion in WebStorm. In general: suggestions are taken from index that is built for all .js and d.ts files available in the project/set up as libraries. To make the completion better, you can try using JSDoc annotations/typescript definition stubs
In Eclipse you can write plug-ins which use the org.eclipse.wst.jsdt.ui.javaCompletionProposalComputer extension point to contribute additional Content Assist proposals.
Eclipse gives you access to an Abstract Syntax Tree of the code to help with analyzing the source.
Disclaimer, I'm the author of tern.java
I suggest you that you try tern.java by starting to read Getting Started

google closure compiler - double compilation

I finally got to the point where my javascript code compiles in the google closure compiler without any errors or warnings. Now I want to recompile the code generated and when I paste that code back into the compiler, I get over 100 warnings: most of them are JSC_REDECLARED_VARIABLE and a few JSC_INEXISTENT_PROPERTY.
Why is that?
I do not think the Google Closure Compiler produces code that is intended for further compilation.
To have code compile correctly you have to keep some structure. But that extra structure is among the things removed by the compiler and without it the compiler cannot correctly interpret the code.
You should be able to do the easier modes of compilation but not the advanced one.
REDECLARED_VARIABLE is a WARNING not an error. It is intended to indicate a likely problem to the developer. A developer might ignore this if they know what they are doing and the compiler does.

Are there JSDoc files for Firefox XPCOM?

While I know much of XPCOM is implemented in C++, it would still be great to have JavaScript stubs with empty functions, constants, and JSDoc. These could be used to support code completion, inspection, quick doc, and other features of IDEs like WebStorm (IntelliJ).
It would be great if this just existed somewhere (but I haven't found it). Another approach would be to try and generate them from the IDL, but I haven't found a a way to do that either. I have question on that at Are there JSDoc files for Firefox XPCOM?.
I wrote a PHP script once, which generates PHP stubs from PHPDoc available at http://www.php.net for providing code assist in Eclipse PDT. You can use similar approach to generate JSDoc from XPCOM Doxygen.

Testing minified javascript

Should we test the minified versions of our javascript files as we develop them, or is it an extremely low risk that the minified javascript does not differ in function from the un-minified version?
Running your test suites against them should be enough.
...
You do have test suites... right?
Run them through jslint before minifying them and if they pass that they should minify without a problem. The key here is to not forget a ; since minifying will remove all linefeeds. Also declaring variables helps the minifying process, but not doing so will not break anything by minifying.
I have not seen any of my scripts behaving differently so far after minifying them but sill i do test them before making them public just to make sure everything has been done correctly.
And you are supposed to sort of test it before using/making it public just to make sure that it works the way you wanted.
If you have done everything correctly in non-minified version, it should be not a problem.
It depends on what you minify with. Closure Compiler and YUI Compressor fully tokenize and parse scripts and tend to handle almost anything most browsers will accept. JSMin (particularly modified versions) are mostly reliable, but stay away from anything regex-based.
Agreed on svinto's advice.

Categories