Skip to content

Commit d95b457

Browse files
janiceilenephated
authored andcommittedOct 19, 2018
Docs: Update src() documentation
1 parent 4169cb6 commit d95b457

File tree

1 file changed

+97
-113
lines changed

1 file changed

+97
-113
lines changed
 

‎docs/api/src.md

+97-113
Original file line numberDiff line numberDiff line change
@@ -5,135 +5,119 @@ hide_title: true
55
sidebar_label: src()
66
-->
77

8-
# `gulp.src(globs[, options])`
8+
# src()
99

10-
Emits files matching provided glob or array of globs.
11-
Returns a [stream] of [Vinyl files] that can be [piped] to plugins.
10+
Creates a stream for reading [Vinyl][vinyl-concepts] objects from the file system.
1211

13-
```javascript
14-
gulp.src('client/templates/*.pug')
15-
.pipe(pug())
16-
.pipe(minify())
17-
.pipe(gulp.dest('build/minified_templates'));
18-
```
19-
20-
`glob` refers to [node-glob syntax][node-glob] or it can be a direct file path.
21-
22-
## globs
23-
Type: `String` or `Array`
24-
25-
Glob or array of globs to read. Globs use [node-glob syntax] except that negation is fully supported.
12+
**Note:** Any UTF-8 BOMs will be removed from UTF-8 files read by `src()`, unless disabled using the `removeBOM` option.
2613

27-
A glob that begins with `!` excludes matching files from the glob results up to that point. For example, consider this directory structure:
28-
29-
client/
30-
a.js
31-
bob.js
32-
bad.js
33-
34-
The following expression matches `a.js` and `bad.js`:
35-
36-
gulp.src(['client/*.js', '!client/b*.js', 'client/bad.js'])
14+
## Usage
3715

