Skip to content

Commit 4c624e5

Browse files
authored
Merge pull request #148 from rogeriopvl/refactor/complete-rewrite
refactor: es6 rewrite and code cleanup
2 parents c88455d + b0a42a1 commit 4c624e5

File tree

16 files changed

+598
-1770
lines changed

16 files changed

+598
-1770
lines changed

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ gulp.src("./templates/*.ejs")
2323
}))
2424
.pipe(gulp.dest("./dist"))
2525
```
26-
If you want to use `gulp-ejs` in a watch/livereload task, you may want to avoid gulp exiting on error when, for instance, a partial file is `ENOENT`.
26+
27+
### Watch mode error handling (for gulp v3 or below)
28+
29+
If you want to use `gulp-ejs` in a watch/livereload task, you may want to avoid gulp exiting on error when, for instance, a partial file is `ENOENT` or an ejs syntax error.
30+
2731
Here's an example on how to make it work:
2832

2933
```javascript
@@ -38,29 +42,23 @@ gulp.src('./templates/*.ejs')
3842
```
3943
This will make gulp log the error and continue normal execution.
4044

41-
If you want to specify the extension of output files, set the ext option:
45+
**Please note that you don't need to do this for Gulp v4.**
4246

43-
```javascript
44-
var ejs = require('gulp-ejs')
45-
46-
gulp.src('./templates/*.ejs')
47-
.pipe(ejs({ msg: 'Hello Gulp!'}, {}, { ext: '.html' }))
48-
.pipe(gulp.dest('./dist'))
49-
```
50-
51-
### Acessing the ejs object
47+
### Accessing the ejs object
5248

5349
The ejs object is also exported and you can use it to configure ejs:
5450

5551
```javascript
56-
var gulpEjs = require('gulp-ejs')
52+
const ejs = require('gulp-ejs')
5753

58-
gulpEjs.ejs.fileLoader = function () { /* custom file loader */ }
54+
ejs.__EJS__.fileLoader = function () { /* custom file loader */ }
5955
```
6056

57+
**Note:** As of version 4, the exported ejs object was renamed from `ejs` to `__EJS__`.
58+
6159
## API
6260

63-
### ejs(data, options, settings)
61+
### ejs(data, options)
6462

6563
#### data
6664
Type: `hash`
@@ -78,20 +76,21 @@ A hash object for ejs options.
7876

7977
For more info on `ejs` options, check the [project's documentation](https://github.com/mde/ejs).
8078

81-
#### settings
82-
Type: `hash`
83-
Default: `{}`
84-
85-
A hash object to configure the plugin.
79+
### Renaming file extensions
8680

87-
##### settings.ext
88-
Type: `String`
89-
Default: `undefined`
81+
As of version 4, the third api parameter `settings` was removed. You can no longer provide an extension. This is because it falls out of the scope of `gulp-ejs`. So if you need to save the file with a different extension you can use [gulp-rename](https://npmjs.com/package/gulp-rename).
9082

91-
Defines the file extension that will be appended to the filename. If no extension is provided, the same extension of the input file will be used.
83+
Here's an example for template files with `.ejs` extension that are rendered into `.html` files:
9284

93-
**Note:** As of `v2.0.0` the output file extension is no longer `.html` by default, you need to specify it, otherwise it will have the same extension of the input file.
85+
```javascript
86+
const ejs = require('gulp-ejs')
87+
const rename = require('gulp-rename')
9488

89+
gulp.src('./templates/*.ejs')
90+
.pipe(ejs({ title: 'gulp-ejs' }))
91+
.pipe(rename({ extname: '.html' }))
92+
.pipe(gulp.dest('./dist'))
93+
```
9594

9695
## License
9796

index.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
11
'use strict'
22

3-
var through = require('through2')
4-
var PluginError = require('plugin-error')
5-
var replaceExtension = require('replace-ext')
6-
var ejs = require('ejs')
3+
const through = require('through2')
4+
const PluginError = require('plugin-error')
5+
const ejs = require('ejs')
76

8-
var gulpEjs = function(data, options, settings) {
9-
data = data || {}
10-
options = options || {}
11-
settings = settings || {}
7+
const PLUGIN_NAME = 'gulp-ejs'
128

13-
return through.obj(function(file, enc, cb) {
9+
function render(data, options = {}) {
10+
return through.obj(function(file, encoding, callback) {
1411
if (file.isNull()) {
15-
this.push(file)
16-
return cb()
12+
return callback(null, file)
1713
}
1814

1915
if (file.isStream()) {
20-
this.emit('error', new PluginError('gulp-ejs', 'Streaming not supported'))
16+
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
2117
}
2218

23-
var fileData = Object.assign({}, data, file.data)
2419
options.filename = file.path
2520

21+
const ejsData = Object.assign({}, data, file.data)
22+
2623
try {
2724
file.contents = Buffer.from(
28-
ejs.render(file.contents.toString(), fileData, options)
25+
ejs.render(file.contents.toString(), ejsData, options)
2926
)
3027

31-
if (typeof settings.ext !== 'undefined') {
32-
file.path = replaceExtension(file.path, settings.ext)
33-
}
28+
this.push(file)
3429
} catch (err) {
35-
this.emit('error', new PluginError('gulp-ejs', err.toString()))
30+
this.emit(
31+
'error',
32+
new PluginError(PLUGIN_NAME, err, { fileName: file.path })
33+
)
3634
}
3735

38-
this.push(file)
39-
cb()
36+
callback()
4037
})
4138
}
4239

43-
gulpEjs.ejs = ejs
40+
// expose ejs object for configuration
41+
render.__EJS__ = ejs
4442

45-
module.exports = gulpEjs
43+
module.exports = render

0 commit comments

Comments
 (0)