5

I am currently refactoring a project where previously all of the minified JavaScript files were being placed in a specific directory. Now I need to the minified versions to stay in the same directories as their source files.

Currently I do this:

gulp.task( 'scripts', function () {
    return gulp.src( source_paths.scripts )
        .pipe( uglify( {
            preserveComments: 'false'
        } ) )
        .pipe( rename( {suffix: ".min"} ) )
        .pipe( gulp.dest( './build/js' ) )
        .pipe( notify( {
            message: 'Scripts task complete!',
            onLast : true
        } ) );

} );

This works, except it moves my files. I tried changing my use of gulp.dest() to .pipe( gulp.dest( '' ) ) as well as just removing that line. In both cases no minified JS was written and I was very sad.

How can I make it write all files to the same directory as their source files?

scniro
  • 16,844
  • 8
  • 62
  • 106
JPollock
  • 3,218
  • 4
  • 26
  • 36
  • What's the difference between your question and [this one](http://stackoverflow.com/questions/25631589/have-gulp-dynamically-write-css-in-the-same-directory-as-the-processed-scss-file)? – gideonite Mar 01 '16 at 04:30

2 Answers2

7

You're currently writing to build/js, and of course removing the dest results in no files written. As comments suggest, you can call file.base in an anonymous function in your dest call. Observe the following...

// fixed spacing madness
gulp.task('scripts', function () {
    return gulp.src(source_paths.scripts)
        .pipe(uglify({
            preserveComments: 'false'
        }) 
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(function(file) {
            return file.base;
        }))
        .pipe(notify({
            message: 'Scripts task complete!',
            onLast : true
        }));
});
Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
scniro
  • 16,844
  • 8
  • 62
  • 106
0

USE " ./ " ... current location

gulp.task('task_name', function () {
    return gulp.src(path\*.js) // or other selection
    .pipe(uglify()) 
    .pipe(rename({suffix: '.min'})).pipe(gulp.dest("./"));
});
bortunac
  • 4,642
  • 1
  • 32
  • 21