65

I am looking for a better glob pattern for usemin, i want to to find all .js files but exclude the .spec.js files. I have the following solution so far.

<script src="components/**/*(.js|!(*.spec.js|*.scss))"></script>

The solution i have at the moment requires me to keep adding file extensions to exclude them, else they get picked up, for example .html files.

I tried to make it only look for .js files and exclude the .spec.js from them but it does not seem to work.

Also adding a !components/**/*.spec.js as another script below does not seem to work.

roughcoder
  • 1,190
  • 1
  • 8
  • 11
  • 2
    You may be able to exclude those test scripts with a glob pattern, but perhaps a better approach would be to put them in a separate `test` directory at the same level as `components`. That way they're easily excluded from your build process. – Troy Gizzi Oct 29 '14 at 19:49
  • Agreed +1 and I have many times, but this time I have a component based folder structure with code and spec together. – roughcoder Oct 29 '14 at 19:52
  • I renamed `*.spec.js` files to just `*.spec`. – Heikki Oct 30 '14 at 00:11

1 Answers1

165

This glob includes all *.js but not *.spec.js:

components/**/!(*.spec).js
Heikki
  • 15,329
  • 2
  • 54
  • 49
  • What if I have more files to ignore, like *.controller.js, *.module.js, etc? I'm trying to modify your answer to work but without success so far. – Justin Oct 19 '16 at 18:05
  • 17
    Ok I figured it out: `components/**/!(*.spec|*.controller).js` – Justin Oct 19 '16 at 18:06
  • 5
    What does it compile to? I just imagined it as "components/**/*.!(spec).js" I thinking regex way. Why star is not negated between the brackets? – Lajos Apr 21 '17 at 20:29
  • 1
    from another post I've read that the node-glob negation using '!' "is deprecated in version 5, and will be removed entirely in version 6": [see post here](https://stackoverflow.com/questions/26563048/including-excluding-globs-for-gulp-src) – dandersendev Oct 19 '18 at 16:54
  • A guess for `all *.spec.js but not *.js` → `components/**/*.spec.js` – Heikki Jan 01 '20 at 23:08
  • @Lajos 'cause that's how glob works? :) https://www.npmjs.com/package/glob#glob-primer – Glenn Mohammad Mar 18 '20 at 09:25