script error if minified with closure compiler and ADVANCED_OPTIMIZATIONS option

191 views
Skip to first unread message

doom star

unread,
Jun 12, 2016, 5:21:01 PM6/12/16
to Paper.js
Hi evrerybody,

I have an error when I minify paper-core.js (v0.9.25) and my ultra simple script with Closure Compiler and ADVANCED_OPTIMIZATIONS option :  
`test-bug-minification.min.js:230 Uncaught TypeError: Cannot read property 'length' of undefined
k @ test-bug-minification.min.js:230
b.extend.g @ test-bug-minification.min.js:239
(anonymous function) @ test-bug-minification.min.js:238
(anonymous function) @ test-bug-minification.min.js:1

test-bug-minification.min.html:8 Uncaught ReferenceError: Test is not defined`

All goes well with SIMPLE_OPTIMIZATIONS option.

Here is my files : (all files attached also) : 

my no-minified html : 
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="test-bug-minification.js"></script>
<script src="paper-core.js"></script>
<script>
test=null;
window.onload=function() { test=new Test('myCanvas'); }
</script>
</head>
<body>
<canvas id="myCanvas" resize>Sorry no &lt;canvas&gt;.</canvas>
</body>
</html>`

my no-minified js : 
`function Test(canvas) {
paper.setup(document.getElementById(canvas));
var path = new paper.Path();
path.strokeColor = 'black';
var start = new paper.Point(100, 100);
path.moveTo(start);
path.lineTo(start.add([ 200, -50 ]));
path.lineTo(start.add([ 200, 150 ]));
paper.view.draw();
}

// for closure
window['Test'] = Test;
`

my html for minified js : 
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="test-bug-minification.min.js"></script>
<script>
test=null;
window.onload=function() { test=new Test('myCanvas'); }
</script>
</head>
<body>
<canvas id="myCanvas" resize>Sorry no &lt;canvas&gt;.</canvas>
</body>
</html>`


minification with latest version of closure compiler (v20160517) : 
`java -jar compiler.jar --compilation_level=ADVANCED_OPTIMIZATIONS --js paper-core.js test-bug-minification.js --js_output_file test-bug-minification.min.js`

Is it a nodejs bug, a closure compiler bug or a justMe bug ???

Thanks for your answer and your help if you know how to save me !

Doom.


test-bug-minification.html
test-bug-minification.js
test-bug-minification.min.html
minification.txt

doom star

unread,
Jun 26, 2016, 2:25:28 PM6/26/16
to Paper.js
up ! thx

Jürg Lehni

unread,
Jun 26, 2016, 5:22:54 PM6/26/16
to pap...@googlegroups.com
What's wrong with the minified version that we're providing?

If the ADVANCED_OPTIMIZATIONS settings produces an error in code that otherwise runs well, maybe don't use the option, or the minifier. I'm pretty sure this is a bug in the closure compiler, not in paper.js.

I'm happy to accept a PR that addresses whatever part of the code makes their minifier stumble.

J

--
You received this message because you are subscribed to the Google Groups "Paper.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to paperjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Doom

unread,
Jun 26, 2016, 5:44:30 PM6/26/16
to pap...@googlegroups.com
Hi !

I am not enough expert to know if the problem is in Paper or Closure, but think you are right. So I will open issue in closure compiler project, and we will see.

Thanks for your answer and good luck for the 0.10.1 milestone !

Doom.


--
You received this message because you are subscribed to a topic in the Google Groups "Paper.js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/paperjs/fRaPRFT-BSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to paperjs+u...@googlegroups.com.

doom star

unread,
Jun 27, 2016, 2:58:35 PM6/27/16
to Paper.js
Closure Compiler Issue posted : https://github.com/google/closure-compiler/issues/1863
Now I will just wait ...

doom star

unread,
Jun 28, 2016, 10:18:39 AM6/28/16
to Paper.js
Closure Compiler have some limitations : https://developers.google.com/closure/compiler/docs/limitations

Do you think that paperjs meet these limitations ?

Doom

unread,
Jun 28, 2016, 12:51:33 PM6/28/16
to pap...@googlegroups.com
I have compile PaperJS code with verbose flag in Closure Compiler and some warnings and errors come out.

I have seen the code and I think that there is effectively some redeclarations.

for example :
        'hsb-rgb': function(h, s, b) {
            h = (((h / 60) % 6) + 6) % 6;
            var i = Math.floor(h),
                f = h - i,
                i = hsbIndices[i],
                v = [
                    b,
                    b * (1 - s),
                    b * (1 - s * f),
                    b * (1 - s * (1 - f))
                ];
            return [v[i[0]], v[i[1]], v[i[2]]];
        }
i is declared twice.

and in :
function setLum(r, g, b, l) {
        var d = l - getLum(r, g, b);
        dr = r + d;
        dg = g + d;
        db = b + d;
        var l = getLum(dr, dg, db),
            mn = min(dr, dg, db),
            mx = max(dr, dg, db);
        if (mn < 0) {
            var lmn = l - mn;
            dr = l + (dr - l) * l / lmn;
            dg = l + (dg - l) * l / lmn;
            db = l + (db - l) * l / lmn;
        }
        if (mx > 255) {
            var ln = 255 - l,
                mxl = mx - l;
            dr = l + (dr - l) * ln / mxl;
            dg = l + (dg - l) * ln / mxl;
            db = l + (db - l) * ln / mxl;
        }
    }
