【问题标题】:GULP: How to combine files AFTER uglifyGULP:如何在 uglify 之后合并文件
【发布时间】:2018-01-28 17:57:28
【问题描述】:

场景 我需要抓取一个文件夹中的所有 *.js 文件,将它们合并并丑化它们。这就是我实现的目标:

gulp.task('default', function(){
    return gulp.src('resources_folder/*.js')
        .pipe(plumber({
            errorHandler: function (error) {
                console.log(error.message);
                this.emit('end');
            }}))
        .pipe(concat('output.js'))
        .pipe(babel())
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify())
        .pipe(gulp.dest('js/'))
});

但我正在为两件事苦苦挣扎:

  1. 在 uglify 之后,我想将结果与另一个(已经缩小的文件)连接起来。我不想再次丑化这个文件 - 我希望它只是与结果相结合。

  2. 请问如何将结果与“换行”或注释连接起来?

我想要达到的目标: 我在“res”中有我的源文件,并且我使用了一个外部小型库(已经是 minifeid),我想将它包含到我的输出文件中。

类似:

  • 发表评论
  • 获取所有来源
  • 丑化
  • 换行
  • 发表评论
  • 换行
  • 结合资源库

【问题讨论】:

    标签: gulp gulp-uglify gulp-concat


    【解决方案1】:

    您将需要两个插件:

    gulp-footergulp-add-src

     var footer = require('gulp-footer');
     var addsrc = require('gulp-add-src');
    

    所以在你的 uglify 管道之后放一些类似的东西:

    .pipe(uglify())
    .pipe(footer('\n// my comment\n'))
    .pipe(addsrc.append('your resource library to append'))
    .pipe(concat('new file name here'))
    .pipe(gulp.dest('js'))
    

    查看 gulp-footer 文档,了解构建您希望插入的注释的不同方法。例如,您可以从文件或变量中读取它。

    【讨论】:

    • 对迟到的 naswer 感到抱歉 - 但这就像一个魅力! :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多