解释通配符#

¥Explaining Globs

glob 是用于匹配文件路径的字面量和/或通配符字符串。通配符是使用一个或多个通配符在文件系统上定位文件的行为。

¥A glob is a string of literal and/or wildcard characters used to match filepaths. Globbing is the act of locating files on a filesystem using one or more globs.

src() 方法需要单个 glob 字符串或 glob 数组来确定管道将操作哪些文件。必须为你的 glob 找到至少一个匹配项,否则 src() 将出错。当使用 glob 数组时,任何负 glob 都会从任何正 glob 中删除匹配项。

¥The src() method expects a single glob string or an array of globs to determine which files your pipeline will operate on. At least one match must be found for your glob(s) otherwise src() will error. When an array of globs is used, any negative globs will remove matches from any positive glob.

片段和分隔符#

¥Segments and separators

段是分隔符之间的所有内容。glob 中的分隔符始终是 / 字符 - 与操作系统无关 - 即使在路径分隔符为 \\ 的 Windows 中也是如此。在 glob 中,\\ 被保留作为转义字符。

¥A segment is everything between separators. The separator in a glob is always the / character - regardless of the operating system - even in Windows where the path separator is \\. In a glob, \\ is reserved as the escape character.

此处,* 被转义,因此它被视为字面量而不是通配符。

¥Here, the * is escaped, so it is treated as a literal instead of a wildcard character.

'glob_with_uncommon_\\*_character.js'

避免使用 Node 的 path 方法(如 path.join)来创建 glob。在 Windows 上,它会生成无效的 glob,因为 Node 使用 \\ 作为分隔符。出于同样的原因,还应避免使用 __dirname 全局、__filename 全局或 process.cwd()

¥Avoid using Node's path methods, like path.join, to create globs. On Windows, it produces an invalid glob because Node uses \\ as the separator. Also avoid the __dirname global, __filename global, or process.cwd() for the same reasons.

const invalidGlob = path.join(__dirname, 'src/*.js');

特殊字符:*(单星)#

¥Special character: * (single-star)

匹配任何金额 - 不包括任何 - 单个段内的字符数。对于在一个目录中通配文件很有用。

¥Matches any amount - including none - of characters within a single segment. Useful for globbing files within one directory.

该 glob 将匹配 index.js 等文件,但不匹配 scripts/index.jsscripts/nested/index.js 等文件

¥This glob will match files like index.js, but not files like scripts/index.js or scripts/nested/index.js

'*.js'

特殊字符:**(双星)#

¥Special character: ** (double-star)

匹配任何金额 - 不包括任何 - 跨段的字符。对于嵌套目录中的通配文件很有用。确保适当限制双星 glob,以避免不必要地匹配大型目录。

¥Matches any amount - including none - of characters across segments. Useful for globbing files in nested directories. Make sure to appropriately restrict your double-star globs, to avoid matching large directories unnecessarily.

这里,glob 被适当地限制在 scripts/ 目录中。它将匹配 scripts/index.jsscripts/nested/index.jsscripts/nested/twice/index.js 等文件。

¥Here, the glob is appropriately restricted to the scripts/ directory. It will match files like scripts/index.js, scripts/nested/index.js, and scripts/nested/twice/index.js.

'scripts/**/*.js'
在前面的示例中,如果 `scripts/` 没有前缀,则 `node_modules` 或其他目录中的所有依赖也会匹配。

¥In the previous example, if scripts/ wasn't prefixed, all dependencies in node_modules or other directories would also be matched.

特殊字符:!(否定)#

¥Special character: ! (negative)

! 字符为前缀的 Glob 将对 glob 进行 "negate",完全排除匹配。所有负 glob 都会应用于每个正 glob,这与 v5 之前的 gulp 版本不同。

¥Globs prefixed with the ! character will "negate" the glob, excluding the match completely. All negative globs are applied to every positive glob, which is a departure from gulp versions before v5.

这里,将遍历 scripts/ 目录查找以 .js 结尾的所有文件,但排除 scripts/vendor/ 目录中的所有文件。

¥Here, the scripts/ directory will be traversed for all files ending in .js, but all files from the scripts/vendor/ directory will be excluded.

['scripts/**/*.js', '!scripts/vendor/**']

负 glob 可以用作限制双星 glob 的替代方法。

¥Negative globs can be used as an alternative for restricting double-star globs.

['**/*.js', '!node_modules/**']

有序的通配符#

¥Ordered globs

v5 之前的 gulp 版本允许 "有序的通配符";然而,为了与生态系统中的大多数全局库保持一致,它已被删除。

¥Versions of gulp before v5 allowed "ordered globs"; however, that has been removed to align with most globbing libraries in the ecosystem.

如果你需要 "有序的通配符" 功能,可以使用 ordered-read-streams 库来组合流:

¥If you need the "ordered glob" functionality, you can use the ordered-read-streams library to combine streams:

const order = require("ordered-read-streams");
exports.default = function () {
return order([
gulp.src("input/jquery/dist/jquery.js"),
gulp.src("input/detect_swipe/jquery.detect_swipe.js"),
]).pipe(gulp.dest('output/'));
}

重叠的通配符#

¥Overlapping globs

两个或多个有意(无意)匹配同一文件的 glob 被视为重叠。当在单个 src() 中使用重叠的 glob 时,gulp 会尽力删除重复项,但不会尝试跨单独的 src() 调用进行数据去重。

¥Two or more globs that (un)intentionally match the same file are considered overlapping. When overlapping globs are used within a single src(), gulp does its best to remove the duplicates, but doesn't attempt to deduplicate across separate src() calls.

高级资源#

¥Advanced resources

这里涵盖了在 gulp 中使用 glob 所需的大部分内容。如果你想更深入地了解,这里有一些资源。

¥Most of what you'll need to work with globs in gulp is covered here. If you'd like to get more in depth, here are a few resources.