Gulp 插件开发

Fork Me On Github

基本模板

js
              
'use strict';

var Transform = require('readable-stream/transform');

module.exports = function (options) {
    return new Transform({
        objectMode: true,
        transform: function (file, enc, callback) {
            // TODO
            callback(null, file)
        }
    });
};
1234567891011121314

file 说明

file.path 完整路径
file.contents 内容 Buffer 或 Stream
file.basename 文件名 file.txt
file.extname 文件拓展名 .txt
file.isNull() 是否为空
file.isBuffer() 是否为Buffer 可以使用 String(file.contents) 转化为字符串, file.contents = Buffer.from(''); 可以修改内容
file.isStream() 是否为Stream

更多请参考 【vinylv

callback 使用

正常返回

js
  
callback(null, file);
12

返回报错(会中断后续所有文件任务)

js
  
callback({stack: 'error file'}, file)
12

中断本次任务,继续操作其他文件

js
 
callback()
1

使用插件

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

gulp.task('default', async() => {
    await gulp.src('src/**/*')
        .pipe(my())
        .pipe(gulp.dest('dist/'));
});
12345678
点击查看全文
0 298 0
42.4 ms
系统信息
Execution time42.4 ms
CPU usage user + system70 % + 0 %
Peak of allocated memory7.13 MB
Included files228
Classes + interfaces + traits142 + 29 + 26
Your IP18.221.190.79
Server IP172.21.0.9
HTTP method / response codeGET / 200
PHP8.4.2
Zodream5.1.0
Servernginx/1.27.3
运行信息
start0.0 ms
match route15.2 ms
controller response3.7 ms
db engine init6.4 ms
db engine end0.6 ms
db init end0.0 ms
view render5.5 ms
end42.6 ms
Queries(10)
[0.3ms] SELECT * FROM `blog` WHERE id = ? LIMIT 1
[0.25ms] SELECT * FROM `blog_term`
[0.63ms] SELECT term_id,COUNT(*) as count FROM `blog` WHERE parent_id = ? GROUP BY term_id
[0.44ms] SELECT id,language FROM `blog` WHERE parent_id = ?
[0.17ms] SELECT `tag_id` FROM `blog_tag_relationship` WHERE blog_id = ?
[0.18ms] SELECT `content`,`name` FROM `blog_meta` WHERE blog_id = ?
[0.19ms] SELECT id,name,avatar FROM `user` WHERE id in (?)
[0.27ms] SELECT id,title,thumb,parent_id,language,description,created_at FROM `blog` WHERE id < ? and language = ? and publish_status = ? ORDER BY id DESC LIMIT 1
[0.23ms] SELECT id,title,thumb,parent_id,language,description,created_at FROM `blog` WHERE id > ? and language = ? and publish_status = ? ORDER BY id ASC LIMIT 1
[0.21ms] SELECT * FROM `ad_position` WHERE code = ? and status = ? LIMIT 1
Views(5)
[Rendered] Module/Blog/UserInterface/layouts/header.php : 0.44ms
[Rendered] Module/Blog/UserInterface/Home/content.php : 3.52ms
[Rendered] Module/Blog/UserInterface/layouts/footer.php : 0.07ms
[Rendered] Module/Blog/UserInterface/Home/detail.php : 8.91ms
[Rendered] UserInterface/Home/layouts/main.php : 1.01ms