Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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を使用すれば処理が記述できます!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした