Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update glob docs with replacement to ordered globs #2788

Merged
merged 1 commit into from Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions docs/getting-started/6-explaining-globs.md
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