adamayres / gulp-wrap

A gulp plugin to wrap the stream contents with a lodash template.
https://www.npmjs.com/package/gulp-wrap
MIT License
84 stars 17 forks source link

add support for file specific data and options #37

Open bretkikehara opened 7 years ago

bretkikehara commented 7 years ago

Bug

The config function only runs once for the first file. The result is then used for remainder the files in the pipe.

Solution

Run the config function for each file. Inside the function, if-statements can be used to control the config output per file.

Example 1

var path = require('path');
var wrap = require('gulp-wrap');

gulp.src([
  'a.html',
  'b.html',
])
.pipe(wrap('<script>var DATA = <%= DATA %>;</script>', function (file) {
  if (path.basename(file.path) === 'a.html') {
    return {
      DATA: 10,
    };
  }
  return { 
    DATA: 0,
  };
}))
.pipe(gulp.dest('./dist'));

Example 2

var path = require('path');
var wrap = require('gulp-wrap');

gulp.src([
  'a.html',
  'b.html',
])
.pipe(wrap('<!-- file: <%= filename %> -->', function (file) {
  return { 
    filename: path.basename(file.path),
  };
}))
.pipe(gulp.dest('./dist'));

EDIT: updated description and examples

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 23c0b3e79f2c318cbe9aad90af620d78792489b7 on bretkikehara:data-per-file into 11cd463ddcd19becb868e19af40e34be5513253d on adamayres:master.

artemv commented 6 years ago

👍 Thanks @bretkikehara, I'm switching to your version )

artemv commented 6 years ago

Just to make it clear - current version only runs data function for first file, and then uses it for all of them - which looks like a bug because data function receives the file parameter as first argument. This PR fixes it, now data is called for each file.

bretkikehara commented 6 years ago

yes exactly. i've update the description and examples.