Exemplo n.º 1
0
  return through.obj(function (file, encoding, done) {
    var stream = this;

    // infer the html base path from the file.base and use this as a base to locate
    //  the corresponding javascript file
    function srcStream() {
      var htmlName  = path.basename(file.path);
      var htmlPath  = path.resolve(file.path.replace(htmlName, ''));
      var htmlBase  = path.resolve(file.base);
      var jsBase    = (basePath) ? path.resolve(basePath)  : htmlBase;
      var relative  = htmlPath.replace(htmlBase, '').split(/[\\\/]/g);
      var glob      = [ ];
      relative.forEach(function(unused, i, array) {
        extensions.forEach(function(extension) {
          glob.push([ basePath ].concat(array.slice(0, i + 1)).concat('*.' + extension).join('/'));
        });
      });
      return gulp.src(glob, { read: false })
        .pipe(semiflat(jsBase))
        .pipe(slash());
    }

    // process the single file using a stream
    throughPipes(function (readable) {
      return readable
        .pipe(inject(srcStream(), opts));
    })
      .input(file)
      .output(function (file) {
        stream.push(file);
        done();
      });
  });
Exemplo n.º 2
0
      return through.obj(function (file, encoding, done) {
        var stream = this;

        // infer the html base path from the file.base and use this as a base to locate
        //  the corresponding css files
        var htmlName  = path.basename(file.path);
        var htmlPath  = path.resolve(file.path.replace(htmlName, ''));
        var htmlBase  = path.resolve(file.base);
        var cssBase   = (cssBasePath) ? path.resolve(cssBasePath) : htmlBase;
        var relative  = htmlPath.replace(htmlBase, '').split(/[\\\/]/g);
        var glob      = relative.map(function (unused, i, array) {
          return [ cssBase ].concat(array.slice(0, i + 1)).concat('*.css').join('/');
        });
        var sources = gulp.src(glob, { read: false })
          .pipe(semiflat(cssBase))
          .pipe(slash());

        // pass the html file into a stream that injects the given sources
        //  then add the resulting file to the output stream
        throughPipes(function (readable) {
          return readable
            .pipe(inject(sources, opts));
        })
          .output(function (file) {
            stream.push(file);
            done();
          })
          .input(file)
          .end();
      });
Exemplo n.º 3
0
  return through.obj(function (file, encoding, done) {
    var stream = this;
    function srcStream() {

      // ensure any relative path is an ancestor of the file path
      var filePath     = path.dirname(file.path);
      var fileRecurse  = path.resolve((typeof recurse === 'string') ? recurse : filePath);
      var fileRelative = path.relative(fileRecurse, filePath);
      if (fileRelative.slice(0, 2) === '..') {
        throw new Error('encountered a file that is outside the given recurse path');
      }

      // use elements in the relative address, from none to all
      //  for each term add a matcher for all files of the given extension
      // TODO @bholloway can this be replaced by a single term? i.e. /**/*.<ext>
      var glob = [];
      fileRelative
        .split(/[\\\/]/g)
        .forEach(function eachPathElement(value, i, array) {
          extensions.forEach(function eachExtension(extension) {
            var item = [ fileRecurse ]
              .concat(array.slice(0, i + 1))
              .concat('*.' + extension)
              .join('/');
            if (glob.indexOf(item) < 0) {
              glob.push(item);
            }
          });
        });
      return gulp.src(glob, { read: false })
        .pipe(semiflat(file.base))
        .pipe(slash());
    }

    // process the single file using a stream
    throughPipes(function (readable) {
      return readable
        .pipe(inject(srcStream(), opts));
    })
      .input(file)
      .output(function (file) {
        stream.push(file);
        done();
      });
  });