I am using gulp and gulp-uglify to minify my javascript code.
Gulp
var uglify= require('gulp-uglify');
gulp.task('javascript', function() {
return gulp.src('./scripts/*.js')
.pipe(uglify())
.pipe(gulp.dest('./'));
});
Original javascript
var Site = Site || {};
Site.Code = {
obj: new ThirdPartyObject(),
init: function() {
obj.thirdPartyMethod();
}
};
Minified javascript
var Site = Site || {};
Site.Code = {obj: new ThirdPartyObject,init: function() {
obj.thirdPartyMethod()
}};
The minifier is removing the parentheses for obj: new ThirdPartyObject
and therefore my code breaks when I make the call obj.thirPartyMethod()
.
How can I fix this?
The minifier is correct new X()
and new X
are equivalent.
Your code however, is not as correct. Here is the correct version:
var Site = Site || {};
Site.Code = {
obj: new ThirdPartyObject(),
init: function() {
Site.Code.obj.thirdPartyMethod(); // <-- obj didn't exist
// this.obj.thirdPartyMethod(); <-- also works, because this will be Site.Code when invoking Site.Code.init()
}
};