|
1 | | - |
2 | 1 | var through = require ('through2') |
3 | | - , mjmlDefaultEngine = require ('mjml') |
4 | | - , gutil = require ('gulp-util'); |
| 2 | +var mjmlDefaultEngine = require ('mjml') |
| 3 | +var gutil = require ('gulp-util') |
5 | 4 |
|
6 | | -var GulpError = gutil.PluginError, |
7 | | - NAME = 'MJML'; |
| 5 | +var GulpError = gutil.PluginError |
| 6 | +var NAME = 'MJML' |
8 | 7 |
|
9 | 8 | module.exports = function mjml (mjmlEngine) { |
10 | 9 | if(mjmlEngine === undefined) { |
11 | | - mjmlEngine = mjmlDefaultEngine; |
| 10 | + mjmlEngine = mjmlDefaultEngine |
12 | 11 | } |
13 | 12 |
|
14 | | - return through.obj(function (file, enc, callback) { |
| 13 | + return through.obj(function (file, enc, callback) { |
| 14 | + |
| 15 | + if (file.isStream()) { |
| 16 | + this.emit('error', new GulpError(NAME, 'Streams are not supported!')) |
| 17 | + return callback() |
| 18 | + } |
| 19 | + |
| 20 | + if (file.isBuffer()) { |
| 21 | + var output = file.clone() |
| 22 | + var render |
15 | 23 |
|
16 | | - if (file.isStream()) { |
17 | | - this.emit('error', new PluginError(NAME, 'Streams are not supported!')); |
18 | | - return callback() |
19 | | - } |
| 24 | + try { |
| 25 | + render = mjmlEngine.mjml2html(file.contents.toString()) |
| 26 | + } catch (e) { |
| 27 | + this.emit('error', new GulpError(NAME, e)) |
| 28 | + return callback() |
| 29 | + } |
20 | 30 |
|
21 | | - if (file.isBuffer()) { |
22 | | - var output = file.clone(); |
23 | | - output.contents = new Buffer(mjmlEngine.mjml2html(file.contents.toString())); |
24 | | - output.path = gutil.replaceExtension(file.path.toString(), '.html'); |
25 | | - this.push(output); |
26 | | - } |
27 | | - return callback(); |
28 | | - }) |
29 | | -}; |
| 31 | + output.contents = new Buffer(render.html) |
| 32 | + output.path = gutil.replaceExtension(file.path.toString(), '.html') |
| 33 | + this.push(output) |
| 34 | + } |
| 35 | + return callback() |
| 36 | + }) |
| 37 | +} |
0 commit comments