Skip to content

Commit

Permalink
stream errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Sep 8, 2018
1 parent f954dda commit 0b8a331
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 81 deletions.
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
};
126 changes: 58 additions & 68 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const minify = require('..');
function toStream(contents) {
let stream = through();
stream.write(contents);
stream.end();
return stream;
}

Expand All @@ -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('<div ></div>') });

minify()
.on('error', cb)
.on('data', file => {
assert(file);
assert(file.isStream());
file.contents.on('data', data => {
assert.equal(data.toString(), '<div></div>');
});
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(), '<div></div>');
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) }));
});
});
});
Expand Down

0 comments on commit 0b8a331

Please sign in to comment.