谷歌关闭编译jQuery插件 [英] google closure compile jQuery Plugin

查看:137
本文介绍了谷歌关闭编译jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 Google封闭编译器,并且希望编译

有人尝试使用此选项jQuery插件进行编译,但效果很好.

谢谢.

显然,这可以重命名jQuery方法,但是是否可以包含jQuery并平等地重命名所有方法呢?

编辑

带有"externs_url"选项的代码示例:

使用闭包编译器

js输入代码

// ==ClosureCompiler==
// @output_file_name default.js
// @formatting pretty_print
// @compilation_level ADVANCED_OPTIMIZATIONS
// @warning_level QUIET
// @externs_url http://code.jquery.com/jquery-1.5.min.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE

var test = function($, context) {

  var
    _self = this;

  _self.mymethod = function() {

    var lista = $("a", context);

    lista.attr("target", "_blank");

    return lista.html();

  };


  return {"mymethod":_self.mymethod};

}.call({}, jQuery, context);    

js输出代码

(function(b, c) {
  this.a = function() {
    var a = b("a", c);
    a.attr("target", "_blank");
    return a.html()
  };
  return{mymethod:this.a}
}).call({}, jQuery, context);

解决方案

在尝试压缩jQuery插件的自定义JS库时遇到了相同的问题.闭包(除非您给出拒绝的理由)只会重命名插件中的所有jQuery库调用.更糟糕的是,它甚至不会警告您(怎么办?它假定您程序员已经知道自己在做什么!)解决方案是引用外部js文件(或指向库的url).我使用了网络工具: http://closure-compiler.appspot.com/home

通过应用愚蠢的前言,我能够成功编译文件:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @warning_level QUIET
// @output_file_name default.js
// @formatting pretty_print
// @externs_url http:// ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
// @externs_url http:// ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.js
// ==/ClosureCompiler==

剩下的唯一问题是使编译器意识到插件名称无法简化:

(function($) { $.fn.extend({ <name>:function(options) { } }); })(jQuery);

因为这是暴露给全世界的名字.有什么想法吗?

I'm testing google closure-compiler and wanting to compile facebox Plugin with the option "Advanced" , an error occurs the function trying to find "a.H".

Has anyone tried to compile with this option jQuery plugins with a good result.

Thank you.

EDIT: Clearly this re-naming the jQuery methods, but is it possible to include jQuery and re-name all methods equally?.

EDIT

example of code with the option "externs_url":

with closure-compiler

js input code

// ==ClosureCompiler==
// @output_file_name default.js
// @formatting pretty_print
// @compilation_level ADVANCED_OPTIMIZATIONS
// @warning_level QUIET
// @externs_url http://code.jquery.com/jquery-1.5.min.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE

var test = function($, context) {

  var
    _self = this;

  _self.mymethod = function() {

    var lista = $("a", context);

    lista.attr("target", "_blank");

    return lista.html();

  };


  return {"mymethod":_self.mymethod};

}.call({}, jQuery, context);    

js ouput code

(function(b, c) {
  this.a = function() {
    var a = b("a", c);
    a.attr("target", "_blank");
    return a.html()
  };
  return{mymethod:this.a}
}).call({}, jQuery, context);

解决方案

I encountered the same issue when trying to compress a custom JS library of jQuery Plugins. Closure (unless you give it a reason not to) will just rename any jQuery library calls from within your plugin. And worse yet it will not even warn you (how could it? it assumes you the programmer know what you are doing!) the solution is to reference the external js file (or url points to the library). I used the web tool: http://closure-compiler.appspot.com/home

by applying the fowlloing preamble, i was able to compile the file successfully:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @warning_level QUIET
// @output_file_name default.js
// @formatting pretty_print
// @externs_url http:// ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
// @externs_url http:// ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.js
// ==/ClosureCompiler==

The only issue remaining is making the compiler realize that the plugins name, cannot be simplified:

(function($) { $.fn.extend({ <name>:function(options) { } }); })(jQuery);

Because this is the name exposed to the world. Any ideas?

这篇关于谷歌关闭编译jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