-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (118 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* assemble-render-file <https://github.com/assemble/assemble-render-file>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var PluginError = require('plugin-error');
var utils = require('./utils');
/**
* Render a vinyl file.
*
* ```js
* app.src('*.hbs')
* .pipe(app.renderFile());
* ```
*
* @name .renderFile
* @param {Object} `locals` Optional locals to pass to the template engine for rendering.
* @return {Object}
* @api public
*/
module.exports = function(config) {
return function plugin(app) {
if (!utils.isValid(app, 'assemble-render-file', ['app', 'collection'])) return;
var opts = utils.merge({}, app.options, config);
var debug = utils.debug;
app.define('renderFile', function(engine, locals) {
if (typeof engine !== 'string') {
locals = engine;
engine = null;
}
debug('renderFile: engine "%s"', engine);
locals = locals || {};
var collection = {};
if (locals && !locals.isCollection) {
opts = utils.merge({}, opts, locals);
} else {
collection = locals;
locals = {};
}
var View = opts.View || opts.File || collection.View || this.View;
var files = [];
return utils.through.obj(function(file, enc, next) {
var stream = this;
if (file.isNull()) {
next(null, file);
return;
}
if (utils.isBinary(file)) {
next(null, file);
return;
}
if (file.data.render === false || opts.render === false) {
next(null, file);
return;
}
if (!file.isView) file = new View(file);
files.push(file);
app.handleOnce('onLoad', file, function(err) {
if (err) {
handleError(app, err, file, files, next);
return;
}
app.emit('_prepare', file);
next();
});
}, function(cb) {
var stream = this;
// run `onLoad` middleware
utils.reduce(files, [], function(acc, file, next) {
debug('renderFile, preRender: %s', file.path);
resolveEngine(app, locals, engine);
if (!locals.engine && app.isFalse('engineStrict')) {
stream.push(file);
next();
return;
}
// render the file
app.render(file, locals, function(err, res) {
if (typeof res === 'undefined' || err) {
handleError(app, err, file, files, next);
return;
}
debug('renderFile, postRender: %s', file.relative);
stream.push(res);
next();
});
}, cb);
});
});
return plugin;
};
};
function handleError(app, err, view, files, next) {
var last = files[files.length - 1];
var errOpts = {fileName: last.path, showStack: true};
if (!err || !err.message) {
err = 'view cannot be rendered';
}
err = new PluginError('assemble-render-file', err, errOpts);
err.files = files;
err.view = last;
next(err);
}
function resolveEngine(app, locals, engine) {
if (typeof engine === 'string') {
locals.engine = engine;
return;
}
if (locals.engine) {
return;
}
// allow a `noop` engine to be defined
if (app.engines['.noop']) {
locals.engine = '.noop';
}
}