Привет!

Делаем плагин для 

twitter, github - lapanoid

 Сергей Лапин - freelance Upwork

Компилятор

Шаблонизатор

Тестов-запускатель

Синтаксиса-проверятель

Минификатор-оптимизатор

gulp-postcss

gulp-mocha

gulp-babel

gulp-webpack

gulp-less

gulp-eslint 

И еще 

1979

плагинов...

Таск менеджер

Единое кроссплатформенное 

апи 

Мощнее

легко писать

Компактнее

Исполняется из терминала

легко читать

gulp.src(globs[, options])

gulp.dest(path[, options])

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

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

API

gulp.task('default', ['scripts', 'styles'], () =>{...});

gulp.task('styles', ['clean'], () =>{...});
gulp.task('scripts', ['clean'], () =>{...});

gulp.task('clean', () =>{...});

gulp.task

gulp.watch


const watcher = gulp.watch('js/**/*.js', ['uglify','reload']);
watcher.on('change', ({path, type})=> {
  console.log('File ' + path + ' was ' + type);
});

gulp.src('./client/templates/*.jade')
  .pipe(jade())
  .pipe(gulp.dest('./build/templates'))
  .pipe(minify())
  .pipe(gulp.dest('./build/minified_templates'));

gulp.src и gulp.dest

Vinyl - files

данные  и путь к ним

file.contents -бывает двух видов

Стримы

Буферы

Streams

Плагины!

const gulpPrefixer = (prefixText) => {
  if (!prefixText) {
    throw new PluginError(PLUGIN_NAME, 'Missing prefix text!');
  }

  return through.obj(function(file, enc, cb) {  
    if (file.isStream()) {      
      const streamer = prefixStream(prefixText);
      streamer.on('error', this.emit.bind(this, 'error'));
      file.contents = file.contents.pipe(streamer);
    }
   
    this.push(file);    
    cb();
  });
}

const prefixStream = (prefixText) => {
  const stream = through();
  stream.write(prefixText);
  return stream;
}

Плагины!

function gulpPrefixer(prefixText) {
  if (!prefixText) {
    throw new PluginError(PLUGIN_NAME, 'Missing prefix text!');
  }

  return through.obj(function(file, enc, cb) {
    if (file.isStream()) {
      this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
      return cb();
    }

    if (file.isBuffer()) {
      file.contents = Buffer.concat([prefixText, file.contents]);
    }

    this.push(file);
    cb();
  });
};

Плагины!


import gulp from 'gulp';
import gulpPrefixer from 'gulp-prefixer';

gulp.src('files/**/*.js')
  .pipe(gulpPrefixer('prepended string'))
  .pipe(gulp.dest('modified-files'));
tap(function(file) {
    file.contents = Buffer.concat([
        new Buffer('HEADER'),
        file.contents
    ]);
});

gulp.src("src/**/*.{coffee,js}")
    .pipe(tap(function(file, t) {
        if (path.extname(file.path) === '.coffee') {
            return t.through(coffee, []);
        }
    }))
    .pipe(gulp.dest('build'));

gulp-tap 

gulp-ast

import AST from 'gulp-ast';
 
gulp.task('default', ()=> {
  gulp.src('./src/*.js')
    .pipe(AST.parse())
    .pipe(AST.rewriteRequire(
        (name)=> 'prefix/' + name
    ))
    .pipe(AST.render())
    .pipe(gulp.dest('./lib/'))
});

Спасибо!

Yeoman генератор для плагина -

https://github.com/sindresorhus/generator-gulp-plugin-boilerplate

Плагин для gulp

By Sergey Lapin

Плагин для gulp

  • 177