Article From:https://www.cnblogs.com/wanghui-garcia/p/9725111.html

https://www.gulpjs.com.cn/docs/getting-started/ ,This is the 3.9.0 version.

The installation version found later is 4.0.0. Look at the following:

https://github.com/gulpjs/gulp/blob/master/docs/API.md

Reference: https://blog.csdn.net/jianjianjianjiande/article/details/79048778? Utm_source=copy

4.0.0 update”:

  • The new mission system (based on Bach, replaced the original orchestrator based task system).
  • Remove gulp.reset
  • gulp.task No longer support the use of three parameters.
  • gulp.task The task registered with string must be the task that is called directly in the command line.
  • gulp.task You can accept a single parameter syntax, which must be a named function whose name is used as the task name
  • Added gulp.series and gulp.parallel methods for composite tasks.
  • Adding the gulp. tree method to get the task tree, passing in the {deep: true} parameter yields an archy-compatible list of nodes
  • The gulp.registry method is added to customize the registry.
  • The gulp.symlink method is added, and the function is consistent with gulp.dest, but only by way of soft links.
  • gulp.dest Adding the dirMode parameter to the gulp.symlink method allows for better control of the target directory.
  • gulp.src The file matching strings will be interpreted sequentially, so you can write gulp.src (“.js’, ‘!b.js’, ‘bad.js’])(Exclude all JS files starting with B except bad.js)
  • gulp.src Method adds the sin option to filter files that have been modified at a specific point in time (for incremental compilation)
  • Separate the command line into an independent module to save bandwidth / space. You can install the command line with NPM install gulp-g or NPM install gulp-cli-g, but gulp-cli does not contain modulesBlock code is relatively small.
  • The command line adds the – tasks-json parameter to export the entire task tree for his use.
  • The command line adds the – verify parameter to check whether package.json contains blacklist plug-ins.

 

Reasons for using gulp: https://blog.csdn.net/xllily_11/article/details/51320002

gulp Command line (CLI) document

Parameter marker

gulp Only parameter markers you need to be familiar with, and all other parameter markers are used only when some tasks need them.

  • -v or --version The gulp version number installed on the global and local items will be displayed.
  • --require <module path> Reqiure will be a module before execution. This is useful for some language compiler or other applications. You can use more than one.--require
  • --gulpfile <gulpfile path> Manually specify a gulpfile path, which is very useful when you have many gulpfile. This will also set CWD to the directory where the gulpfile is located.
  • --cwd <dir path> Manually specify CWD. Define the location of the gulpfile lookup, and all the corresponding dependencies (require) will calculate the relative path from there
  • -T or --tasks The task dependency tree of the specified gulpfile will be displayed.
  • --tasks-simple Displays the task list in the gulpfile loaded by YISHION text.
  • --color Force gulp and gulp plug-ins to display colors, even without color support.
  • --no-color Force no color display, even if color support is detected.
  • --silent All gulp logs are prohibited.

The command line will record where it is running in process.env.INIT_CW.

 

Introductory guide

1.First, generate the package.json file:

npm init

Get:

About to write to /Users/user/gulp-metamask/package.json:

