Gulp - Target all files in a folder and its subfolders

試著忘記壹切 提交于 2019-12-17 18:16:09

问题


I'd like to be able to add a watch task in gulp to all of the js files in the frontend/js and any other js files below

 gulp.watch('./frontend/js/**/*.js', ['browserify']);

This will only target js files one folder deep


回答1:


It's supposed to match any number of subdirectories:

** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.

https://github.com/isaacs/node-glob

Do you have symlinked directories in there?

Symlinks

I don't think you'll get gulp to natively traverse your symlinked directories. I recommend you take a look at node.js fs.readdir recursive directory search and see if any of those solutions can be applied to your use case. Nothing in the question or answers specifically addresses symlinks, so I don't know if there's a solution for you there or not. If you can get an array of dereferenced pathnames using one of those solutions, then you can just pass the array to gulp.src().




回答2:


I just did some testing - and this actually works just fine for me.

I currently have the following structure -

--apps
  --scripts
  ----test.js
  ----test-folder
  ------test2.js
  ------test-folder-deep
  --------test3.js
  --myApp
  ----scripts-symlinked (symlinked to apps/scripts)
  ----gulpfile.js

I set up my symlink folder (on Mac - from 'myApp' folder) using:

ln -s /Users/kandrews/apps/scripts ./scripts-symlinked

In my gulpfile.js I have the following:

var gulp = require('gulp'),
    jshint = require('gulp-jshint');

gulp.task('jshint', function () {
    gulp.src('./scripts-symlinked/**/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

gulp.task('watch', function () {
    gulp.watch('./scripts-symlinked/**/*.js', ['jshint']);
});

Works perfectly. I also tried this in a sub directory as well ('scripts/symlinked-scripts') and was successful as well.



来源:https://stackoverflow.com/questions/27526891/gulp-target-all-files-in-a-folder-and-its-subfolders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!