Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mangle all variables except one with gulp-uglify

Is there a way to mangle all variables except a specific one with gulp-uglify?

like image 611
deleted Avatar asked Jul 29 '15 08:07

deleted


People also ask

What is the difference between gulp and uglify?

Basically, gulp is used to automate developer tasks as per their requirement, gulp provides the different types of features to the developer. In that, uglify is one of the features provided by the gulp. Basically uglify is used to avoid the error event if it is not able to minify our file.

How to coordinate mangled symbols between uglify files?

Turn on use of name mangling cache to coordinate mangled symbols between outputted uglify files. uglify will the generate a JSON cache file with the name provided in the options. Note: this generated file uses the same JSON format as the exceptionsFiles files.

What is uglify source and sourcemap?

If a function is provided, the uglify destination is passed as the argument and the return value will be used as the file name. The location of an input source map from an earlier compilation, e.g. from CoffeeScript. If a function is provided, the uglify source is passed as the argument and the return value will be used as the sourceMap name.

Can I use UglifyJS with a JSON file?

That being said, if you already have a JSON file, you might as well reference it. You can also enable UglifyJS conditional compilation. This is commonly used to remove debug code blocks for production builds. This is equivalent to the command line --define option. See UglifyJS global definitions documentation for more information.


Video Answer


2 Answers

There definitely is, I couldn't find any documentation for gulp-uglify regarding these sub-options, so relied on grunt-uglify & wrote it the gulp way.

.pipe( uglify({ mangle: {except: ['jQuery']} }) ) // for gulp-uglify ^2.0.1

The above code will mangle every variable except jQuery

Hopefully this will still be useful for you. ;)

Sam.

========

NB: If you are using gulp-uglify ^3.0.0 please replace except with reserved eg:

.pipe( uglify({ mangle: {reserved: ['jQuery']} }) ) // for gulp-uglify ^3.0.0

Kudos to Lukas Winkler

like image 112
Sam Avatar answered Oct 23 '22 16:10

Sam


That's right.

UglifyJS 3 except was replaced with reserved. Otherwise it will throw

DefaultsError: except` is not a supported option

it seem like this

.pipe(uglify({mangle: {reserved: ['$.loadEditor']}}))

like image 23
fydor Avatar answered Oct 23 '22 15:10

fydor