Javascript Gulp - 定位文件夹及其子文件夹中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27526891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:16:30  来源:igfitidea点击:

Gulp - Target all files in a folder and its subfolders

javascriptgulp

提问by Jamie Hutber

I'd like to be able to add a watchtask in gulpto all of the jsfiles in the frontend/jsand any other js files below

我希望能够将watch任务添加gulp到下面的所有js文件frontend/js和任何其他 js 文件中

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

This will only target jsfiles one folder deep

这只会针对js一个文件夹深的文件

回答by JMM

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.

**如果“globstar”在路径部分中单独存在,则它匹配零个或多个目录和子目录以搜索匹配项。它不会抓取符号链接的目录。

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

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 searchand 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().

我不认为你会得到 gulp 来原生地遍历你的符号链接目录。我建议您查看node.js fs.readdir 递归目录搜索,看看这些解决方案中的任何一个是否可以应用于您的用例。问题或答案中没有任何内容专门针对符号链接,所以我不知道那里是否有适合您的解决方案。如果您可以使用这些解决方案之一获得一组取消引用的路径名,那么您只需将该数组传递给gulp.src().

回答by Kelly J Andrews

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:

我使用以下方法设置了我的符号链接文件夹(在 Mac 上 - 来自“myApp”文件夹):

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

In my gulpfile.jsI have the following:

在我的gulpfile.js我有以下内容:

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.

完美运行。我也在子目录('scripts/symlinked-scripts')中也尝试了这个,并且也成功了。

回答by Jacob

I think it's not worth doing difficult:

我认为不值得做困难:

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