简体   繁体   中英

How to exclude all node_modules directories from gulp src stream?

My monorepo contains lots of node_modules directories. I want to clean all js files excluding those present in node_modules directory. Unfortunately, I can't make it work. Here're my tries with Gulp:

import { src } from 'gulp';
import * as clean from 'gulp-clean';

const SRC_DIR = '../../apps/lambda/';

function clean() {
  return src([SRC_DIR + '**/*.js', '!' + SRC_DIR + '**/node_module/*.js'], {
    read: false,
  })
    .pipe(clean({ force: true }));
}

The above function deletes js files also from node_modules directory.

import { src } from 'gulp';
import * as clean from 'gulp-clean';
import * as ignore from 'gulp-ignore';

const SRC_DIR = '../../apps/lambda/';

function clean() {
  return src(SRC_DIR + '**/*.js', {
    read: false,
  })
    .pipe(ignore.exclude('**/node_module/*.js'))
    .pipe(clean({ force: true }));
}

Again, the same issue.

What am I doing wrong?

Update: I found the answer: https://stackoverflow.com/a/62708117/6910860

I found the answer. Globbing also needs to exclude parent directory. Here's the correct code:

import { src } from 'gulp';
import * as clean from 'gulp-clean';

const SRC_DIR = '../../apps/lambda/';

function clean() {
  return src(
    [
      SRC_DIR + '**/*.js',
      '!' + SRC_DIR + '**/node_modules/',
      '!' + SRC_DIR + '**/node_modules/**/*.js',
    ],
    {
      read: false,
    }
  ).pipe(clean({ force: true }));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM