Brief introduction:
This article mainly translates Gulp official API, add oneself a little humble solution.
Gulp API docs1, Gulp.src (globs[, Options])
1.1, Description: Src method is to specify the path of the source file to be processed, gulp borrowed from the UNIX operating system pipe (pipe) thought, the output of the previous level, directly into the back-level input, GULP.SRC return the current file stream to the available plug-in;
1.2.globs: The source file match path to be processed. Type (required):String or Stringarray;
Wildcard Path Matching Example:
"Src/a.js": Specify specific documents;
"*": Match All Files Example: Src/*.js (contains all JS files under SRC);
"* *": matches 0 or more sub-folders example: Src/**/*.js (contains the SRC 0 or more subfolders under the JS file);
' {} ': Match multiple attribute examples: Src/{a,b}.js (contains A.js and b.js files) src/*. {Jpg,png,gif} (All jpg/png/gif files under SRC);
“!” : Exclude File Example:!src/a.js (does not contain src under the a.js file);
JavaScript
123456789 |
var gulp = require(' Gulp '), Less = require(' gulp-less '); Gulp. Task(' testless ', function () { //gulp.src (' less/test/style.less ') Gulp. SRC([' less/**/*.less ','!less/{extend,page}/*.less ') . Pipe(less()) . Pipe(gulp. Dest('./css ')); }); |
1.3,Options: Type (optional):Object, there are 3 properties buffer, read, base;
Options.buffer: Type:Boolean default:True is set to false, the stream of File.content is returned and the file is not buffered, which is useful when processing large files;
Options.read: Type:Boolean default:True sets false, the read file operation is not performed and Null is returned;
Options.base: Type:String sets the output path to be stitched backwards based on some part of a path, as shown in the following example:
JavaScript
1234567 |
Gulp. SRC(' client/js/**/*.js ') . Pipe(minify()) . Pipe(gulp. Dest(' Build ')); //Writes ' build/somedir/somefile.js ' Gulp. SRC(' client/js/**/*.js ', { base: ' client ' }) . Pipe(minify()) . Pipe(gulp. Dest(' Build ')); //Writes ' build/js/somedir/somefile.js ' |
2, Gulp.dest (path[, Options])
2.1, Description: Dest method is to specify the path of the file output after processing;
JavaScript
12345 |
Gulp. SRC('./client/templates/*.jade ') . Pipe(Jade()) . Pipe(gulp. Dest('./build/templates ')) . Pipe(minify()) . Pipe(gulp. Dest('./build/minified_templates ')); |
2.2,Path: Type (required):String or function to specify the file output path, or define function return file output path also;
2.3,Options: Type (optional):Object, there are 2 properties CWD, mode;
OPTIONS.CWD: Type:String default:process.cwd (): The path to the working directory of the front script will be used when the file output path is relative to the path;
Options.mode: Type:String default:0777 Specifies the permissions of the folder being created;
3, Gulp.task (name[, Deps], FN)
3.1, Description: Task defines a gulp task;
3.2.name: type (required):String Specifies the name of the task (there should be no spaces);
3.3.deps: type (optional):Stringarray, the task depends on the task (note: The dependent task needs to return the current task of the event stream, see the following example);
JavaScript
1234567891011 |
Gulp. Task(' testless ', function () { return Gulp. SRC([' less/style.less ']) . Pipe(less()) . Pipe(gulp. Dest('./css ')); }); gulp. Task ( ' minicss ' , Span class= "Crayon-sy" >[ ' testless ' ],< Span class= "crayon-h" > function ( { // Perform MINICSS task after testless task Gulp. SRC([' Css/*.css ']) . Pipe(minifycss()) . Pipe(gulp. Dest('./dist/css ')); }); |
3.4,fn: type (required):Function The task to invoke the plug-in operation;
4, Gulp.watch (Glob [, opts], tasks) or Gulp.watch (Glob [, opts, CB])
4.1, Description: Watch method is used to monitor the file changes, the file changes will perform the specified task;
4.2.Glob: The source file match path to be processed. Type (required):String or Stringarray;
4.3,opts: type (optional):Object specific see Https://github.com/shama/gaze;
4.4.Tasks: Type (required):Stringarray The name array of the task to be performed;
4.5,CB (Event): type (optional):function Each file changes the execution of the callback function;
JavaScript
123456789 |
Gulp. Task(' Watch1 ', function () { Gulp. Watch(' less/**/*.less ', [' testless ']); });Gulp. Task(' watch2 ', function () { gulp.watch ( ' js/**/*.js ' , function ( event) { consolelog ( ' File ' + event. Path + ' was ' + event type + ", running tasks ... ' });}); |
5. Concluding remarks
5.1, this article has any mistake, or has any question, welcome the message explanation.
Gulp Tutorial Gulp Chinese API