{
  "name": "gulp-metamask",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

2. As a project development dependency (devDependencies) installation:

npm install --save-dev gulp

or

Global install gulp:

npm install --global gulp

3. Create a name under the project root directory. gulpfile.js Documents:

var gulp = require('gulp');

gulp.task('default', function() {
  // Put your default task code here.
  console.log('hello default');
});

4. Run gulp:

Return:

userMacBook-Pro:gulp-metamask user$ gulp
[09:56:36] Using gulpfile ~/gulp-metamask/gulpfile.js
[09:56:36] Starting 'default'...
hello default
[09:56:36] The following tasks did not complete: default
[09:56:36] Did you forget to signal async completion?

 

gulp API

gulp.src(globs[, options])

Emits are files that match the provided file matching pattern (glob) or array of globs, []. Will return one Vinyl files A stream It can be used piped To other plug-ins.

 

Parameters:

glob Please refer to node-glob grammar Or you can write the path of the file directly.

Name specification*Matches 0 or more characters in the file path, but does not match the path separator unless the path separator appears at the end* *The 0 or more directories and their subdirectories in the matching path need to appear separately, that is, it can't have anything else left or right. If at the end, it can also match files.?Matches a character in file path (not matching path delimiter).[... Match any character in square brackets when the first character in square brackets is ^ or!When it does not match any of the other characters appearing in square brackets, it is similar to the usage in JS regular expressions! (pattern|pattern|pattern)      Matches any pattern that does not match any given pattern in parentheses.? (pattern|pattern|pattern)          Match any pattern given in parentheses 0 or 1 times.+ (pattern|pattern|)pattern)          Matches any pattern given in brackets at least 1 times.* (pattern|pattern|pattern)          Matches any pattern given in parentheses 0 times or more times.
 @(pattern|pattern|pattern)          Match any pattern given in parenthesis 1 times.

  globs

    Type: String or Array

    The glob to be read or contains an array of globs (when there are multiple matching patterns).

  options

    Type: Object

    adopt glob-stream Pass to node-glob Parameters.

    except node-glob and glob-stream Outside the parameters supported, gulp adds some additional options:

    options.buffer

      Type: Boolean Default value: true

      If the item is set to false,Then it will return in stream mode. file.contents Rather than the form of file buffer. This will be useful when dealing with some large files. * * Note: * plug in may not support stream.

    options.read

      Type: Boolean Default value: true

      If the item is set to false, that file.contents It will return empty value (null), that is, it will not read the file.

    options.base:Indicates the root directory base

      Type: String Default value: it will be added before glob. glob2base)

    options.since

      Type: Date or Number

       Setting this to a Date or a time stamp will discard any file that have not been modified since the time specified.Filter out the files that have not been modified from the time specified by since.

    options.passthrough

      Type: Boolean

      Default: false

        If true, it will create a duplex stream which passes items through and emits globbed files.If it is true, it will create a duplex stream and transmit the items by issuing the globbed file.

    options.allowEmpty

      Type: Boolean

      Default: false

        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.Allow no globs to be matched, otherwise it will be wrong if it is not matched.

Illustrating

For example, imagine that you are in the local area. client/js/somedir In the directory, there is a file called somefile.js :

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('default', function() {
  // Put your default task code here.
  console.log('hello default');
});

gulp.src('./client/js/**/*.js') // Match'./client/js/somedir/somefile.js'and resolve `base` to `./client/js/` by default.
  .pipe(minify())
  .pipe(gulp.dest('build'));  // Write'./build/somedir/somefile.js', part of buildbase.Change to '.build',./buildwillReplace the part before the wildcard begins.

gulp.src('./client/js/**/*.js', { base: './client' }) //Here is to set base to `./client/`.
  .pipe(minify())
  .pipe(gulp.dest('./build'));  // Write to'./build/js/somedir/somefile.js'

Before you run, install plug-in gulp-minify to compress files.

.min.jsFor the compressed version, the file will be smaller and the transmission efficiency will be faster. .js is not compressed version, which is convenient for debugger debugging.

npm install --save-dev gulp-minify

Operation results:

userdeMacBook-Pro:gulp-metamask user$ gulp
[10:27:29] Using gulpfile ~/gulp-metamask/gulpfile.js
[10:27:29] Starting 'default'...
hello default
[10:27:29] The following tasks did not complete: default
[10:27:29] Did you forget to signal async completion?

Then check the folder to see the files generated in the corresponding locations:

 It will be born in the dest folder into a SRC file somefile.js and a min file somefile-min.js.

 

Here are some configurations that can be done in minify, for more detailed information at https://www.npmjs.com/package/gulp-minify

