From 0b8a3310ca9d19014cab647d81bbb61c236b3ccb Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Sat, 8 Sep 2018 08:11:01 -0400 Subject: [PATCH] stream errors --- index.js | 28 ++++++------ test/test.js | 126 ++++++++++++++++++++++++--------------------------- 2 files changed, 73 insertions(+), 81 deletions(-) diff --git a/index.js b/index.js index 0df8f0d..eedad38 100644 --- a/index.js +++ b/index.js @@ -11,29 +11,31 @@ module.exports = options => { return; } - const isStream = file.isStream(); - const minify = (buf, cb) => { + const minify = (buf, _, cb) => { try { - const contents = Buffer.from(htmlmin.minify(buf.toString(), options)); - if (!isStream) { + let contents = Buffer.from(htmlmin.minify(buf.toString(), options)); + if (next === cb) { file.contents = contents; cb(null, file); - } else { - cb(null, contents); + return; } + cb(null, contents); + next(null, file); } catch (err) { - const opts = Object.assign({}, options, { fileName: file.path }); - const error = new PluginError('gulp-htmlmin', err, opts); - if (isStream) this.emit('error', error); + let opts = Object.assign({}, options, { fileName: file.path }); + let error = new PluginError('gulp-htmlmin', err, opts); + if (next !== cb) { + next(error); + return; + } cb(error); } }; - if (isStream) { - file.contents = file.contents.pipe(through((buf, enc, cb) => minify(buf, cb))); - next(null, file); + if (file.isStream()) { + file.contents = file.contents.pipe(through(minify)); } else { - minify(file.contents, next); + minify(file.contents, null, next); } }); }; diff --git a/test/test.js b/test/test.js index df0fd69..0308ac2 100644 --- a/test/test.js +++ b/test/test.js @@ -10,7 +10,6 @@ const minify = require('..'); function toStream(contents) { let stream = through(); stream.write(contents); - stream.end(); return stream; } @@ -28,97 +27,88 @@ let errorFile = new File({ describe('gulp-htmlmin', () => { describe('file.contents - buffer', () => { it('should ignore empty file', cb => { - minify() - .on('error', cb) - .on('data', file => { - assert(file.isNull()); - cb(); - }) - .end(new File({})); + let stream = minify(); + stream.on('error', cb); + stream.on('data', file => { + assert(file.isNull()); + cb(); + }); + stream.write(new File({})); }); it('should minify my HTML files', cb => { let expected = fs.readFileSync('test/expected/normal.html', 'utf8'); - - minify() - .on('error', cb) - .on('data', file => { - assert(file); - assert(file.isBuffer()); - assert.equal(file.contents.toString(), expected); - cb(); - }) - .end(fakeFile); + let stream = minify(); + stream.on('error', cb); + stream.on('data', file => { + assert(file); + assert(file.isBuffer()); + assert.equal(file.contents.toString(), expected); + cb(); + }); + stream.write(fakeFile); }); it('should collapse whitespace', cb => { let expected = fs.readFileSync('test/expected/collapse.html', 'utf8'); - - minify({ collapseWhitespace: true }) - .on('error', cb) - .on('data', file => { - assert(file); - assert.equal(file.contents.toString(), expected); - cb(); - }) - .end(fakeFile); + let stream = minify({ collapseWhitespace: true }); + stream.on('error', cb); + stream.on('data', file => { + assert(file); + assert.equal(file.contents.toString(), expected); + cb(); + }); + stream.write(fakeFile); }); it('should emit a gulp error', cb => { - minify() - .on('error', err => { - assert.equal(err.message, 'Parse Error: ' + errorFileContents); - assert.equal(err.fileName, errorFile.path); - cb(); - }) - .on('end', () => { - cb(new Error('No error.')); - }) - .end(errorFile); + let stream = minify(); + stream.on('error', err => { + assert.equal(err.message, 'Parse Error: ' + errorFileContents); + assert.equal(err.fileName, errorFile.path); + cb(); + }); + stream.on('end', () => cb(new Error('No error.'))); + stream.write(errorFile); }); it('should emit a plugin error with a stack trace', cb => { - minify({ showStack: true }) - .on('error', err => { - assert.equal(err.message, 'Parse Error: ' + errorFileContents); - assert.equal(err.fileName, errorFile.path); - assert(err.showStack); - cb(); - }) - .on('end', () => { - cb(new Error('No error.')); - }) - .end(errorFile); + let stream = minify({ showStack: true }); + stream.on('error', err => { + assert.equal(err.message, 'Parse Error: ' + errorFileContents); + assert.equal(err.fileName, errorFile.path); + assert(err.showStack); + cb(); + }); + stream.on('end', () => cb(new Error('No error.'))); + stream.write(errorFile); }); }); describe('file.contents - stream', () => { it('should minify my HTML files', cb => { let fixture = new File({ contents: toStream('
') }); - - minify() - .on('error', cb) - .on('data', file => { - assert(file); - assert(file.isStream()); - file.contents.on('data', data => { - assert.equal(data.toString(), '
'); - }); + let stream = minify(); + stream.on('error', cb); + stream.on('data', file => { + assert(file); + assert(file.isStream()); + file.contents.on('data', data => { + assert.equal(data.toString(), '
'); cb(); - }) - .end(fixture); + }); + }); + stream.write(fixture); }); it('should emit a plugin error', cb => { - let fixture = new File({ contents: toStream(errorFileContents) }); - - minify() - .on('error', err => { - assert.equal(err.message, 'Parse Error: ' + errorFileContents); - cb(); - }) - .on('end', () => cb(new Error('Expected an error'))) - .end(fixture); + let stream = minify(); + stream.on('error', err => { + assert.equal(err.message, 'Parse Error: ' + errorFileContents); + cb(); + }); + stream.on('end', () => cb(new Error('Expected an error'))); + stream.write(new File({ contents: toStream(errorFileContents) })); }); }); });