【问题标题】:Gulp - Minify JS and write to same destinationGulp - 缩小 JS 并写入相同的目的地
【发布时间】:2016-03-01 04:26:18
【问题描述】:

我目前正在重构一个项目,以前所有缩小的 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
        } ) );

} );

这可行,但它会移动我的文件。我尝试将我对 gulp.dest() 的使用更改为 .pipe( gulp.dest( '' ) ) 以及删除该行。在这两种情况下,都没有编写缩小的 JS,我很伤心。

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

【问题讨论】:

  • 你的问题和this one有什么区别?

标签: javascript node.js gulp gulp-uglify


【解决方案1】:

您当前正在写信给build/js,当然删除dest 会导致没有文件写入。正如 cmets 建议的那样,您可以在 dest 调用中的匿名函数中调用 file.base。请注意以下...

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

【讨论】:

  • 不要忘记使用 npm install gul-rename 和“var rename = require('gulp-rename');”添加 gulp-rename在 gulpfile 需要区域。
【解决方案2】:

使用“./”...当前位置

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多