Thursday 1 June 2017

How to prevent `gulp-uglify` from moving my global variables?

I am using gulp-uglify with my gulp setup to mimify my js code for production environment.

I have code like this

var test = 1;

function getTest(myTest)
{
   if(!myTest) {
      return test;
   }

  return myTest;
}

However when I run gulp the output becomes (I kept it clean for readability)

function getTest(myTest)
{
   if(!myTest) {
      return test;
   }

  return myTest;
}
var test = 1;

How can I prevent gulp-uglify from changing the order or my global variables.

I first concatenate my files, then uglify.

I try to pass the following object to uglify but it is still shifting the order.

uglify({ mangle: false })
uglify({ mangle: { reserved: ['test'] } })

Here is a list of the dependencies

  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-concat": "^2.6.0",
    "gulp-debug": "^2.1.2",
    "gulp-ignore": "^2.0.1",
    "gulp-minify-css": "^1.2.4",
    "gulp-sass": "^2.3.2",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^2.0.0",
    "gulp-util": "^3.0.7"
  }

here is my gulp task

gulp.task('compileJs', function () {
        gulp.src(jsFiles['final'])
               .pipe(sourcemaps.init())
               //only uglify if gulp is ran with '--type production'
               .pipe(isProduction() ? uglify() : gutil.noop())
               .pipe(concat('final.js'))
               .pipe(sourcemaps.write())
               .pipe(gulp.dest('./Public/Js'));

});



via Mike A

No comments:

Post a Comment