简体   繁体   English

如何使用Google Closure编译器压缩可从外部脚本访问的代码

[英]How to use google closure compiler to compress code accessible from outside script

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. 使用$.getScript加载了一些代码,因此我需要能够保留对jQuery和其他一些功能的访问权限。 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. 我已经阅读了以下Google教程: https : //developers.google.com/closure/compiler/docs/api-tutorial3为了对其进行测试,我尝试压缩jquery 3.1.0来删除除showhide所有内容,因此将以下内容附加到结束。

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. 并将//# sourceMappingURL=/all.map到输出文件的末尾。

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. 我是在做错什么,还是Google闭包编译器无法提供此功能? I don't mind using a different minifier as long as I can run it from php using shell_exec 我不介意使用其他的压缩程序,只要可以使用shell_exec从php运行它即可

Any script included as source in an ADVANCED compilation, must be fully compatible with the ADVANCED mode of the compiler. 作为ADVANCED编译中的源代码包含的任何脚本,必须与编译器的ADVANCED模式完全兼容。 jQuery is not. jQuery不是。

There is no easy way to make it compatible either. 也没有简单的方法使其兼容。 It violates too many of the assumptions made by the compiler for ADVANCED mode. 它违反了编译器对ADVANCED模式所做的太多假设

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM