gulpfile.jsでdelモジュールで削除する

gulpfile.jsでdelモジュールで削除する

gulpfile.jsのタスクでフォルダ削除したい場合にdelモジュールを使用すると便利です。

del,gulpをインストールします。

npm i -D del gulp

gulpfile.jsを作成します。

testDelタスクで、tmp配下を全削除します。ただしtmp/node_modules配下は削除しません。

const gulp = require('gulp')
const del = require('del')

function testDel(cb) {
  del(['./tmp/**', '!./tmp/node_modules/**'])
  cb()
}
exports.testDel = testDel

!ディレクトリ/**と書けばそのフォルダ配下は削除されません。上記の!./tmp/node_modules/**を省くと、./tmp配下すべて削除されます。

const gulp = require('gulp')
const del = require('del')

function testDel(cb) {
  del(['./tmp/**'])
  cb()
}
exports.testDel = testDel

再帰的に削除する

globが使えますので./tmp/**/*.jsという風に記述すればtmpフォルダ配下のjsファイルを再帰的にすべて削除してくれます。

const gulp = require('gulp')
const del = require('del')

function testDel(cb) {
  del(['./tmp/**/*.js'])
  cb()
}
exports.testDel = testDel

del公式サイト

コメント

タイトルとURLをコピーしました