0

I want to compile jQuery 2.0.0 using the Google Closure Compiler. When I do it using the this extern which states Externs for jQuery 1.9 - 1.11 & 2.0 - 2.1

I get the following error: Uncaught TypeError: Cannot read property 'source' of undefined. That flags up the following piece of compiled code:

d.each(d.i.match.ja.source.match(/\w+/g), function(a, b) {
  var c = ra[b] || d.find.attr;
  ra[b] = function(a, b, d) {
    var h, k;
    d || (k = ra[b],
      ra[b] = h,
      h = null != c(a, b, d) ? b.toLowerCase() : null,
      ra[b] =
      k);
    return h
  }
});

That code corresponds to here in the non-compiled code:

jQuery.each(jQuery.expr.match.bool.source.match(/\w+/g), function(i, name) {
  var getter = jQuery.expr.attrHandle[name] || jQuery.find.attr;

  jQuery.expr.attrHandle[name] = getSetInput && getSetAttribute || !ruseDefault.test(name) ?
    function(elem, name, isXML) {
      var fn = jQuery.expr.attrHandle[name],
        ret = isXML ?
        undefined :
        /* jshint eqeqeq: false */
        (jQuery.expr.attrHandle[name] = undefined) !=
        getter(elem, name, isXML) ?

        name.toLowerCase() :
        null;
      jQuery.expr.attrHandle[name] = fn;
      return ret;
    } :
    function(elem, name, isXML) {
      return isXML ?
        undefined :
        elem[jQuery.camelCase("default-" + name)] ?
        name.toLowerCase() :
        null;
    };
});

Below is what I'm entering into the online closure compiler:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @code_url https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.js
// @externs_url https://raw.githubusercontent.com/google/closure-compiler/master/contrib/externs/jquery-1.9.js
// ==/ClosureCompiler==
// ADD YOUR CODE HERE

$("#foo").html("Example Text");

I was wondering if anyone knew how to fix the extern so that it will work with later versions of jQuery or if there was an extern available anywhere for later versions?

urbz
  • 2,663
  • 1
  • 19
  • 29
Kian Cross
  • 1,818
  • 2
  • 21
  • 41
  • 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) – Chad Killingsworth Mar 30 '16 at 13:02

1 Answers1

0

The extern you are using doesn't define expr anywhere. (I'm not finding online documentation for jQuery.expr, are you sure that is part of jQuery?).

I suggest adding some "sanity checks" at the start of your code, like this:

goog.require('goog.asserts');
goog.asserts.assert(goog.isFunction(jQuery.each));
goog.asserts.assert(goog.isObject(jQuery.expr));
goog.asserts.assert(goog.isObject(jQuery.expr.match));
goog.asserts.assert(goog.isObject(jQuery.expr.match.bool));
goog.asserts.assert(goog.isObject(jQuery.expr.match.bool.source));
goog.asserts.assert(goog.isFunction(jQuery.expr.match.bool.source.match));

That should help show if your externs are working with Closure Compiler.

Also for debugging use simple-compile, or even better run uncompiled.

The extern might not be complete, you can make your own copy and edit it. But I think then you would need to rebuild the compiler (I believe the externs become part of the compiler.jar file).

owler
  • 1,059
  • 8
  • 13
  • Externs are for external code - the asker is compiling the source of jQuery. jQuery source isn't compatible with the ADVANCED_OPTIMIZATIONS mode of the compiler. – Chad Killingsworth Mar 30 '16 at 20:50
  • OK, that explains why I didn't find `jQuery.expr`. Agreed, jQuery source is not going to compile with Closure Compiler. I believe you can use jQuery as uncompiled external code alongside your own code that is compiled with Closure Compiler (maybe not what OP wants). – owler Mar 30 '16 at 22:43