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