Skip to content

Commit

Permalink
Only forward data from vinyl if defined
Browse files Browse the repository at this point in the history
By default `vfile.data` is an empty object. However, if `undefined` is
passed as `data` when creating a vfile, it becomes `undefined` instead,
which causes errors in libraries which count on it being an object.
  • Loading branch information
silvenon committed Jun 14, 2020
1 parent 6b710b6 commit 4620a7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/index.js
Expand Up @@ -26,12 +26,14 @@ module.exports = function (vinyl) {
throw new TypeError('Streams are not supported')
}

const contents = newVinyl.contents
const path = newVinyl.path

return new VFile({
path,
contents,
data: newVinyl.data
})
const options = {}

options.contents = newVinyl.contents
options.path = newVinyl.path

if (typeof newVinyl.data !== 'undefined') {
options.data = newVinyl.data
}

return new VFile(options)
}
8 changes: 8 additions & 0 deletions test/test.js
Expand Up @@ -5,6 +5,7 @@ import {expect} from 'chai'
import {join} from 'path'
import {Stream} from 'stream'
import Vinyl from 'vinyl'
import VFile from 'vfile'

describe('convert-vinyl-to-vfile', () => {
let vinylFile
Expand Down Expand Up @@ -59,5 +60,12 @@ describe('convert-vinyl-to-vfile', () => {
it('should have custom data', () => {
expect(result.data).to.eql(vinylFile.data)
})

it('should have default data if not defined', () => {
const plainVinyl = new Vinyl({path: 'plain.txt'})
const plainVfile = new VFile({path: 'plain.txt'})
const defaultData = plainVfile.data
expect(convertVinylToVfile(plainVinyl).data).to.deep.eql(defaultData)
})
})
})

0 comments on commit 4620a7f

Please sign in to comment.