variable l i redeclared (it's an argument of the function).


all warnings and  errors are reported below :

paper-core.js:1079: WARNING - Redeclared variable: count
                        var count = Numerical.solveQuadratic(a, b1, c2, roots, min, max);
                            ^
paper-core.js:2943: WARNING - Redeclared variable: name
                        name = '_' + name;
                        ^
paper-core.js:5228: WARNING - Redeclared variable: selected
                        selected = !!selected,
                        ^
paper-core.js:6928: WARNING - Redeclared variable: path
                        path;
                        ^
paper-core.js:7296: WARNING - Redeclared variable: index
                        index = append ? segments.length : index;
                        ^
paper-core.js:7411: WARNING - Redeclared variable: curves
                                curves = curves.splice(index, amount);
                                ^
paper-core.js:8241: WARNING - Redeclared variable: through
                                through = middle.add(middle.subtract(from).rotate(
                                ^
paper-core.js:8251: WARNING - Redeclared variable: clockwise
                                        clockwise = !!Base.read(arguments),
                                        ^
paper-core.js:10048: WARNING - Redeclared variable: i
                                i = hsbIndices[i],
                                ^
paper-core.js:10737: WARNING - Redeclared variable: value
                                var value = this._values[key];
                                    ^
paper-core.js:12220: WARNING - Redeclared variable: l
                var l = getLum(dr, dg, db),
                    ^
paper-core.js:232: ERROR - variable module is undeclared
if (typeof module !== 'undefined')
           ^
paper-core.js:11076: ERROR - variable Stats is undeclared
                                && typeof Stats !== 'undefined') {
                                          ^
paper-core.js:13368: ERROR - variable define is undeclared
if (typeof define === 'function' && define.amd) {
           ^
3 error(s), 11 warning(s)

Jürg Lehni

unread,
Jun 28, 2016, 4:25:48 PM6/28/16
to pap...@googlegroups.com
While those aren't great, I don't think any of them should produce the error that you observe.

but it should be easy enough to test that. You could rename the variables and their use and compress again to see if that solves it.

Jürg Lehni

unread,
Jun 28, 2016, 4:26:50 PM6/28/16
to pap...@googlegroups.com
PS:

The last three warnings aren't mistakes, but a common pattern and really the only way to detect in JS if a variable was declared or not.

On Jun 28, 2016, at 18:51 , Doom <doom...@gmail.com> wrote:

Doom

unread,
Jun 28, 2016, 4:54:16 PM6/28/16
to pap...@googlegroups.com
Yeah, I have modified the paper-core.js for the redeclaration and it effectively doesn't change the result (I have not renamed the variables, but take them out of the var statement).
I show it to you as I think that it has to be changed to be rigorous (even if paperjs works fine as is).

Secondly, the detected errors seem also to be good code for me. So I have asked on my closure compiler issue how to do that with closure compiler. When I will have the answer, I will try to solve this problem, compile and test the new file. And I keep you informed.

If not, have you take a look at the closure compiler limitations ? Do you think you are compliant (even if it is not the aim for you) ?

Thx.

Doom.

--
You received this message because you are subscribed to a topic in the Google Groups "Paper.js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/paperjs/fRaPRFT-BSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to paperjs+u...@googlegroups.com.

sapics

unread,
Jun 28, 2016, 11:28:29 PM6/28/16
to Paper.js
I guess that property renaming in ADVANCED_OPTIMIZATIONS is not good for paperjs.
paperjs create some function names by combinating of letters.

2016年6月29日水曜日 5時54分16秒 UTC+9 doom star:

doom star

unread,
Jun 30, 2016, 8:04:39 AM6/30/16
to Paper.js
thanks for your answer. Try to do it in another way.

Best.

Jürg Lehni

unread,
Jul 1, 2016, 9:05:14 AM7/1/16
to pap...@googlegroups.com
Why not just use the already minified version that works?

Doom

unread,
Jul 1, 2016, 9:20:10 AM7/1/16
to pap...@googlegroups.com
I want to minified my sources with paperjs sources to have only one file and increase minification/optimisation.

Seems that paperjs does not meet closure compiler limitations. So I will use your minified version.

D.

--
You received this message because you are subscribed to a topic in the Google Groups "Paper.js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/paperjs/fRaPRFT-BSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to paperjs+u...@googlegroups.com.

Jürg Lehni

unread,
Jul 9, 2016, 7:59:45 AM7/9/16
to pap...@googlegroups.com
What's your reason to choose closure over uglifyjs? It looks like the latter is better and faster now, and it doesn't struggle with paper.js:

https://www.peterbe.com/plog/advanced-closure-compiler-vs-uglifyjs2

Jürg Lehni

unread,
Jul 9, 2016, 8:00:49 AM7/9/16
to pap...@googlegroups.com
PS:

From the linked document:

"Also, worth noting that in 2 files Closure Compiler failed with -O advanced so those 2 files had to be re-attempted with -O simple."
Reply all
Reply to author
Forward
0 new messages