'How to use uglifyjs 3 mangle option with toplevel and reserverd?

I have the below command:

uglifyjs  main.js bar.js  -m  toplevel reserved=['$','addTo','exports']   -c --source-map -o out.js

But it throws error:

ERROR: ENOENT: no such file or directory, open 'reserved=[$,addTo,exports]'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)

How can I use both options toplevel (mangle function names) and reserved under -m option? I am using latest uglifyJs and node 8.6 .



Solution 1:[1]

This should work:

uglifyjs -m reserved=['$','addTo','exports'] toplevel=true  -c --source-map -o out.js -- main.js bar.js

Solution 2:[2]

Correct answer is:

uglifyjs main.js bar.js -m toplevel,reserved=['$','addTo','exports'] -c --source-map -o out.js

Solution 3:[3]

Maybe you can try not using command line, this works for me in the latest version of uglify-JS 3

var fs = require("fs");

var UglifyJs = require('uglify-js');

let data = fs.readFileSync('main.js','utf8')

let options = {
    sourceMap:{
        filename:"out.js",
        url:"out.js.map"
    },
    mangle:{
        reserved:["addTo","exports"],
        toplevel:true,
    }
};

var result = UglifyJs.minify(data,options)
console.log(result.code)
console.log(result.map)
fs.writeFile("out.js",result.code,function(err){
    if(err){
        console.log(err);
    }else{
        console.log("File was successfully saved")
    }
})
fs.writeFile("out.js.map",result.map,function(err){
    if(err){
        console.log(err);
    }else{
        console.log("File was successfully saved")
    }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ace
Solution 3