Options(There are only three.

  • ext An object that specifies output src and minified file extensions.

    • src

      The suffix string of the filenames that output source files ends with.The suffix logo added to the output SRC file is usually not added, that is, the original filename.

    • min

      • When string: The suffix string of the filenames that output minified files ends with. The suffix logo that is added to the output min file is generally defaults to’-min.js’.
      • When Array: The regex expressions to be replaced with input filenames. For example: [/\.(.*)-source\.js$/, '$1.js']
  • exclude

    Will not minify files in the dirs. The files in this folder are not compressed, either.

  • ignoreFiles :Will not minify files which matches the pattern.Files that satisfy this pattern do not compress.

gulp.src('./client/js/**/*.js', { base: './client' }) //Here is to set base to `./client/`./**/There may be multiple catalogs in the middle.
  .pipe(minify({
        ext:{
            src:'-mydebug.js',
            min:'-mymin.js'
        },
        exclude: ['tasks'], //because/**/There may be multiple catalogs in the middle.,So exclude.tasksFile compression under directoryIgnoreFiles: ['.combo.js', '-min.js']
    }))
  .pipe(gulp.dest('./build'));  // Write to'build/js/somedir/'

Return to visibleignoreFiles: [.combo.js‘, -min.js]No success

This is because it is not written correctly, as ignoreFiles: [‘*.combo.js’,’*-min.js’] and then it becomes:

 

exclude: ['tasks']It is successful. The files under the Tasks folder are not compressed.

 

Withoutexclude: [tasks],The results are:

 

 

gulp.dest(path[, options])

It can be piped in and will write to the specified path, which will emit all the data, so you can pipe it to multiple folders. If a folder does not exist, it will be created automatically. The above example can also be explained. If given the pathIt is relative path, then it will produce the corresponding path, path according to base.The part before the wildcard will be changed.

Be careful:gulp.dest()Incoming path parameters can only be used to specify the files to be generated.Catalogues,You can’t specify the file name of the generated file. The file name of the generated file uses the file name imported into its file stream itself, soThe generated file name is determined by the import to its file stream.,Even if we pass it a path parameter with a filename, it will then treat the filename as a directory name, such as:

gulp.src('script/jquery.js')
    .pipe(gulp.dest('dist/foo.js'));
//The resulting file path is dist/foo.js/jquery.js instead of dist/foo.js.

 Parameters:

  path

    Type: String or Function

    The path to which the file will be written (output directory). You can also pass in a function and return the corresponding path in the function. vinyl File instance To provide.

  options

    Type: Object

    options.cwd

      Type: String Default value: process.cwd()

      Output directory cwd Parameters are valid only when the output directory is relative path.

    options.mode

      Type: String Default value: 0777

      The octal access character is used to define all the permissions of the directory created in the output directory.

 

gulp.task(name[, deps], fn)

Define a usage Orchestrator The task of implementation (task).

Parameters:

  name

    Task name, if you need to run some of your tasks on the command line, then do not use spaces in the name.

  deps

    Type: Array

    An array that contains the task list. These tasks will be completed before your current task is run.

  Be careful: Are your tasks running before these pre dependent tasks are completed? Make sure that the tasks in the task list you rely on are executed asynchronously correctly: using a callback, or returning a promise or stream.

  fn

    This function defines some operations to be performed by the task. Generally speaking, it will be this form:gulp.src().pipe(someplugin())

    Asynchronous task support

    Tasks can be executed asynchronously, if fn One of the following points can be achieved:

      Accept a callback

Be careful: By default, task will be executed with the largest number of concurrency, that is to say, gulp will.Run all task at once and do not wait. If you want to create a serialized task queue and execute it in a specific order, you need to do two things:

  • Give a hint to tell task when to finish.
  • And give a hint to tell a task to rely on the completion of another task.

Examples of how to serialize:

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('default',['two','one'], function() {
  // Put your default task code here.
  console.log('hello default');
});

gulp.task('one',function(){
  //oneIs an asynchronous execution task.
  setTimeout(function(){
    console.log('hello one')
  },5000);
});

//twoAlthough the task is dependent on the one task, it does not wait until the asynchronous operation in the one task is completed.
gulp.task('two',['one'],function(){
  console.log('hello two');
});

Be careful:

Operation error: AssertionError [ERR_ASSERTION]: Task function must be specified

Reason: Gulp 4 no longer uses the [] way to define DEPs instead of using it instead.gulp.series() and gulp.parallel(),Functions that serialize are implemented as follows:

  • gulp.series: will run the tasks in order
  • gulp.parallel: will run the tasks in parallel

Give an example:

gulp.task(
  'build',
  gulp.series(
    'clean', //beforeclean
    gulp.parallel('sass', 'copy-assets', 'ts-compile', 'templates', 'copy-vendor'), //Then these 5 task simultaneously.'index' //Again index
  )
);

 

1.gulp.seriesAn example

var gulp = require('gulp');
var minify = require('gulp-minify');
gulp.task('one',function(){
  //oneIs an asynchronous execution task.
  setTimeout(function(){
    console.log('hello one');
  },5000);
});

//twoAlthough the task is dependent on the one task, it does not wait until the asynchronous operation in the one task is completed.
gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two','one', function(done) {
  // Put your default task code here.
  console.log('hello default');
  done()
}));

