简体   繁体   English

jQuery的闭包编译器命令行

[英]Closure compiler command line for jQuery

I found on the Internet severals forum about importation of jquery in compiled file 我在Internet Somes论坛上找到了有关在编译文件中导入jquery的信息

https://developers.google.com/closure/compiler/docs/api-tutorial3?csw=1 https://developers.google.com/closure/compiler/docs/api-tutorial3?csw=1

Using Google Closure compiler 使用Google Closure编译器

Is it possible to use Closure Compiler ADVANCED_OPTIMIZATIONS with jQuery? 是否可以在jQuery中使用Closure Compiler ADVANCED_OPTIMIZATIONS?

They talk about Externs but when I compile my code I got an error : 他们谈论Externs,但是当我编译代码时出现错误:

$ is not defined $未定义

I tried this commands : 我尝试了以下命令:

java -jar compiler.jar --js js/test.js js/test.js --js_output_file min-js/app.min.js --externs jquery-3.3.externs.js --compilation_level ADVANCED_OPTIMIZATIONS java -jar编译器.jar --js js / test.js js / test.js --js_output_file min-js / app.min.js --externs jquery-3.3.externs.js --compilation_level ADVANCED_OPTIMIZATIONS

java -jar compiler.jar --js js/test.js js/test.js --js_output_file min-js/app.min.js --externs https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js --compilation_level ADVANCED_OPTIMIZATIONS Java -jar编译器.jar --js js / test.js js / test.js --js_output_file min-js / app.min.js --externs https://cdnjs.cloudflare.com/ajax/libs/jquery/ 3.3.1 / jquery.min.js --compilation_level ADVANCED_OPTIMIZATIONS

test.js is a simple code that i want to compresss : test.js是一个我想压缩的简单代码:

$( document ).ready( function() {

    var val = 0;

    //document.querySelector( ".button" ).addEventListener( "click", upButton );
    $( ".button" ).on( "click", upButton );

    function upButton() {

        val++;
        document.querySelector( ".show" ).innerText = val;

    }

} )

Test JakeParis 测试巴黎

File test.js 文件test.js

jQuery( function($) {

    var val = 0;

    //document.querySelector( ".button" ).addEventListener( "click", upButton );
    $( ".button" ).on( "click", upButton );

    function upButton() {

        val++;
        document.querySelector( ".show" ).innerText = val;

    }

} )

Command 命令

java -jar compiler.jar --js js/test.js --js_output_file min-js/app.min.js --compilation_level ADVANCED_OPTIMIZATIONS --externs js/jquery.js java -jar editor.jar --js js / test.js --js_output_file min-js / app.min.js --compilation_level ADVANCED_OPTIMIZATIONS --externs js / jquery.js

Error error image 错误错误图片

This is probably due to the $ variable not being available in the global scope. 这可能是由于$变量在全局范围内不可用。 Oftentimes, the jQuery library is available globally as jQuery instead of $ . 通常,jQuery库在全球范围内以jQuery而不是$ Perhaps, you should use this instead: 也许,您应该改用以下方法:

jQuery( document ).ready( function($) {

which can also be shortened to 也可以缩短为

jQuery( function($) {

Then everything inside that code block can use the $ as a reference to jQuery. 然后,该代码块中的所有内容都可以使用$作为对jQuery的引用。

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

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