Gulp - 缩小JS并写入同一目的地

JPo*_*ock 5 javascript node.js gulp gulp-uglify

我目前正在重构一个项目,以前所有缩小的JavaScript文件都放在一个特定的目录中.现在我需要将缩小版本保留在与其源文件相同的目录中.

目前我这样做:

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
        } ) );

} );
Run Code Online (Sandbox Code Playgroud)

这有效,除了它移动我的文件.我试图改变我的使用gulp.dest().pipe( gulp.dest( '' ) )以及刚刚删除该行.在这两种情况下都没有编写缩小的JS,我非常难过.

如何将所有文件写入与源文件相同的目录?

scn*_*iro 6

您目前正在写信build/js,当然也dest没有写入任何文件而删除结果.正如评论所示,您可以file.base在通话中使用匿名功能dest.请注意以下内容......

// 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
        }));
});
Run Code Online (Sandbox Code Playgroud)