⚠️Error, only output’one’

userdeMacBook-Pro:gulp-metamask user$ gulp
[14:30:24] Using gulpfile ~/gulp-metamask/gulpfile.js
[14:30:24] Starting 'default'...
[14:30:24] Starting 'two'...
[14:30:24] Starting 'one'...
hello one
[14:30:29] The following tasks did not complete: default, two, one
[14:30:29] Did you forget to signal async completion?

 

Solution: execution callback

var gulp = require('gulp');
var minify = require('gulp-minify');
gulp.task('one',function(done){
  //oneIs an asynchronous execution task.
  setTimeout(function(){
    console.log('hello one');
  },5000);
  done(); //The asynchronous function setTimeout runs asynchronously. You can run two without waiting.
});

//twoAlthough the task is dependent on the one task, it does not wait until the asynchronous operation in the one task is completed.
gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two','one', function(done) {
  // Put your default task code here.
  console.log('hello default');
  done()
}));

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
[14:28:56] Using gulpfile ~/gulp-metamask/gulpfile.js
[14:28:56] Starting 'default'... 
[14:28:56] Starting 'two'... 
[14:28:56] Starting 'one'...
[14:28:56] Finished 'one' after 871 μs
[14:28:56] Starting '<anonymous>'...   //Do not wait for asynchronous function to complete.Hello two[14:28:56] Finished '<anonymous>' after 355 μs
[14:28:56] Finished 'two' after 2.31 ms
[14:28:56] Starting 'one'...
[14:28:56] Finished 'one' after 295 μs
[14:28:56] Starting '<anonymous>'...
hello default
[14:28:56] Finished '<anonymous>' after 297 μs
[14:28:56] Finished 'default' after 5.03 ms
hello one  //Executed two times.Hello one
The reason for the two execution here is that there is a wrong place on it.'default'And'one'in'two' is repeated, and the runtime becomes one-two-one-default.

It should be changed to:

gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two', function(done) {
  console.log('hello default');
  done()
}));

Or:

gulp.task('two',function(done){
  console.log('hello two');
  done();
});

gulp.task('default',gulp.series('two','one',function(done) {
  console.log('hello default');
  done()
}));

 

 

What if we want two to wait for one to start again after the end of asynchronous execution?

1)

var gulp = require('gulp');
var minify = require('gulp-minify');
gulp.task('one',function(done){
  setTimeout(function(){
    console.log('hello one');
    done(); //Instead of writing at the end, waiting for the asynchronous function to be executed, it means that one is executed and two can be executed.
  },5000);
});

gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two', function(done) {
  console.log('hello default');
  done()
}));

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
[14:50:50] Using gulpfile ~/gulp-metamask/gulpfile.js
[14:50:50] Starting 'default'...
[14:50:50] Starting 'two'...
[14:50:50] Starting 'one'...
hello one
[14:50:55] Finished 'one' after 5.01 s
[14:50:55] Starting '<anonymous>'...
hello two
[14:50:55] Finished '<anonymous>' after 509 μs
[14:50:55] Finished 'two' after 5.01 s
[14:50:55] Starting '<anonymous>'...
hello default
[14:50:55] Finished '<anonymous>' after 307 μs
[14:50:55] Finished 'default' after 5.01 s

 

2)Returns a stream object when a task is defined. The task is to operate the flow obtained by gulp.src:

var gulp = require('gulp');
var minify = require('gulp-minify');


gulp.task('one',function(){
  var stream = gulp.src('./client/**/*.js')
      .pipe(minify()) //dosomething()There are some asynchronous operations.
      .pipe(gulp.dest('./build1'));
    return stream;
});

gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two', function(done) {
  console.log('hello default');
  done()
}));

 

Return:

 

3)Returns a promise object.

var gulp = require('gulp');
var Q = require('q'); //A famous asynchronous processing libraryhttps://github.com/kriskowal/q
gulp.task('one',function(){
  var deferred = Q.defer();
  // Do some asynchronous operations.
  setTimeout(function() {
     deferred.resolve();
     console.log('hello one');
  }, 5000);
  return deferred.promise;
});

gulp.task('two',gulp.series('one',function(done){
  console.log('hello two');
  done();
}));

gulp.task('default',gulp.series('two', function(done) {
  console.log('hello default');
  done()
}));

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
[15:00:54] Using gulpfile ~/gulp-metamask/gulpfile.js
[15:00:54] Starting 'default'...
[15:00:54] Starting 'two'...
[15:00:54] Starting 'one'...
hello one
[15:00:59] Finished 'one' after 5.01 s
[15:00:59] Starting '<anonymous>'...
hello two
[15:00:59] Finished '<anonymous>' after 428 μs
[15:00:59] Finished 'two' after 5.01 s
[15:00:59] Starting '<anonymous>'...
hello default
[15:00:59] Finished '<anonymous>' after 297 μs
[15:00:59] Finished 'default' after 5.01 s

 

2.Here is an example of gulp.parallel:

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('one', function(done) {
  // do stuff
  // setTimeout(function(){
  //   console.log('hello one')
  // },5000);
  console.log('hello one');
  done();
});

gulp.task('two', function(done) {
  // do stuff
  console.log('two');
  done();
});

gulp.task('default', gulp.parallel('two', 'one', function(done) {
  // do more stuff
  console.log('default');
  done();
}));

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
[14:20:12] Using gulpfile ~/gulp-metamask/gulpfile.js
[14:20:12] Starting 'default'...
[14:20:12] Starting 'two'...
[14:20:12] Starting 'one'... //two,oneAt the same time[14:20:12] Starting '<anonymous>'...
two
[14:20:12] Finished 'two' after 1.01 ms
hello one
[14:20:12] Finished 'one' after 1.35 ms
default
[14:20:12] Finished '<anonymous>' after 1.46 ms
[14:20:12] Finished 'default' after 3.4 ms

 

 

Monitor files and do something when the file changes. It always returns a EventEmitter to launch (EMIT). change Event.

gulp.watch(glob[, opts], tasks)

  glob

    Type: String or Array

    A glob string, or an array containing multiple glob strings, is used to specify which files to monitor for changes, with the same rules and usage as glob in the gulp. SRC () method.

  opts

    Type: Object

    Pass to gaze The parameter is an optional configuration object which is usually not required.

  tasks

    Type: Array

    One or more passes that need to be executed after the change of the document. gulp.task() The name of the task created

 Give an example:

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('one', function(done) {
  console.log('hello one');
  done();
});

gulp.task('two', function(done) {
  console.log('two');
  done();
});
gulp.task('default', gulp.parallel('one', 'two',function(done) {
  console.log('default');
  done();
}));

