0

I am trying to compile all the source code in my web site that is loaded at startup into a single file. Some code is loaded using $.getScript so I need to be able to leave access to jQuery and a few other functions. Below is what I did. What am I doing wrong?

I have read through the google tutorial at: https://developers.google.com/closure/compiler/docs/api-tutorial3 To test it I tried compressing jquery 3.1.0 removing everything but show and hide so I appended the following to the end.

window['jQuery'] = window['$'] = jQuery;
jQuery.prototype['hide'] = jQuery.prototype.hide;
jQuery.prototype['show'] = jQuery.prototype.show;

I then compiled using

java -jar compiler.jar --js_output_file all.js --compilation_level ADVANCED --create_source_map all.map --js _common/jquery/js/jquery-3.1.0.js

and added //# sourceMappingURL=/all.map to the end of the outputed file.

To test I created a simple web page

<html>
    <head>
        <script src="/all.js"></script>
    </head>
    <body>
        <div id="a1" onclick="$.hide('#a1');">a1</div>
        <div id="a2" onclick="$.hide('#a2');">a2</div>
        <div id="a3" onclick="$.hide('#a3');">a3</div>
        <div id="a4" onclick="$.hide('#a4');">a4</div>
    </body>
</html>

When I run this I get the following errors in the console.

jquery-3.1.0.js:7441 Uncaught TypeError: Cannot read property 'source' of undefined
jquery-3.1.0.js:3853 Uncaught TypeError: Za.ga is not a function
test.html:8 Uncaught ReferenceError: $ is not definedonclick @ test.html:6

Am I doing something wrong or is this beyond google closure compilers capability. I don't mind using a different minifier as long as I can run it from php using shell_exec

  • To my knowledge, jQuery isn't internally annotated for closure compiler, so it won't just be as simple as that. Why not just replace the little bit of jQuery that you need? –  Sep 08 '16 at 13:08
  • This is just an example. I wish to append several of my own scripts with it. The compiler works as long as I use basic compression but it would be nice to get advanced working – Matthew Cornelisse Sep 08 '16 at 13:10
  • for jQuery itself I wish I knew how to export everything. It is my own code that I wish to limit to just a few functions. – Matthew Cornelisse Sep 08 '16 at 13:13
  • 1
    Possible duplicate of [How to make Jquery work with google closure compiler](http://stackoverflow.com/questions/16461915/how-to-make-jquery-work-with-google-closure-compiler) and several others – google for "closure compiler with jquery". – JJJ Sep 08 '16 at 13:14
  • If you want to keep all of jQuery, then you just wouldn't compile it with the rest of the app. For your own code, [read through](https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler) annotating it for advanced compilation. –  Sep 08 '16 at 13:15
  • @Juhana I did not see that one and you are probably right. However adding the `--process_jquery_primitives` flag as recomended results in an error: `The jQuery pass and the Closure pass cannot both be enablled` – Matthew Cornelisse Sep 08 '16 at 13:22

1 Answers1

0

Any script included as source in an ADVANCED compilation, must be fully compatible with the ADVANCED mode of the compiler. jQuery is not.

There is no easy way to make it compatible either. It violates too many of the assumptions made by the compiler for ADVANCED mode.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57