Skip to content

Commit 81ba1cb

Browse files
authored
Merge pull request #12 from mjmlio/mjml3
MJML 3 : fix #11
2 parents 7d5a835 + d9e2783 commit 81ba1cb

File tree

5 files changed

+54
-30
lines changed

5 files changed

+54
-30
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ Add Gulp to your MJML workflow!
99
1010
``` javascript
1111

12-
var gulp = require('gulp');
12+
var gulp = require('gulp')
1313
var mjml = require('gulp-mjml')
1414

1515
gulp.task('default', function () {
1616
return gulp.src('./test.mjml')
1717
.pipe(mjml())
1818
.pipe(gulp.dest('./html'))
19-
});
19+
})
2020

2121
```
2222

2323
> If you have custom components linked to your own mjmlEngine, you can pass it to the gulp task so it uses your engine to render the html:
2424
2525
``` javascript
2626

27-
var gulp = require('gulp');
27+
var gulp = require('gulp')
2828
var mjml = require('gulp-mjml')
2929

3030
// Require your own components if needed, and your mjmlEngine
@@ -35,6 +35,6 @@ gulp.task('default', function () {
3535
return gulp.src('./test.mjml')
3636
.pipe(mjml(mjmlEngine))
3737
.pipe(gulp.dest('./html'))
38-
});
38+
})
3939

4040
```

examples/gulpfile.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
2-
var gulp = require('gulp');
3-
var mjml = require('../index')
1+
var gulp = require('gulp')
2+
var mjml = require('../src/index')
43

54
gulp.task('default', function () {
65
gulp.src('./test.mjml')
76
.pipe(mjml())
87
.pipe(gulp.dest('./html'))
9-
});
8+
})

examples/test.mjml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<mjml>
2+
<mj-body>
3+
<mj-container>
4+
<mj-section>
5+
<mj-column>
6+
7+
<mj-image width="100" src="/assets/img/logo-small.png"></mj-image>
8+
9+
<mj-divider border-color="#F45E43"></mj-divider>
10+
11+
<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
12+
13+
</mj-column>
14+
</mj-section>
15+
</mj-container>
16+
</mj-body>
17+
</mjml>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-mjml",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"description": "Add Gulp to your MJML workflow",
55
"main": "src/index",
66
"scripts": {
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"gulp": "^3.9.0",
1313
"gulp-util": "^3.0.7",
14-
"mjml": "latest",
14+
"mjml": "^3.0.0",
1515
"through2": "^2.0.0"
1616
}
1717
}

src/index.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1-
21
var through = require ('through2')
3-
, mjmlDefaultEngine = require ('mjml')
4-
, gutil = require ('gulp-util');
2+
var mjmlDefaultEngine = require ('mjml')
3+
var gutil = require ('gulp-util')
54

6-
var GulpError = gutil.PluginError,
7-
NAME = 'MJML';
5+
var GulpError = gutil.PluginError
6+
var NAME = 'MJML'
87

98
module.exports = function mjml (mjmlEngine) {
109
if(mjmlEngine === undefined) {
11-
mjmlEngine = mjmlDefaultEngine;
10+
mjmlEngine = mjmlDefaultEngine
1211
}
1312

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
1523

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+
}
2030

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

Comments
 (0)