var watcher = gulp.watch('./client/js/**/*.js',gulp.parallel('default'));//It is necessary to perform some task task operations while monitoring changes.
watcher.on('change', function(path, stats) {
  console.log('File ' + path + ' was changed');
});

gulp.src('./client/js/**/*.js') // Match'client/js/somedir/somefile.js'and resolve `base` to `./client/js/` by default.
  .pipe(minify())
  .pipe(gulp.dest('./build2'));  // Write to'build/somedir/somefile.js'because build changes part of base.

The result is that only task is running, and watcher.on has not responded.Do not know why?

userdeMacBook-Pro:gulp-metamask user$ gulp
[15:34:17] Using gulpfile ~/gulp-metamask/gulpfile.js
[15:34:17] Starting 'default'...
[15:34:17] Starting 'one'...
[15:34:17] Starting 'two'...
[15:34:17] Starting '<anonymous>'...
hello one
[15:34:17] Finished 'one' after 1.28 ms
two
[15:34:17] Finished 'two' after 1.65 ms
default
[15:34:17] Finished '<anonymous>' after 1.88 ms
[15:34:17] Finished 'default' after 4.01 ms

 

 

gulp.lastRun(taskName, [timeResolution])

Returns the timestamp of the last time the task ran successfully. The time will be the time the task started. Returns undefined if the task has not run yet.

Returns the timestamp of the last successful task that started, and undefined if the task has not been run.

taskName:taskName

Type: String

The name of the registered task or of a function.

timeResolution:The precision of the time stamp returned

Type: Number.

Default: 1000 on node v0.10, 0 on node v0.12 (and iojs v1.5).

Set the time resolution of the returned timestamps. Assuming the task named “someTask” ran at 1426000004321:

  • gulp.lastRun('someTask', 1000) would return 1426000004000.
  • gulp.lastRun('someTask', 100) would return 1426000004300.

For example:

var gulp = require('gulp');
gulp.task('two', function(done) {
  console.log('two');
  done();
});
gulp.task('default', gulp.parallel('two', function(done) {
  console.log('default');
  done();
}));
console.log(gulp.lastRun('two',1000));
console.log(gulp.lastRun('two',100));
console.log(gulp.tree());

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
undefined //Not expected valueUndefined{label:'Tasks', nodes: [ 'two', 'default' ] } //Get the runtime task tree[16:00:08] Using gulpfile ~/gulp-metamask/gulpfile.js
[16:00:08] Starting 'default'...
[16:00:08] Starting 'two'...
[16:00:08] Starting '<anonymous>'...
two
[16:00:08] Finished 'two' after 976 μs
default
[16:00:08] Finished '<anonymous>' after 1.35 ms
[16:00:08] Finished 'default' after 3.11 ms

Sometimes errors occur: AssertionError [ERR_ASSERTION]: Only functions can check lastRun

So willgulp.lastRun(two‘,1000)The write function succeeded.

var gulp = require('gulp');
gulp.task('two', function(done) {
  console.log('two');
  done();
});
gulp.task('default', gulp.parallel('two', function(done) {
  console.log('default');
  console.log(gulp.lastRun('two',1000));
  console.log(gulp.lastRun('two',100));
  done();
}));

Only then can we get:

userdeMacBook-Pro:gulp-metamask user$ gulp
[16:02:11] Using gulpfile ~/gulp-metamask/gulpfile.js
[16:02:11] Starting 'default'...
[16:02:11] Starting 'two'...
[16:02:11] Starting '<anonymous>'...
two
[16:02:11] Finished 'two' after 763 μs
default
1538294531000
1538294531800
[16:02:11] Finished '<anonymous>' after 2.11 ms
[16:02:11] Finished 'default' after 3.98 ms

 

 

gulp.tree(options)

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('one', function(done) {
  console.log('hello one');
  done();
});

gulp.task('two', function(done) {
  console.log('two');
  done();
});
gulp.task('default', gulp.parallel('one', 'two',function(done) {
  console.log('default');
  done();
}));

