LoginSignup
3
3

More than 5 years have passed since last update.

gulpでの独自の関数を実行する

Last updated at Posted at 2019-01-15

gulpで独自の処理をいれたい時がありますよね。
そんな時の覚書。

through2

https://qiita.com/morou/items/1297d5dd379ef013d46c
上記記事がとてもわかりやすく書いてありました!
これを参考にプラグインは作成できましたが、そんな大層なものは不要なときはthrough2を使って記述しましょう。

gulpfile.js
const through = require('through2');

gulp.task('deploy', (cb) => {
  gulp.src(['src/**'], { base: '.' })
    .pipe(through.obj((file, enc, callback) => {
      if (file.isNull()) {
        // 何もしない
        return callback(null, file);
      }
      if (file.isStream()) {
        // ストリームの場合はエラー
        this.emit('error', new Error('Streams not supported!'));
      }else if (file.isBuffer()) {
        // ファイルの内容をcontentsに読み込み
        let contents = String(file.contents);
        // 独自の処理を記述する
        // ・・・
        return callback(null, file);
      }
    }));
});

このようにthrough.objを使用すれば処理が記述できます!

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3