16+
```javascript
17+
const { src, dest } = require('gulp');
3818

39-
Note that globs are evaluated in order, which means this is possible:
19+
function copy() {
20+
return src('input/*.js')
21+
.pipe(dest('output/'));
22+
}
4023

41-
```js
42-
// exclude every JS file that starts with a b except bad.js
43-
gulp.src(['*.js', '!b*.js', 'bad.js'])
24+
exports.copy = copy;
4425
```
4526

46-
**Note:** glob symlink following behavior is opt-in and you must specify
47-
`follow: true` in the options object that is passed to [node-glob].
48-
49-
## options
50-
Type: `Object`
51-
52-
Options to pass to [node-glob] through [glob-stream].
53-
54-
gulp adds some additional options in addition to the
55-
[options supported by node-glob][node-glob documentation] and [glob-stream]:
56-
57-
### options.cwd
58-
59-
The working directory the folder is relative to.
60-
61-
Type: `String`
62-
63-
Default: `process.cwd()`
64-
6527

66-
### options.buffer
67-
Type: `Boolean`
68-
69-
Default: `true`
70-
71-
Setting this to `false` will return `file.contents` as a stream and not
72-
buffer files. This is useful when working with large files.
73-
74-
**Note:** Plugins might not implement support for streams.
75-
76-
### options.read
77-
Type: `Boolean`
78-
79-
Default: `true`
80-
81-
Setting this to `false` will return `file.contents` as null and not read
82-
the file at all.
83-
84-
### options.base
85-
Type: `String`
86-
87-
Default: everything before a glob starts (see [glob-parent])
88-
89-
E.g., consider `somefile.js` in `client/js/somedir`:
28+
## Signature
9029

9130
```js
92-
// Matches 'client/js/somedir/somefile.js' and resolves `base` to `client/js/`
93-
gulp.src('client/js/**/*.js')
94-
.pipe(minify())
95-
.pipe(gulp.dest('build')); // Writes 'build/somedir/somefile.js'
96-
97-
gulp.src('client/js/**/*.js', { base: 'client' })
98-
.pipe(minify())
99-
.pipe(gulp.dest('build')); // Writes 'build/js/somedir/somefile.js'
31+
src(globs, [options])
10032
```
10133

102-
### options.since
103-
Type: `Date` or `Number`
104-
105-
Setting this to a Date or a time stamp will discard any file that have not been
106-
modified since the time specified.
107-
108-
### options.passthrough
109-
Type: `Boolean`
110-
111-
Default: `false`
112-
113-
If true, it will create a duplex stream which passes items through and
114-
emits globbed files.
115-
116-
### options.allowEmpty
117-
Type: `Boolean`
118-
119-
Default: `false`
34+
### Parameters
35+
36+
| parameter | type | note |
37+
|:--------------:|:------:|-------|
38+
| globs | string <br> array | [Globs][globs-concepts] to watch on the file system. |
39+
| options | object | Detailed in [Options][options-section] below. |
40+
41+
### Returns
42+
43+
A stream that can be used at the beginning or in the middle of a pipeline to add files based on the given globs.
44+
45+
### Errors
46+
47+
When the `globs` argument can only match one file (such as `foo/bar.js`) and no match is found, throws an error with the message, "File not found with singular glob". To suppress this error, set the `allowEmpty` option to `true`.
48+
49+
When an invalid glob is given in `globs`, throws an error with the message, "Invalid glob argument".
50+
51+
### Options
52+
53+
**For options that accept a function, the passed function will be called with each Vinyl object and must return a value of another listed type.**
54+
55+
56+
| name | type | default | note |
57+
|:--------:|:------:|------------|--------|
58+
| buffer | boolean <br> function | true | When true, file contents are buffered into memory. If false, the Vinyl object's `contents` property will be a paused stream. Contents of large files may not be able to be buffered. <br> **Note:** Plugins may not implement support for streaming contents. |
59+
| read | boolean <br> function | true | If false, files will be not be read and their Vinyl objects won't be writable to disk via `.dest()`. |
60+
| since | date <br> timestamp <br> function | | When set, only creates Vinyl objects for files that have been modified since the specified time. |
61+
| removeBOM | boolean <br> function | true | When true, removes the BOM from UTF-8 encoded files. If false, ignores a BOM.. |
62+
| sourcemaps | boolean <br> function | false | If true, enables [sourcemaps][sourcemaps-section] support on Vinyl objects created. Loads inline sourcemaps and resolves external sourcemap links. |
63+
| resolveSymlinks | boolean <br> function | true | When true, recursively resolves symbolic links to their targets. If false, preserves the symbolic links and sets the Vinyl object's `symlink` property to the original file's path. |
64+
| cwd | string | `process.cwd()` | The directory that will be combined with any relative path to form an absolute path. Is ignored for absolute paths. Use to avoid combining `globs` with `path.join()`. <br> _This option is passed directly to [glob-stream][glob-stream-external]._ |
65+
| base | string | | Explicitly set the `base` property on created Vinyl objects. Detailed in [API Concepts][glob-base-concepts]. <br> _This option is passed directly to [glob-stream][glob-stream-external]._ |
66+
| cwdbase | boolean | false | If true, `cwd` and `base` options should be aligned. <br> _This option is passed directly to [glob-stream][glob-stream-external]._ |
67+
| root | string | | The root path that `globs` are resolved against. <br> _This option is passed directly to [glob-stream][glob-stream-external]._ |
68+
| allowEmpty | boolean | false | When false, `globs` which can only match one file (such as `foo/bar.js`) causes an error to be thrown if they don't find a match. If true, suppresses glob failures. <br> _This option is passed directly to [glob-stream][glob-stream-external]._ |
69+
| uniqueBy | string <br> function | `'path'` | Remove duplicates from the stream by comparing the string property name or the result of the function. <br> **Note:** When using a function, the function receives the streamed data (objects containing `cwd`, `base`, `path` properties). |
70+
| dot | boolean | false | If true, compare globs against dot files, like `.gitignore`. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
71+
| silent | boolean | true | When true, suppresses warnings from printing on `stderr`. <br> **Note:** This option is passed directly to [node-glob][node-glob-external] but defaulted to `true` instead of `false`. |
72+
| mark | boolean | false | If true, a `/` character will be appended to directory matches. Generally not needed because paths are normalized within the pipeline. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
73+
| nosort | boolean | false | If true, disables sorting the glob results. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
74+
| stat | boolean | false | If true, `fs.stat()` is called on all results. This adds extra overhead and generally should not be used. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
75+
| strict | boolean | false | If true, an error will be thrown if an unexpected problem is encountered while attempting to read a directory. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
76+
| nounique | boolean | false | When false, prevents duplicate files in the result set. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
77+
| debug | boolean | false | If true, debugging information will be logged to the command line. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
78+
| nobrace | boolean | false | If true, avoids expanding brace sets - e.g. `{a,b}` or `{1..3}`. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
79+
| noglobstar | boolean | false | If true, treats double-star glob character as single-star glob character. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
80+
| noext | boolean | false | If true, avoids matching [extglob][extglob-docs] patterns - e.g. `+(ab)`. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
81+
| nocase | boolean | false | If true, performs a case-insensitive match. <br> **Note:** On case-insensitive file systems, non-magic patterns will match by default. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
82+
| matchBase | boolean | false | If true and globs don't contain any `/` characters, traverses all directories and matches that glob - e.g. `*.js` would be treated as equivalent to `**/*.js`. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
83+
| nodir | boolean | false | If true, only matches files, not directories. <br> **Note:** To match only directories, end your glob with a `/`. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
84+
| ignore | string <br> array | | Globs to exclude from matches. This option is combined with negated `globs`. <br> **Note:** These globs are always matched against dot files, regardless of any other settings. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
85+
| follow | boolean | false | If true, symlinked directories will be traversed when expanding `**` globs. <br> **Note:** This can cause problems with cyclical links. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
86+
| realpath | boolean | false | If true, `fs.realpath()` is called on all results. This may result in dangling links. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
87+
| cache | object | | A previously generated cache object - avoids some file system calls. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
88+
| statCache | object | | A previously generated cache of `fs.Stat` results - avoids some file system calls. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
89+
| symlinks | object | | A previously generated cache of symbolic links - avoids some file system calls. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
90+
| nocomment | boolean | false | When false, treat a `#` character at the start of a glob as a comment. <br> _This option is passed directly to [node-glob][node-glob-external]._ |
91+
92+
## Sourcemaps
93+
94+
Sourcemap support is built directly into `src()` and `dest()`, but is disabled by default. Enable it to produce inline or external sourcemaps.
95+
96+
Inline sourcemaps:
97+
```js
98+
const { src, dest } = require('gulp');
99+
const uglify = require('gulp-uglify');
120100

121-
When true, will allow singular globs to fail to match. Otherwise, globs which are only supposed to match one file (such as `./foo/bar.js`) will cause an error to be thrown if they don't match.
101+
src('input/**/*.js', { sourcemaps: true })
102+
.pipe(uglify())
103+
.pipe(dest('output/', { sourcemaps: true }));
104+
```
122105

106+
External sourcemaps:
123107
```js
124-
// Emits an error if app/scripts.js doesn't exist
125-
gulp.src('app/scripts.js')
126-
.pipe(...);
108+
const { src, dest } = require('gulp');
109+
const uglify = require('gulp-uglify');
127110

