Problem
I'm attempting to use uglify.js within a gruntfile.js, via the grunt-contrib-uglify task, but have encountered an issue whereby references to jQuery functions are being mangled and therefore can't be resolved as a function.
Uncaught TypeError: $(...).V is not a function
Having checked the produced .min.js script, I can see the on(...) function has been mangled.
$("#myElementId").V("click", function() {
var e = D.r.M();
C.r.w(e)
}),
Attempted Solution
As you can see below, having checked through the docs, I've attempted to exclude this function from the mangling using the reserved option, but it doesn't seem to be having an effect.
...
uglify: {
all: {
options: {
sourceMap: true,
nameCache: 'temp/grunt-uglify-cache.json',
mangle: {
toplevel: true,
properties: true,
reserved: ['jQuery']
}
},
files: [{
expand: true,
flatten: true,
cwd: 'Content/Scripts/',
src: ['**/*.js', '!*.min.js'],
dest: 'Content/Scripts/',
ext: '.min.js',
}]
}
},
...
I've also seen suggestions for using reserved: ['jQuery'] (for example, the task documentation) but that didn't seem to resolve the issue, either.
Have I missed something in how to use the uglify task with scripts containing jQuery usages, or is it necessary to exclude jQuery or each of its functions we use from mangling?
Update #1
Disabling the properties mangle option stops the issue, as one might expect, but ideally, I'd like to mangle properties for custom objects but without impacting those jQuery functions (or, mangle them, but map them correctly).
I've since used the concat task to combine the essential page layout scripts into a single app.js file which is minified with a source map to the source correctly. However, enabling properties still causes the above issue with the jQuery functions. Here's how the uglify task looks now...
...
uglify: {
all: {
options: {
sourceMap: true,
nameCache: 'temp/grunt-uglify-cache.json',
mangle: {
toplevel: true,
// reserved: ['jQuery'],
// properties: true,
}
},
files: [{
expand: true,
flatten: true,
cwd: 'Content/Scripts/',
src: ['app.js'],
dest: 'Content/Scripts/',
ext: '.min.js',
}]
}
},
...