Skip to content

add support for file specific data and options#37

Open
bretkikehara wants to merge 1 commit intoadamayres:masterfrom
bretkikehara:data-per-file
Open

add support for file specific data and options#37
bretkikehara wants to merge 1 commit intoadamayres:masterfrom
bretkikehara:data-per-file

Conversation

@bretkikehara
Copy link
Copy Markdown

@bretkikehara bretkikehara commented Aug 3, 2017

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
Copy link
Copy Markdown

coveralls commented Aug 3, 2017

Coverage Status

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

@artemv
Copy link
Copy Markdown

artemv commented Aug 15, 2018

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

@artemv
Copy link
Copy Markdown

artemv commented Aug 15, 2018

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
Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants