Skip to content

Commit

Permalink
feat: Add streaming support using vinyl-contents (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Jun 9, 2021
1 parent 53a8769 commit 642d1ea
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
});
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 43 additions & 13 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
});

0 comments on commit 642d1ea

Please sign in to comment.