mariocasciaro / gulp-multinject

Inject scripts, stylesheets and more into templates and htmls, with support for namespaces
MIT License
3 stars 3 forks source link

Resolve filepaths based on glob patterns #1

Open kennethlynne opened 10 years ago

kennethlynne commented 10 years ago

It should be possible to define files to be injected as globs

 gulp.task(Tasks.INJECT, [Tasks.SASS], function () {
    return gulp.src(paths.linker.input)
//Instead of defining a list of css files manually, inject all of them in the order that the glob pattern describes
      .pipe(inject([paths.sass.output + '*.css'], 'css', {
        base: paths.tmp
      }))
      .pipe(gulp.dest(paths.linker.output));
  });
mariocasciaro commented 10 years ago

Yes, it's definitely useful. I'll put it in my queue, in the meanwhile any PR is welcome.

kennethlynne commented 10 years ago

Easy enough with node-glob:

var glob = require('glob');

  // Takes a single glob, or an array of globs and returns the paths to resolved files
function (patterns, cwd) {
  if (!cwd) {
    cwd = process.cwd();
  }
  if (!Array.isArray(patterns)) {
    patterns = [patterns];
  }
  return patterns
    .map(function (pattern) {
      return glob.sync(pattern);
    })
    .reduce(function (val, list) {
      list.forEach(function (file) {
        var filePath = path.relative(cwd, file);
        // Make sure that files are not added multiple times (a more specific glob before a catchall)
        if (val.indexOf(filePath) < 0) {
          val.push(filePath);
        }
      });
      return val;
    }, []);
}; 
  gulp.task(Tasks.INJECT, [Tasks.SASS], function () {
    return gulp.src(paths.linker.input)
      // Does not support urls tho
      .pipe(inject(globIt(paths.sass.output + '/*.css')), 'css', {
        base: paths.tmp
      })
      .pipe(gulp.dest(paths.linker.output));
  });
kennethlynne commented 10 years ago

I'll send a PR :)

Edit: I may send a PR :dancer: