简体   繁体   English

在源文件上运行gulp模块

[英]Run gulp module on source files

I'm kinda new to gulp, and I've been trying to beautify my js source files. 我对gulp有点陌生,并且我一直在尝试美化我的js源文件。 I have this task (which runs 100%) which uses 'gulp-beautify' to beautify the js files: 我有这个任务(运行100%),它使用'gulp-beautify'来美化js文件:

gulp.task('js', function() {
    return gulp.src('src/js/**/*.js')
        .pipe(beautify({indent_size: 4}));
});

And it doesn't work. 而且它不起作用。 The file stays the same. 该文件保持不变。 But , if I do this: 但是 ,如果我这样做:

gulp.task('js', function() {
    return gulp.src('src/js/**/*.js')
        .pipe(beautify({indent_size: 4}))
        .pipe(gulp.dest('./foo/'));
});

The files outputted are beautified. 输出的文件已美化。 Any idea why this is happening? 知道为什么会这样吗?

Thanks in advance! 提前致谢!

The first task is beautifying the streams in memory, but they're not saved on the disk automatically. 第一项任务是美化内存中的流,但是它们不会自动保存在磁盘上。 You must explicitly output the files somewhere to save the beautified files, like in your second task. 您必须显式地将文件输出到某个地方以保存美化的文件,例如在第二个任务中。

If you want to overwrite the files with their beautified version, you can use gulp.dest to output the files at the same place they are read. 如果要使用美化版本覆盖文件,可以使用gulp.dest在读取文件的同一位置输出文件。

gulp.task('beautify', function() {
  return gulp.src('src/js/**/*.js', { 
      base: "./" // specify the base option
    })
    .pipe(beautify({ indent_size: 4 }))
    .pipe(gulp.dest('./'));
}); 

Modify file in place (same dest) using Gulp.js and a globbing pattern 使用Gulp.js和通配模式在适当位置修改文件(相同目标)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM