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.
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).