console.log(gulp.tree());//Task tree for detecting runtime

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
{ label: 'Tasks', nodes: [ 'one', 'two', 'default' ] } //
[15:50:27] Using gulpfile ~/gulp-metamask/gulpfile.js
[15:50:27] Starting 'default'...
[15:50:27] Starting 'one'...
[15:50:27] Starting 'two'...
[15:50:27] Starting '<anonymous>'...
hello one
[15:50:27] Finished 'one' after 1.2 ms
two
[15:50:27] Finished 'two' after 1.66 ms
default
[15:50:27] Finished '<anonymous>' after 1.87 ms
[15:50:27] Finished 'default' after 3.96 ms

 

gulp.tree({deep:true})
var gulp = require('gulp');
gulp.task('two', function(done) {
  console.log('two');
  done();
});
gulp.task('default', gulp.parallel('two', function(done) {
  console.log('default');
  done();
}));
console.log(gulp.tree({deep:true}));

Return:

userdeMacBook-Pro:gulp-metamask user$ gulp
{ label: 'Tasks',
  nodes:
   [ { label: 'two', type: 'task', nodes: [] },
     { label: 'default', type: 'task', nodes: [Array] } ] }
[16:07:29] Using gulpfile ~/gulp-metamask/gulpfile.js
[16:07:29] Starting 'default'...
[16:07:29] Starting 'two'...
[16:07:29] Starting '<anonymous>'...
two
[16:07:29] Finished 'two' after 758 μs
default
[16:07:29] Finished '<anonymous>' after 1.08 ms
[16:07:29] Finished 'default' after 2.69 ms

 

gulp.registry([registry])Custom registry

Get or set the underlying task registry. Inherited from undertaker; see the undertaker documention on registries. Using this, you can change registries that enhance gulp in different ways. Utilizing a custom registry has at least three use cases:

  • Sharing tasks
  • Sharing functionality (e.g. you could override the task prototype to add some additional logging, bind task metadata or include some config settings.)
  • Handling other behavior that hooks into the registry lifecycle (see gulp-hub for an example)

To build your own custom registry see the undertaker documentation on custom registries.

Parameters:

registry

A registry instance. When passed in, the task of the current Registry will be transferred to the new registry, which will then replace the current registry with a new registry

Example(No tests on her own)

This example shows how to create and use a simple custom registry to add tasks.

//gulpfile.js
var gulp = require('gulp');

var companyTasks = require('./myCompanyTasksRegistry.js');

gulp.registry(companyTasks);

gulp.task('one', gulp.parallel('someCompanyTask', function(done) {
  console.log('in task one');
  done();
}));
//myCompanyTasksRegistry.js
var util = require('util');

var DefaultRegistry = require('undertaker-registry');

function MyCompanyTasksRegistry() {
  DefaultRegistry.call(this);
}
util.inherits(MyCompanyTasksRegistry, DefaultRegistry);

MyCompanyTasksRegistry.prototype.init = function(gulp) {
  gulp.task('clean', function(done) {
    done();
  });
  gulp.task('someCompanyTask', function(done) {
    console.log('performing some company task.');
    done();
  });
};

module.exports = new MyCompanyTasksRegistry();

 

gulp.symlink(folder[, options])Similar to gulp.dest, but it is soft connection, no testing.

Functions exactly like gulp.dest, but will create symlinks instead of copying a directory.

folder

Type: String or Function

A folder path or a function that receives in a file and returns a folder path.

options

Type: Object

options.cwd

Type: String

Default: process.cwd()

cwd for the output folder, only has an effect if provided output folder is relative.

options.dirMode

Type: String or Number

Default: Default is the process mode.

Octal permission specifying the mode the directory should be created with: e.g. "0755", 0755 or 493 (0755 in base 10).


The gulp API is briefly described above, and for further study you can see https://blog.csdn.net/c_kite/article/details/73165427
Learn about the use of plug-ins.

Leave a Reply

Your email address will not be published. Required fields are marked *