14

I have the following task:

var uglify = require('gulp-uglify');

gulp.task('scripts', function () {
  gulp.src('./src/scripts/*.js')
    .pipe(concat('main.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
});

And the following 2 javascript files, test1.js :

var testOneOutput = 'function one';
console.log(testOneOutput);

And test2.js

var testTwoOutput = 'function two';
console.log(testTwoOutput);

And all the directories are setup correctly. Though when I run the task, I have no indication of whether the uglifying work. The concatenation works great though. Am I missing something?

3 Answers3

17

In your scripts testOneOutput and testTwoOutput are global variables and by default gulp-uglify only mangles local vars.

Put your code in a function and you'll see some mangling.

function go() {
  var testOneOutput = 'function one';
  console.log(testOneOutput);
}
psalaets
  • 707
  • 3
  • 9
12

You can give {mangle: {toplevel: true}} option if you want to mangle your top level functions as well.

uglify({mangle: {toplevel: true}})
trsquare lab
  • 131
  • 1
  • 2
1

Perform the uglify first. Then concat the uglified files.

gulp.task('scripts', function () {
  gulp.src('./src/scripts/*.js')
    .pipe(uglify())
    .pipe(concat('main.js'))
    .pipe(gulp.dest('./dist'));
});
Danny Delott
  • 6,756
  • 3
  • 33
  • 57