forked from brunch/eco-brunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (38 loc) · 1.11 KB
/
Copy pathindex.js
File metadata and controls
48 lines (38 loc) · 1.11 KB
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
'use strict';
const eco = require('eco');
const umd = require('umd-wrapper');
class EcoCompiler {
constructor(config) {
const options = config && config.plugins && config.plugins.eco || {};
if (options) {
if (typeof options.overrides === 'function') {
options.overrides(eco);
}
this.namespace = options.namespace;
}
}
compile(params) {
const data = params.data;
const path = params.path;
let error, result;
return new Promise((resolve, reject) => {
try {
const source = eco.compile(data).toString();
const ns = this.namespace;
const key = path.replace(/^.*templates\//, '').replace(/\..+?$/, '');
result = ns ?
`if (typeof ${ns} === 'undefined'){ ${ns} = {} }; ${ns}['"${key}'] = ${source}` :
umd(source);
} catch (err) {
error = err;
} finally {
if (error) return reject(error);
resolve(result);
}
});
}
}
EcoCompiler.prototype.brunchPlugin = true;
EcoCompiler.prototype.type = 'template';
EcoCompiler.prototype.extension = 'eco';
module.exports = EcoCompiler;