symfonyasseticuglifyjs2

Passing options to UglifyJS2 when using Assetic Bundle


As title, is there any way of passing options to UglifyJS2 when using Assetic? Specifically I need to pass the --comments option.

I have tried to add the option to the bin setting in my config.yml: bin: "%uglifyjs_bin_path% --comments", but that won't run as it tries to include --comments in the path:

[Assetic\Exception\FilterException]
An error occurred while running:
'/usr/bin/nodejs' '/usr/local/bin/uglifyjs --comments' '-o' '/tmp/assetic_ uglifyjs2_outyYVBye' '/tmp/assetic_uglifyjs2_ind932Xh' Error Output:
module.js:328

throw err;
Error: Cannot find module '/usr/local/bin/uglifyjs --comments'

at Function.Module._resolveFilename (module.js:326:15)
at Function.Module._load (module.js:277:25)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3


Solution

  • After some digging in the Assetic bundle's source code I found the configuration for the UglifyJS2 filter, UglifyJS2 Configuration. Thanks to that I was able to figure out that to enable the comments option I simply need to pass it along with the bin option, like so:

    uglifyjs2:
            # the path to the uglifyjs executable
            bin: "%uglifyjs_bin_path%"
            comments: true
    

    Edit: Additionally, if you want to pass arguments to a given option you just pass the argument to the wanted option and the option itself will be added automatically. I am for example running comments with a custom regex:

    uglifyjs2:
            # the path to the uglifyjs executable
            bin: "%uglifyjs_bin_path%"
            comments: /^\/*\**!/
            compress: true
            mangle: true