Is there a way to mangle all variables except a specific one with gulp-uglify?
Asked
Active
Viewed 8,192 times
4 Answers
16
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

Sam
- 755
- 4
- 15
-
3It seems like with UglifyJS 3 `except` was replaced with `reserved`. Otherwise it will throw `DefaultsError: `except` is not a supported option` – lw1.at May 16 '17 at 06:14
0
This work for me
.pipe(uglify({
mangle: { toplevel: true, except: ['variable1', 'variable2',
'function1'], 'function2' }
}))

Hugz
- 123
- 6
-
Could you please explain to me what exactly toplevel does? And why function2 is written out of the array of except? I want to try this for my code. – Kedar Ghadge Jun 14 '22 at 11:41
0
You can use like this:
gulp.src(['src/*.js'])
.pipe(uglify({
mangle: {
except: ['ExtensionId'] // (base on gulp-uglify v1.5.3)
}
}))

duXing
- 1,377
- 1
- 10
- 24
-
Thanks @sigma, I've updated the accepted answer now gulp-uglify has updated – Sam May 09 '17 at 18:06