如何使用gulp.src获取整个文件夹及其子文件夹,然后根据文件位置使用gulp.dest?

10

编辑:有没有一种方法可以优化这段代码?

task.coffee

# Watch pages
gulp.task 'jade', ->
  # Watch index
  gulp.src('src/jade/index.jade')
  .pipe(jade(pretty: true))
  .pipe gulp.dest('dist')
  # Watch views
  gulp.src('src/jade/views/*.jade')
  .pipe(jade(pretty: true))
  .pipe gulp.dest('dist/views')
  # Watch views/products
  gulp.src('src/jade/views/products/*.jade')
  .pipe(jade(pretty: true))
  .pipe gulp.dest('dist/views/products')

gulp.watch 'src/jade/*.jade', ['html']
gulp.task 'html', (callback) ->
  runSequence 'jade', callback
  return

假设我正在运行gulp任务来处理我的.jade文件,并且我正在开发一个angular应用程序(views/**/*.html),我该如何使我的任务保持简洁,以便更改我的任务来实现这一点?
// gulp.src('src/jade/**/*.jade')
// gulp.dest('dist/path/*.html') so for example 'src/jade/index.jade'
// will be output into 'dist/index.html' and
// 'src/jade/views/products/product.jade' will be
// output into 'dist/views/products/product.html'

task.coffee

# Watch pages
gulp.task 'jade', ->
  gulp.src('src/jade/*.jade')
  .pipe(jade(pretty: true))
  .pipe gulp.dest('dist')
gulp.watch 'src/jade/*.jade', ['html']
gulp.task 'html', (callback) ->
  runSequence 'jade', callback
  return

task.js

gulp.task('jade', function() {
  return gulp.src('src/jade/*.jade').pipe(jade({
    pretty: true
  })).pipe(gulp.dest('dist'));
});

gulp.watch('src/jade/*.jade', ['html']);

gulp.task('html', function(callback) {
  runSequence('jade', callback);
});
1个回答

15

你的问题的答案已经在你自己的帖子中了:

// gulp.src('src/jade/**/*.jade')

在你的jade任务中使用这个,并且设置watch应该能够精确地完成你想要的:

gulp.task('jade', function() {
  return gulp.src('src/jade/**/*.jade')
    .pipe(jade({pretty: true}))
    .pipe(gulp.dest('dist'));
});

gulp.watch('src/jade/**/*.jade', ['html']);

这将在dist文件夹中生成如下文件:

src/jade/index.jade -> dist/index.html
src/jade/views/example.jade -> dist/views/example.html
src/jade/views/products/product.jade -> dist/views/products/product.html
...

每个文件都会自动输出到相应的路径吗?gulp.dest('dist')只是将'src/path/of/the/.file'更改为'dist/path/of/the/.file'吗? - user2537154
是的,请查看我的编辑答案。**之前的所有内容都被剥离掉了(即src/jade/),但**之后的文件夹结构保留不变。 - Sven Schoenung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接