128-
// Won't emit an error
129-
gulp.src('app/scripts.js', { allowEmpty: true })
130-
.pipe(...);
111+
src('input/**/*.js', { sourcemaps: true })
112+
.pipe(uglify())
113+
.pipe(dest('output/', { sourcemaps: '.' }));
131114
```
132115

133-
[glob-stream]: https://github.com/gulpjs/glob-stream
134-
[glob-parent]: https://github.com/es128/glob-parent
135-
[node-glob documentation]: https://github.com/isaacs/node-glob#options
136-
[node-glob]: https://github.com/isaacs/node-glob
137-
[piped]: http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
138-
[stream]: http://nodejs.org/api/stream.html
139-
[Vinyl files]: https://github.com/gulpjs/vinyl-fs
116+
[sourcemaps-section]: #sourcemaps
117+
[options-section]: #options
118+
[vinyl-concepts]: concepts.md#vinyl
119+
[glob-base-concepts]: concepts.md#glob-base
120+
[globs-concepts]: concepts.md#globs
121+
[extglob-docs]: LINK_NEEDED
122+
[node-glob-external]: https://github.com/isaacs/node-glob
123+
[glob-stream-external]: https://github.com/gulpjs/glob-stream

0 commit comments

Comments
 (0)
Please sign in to comment.