How to fix problem when uglifyjs and jsasync libs in js output

Hi,

I use for minifying js https://lib.haxe.org/p/uglifyjs/ which is pretty good until I used in my JS app https://lib.haxe.org/p/jsasync/ library.

At minifying my js output i got this error:

Parse error at ../project/main.js:146,34
AsyncAwait.fetchURLAsText = async function(url,data) {
                                  ^
ERROR: Unexpected token: keyword «function», expected: punc «;»
    at JS_Parse_Error.get (eval at <anonymous> (/usr/local/lib/node_modules/uglify-js/tools/node.js:18:1), <anonymous>:71:23)
    at fatal (/usr/local/lib/node_modules/uglify-js/bin/uglifyjs:394:27)
    at run (/usr/local/lib/node_modules/uglify-js/bin/uglifyjs:343:9)
    at Object.<anonymous> (/usr/local/lib/node_modules/uglify-js/bin/uglifyjs:259:5)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:18:47

Code works fine without minifying.

Did you already meet with the same problem and what do you recommend to fix it, please?

UglifyJS used to be pretty popular in the JS ecosystem, but a lot of new JS syntax (such as async) is not supported by it. In the JS ecosystem, UglifyJS was forked and became Terser.

There isn’t a Haxe library specifically for Terser. However, looking at the README for hx-uglifyjs, I see a section titled Using Terser as alternative of Uglify. You should read through that and give it a try. I’m guessing that you’ll have better luck using hx-uglifyjs with Terser.

2 Likes

Thank you for it. I will check it.