Skip to content

Commit

Permalink
chore: Update glob docs with replacement to ordered globs (#2788)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Apr 8, 2024
1 parent fe9dee6 commit 2fa4981
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions docs/getting-started/6-explaining-globs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sidebar_label: Explaining Globs

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.

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, they are matched in array order - especially useful for negative globs.
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

Expand Down Expand Up @@ -49,26 +49,37 @@ Here, the glob is appropriately restricted to the `scripts/` directory. It will

## Special character: ! (negative)

Since globs are matched in array order, a negative glob must follow at least one non-negative glob in an array. The first finds a set of matches, then the negative glob removes a portion of those results. When excluding all files within a directory, you must add `/**` after the directory name, which the globbing library optimizes internally.
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.

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

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

If any non-negative globs follow a negative, nothing will be removed from the later set of matches.
Negative globs can be used as an alternative for restricting double-star globs.

```js
['scripts/**/*.js', '!scripts/vendor/**', 'scripts/vendor/react.js']
['**/*.js', '!node_modules/**']
```

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

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

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

```js
['**/*.js', '!node_modules/**']
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/'));
}
```

<small>In the previous example, if the negative glob was `!node_modules/**/*.js`, the globbing library wouldn't optimize the negation and every match would have to be compared against the negative glob, which would be extremely slow. To ignore all files in a directory, only add the `/**` glob after the directory name.</small>

## Overlapping globs

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.
Expand All @@ -86,3 +97,4 @@ Most of what you'll need to work with globs in gulp is covered here. If you'd li
[glob-primer-docs]: https://github.com/isaacs/node-glob#glob-primer
[begin-globbing-docs]: https://github.com/begin/globbing#what-is-globbing
[wikipedia-glob]: https://en.wikipedia.org/wiki/Glob_(programming)
[ordered-read-streams-docs]: https://github.com/gulpjs/ordered-read-streams#orderedstreams-options

0 comments on commit 2fa4981

Please sign in to comment.