Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dest): don't modify original file object #20

Closed
wants to merge 3 commits into from
Closed

fix(dest): don't modify original file object #20

wants to merge 3 commits into from

Conversation

laurelnaiad
Copy link

Instead of modifying the path of a file object when writing it to dest, construct a new File(file) from it so that the original object and upstream streams are not impacted.

Closes #19

The major issue with this change was that some of the unit tests were testing whether the file that came out was the same object as the one that came in, instead of actually verifying that the properties on the outgoing file were correct. So I think I've improved the tests along the way.

Instead of modifying the path of a file object when writing it to dest, construct a new File(file) from it so that the original object and upstream streams are not impacted.

Closes #19
@yocontra
Copy link
Member

Should use file.clone not wrapping a new file. Can we consolidate all of this into one issue? It seems like this conversation is happening in 3 locations

@laurelnaiad
Copy link
Author

But we don't need to clone it...because gulp.dest isn't modifying the buffer.

Copy own properties of original file so any custom properties that have been added by plugins are preserved.
@laurelnaiad
Copy link
Author

I added a commit when I realized that there are plugins out there that assign custom properties to files.

@yocontra
Copy link
Member

Seems like a flaw that would also make clone not copy custom properties

@laurelnaiad
Copy link
Author

True, but I still don't want to clone the file because I don't want to clone the buffer.

@yocontra
Copy link
Member

@stu-salsbury Yup, just suggesting it as another potential issue. The code for copying the properties (and custom properties) should be in the same spot. Maybe we can make clone take an option to not copy the buffer/stream but just the rest of the properties.

@laurelnaiad
Copy link
Author

That sounds reasonable. If you give me the option name you want to use, I'm willing to take that on. However, the changes I made in vinyl-fs are still relevant, since it was actually testing whether the references were the same instead of the properties. If I implement the clone option I can commit again to this branch to fix the line in the dest function and keep the test changes.

@yocontra
Copy link
Member

@stu-salsbury file.clone({propertyName: false}) so in our case file.clone({contents: false}) seems reasonable and would allow people to maintain references to arbitrary fields if needed

@laurelnaiad
Copy link
Author

Ok, I will do this with support for custom properties, as well. I know you'll feel free to push back if you don't like it! :)

var oldFile = file;
file = new File(oldFile);
Object.keys(oldFile).forEach(function(key) {
if (Object.prototype.hasOwnProperty(key)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be oldFile.hasOwnProperty(key) or the unnecessarily verbose equivalent Object.prototype.hasOwnProperty.call(oldFile, key)?

The way you're invoking the function directly from the prototype doesn't seem correct, as Object.prototype.hasOwnProperty(key) would check if key is an own property of Object.prototype.

Never mind this comment if that part will be replaced by file.clone()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right though.

Use vinyl clone with `contents: false` option to clone files before modifying their properties.  Ensures upstream plugins do not see changes to properties that result from modifying their destination.

Closes #19
@laurelnaiad
Copy link
Author

new commit -- will only work properly when corresponding PR is merged in vinyl: gulpjs/vinyl#15

@yocontra
Copy link
Member

So I'm torn on this one - in the guidelines we do specify that plugins need to make sure they don't emit the file before they are finished with it. Do you know which plugin is responsible or is it a lazypipe issue?

@yocontra
Copy link
Member

yocontra commented Oct 9, 2014

ping ^

@yocontra
Copy link
Member

Closing due to no response

@yocontra yocontra closed this Dec 12, 2014
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.

request: don't rename original object
3 participants