This repository was archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (102 loc) · 3.37 KB
/
Copy pathindex.js
File metadata and controls
120 lines (102 loc) · 3.37 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
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
'use strict';
const jade = require('jade');
const sysPath = require('path');
const umd = require('umd-wrapper');
const progeny = require('progeny');
// perform a deep cloning of an object
const clone = (obj) => {
if (null == obj || 'object' !== typeof obj) return obj;
const copy = obj.constructor();
for (const attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
};
class JadeCompiler {
constructor(cfg) {
if (cfg == null) cfg = {};
const defaultBaseDir = sysPath.join(cfg.paths.root, 'app');
const defaultStaticBaseDir = sysPath.join(defaultBaseDir, 'assets');
const jade = cfg.plugins && cfg.plugins.jade;
const config = (jade && jade.options) || jade;
// Allow runtime to be excluded
if (config && config.noRuntime) {
this.include = [];
}
// cloning is mandatory because config is not mutable
this.locals = jade && jade.locals || {};
this.options = clone(config) || {};
this.options.compileDebug = false;
this.options.client = true;
this.options.basedir = (config && config.basedir) || defaultBaseDir;
this.options.staticBasedir = (config && config.staticBasedir) || defaultStaticBaseDir;
const getDependencies = progeny({
rootPath: this.options.basedir,
reverseArgs: true
});
const getDependenciesStatic = progeny({
rootPath: this.options.staticBasedir,
reverseArgs: true
});
this.getDependencies = (data, path, cb) => {
if (sysPath.resolve(path).indexOf(sysPath.resolve(this.options.staticBasedir)) === 0) {
return getDependenciesStatic(data, path, cb);
} else {
return getDependencies(data, path, cb);
}
};
}
compile(params) {
const data = params.data;
const path = params.path;
const options = clone(this.options);
options.filename = path;
return new Promise((resolve, reject) => {
let result, error;
try {
let compiled;
// cloning is mandatory because Jade changes it
if (options.preCompile === true) {
const precompiled = jade.compile(data, options)();
compiled = JSON.stringify(precompiled);
} else {
compiled = jade.compileClient(data, options);
}
result = umd(compiled);
} catch (_error) {
error = _error;
} finally {
if (error) return reject(error);
resolve(result);
}
});
}
compileStatic(params) {
const data = params.data;
const path = params.path;
const options = Object.assign({}, this.options);
const locals = Object.assign({}, this.locals);
try {
options.filename = path;
options.basedir = options.staticBasedir;
locals.filename = path.replace(new RegExp('^' + options.basedir + '/'), '');
const fn = jade.compile(data, options);
const compiled = fn(locals);
return Promise.resolve(compiled);
} catch (error) {
return Promise.reject(error);
}
}
}
let jadePath = require.resolve('jade');
while (sysPath.basename(jadePath) !== 'jade') {
jadePath = sysPath.dirname(jadePath);
}
JadeCompiler.prototype.include = [
sysPath.join(jadePath, 'runtime.js')
];
JadeCompiler.prototype.brunchPlugin = true;
JadeCompiler.prototype.type = 'template';
JadeCompiler.prototype.extension = 'jade';
JadeCompiler.prototype.staticTargetExtension = 'html';
module.exports = JadeCompiler;