Skip to content

Commit

Permalink
Fixed tricky bug with renamed file
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Aug 12, 2015
1 parent a7a60e1 commit 357c4fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ function createPipeline(options) {
// each `transform` entry must be a function that returns a transform stream
if (options.transform) {
var t = Array.isArray(options.transform) ? options.transform : [options.transform];
t.forEach(function(streamFactory) {
output = output.pipe(streamFactory(options));
t.forEach(function(factory) {
output = output.pipe(factory(options));
});
}

Expand Down
19 changes: 18 additions & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var through = require('through2');
var postcss = require('postcss');
var readContents = require('./read-contents');
var File = require('vinyl');

/**
* Returns a transformaton stream that applies content transformations from
Expand All @@ -16,8 +17,11 @@ var readContents = require('./read-contents');
* is ready
*/
module.exports = function(file, transform) {
// XXX this is very important: clone file before actual processing starts.
// Otherwise, when transformation process starts the actual file might
// be renamed which may lead to invalid results
var f = cloneFile(file);
return readContents(function(contents, callback) {
var f = file.clone();
f.contents = contents;
f.stringify = stringify;
try {
Expand All @@ -32,6 +36,7 @@ module.exports = function(file, transform) {
.once('finish', function() {
// write transformed tree as file content
self.push(f.css ? new Buffer(f.stringify()) : f.contents);
f = null;
callback();
})
.end(f);
Expand All @@ -40,4 +45,16 @@ module.exports = function(file, transform) {

function stringify() {
return this.css.toResult().toString();
}

function cloneFile(file) {
// do not use `file.clone()` here: if content is stream the original
// reader stream will be paused because of absent cloned stream consumer
return new File({
cwd: file.cwd,
base: file.base,
stat: null,
history: file.history.slice(),
contents: null
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"graceful-fs": "^3.0.8",
"postcss": "^4.1.12",
"through2": "^2.0.0",
"vinyl": "^0.5.1",
"xtend": "^4.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 357c4fa

Please sign in to comment.