Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UglifyJS: error when using optional chaining syntax

I am sending a script through UglifyJS which contains a few instances of optional changing syntax. In the browser, this syntax works flawlessly. However, UglifyJS produces the following error on the file:

Parse error at /Users/username/Developer/script.js:84,32
               node.children[0]?.tagName === "P";
                                ^
ERROR: Unexpected token: punc «.»
    at JS_Parse_Error.get (eval at <anonymous> (/opt/homebrew/lib/node_modules/uglify-js/tools/node.js:18:1), <anonymous>:71:23)
    at fatal (/opt/homebrew/lib/node_modules/uglify-js/bin/uglifyjs:428:27)
    at run (/opt/homebrew/lib/node_modules/uglify-js/bin/uglifyjs:366:9)
    at Object.<anonymous> (/opt/homebrew/lib/node_modules/uglify-js/bin/uglifyjs:275:5)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

I've scoured the documentation, but I am not sure which arguments to add (if any) to make this work.

like image 799
Jacob Avatar asked Mar 25 '26 15:03

Jacob


1 Answers

UglifyJS most probably doesn't support the optional chaining operator ?. yet. I can't find the operator listed in the parser's source code.

Authors of the tool suggest to use transpilers like BabelJS before UglifyJS on files with exotic JS syntax.

This operator was added to the ECMAScript standard just June 2020 (the same ?? operator, but support for it was added to UglifyJS just 18 days ago, on Feb 23 2021).

like image 122
Rafael Avatar answered Mar 27 '26 04:03

Rafael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!