roman01la / webpack-closure-compiler

[DEPRECATED] Google Closure Compiler plugin for Webpack
MIT License
464 stars 25 forks source link

compilation_level: "ADVANCED" producing errors for both Java and JavaScript compilers #38

Closed chrstntdd closed 6 years ago

chrstntdd commented 6 years ago

I just got around to using this plugin to shave off extra bytes from my .js output of an Elm project. I have installed Java 9 SKD as per the requirements as well as the google closure compiler from npm.

This configuration:

 new ClosureCompilerPlugin({
      compiler: {
        jar: './node_modules/google-closure-compiler/compiler.jar',
        language_in: 'ECMASCRIPT6',
        language_out: 'ECMASCRIPT5',
        compilation_level: 'ADVANCED',
        warning_level: 'QUIET'
      },
      jsCompiler: false,
      concurrency: 3
    })

produces these errors and won't compile my output .js file at all:

ERROR in app.js from Closure Compiler
The compiler is waiting for input via stdin.
stdin:1948: ERROR - variable _elm_lang$core$Set$toList is undeclared
                x = _elm_lang$core$Set$toList(x);
                    ^^^^^^^^^^^^^^^^^^^^^^^^^

stdin:5605: ERROR - variable main is undeclared
                view: function(model) { return main; },
                                               ^^^^

stdin:13254: ERROR - variable define is undeclared
if (typeof define === "function" && define['amd'])
           ^^^^^^

3 error(s), 0 warning(s)

However, if I switch the config up to use compilation_level: 'SIMPLE', it will all bundle just fine.


Going further, I tried flipping on the jsCompiler option with compilation_level: ADVANCED, which actually will compile, but when I load the output file in a web browser, I get this error in the console:

app.js:formatted:142 Uncaught TypeError: Cannot read property 'gd' of undefined
    at app.js:formatted:142
    at Object.<anonymous> (app.js:formatted:163)
    at g (app.js:formatted:80)
    at Object.<anonymous> (app.js:formatted:115)
    at g (app.js:formatted:80)
    at app.js:formatted:112
    at app.js:formatted:114

which references this line:

        T = N.Nc.gd(document.getElementById("main"));

Admittedly, the difference between SIMPLE and ADVANCED within the first configuration is 10k, but I'm at a loss for why I'm unable to use the compilation_level: ADVANCED, regardless of the compiler, beit Java or JavaScript.

If anyone could shed some light on these errors, I would really appreciate it. Here is the full repo, if anyone needs it. Love this plugin regardless 👍

bansawbanchee commented 6 years ago

This is actually not an issue with this plugin. ADVANCED compilation with Google Closure Compiler requires you to use externs when referencing external JavaScript. E.g document or any third party node_modules you are including. I recommend taking a look at the docs for ADVANCED compilation located here: https://developers.google.com/closure/compiler/docs/api-tutorial3

roman01la commented 6 years ago

As @bansawbanchee pointed out you might want to add externs to your build. Closing it for now.