-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (45 loc) · 1.64 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
'use strict';
module.exports = instance => {
if (instance === void 0) throw new Error('expected an instance of "engine"');
const engine = {
name: 'base',
instance,
async compile(file, options) {
if (typeof file === 'string') file = { contents: file };
let opts = { ...options };
opts.imports = Object.assign({}, this.helpers, opts.helpers, opts.imports);
if (typeof file.fn !== 'function' || opts.recompile === true) {
file.fn = instance.compile(file.contents.toString(), opts);
}
return file;
},
async render(file, locals, options) {
if (typeof file === 'string') file = { contents: file };
if (typeof file.fn !== 'function') {
await engine.compile.call(this, file, { ...locals, ...options });
}
let res = await file.fn(locals);
let str = this.resolveIds ? await this.resolveIds(res) : res;
file.contents = Buffer.from(str);
return file;
},
compileSync(file, options) {
if (typeof file === 'string') file = { contents: file };
let opts = { ...options };
opts.imports = Object.assign({}, this.helpers, opts.helpers, opts.imports);
if (typeof file.fn !== 'function' || opts.recompile === true) {
file.fn = instance.compile(file.contents.toString(), opts);
}
return file;
},
renderSync(file, locals, options) {
if (typeof file === 'string') file = { contents: file };
if (typeof file.fn !== 'function') {
engine.compileSync.call(this, file, { ...locals, ...options });
}
file.contents = Buffer.from(file.fn(locals));
return file;
}
};
return engine;
};