Cannot minify kotlin.js using Closure Compiler


#1

Hi. I’m trying to reduce the size of the javascript I’m deploying, for a project I’m developing in kotlin.js.

So far I used kotlin-dce-js with good results; now I’d like to minify the resulting javascript(s).

Since I’m using Gradle, I imported the eriwen/gradle-js-plugin, that uses Google’s Closure Compiler to minify files. If I apply it to the (DCE’d) kotlin.js, I get a number of errors like this:

[…]\build\kotlin-js-min\main\kotlin.js:3387 - Parse error. identifier is a reserved word

This is because of methods like this:

function digitOf(char, radix) {
      var tmp$;
      if (char >= (48 | 0) && char <= (57 | 0))
        tmp$ = char - 48;
      else if (char >= (65 | 0) && char <= (90 | 0))
        tmp$ = char - 65 + 10 | 0;
      else if (char >= (97 | 0) && char <= (122 | 0))
        tmp$ = char - 97 + 10 | 0;
      else
        tmp$ = -1;
      var it = tmp$;
      return it >= radix ? -1 : it;
    }

I understand that char is a reserved word in Javascript, or at least that the Closure Compiler thinks it is; my question is: is it possible to do something to rename those chars at the Kotlin level? I can’t find a way to make the compiler ignore them.

Thanks!

– Germano

PS: uglify.js doesn’t complain, but I can’t find a way to integrate it into Gradle.


#2

Have you tried manually running GCC on kotlin-dce output?


#3

Thanks for answering. I tried now with the online service (https://closure-compiler.appspot.com/home), and actually it doesn’t complain. The eriwen plugin seems to use a very old version, and I’m filing a bug report with it, as well, for sure.

Still, as things stand now, it remains true that I can’t minify kotlin.js using gradle. Do you think there’s a workaround for this?


#4

Reported to the plugin provider, here


#5

Don’t use plugins, call gcc directly from gradle via JavaExec or shell exec.
kotlin dce puts kotlin js lib in somewhere in build folder so no need to search for it in dependency jars


#6

Thanks. I could find the DCE’d file in [...]/build/kotlin-js-min/main/kotlin.js. I’ll try to do as you advice.


#7

Ok. I used this task

task minifyJS(type: JavaExec) {
    main = '-jar'

    // arguments to pass to the application
    args = ["${projectDir}/Tools/closure-compiler-v20180402.jar",
            "--process_common_js_modules",
            "--js_output_file=${projectDir}/${web_dir}/${proj}.min.js",
            "build/kotlin-js-min/main/kotlin.js",
            "build/kotlin-js-min/main/WebUI.js"]
}

I put the GCC jar file in a Tools/ subdir of my project. It works. Thanks!