Skip to content

Commit

Permalink
Merge pull request #148 from rogeriopvl/refactor/complete-rewrite
Browse files Browse the repository at this point in the history
refactor: es6 rewrite and code cleanup
  • Loading branch information
rogeriopvl authored Apr 18, 2019
2 parents c88455d + b0a42a1 commit 4c624e5
Show file tree
Hide file tree
Showing 16 changed files with 598 additions and 1,770 deletions.
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ gulp.src("./templates/*.ejs")
}))
.pipe(gulp.dest("./dist"))
```
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`.

### Watch mode error handling (for gulp v3 or below)

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.

Here's an example on how to make it work:

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

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

```javascript
var ejs = require('gulp-ejs')

gulp.src('./templates/*.ejs')
.pipe(ejs({ msg: 'Hello Gulp!'}, {}, { ext: '.html' }))
.pipe(gulp.dest('./dist'))
```

### Acessing the ejs object
### Accessing the ejs object

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

```javascript
var gulpEjs = require('gulp-ejs')
const ejs = require('gulp-ejs')

gulpEjs.ejs.fileLoader = function () { /* custom file loader */ }
ejs.__EJS__.fileLoader = function () { /* custom file loader */ }
```

**Note:** As of version 4, the exported ejs object was renamed from `ejs` to `__EJS__`.

## API

### ejs(data, options, settings)
### ejs(data, options)

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

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

#### settings
Type: `hash`
Default: `{}`

A hash object to configure the plugin.
### Renaming file extensions

##### settings.ext
Type: `String`
Default: `undefined`
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).

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.
Here's an example for template files with `.ejs` extension that are rendered into `.html` files:

**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.
```javascript
const ejs = require('gulp-ejs')
const rename = require('gulp-rename')

gulp.src('./templates/*.ejs')
.pipe(ejs({ title: 'gulp-ejs' }))
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest('./dist'))
```

## License

Expand Down
42 changes: 20 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
'use strict'

var through = require('through2')
var PluginError = require('plugin-error')
var replaceExtension = require('replace-ext')
var ejs = require('ejs')
const through = require('through2')
const PluginError = require('plugin-error')
const ejs = require('ejs')

var gulpEjs = function(data, options, settings) {
data = data || {}
options = options || {}
settings = settings || {}
const PLUGIN_NAME = 'gulp-ejs'

return through.obj(function(file, enc, cb) {
function render(data, options = {}) {
return through.obj(function(file, encoding, callback) {
if (file.isNull()) {
this.push(file)
return cb()
return callback(null, file)
}

if (file.isStream()) {
this.emit('error', new PluginError('gulp-ejs', 'Streaming not supported'))
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
}

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

const ejsData = Object.assign({}, data, file.data)

try {
file.contents = Buffer.from(
ejs.render(file.contents.toString(), fileData, options)
ejs.render(file.contents.toString(), ejsData, options)
)

if (typeof settings.ext !== 'undefined') {
file.path = replaceExtension(file.path, settings.ext)
}
this.push(file)
} catch (err) {
this.emit('error', new PluginError('gulp-ejs', err.toString()))
this.emit(
'error',
new PluginError(PLUGIN_NAME, err, { fileName: file.path })
)
}

this.push(file)
cb()
callback()
})
}

gulpEjs.ejs = ejs
// expose ejs object for configuration
render.__EJS__ = ejs

module.exports = gulpEjs
module.exports = render
Loading

0 comments on commit 4c624e5

Please sign in to comment.