0

On a Gulp task to merge JS files I have the following which is working:

var root = "/app/scripts/"

gulp.task("build:scripts", function () {
  return gulp.src(
    [
      root + "jquery.js",
      root + "angular.js",
      root + "app.js",
      root + "app.config.js",
      root + "*/*.service.js",
      root + "*/*.controller.js"
    ]) 
    .pipe(concat("app.js"))
    .pipe(gulp.dest(paths.scripts.dist))
    .pipe(rename("app.min.js"))
    .pipe(uglify())
    .pipe(gulp.dest(paths.scripts.dist));
});

But I need to first select the files in shared folder and then the others:

var root = "/app/scripts/"

gulp.task("build:scripts", function () {
  return gulp.src(
    [
      root + "jquery.js",
      root + "angular.js",
      root + "app.js",
      root + "app.config.js",
      root + "shared/*.service.js",
      root + "shared/*.controller.js"
      root + "!shared/*.service.js",
      root + "!shared/*.controller.js"
    ]) 
    .pipe(concat("app.js"))
    .pipe(gulp.dest(paths.scripts.dist))
    .pipe(rename("app.min.js"))
    .pipe(uglify())
    .pipe(gulp.dest(paths.scripts.dist));
});

But this is not selecting the files that are not in shared folder.

Why is !shared not working? Should not this work?

UPDATE
I also tried

"!" + root + "shared/*.service.js",

and

root + "[^shared]*/*.service.js",

But none of them worked.

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

0

Try moving the '!' to the beginning of the path:

gulp.task("build:scripts", function () {
  return gulp.src(
    [
      root + "jquery.js",
      root + "angular.js",
      root + "app.js",
      root + "app.config.js",
      root + "shared/*.service.js",
      root + "shared/*.controller.js"
      root + "shared/!(*.service).js",
      root + "shared/!(*.controller).js"
    ]) 
    .pipe(concat("app.js"))
    .pipe(gulp.dest(paths.scripts.dist))
    .pipe(rename("app.min.js"))
    .pipe(uglify())
    .pipe(gulp.dest(paths.scripts.dist));
});

See this SO for details on gulp.src. Note the quote:

if the pattern starts with a ! character, then it is negated.

Edit

I've changed to code above after I had the time to test this. You can use the negation option with parenthesis. So root + !(*.service).js will exclude all .js files ending with .service.

Further more you can combine the two last excludes into one like so:

root + "shared/!(*.service|*.controller).js
Community
  • 1
  • 1
Bjørn Sørensen
  • 901
  • 1
  • 6
  • 14
  • That won't work because root is "/app/scripts/". So I need all files that are not in folder "/app/scripts/shared" but are still inside scripts. – Miguel Moura Dec 14 '15 at 17:06
  • Did you try it? The exclude options is still considering the entire glob, not just the first part of a path, as far as I know. – Bjørn Sørensen Dec 14 '15 at 17:38
  • In my case it did not work ... What I am trying is to first add all JS files in shared and after it all JS files outside of shared but still inside scripts. I added 2 updates to my question which I tried but did not work ... – Miguel Moura Dec 14 '15 at 18:36
  • @Miguel I've updated the answer after testing. This should work. – Bjørn Sørensen Dec 21 '15 at 16:38