diff --git a/index.js b/index.js index c4b5542..229c0a2 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const defaultPug = require('pug'); const ext = require('replace-ext'); const PluginError = require('plugin-error'); const log = require('fancy-log'); +const vinylContents = require('vinyl-contents'); module.exports = function gulpPug(options) { const opts = Object.assign({}, options); @@ -18,14 +19,18 @@ module.exports = function gulpPug(options) { opts.filename = file.path; file.path = ext(file.path, opts.client ? '.js' : '.html'); - if (file.isStream()) { - return cb(new PluginError('gulp-pug', 'Streaming not supported')); - } + vinylContents(file, function onContents(err, contents) { + if (err) { + return cb(new PluginError('gulp-pug', err)); + } + + if (!contents) { + return cb(null, file); + } - if (file.isBuffer()) { try { let compiled; - const contents = String(file.contents); + if (opts.verbose === true) { log('compiling file', file.path); } @@ -34,11 +39,12 @@ module.exports = function gulpPug(options) { } else { compiled = pug.compile(contents, opts)(data); } - file.contents = new Buffer(compiled); - } catch (e) { - return cb(new PluginError('gulp-pug', e)); + file.contents = Buffer.from(compiled); + } catch (err) { + return cb(new PluginError('gulp-pug', err)); } - } - cb(null, file); + + cb(null, file); + }); }); }; diff --git a/package.json b/package.json index 9156e37..45bf0b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "plugin-error": "^1.0.1", "pug": "^2.0.3", "replace-ext": "^1.0.0", - "through2": "^3.0.1" + "through2": "^3.0.1", + "vinyl-contents": "^1.0.0" }, "devDependencies": { "@types/node": "^12.0.0", diff --git a/test/stream.js b/test/stream.js index e65aba0..8f6292b 100644 --- a/test/stream.js +++ b/test/stream.js @@ -4,26 +4,56 @@ const expect = require('expect'); const fs = require('fs'); const { concat, from, pipe } = require('mississippi'); const path = require('path'); -const PluginError = require('plugin-error'); const Vinyl = require('vinyl'); -const task = require('../'); +const pug = require('pug'); + +const plugin = require('../'); const cwd = __dirname; const base = path.join(cwd, 'fixtures'); const filePath = path.join(base, 'helloworld.pug'); +const fileContents = fs.readFileSync(filePath); -const file = new Vinyl({ - path: filePath, - base: base, - cwd: cwd, - contents: fs.createReadStream(filePath), -}); -describe('stream', function () { - it('should error if contents is a stream', function (done) { - pipe([from.obj([file]), task(), concat()], (err) => { - expect(err).toBeInstanceOf(PluginError); - done(); +describe('stream', function() { + it('should handle streaming contents', function(done) { + const file = new Vinyl({ + path: filePath, + base: base, + cwd: cwd, + contents: fs.createReadStream(filePath), }); + + function assert(files) { + var expected = pug.compile(fileContents)(); + expect(files[0]).not.toBeUndefined(); + expect(expected).toEqual(String(files[0].contents)); + } + + pipe([ + from.obj([file]), + plugin(), + concat(assert), + ], done); + }); + + it('bubbles errors if the stream errors', function(done) { + const file = new Vinyl({ + path: filePath, + base: base, + cwd: cwd, + contents: from([new Error("Boom")]) + }); + + function assert(err) { + expect(err).toBeDefined(); + done(); + } + + pipe([ + from.obj([file]), + plugin(), + concat(), + ], assert); }); }); diff --git a/test/test.js b/test/test.js index e5b9f66..c02d803 100644 --- a/test/test.js +++ b/test/test.js @@ -182,4 +182,22 @@ describe('test', function () { done ); }); + + it('skips over any files without contents', function (done) { + const file = getFixture('helloworld.pug'); + file.contents = null; + + function assert(files) { + expect(files.length).toEqual(1); + const newFile = files[0]; + expect(newFile).toBe(file); + expect(newFile.contents).toBe(null); + } + + + pipe( + [from.obj([file]), task(), concat(assert)], + done + ); + }); });