@kosatyi/ejs 0.0.62 → 0.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/element.js +3 -2
- package/dist/cjs/index.js +445 -678
- package/dist/esm/element.js +3 -2
- package/dist/esm/index.js +299 -330
- package/dist/templates.js +1 -0
- package/dist/umd/element.js +3 -2
- package/dist/umd/element.min.js +1 -1
- package/dist/umd/index.js +480 -713
- package/dist/umd/index.min.js +1 -1
- package/package.json +3 -2
- package/type/global.d.ts +0 -101
- package/type/index.d.ts +0 -5
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
1
|
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
3
|
|
|
4
4
|
const typeProp = function () {
|
|
5
5
|
const args = [].slice.call(arguments);
|
|
@@ -9,7 +9,6 @@ const typeProp = function () {
|
|
|
9
9
|
const isFunction = (v) => typeof v === 'function';
|
|
10
10
|
const isString = (v) => typeof v === 'string';
|
|
11
11
|
const isBoolean = (v) => typeof v === 'boolean';
|
|
12
|
-
const isObject = (v) => typeof v === 'object';
|
|
13
12
|
const isUndefined = (v) => typeof v === 'undefined';
|
|
14
13
|
|
|
15
14
|
const isNodeEnv =
|
|
@@ -58,32 +57,36 @@ const symbols = (string) => {
|
|
|
58
57
|
)
|
|
59
58
|
};
|
|
60
59
|
|
|
61
|
-
const safeValue = (value, escape
|
|
62
|
-
|
|
60
|
+
const safeValue = (value, escape) => {
|
|
61
|
+
const check = value;
|
|
62
|
+
return check == null ? '' : escape === true ? entities(check) : check
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const instanceOf = (object, instance) => {
|
|
66
|
+
return object instanceof instance
|
|
63
67
|
};
|
|
64
68
|
|
|
65
69
|
const getPath = (context, name, strict) => {
|
|
66
70
|
let data = context;
|
|
67
71
|
let chunks = String(name).split('.');
|
|
68
72
|
let prop = chunks.pop();
|
|
69
|
-
for (
|
|
73
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
74
|
+
const part = chunks[i];
|
|
75
|
+
if (isFunction(data['toJSON'])) {
|
|
76
|
+
data = data.toJSON();
|
|
77
|
+
}
|
|
70
78
|
if (strict && data.hasOwnProperty(part) === false) {
|
|
71
79
|
data = {};
|
|
72
80
|
break
|
|
73
81
|
}
|
|
74
82
|
data = data[part] = data[part] || {};
|
|
75
83
|
}
|
|
84
|
+
if (isFunction(data['toJSON'])) {
|
|
85
|
+
data = data.toJSON();
|
|
86
|
+
}
|
|
76
87
|
return [data, prop]
|
|
77
88
|
};
|
|
78
89
|
|
|
79
|
-
const bindContext = (object, context, methods = []) => {
|
|
80
|
-
methods.forEach((name) => {
|
|
81
|
-
if (name in object) {
|
|
82
|
-
object[name] = object[name].bind(context);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
|
|
87
90
|
const ext = (path, defaults) => {
|
|
88
91
|
const ext = path.split('.').pop();
|
|
89
92
|
if (ext !== defaults) {
|
|
@@ -96,9 +99,7 @@ const extend = (...args) => {
|
|
|
96
99
|
const target = args.shift();
|
|
97
100
|
return args
|
|
98
101
|
.filter((source) => source)
|
|
99
|
-
.reduce((target, source) =>
|
|
100
|
-
return Object.assign(target, source)
|
|
101
|
-
}, target)
|
|
102
|
+
.reduce((target, source) => Object.assign(target, source), target)
|
|
102
103
|
};
|
|
103
104
|
|
|
104
105
|
const noop = () => {};
|
|
@@ -159,23 +160,14 @@ const hasProp = (object, prop) => {
|
|
|
159
160
|
};
|
|
160
161
|
|
|
161
162
|
const defaults = {};
|
|
162
|
-
|
|
163
163
|
defaults.export = 'ejsPrecompiled';
|
|
164
|
-
|
|
165
|
-
defaults.watch = false;
|
|
166
|
-
|
|
164
|
+
defaults.cache = true;
|
|
167
165
|
defaults.chokidar = null;
|
|
168
|
-
|
|
169
166
|
defaults.path = 'views';
|
|
170
|
-
|
|
171
167
|
defaults.resolver = null;
|
|
172
|
-
|
|
173
168
|
defaults.extension = 'ejs';
|
|
174
|
-
|
|
175
169
|
defaults.rmWhitespace = true;
|
|
176
|
-
|
|
177
170
|
defaults.withObject = true;
|
|
178
|
-
|
|
179
171
|
defaults.vars = {
|
|
180
172
|
SCOPE: 'ejs',
|
|
181
173
|
COMPONENT: 'ui',
|
|
@@ -186,13 +178,57 @@ defaults.vars = {
|
|
|
186
178
|
MACRO: '$$m',
|
|
187
179
|
SAFE: '$$v',
|
|
188
180
|
};
|
|
189
|
-
|
|
190
181
|
defaults.token = {
|
|
191
182
|
start: '<%',
|
|
192
183
|
end: '%>',
|
|
193
184
|
regex: '([\\s\\S]+?)',
|
|
194
185
|
};
|
|
195
186
|
|
|
187
|
+
const selfClosed = [
|
|
188
|
+
'area',
|
|
189
|
+
'base',
|
|
190
|
+
'br',
|
|
191
|
+
'col',
|
|
192
|
+
'embed',
|
|
193
|
+
'hr',
|
|
194
|
+
'img',
|
|
195
|
+
'input',
|
|
196
|
+
'link',
|
|
197
|
+
'meta',
|
|
198
|
+
'param',
|
|
199
|
+
'source',
|
|
200
|
+
'track',
|
|
201
|
+
'wbr',
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
const space = ' ';
|
|
205
|
+
const quote = '"';
|
|
206
|
+
const equal = '=';
|
|
207
|
+
const slash = '/';
|
|
208
|
+
const lt = '<';
|
|
209
|
+
const gt = '>';
|
|
210
|
+
|
|
211
|
+
const element = (tag, attrs, content) => {
|
|
212
|
+
const result = [];
|
|
213
|
+
const hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
214
|
+
const attributes = map(attrs, (value, key) => {
|
|
215
|
+
if (value !== null && value !== undefined) {
|
|
216
|
+
return [
|
|
217
|
+
entities(key),
|
|
218
|
+
[quote, entities(value), quote].join(''),
|
|
219
|
+
].join(equal)
|
|
220
|
+
}
|
|
221
|
+
}).join(space);
|
|
222
|
+
result.push([lt, tag, space, attributes, gt].join(''));
|
|
223
|
+
if (content) {
|
|
224
|
+
result.push(content instanceof Array ? content.join('') : content);
|
|
225
|
+
}
|
|
226
|
+
if (hasClosedTag) {
|
|
227
|
+
result.push([lt, slash, tag, gt].join(''));
|
|
228
|
+
}
|
|
229
|
+
return result.join('')
|
|
230
|
+
};
|
|
231
|
+
|
|
196
232
|
const configSchema = (config, options) => {
|
|
197
233
|
extend(config, {
|
|
198
234
|
path: typeProp(isString, defaults.path, config.path, options.path),
|
|
@@ -226,13 +262,7 @@ const configSchema = (config, options) => {
|
|
|
226
262
|
config.rmWhitespace,
|
|
227
263
|
options.rmWhitespace
|
|
228
264
|
),
|
|
229
|
-
|
|
230
|
-
chokidar: typeProp(
|
|
231
|
-
isObject,
|
|
232
|
-
defaults.export,
|
|
233
|
-
config.export,
|
|
234
|
-
options.export
|
|
235
|
-
),
|
|
265
|
+
cache: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
|
|
236
266
|
token: extend({}, defaults.token, config.token, options.token),
|
|
237
267
|
vars: extend({}, defaults.vars, config.vars, options.vars),
|
|
238
268
|
});
|
|
@@ -262,60 +292,49 @@ const fileSystem = (path, template) => {
|
|
|
262
292
|
})
|
|
263
293
|
};
|
|
264
294
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
this.chokidar = config.chokidar;
|
|
278
|
-
this.resolver = isFunction(config.resolver)
|
|
279
|
-
? config.resolver
|
|
280
|
-
: isNode()
|
|
281
|
-
? fileSystem
|
|
282
|
-
: httpRequest;
|
|
283
|
-
if (config.watch && isNode()) {
|
|
284
|
-
if (this.watcher) {
|
|
285
|
-
this.watcher.unwatch('.');
|
|
286
|
-
}
|
|
287
|
-
if (this.chokidar) {
|
|
288
|
-
this.watcher = this.chokidar
|
|
289
|
-
.watch('.', { cwd: this.path })
|
|
290
|
-
.on('change', (name) => {
|
|
291
|
-
this.cache.remove(name);
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
resolve(template) {
|
|
297
|
-
return this.resolver(this.path, template)
|
|
298
|
-
}
|
|
299
|
-
result(template, content) {
|
|
300
|
-
this.cache.set(template, content);
|
|
295
|
+
const fileResolver = (resolver) => {
|
|
296
|
+
return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
function Template(config, cache, compiler) {
|
|
300
|
+
if (instanceOf(this, Template) === false)
|
|
301
|
+
return new Template(config, cache, compiler)
|
|
302
|
+
|
|
303
|
+
const template = {};
|
|
304
|
+
|
|
305
|
+
const result = function (template, content) {
|
|
306
|
+
cache.set(template, content);
|
|
301
307
|
return content
|
|
302
|
-
}
|
|
303
|
-
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const resolve = function (path) {
|
|
311
|
+
return template.resolver(template.path, path)
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const compile = function (content, template) {
|
|
304
315
|
if (isFunction(content)) {
|
|
305
316
|
return content
|
|
306
317
|
} else {
|
|
307
|
-
return
|
|
318
|
+
return compiler.compile(content, template)
|
|
308
319
|
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
320
|
+
};
|
|
321
|
+
this.configure = function (config) {
|
|
322
|
+
template.path = config.path;
|
|
323
|
+
template.cache = config.cache;
|
|
324
|
+
template.resolver = fileResolver(config.resolver);
|
|
325
|
+
};
|
|
326
|
+
this.get = function (template) {
|
|
327
|
+
if (cache.exist(template)) {
|
|
328
|
+
return cache.resolve(template)
|
|
313
329
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
330
|
+
return result(
|
|
331
|
+
template,
|
|
332
|
+
resolve(template).then((content) =>
|
|
333
|
+
result(template, compile(content, template))
|
|
334
|
+
)
|
|
335
|
+
)
|
|
336
|
+
};
|
|
337
|
+
this.configure(config);
|
|
319
338
|
}
|
|
320
339
|
|
|
321
340
|
const tagList = [
|
|
@@ -345,7 +364,7 @@ const tagList = [
|
|
|
345
364
|
},
|
|
346
365
|
];
|
|
347
366
|
|
|
348
|
-
|
|
367
|
+
function matchTokens(regex, text, callback) {
|
|
349
368
|
let index = 0;
|
|
350
369
|
text.replace(regex, function () {
|
|
351
370
|
const params = [].slice.call(arguments, 0, -1);
|
|
@@ -355,71 +374,70 @@ const matchTokens = (regex, text, callback) => {
|
|
|
355
374
|
index = offset + match.length;
|
|
356
375
|
return match
|
|
357
376
|
});
|
|
358
|
-
}
|
|
377
|
+
}
|
|
359
378
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
379
|
+
function Compiler(config) {
|
|
380
|
+
if (instanceOf(this, Compiler) === false) return new Compiler(config)
|
|
381
|
+
|
|
382
|
+
const compiler = {};
|
|
364
383
|
|
|
365
|
-
configure(config) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
384
|
+
this.configure = function (config) {
|
|
385
|
+
compiler.withObject = config.withObject;
|
|
386
|
+
compiler.rmWhitespace = config.rmWhitespace;
|
|
387
|
+
compiler.token = config.token;
|
|
388
|
+
compiler.vars = config.vars;
|
|
389
|
+
compiler.matches = [];
|
|
390
|
+
compiler.formats = [];
|
|
391
|
+
compiler.slurp = {
|
|
373
392
|
match: '[ \\t]*',
|
|
374
|
-
start: [
|
|
375
|
-
end: ['_',
|
|
393
|
+
start: [compiler.token.start, '_'],
|
|
394
|
+
end: ['_', compiler.token.end],
|
|
376
395
|
};
|
|
377
396
|
tagList.forEach((item) => {
|
|
378
|
-
|
|
379
|
-
|
|
397
|
+
compiler.matches.push(
|
|
398
|
+
compiler.token.start
|
|
380
399
|
.concat(item.symbol)
|
|
381
|
-
.concat(
|
|
382
|
-
.concat(
|
|
400
|
+
.concat(compiler.token.regex)
|
|
401
|
+
.concat(compiler.token.end)
|
|
383
402
|
);
|
|
384
|
-
|
|
403
|
+
compiler.formats.push(item.format.bind(compiler.vars));
|
|
385
404
|
});
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
405
|
+
compiler.regex = new RegExp(
|
|
406
|
+
compiler.matches.join('|').concat('|$'),
|
|
407
|
+
'g'
|
|
408
|
+
);
|
|
409
|
+
compiler.slurpStart = new RegExp(
|
|
410
|
+
[compiler.slurp.match, compiler.slurp.start.join('')].join(''),
|
|
389
411
|
'gm'
|
|
390
412
|
);
|
|
391
|
-
|
|
392
|
-
[
|
|
413
|
+
compiler.slurpEnd = new RegExp(
|
|
414
|
+
[compiler.slurp.end.join(''), compiler.slurp.match].join(''),
|
|
393
415
|
'gm'
|
|
394
416
|
);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
truncate(value) {
|
|
398
|
-
return value && value.replace(/^(?:\r\n|\r|\n)/, '')
|
|
399
|
-
}
|
|
417
|
+
};
|
|
400
418
|
|
|
401
|
-
compile(content, path) {
|
|
402
|
-
const { SCOPE, SAFE, BUFFER, COMPONENT } =
|
|
403
|
-
if (
|
|
419
|
+
this.compile = function (content, path) {
|
|
420
|
+
const { SCOPE, SAFE, BUFFER, COMPONENT } = compiler.vars;
|
|
421
|
+
if (compiler.rmWhitespace) {
|
|
404
422
|
content = content
|
|
405
423
|
.replace(/[\r\n]+/g, '\n')
|
|
406
424
|
.replace(/^\s+|\s+$/gm, '');
|
|
407
425
|
}
|
|
408
426
|
content = content
|
|
409
|
-
.replace(
|
|
410
|
-
.replace(
|
|
427
|
+
.replace(compiler.slurpStart, compiler.token.start)
|
|
428
|
+
.replace(compiler.slurpEnd, compiler.token.end);
|
|
411
429
|
let source = `${BUFFER}('`;
|
|
412
|
-
matchTokens(
|
|
430
|
+
matchTokens(compiler.regex, content, (params, index, offset) => {
|
|
413
431
|
source += symbols(content.slice(index, offset));
|
|
414
432
|
params.forEach((value, index) => {
|
|
415
433
|
if (value) {
|
|
416
|
-
source +=
|
|
434
|
+
source += compiler.formats[index](value);
|
|
417
435
|
}
|
|
418
436
|
});
|
|
419
437
|
});
|
|
420
438
|
source += `');`;
|
|
421
439
|
source = `try{${source}}catch(e){console.info(e)}`;
|
|
422
|
-
if (
|
|
440
|
+
if (compiler.withObject) {
|
|
423
441
|
source = `with(${SCOPE}){${source}}`;
|
|
424
442
|
}
|
|
425
443
|
source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
|
|
@@ -434,92 +452,59 @@ class Compiler {
|
|
|
434
452
|
throw e
|
|
435
453
|
}
|
|
436
454
|
return result
|
|
437
|
-
}
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
this.configure(config);
|
|
438
458
|
}
|
|
439
459
|
|
|
440
460
|
const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
|
|
441
461
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
462
|
+
function Cache(config) {
|
|
463
|
+
if (instanceOf(this, Cache) === false) return new Cache()
|
|
464
|
+
const cache = {
|
|
465
|
+
enabled: true,
|
|
466
|
+
list: {},
|
|
467
|
+
};
|
|
468
|
+
this.configure = function (config) {
|
|
469
|
+
cache.list = {};
|
|
470
|
+
cache.enabled = config.cache;
|
|
449
471
|
if (isNode() === false) {
|
|
450
472
|
this.load(global[config.export]);
|
|
451
473
|
}
|
|
452
|
-
}
|
|
453
|
-
load(data) {
|
|
454
|
-
|
|
474
|
+
};
|
|
475
|
+
this.load = function (data) {
|
|
476
|
+
if (cache.enabled) {
|
|
477
|
+
extend(cache.list, data || {});
|
|
478
|
+
}
|
|
455
479
|
return this
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
resolve(key) {
|
|
467
|
-
return Promise.resolve(this.get(key))
|
|
468
|
-
}
|
|
469
|
-
set(key, value) {
|
|
470
|
-
this.list[key] = value;
|
|
480
|
+
};
|
|
481
|
+
this.get = function (key) {
|
|
482
|
+
if (cache.enabled) {
|
|
483
|
+
return cache.list[key]
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
this.set = function (key, value) {
|
|
487
|
+
if (cache.enabled) {
|
|
488
|
+
cache.list[key] = value;
|
|
489
|
+
}
|
|
471
490
|
return this
|
|
472
|
-
}
|
|
491
|
+
};
|
|
492
|
+
this.resolve = function (key) {
|
|
493
|
+
return Promise.resolve(this.get(key))
|
|
494
|
+
};
|
|
495
|
+
this.remove = function (key) {
|
|
496
|
+
delete cache.list[key];
|
|
497
|
+
};
|
|
498
|
+
this.exist = function (key) {
|
|
499
|
+
return hasProp(cache.list, key)
|
|
500
|
+
};
|
|
473
501
|
}
|
|
474
502
|
|
|
475
|
-
|
|
476
|
-
'
|
|
477
|
-
|
|
478
|
-
'br',
|
|
479
|
-
'col',
|
|
480
|
-
'embed',
|
|
481
|
-
'hr',
|
|
482
|
-
'img',
|
|
483
|
-
'input',
|
|
484
|
-
'link',
|
|
485
|
-
'meta',
|
|
486
|
-
'param',
|
|
487
|
-
'source',
|
|
488
|
-
'track',
|
|
489
|
-
'wbr',
|
|
490
|
-
];
|
|
491
|
-
|
|
492
|
-
const space = ' ';
|
|
493
|
-
const quote = '"';
|
|
494
|
-
const equal = '=';
|
|
495
|
-
const slash = '/';
|
|
496
|
-
const lt = '<';
|
|
497
|
-
const gt = '>';
|
|
498
|
-
|
|
499
|
-
const element = (tag, attrs, content) => {
|
|
500
|
-
const result = [];
|
|
501
|
-
const hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
502
|
-
const attributes = map(attrs, (value, key) => {
|
|
503
|
-
if (value !== null && value !== undefined) {
|
|
504
|
-
return [
|
|
505
|
-
entities(key),
|
|
506
|
-
[quote, entities(value), quote].join(''),
|
|
507
|
-
].join(equal)
|
|
508
|
-
}
|
|
509
|
-
}).join(space);
|
|
510
|
-
result.push([lt, tag, space, attributes, gt].join(''));
|
|
511
|
-
if (content) {
|
|
512
|
-
result.push(content instanceof Array ? content.join('') : content);
|
|
513
|
-
}
|
|
514
|
-
if (hasClosedTag) {
|
|
515
|
-
result.push([lt, slash, tag, gt].join(''));
|
|
516
|
-
}
|
|
517
|
-
return result.join('')
|
|
518
|
-
};
|
|
519
|
-
|
|
520
|
-
const resolve = (list) => Promise.all(list).then((list) => list.join(''));
|
|
503
|
+
function resolve(list) {
|
|
504
|
+
return Promise.all(list || []).then((list) => list.join(''))
|
|
505
|
+
}
|
|
521
506
|
|
|
522
|
-
const createBuffer = ()
|
|
507
|
+
const createBuffer = function () {
|
|
523
508
|
let store = [],
|
|
524
509
|
array = [];
|
|
525
510
|
function buffer(value) {
|
|
@@ -546,64 +531,47 @@ const createBuffer = () => {
|
|
|
546
531
|
return buffer
|
|
547
532
|
};
|
|
548
533
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
this.create = (data)
|
|
534
|
+
function Context(config) {
|
|
535
|
+
if (instanceOf(this, Context) === false) return new Context(config)
|
|
536
|
+
|
|
537
|
+
this.configure = function (config, methods) {
|
|
538
|
+
const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT } = config.vars;
|
|
539
|
+
|
|
540
|
+
this.create = function (data) {
|
|
556
541
|
return new Scope(data)
|
|
557
542
|
};
|
|
558
|
-
|
|
559
|
-
|
|
543
|
+
|
|
544
|
+
this.helpers = function (methods) {
|
|
545
|
+
extend(Scope.prototype, methods || {});
|
|
560
546
|
};
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
this
|
|
564
|
-
|
|
547
|
+
|
|
548
|
+
function Scope(data) {
|
|
549
|
+
this[BLOCKS] = {};
|
|
550
|
+
this[MACRO] = {};
|
|
551
|
+
extend(this, data || {});
|
|
565
552
|
}
|
|
566
553
|
Scope.prototype = extend({}, methods || {});
|
|
567
|
-
|
|
568
|
-
* @param {string} name
|
|
569
|
-
* @param {*} value
|
|
570
|
-
* @param {boolean} [writable]
|
|
571
|
-
* @param {boolean} [configurable]
|
|
572
|
-
* @param {boolean} [enumerable]
|
|
573
|
-
*/
|
|
574
|
-
Scope.define = Scope.method = (
|
|
554
|
+
Scope.method = Scope.define = function (
|
|
575
555
|
name,
|
|
576
556
|
value,
|
|
577
557
|
writable,
|
|
578
558
|
configurable,
|
|
579
559
|
enumerable
|
|
580
|
-
)
|
|
581
|
-
|
|
560
|
+
) {
|
|
561
|
+
Object.defineProperty(Scope.prototype, name, {
|
|
582
562
|
value: value,
|
|
583
563
|
writable: writable || false,
|
|
584
564
|
configurable: configurable || false,
|
|
585
565
|
enumerable: enumerable || false,
|
|
586
|
-
})
|
|
566
|
+
});
|
|
587
567
|
};
|
|
588
568
|
|
|
589
569
|
Scope.define(BUFFER, createBuffer());
|
|
590
|
-
|
|
591
570
|
Scope.define(BLOCKS, {}, true);
|
|
592
|
-
|
|
593
571
|
Scope.define(MACRO, {}, true);
|
|
594
|
-
|
|
595
572
|
Scope.define(LAYOUT, false, true);
|
|
596
|
-
|
|
597
573
|
Scope.define(EXTEND, false, true);
|
|
598
574
|
|
|
599
|
-
Scope.method('initBlocks', function () {
|
|
600
|
-
this[BLOCKS] = {};
|
|
601
|
-
});
|
|
602
|
-
|
|
603
|
-
Scope.method('initMacro', function () {
|
|
604
|
-
this[MACRO] = {};
|
|
605
|
-
});
|
|
606
|
-
|
|
607
575
|
Scope.method('getMacro', function () {
|
|
608
576
|
return this[MACRO]
|
|
609
577
|
});
|
|
@@ -792,42 +760,26 @@ class Context {
|
|
|
792
760
|
}
|
|
793
761
|
each(object, callback);
|
|
794
762
|
});
|
|
795
|
-
}
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
this.configure(config);
|
|
796
766
|
}
|
|
797
767
|
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
configSchema(this.config, options);
|
|
814
|
-
bindContext(this, this, this.export);
|
|
815
|
-
this.context = new Context(this.config);
|
|
816
|
-
this.compiler = new Compiler(this.config);
|
|
817
|
-
this.cache = new Cache(this.config);
|
|
818
|
-
this.template = new Template(this.config, this.cache, this.compiler);
|
|
819
|
-
this.helpers({ require: this.require, render: this.render });
|
|
820
|
-
}
|
|
821
|
-
configure(options = {}) {
|
|
822
|
-
configSchema(this.config, options);
|
|
823
|
-
this.context.configure(this.config, this.scope);
|
|
824
|
-
this.compiler.configure(this.config);
|
|
825
|
-
this.cache.configure(this.config);
|
|
826
|
-
this.template.configure(this.config);
|
|
827
|
-
return this.config
|
|
828
|
-
}
|
|
829
|
-
output(path, scope) {
|
|
830
|
-
return this.template.get(path).then(function (callback) {
|
|
768
|
+
function EJS(options) {
|
|
769
|
+
if (instanceOf(this, EJS) === false) return new EJS(options)
|
|
770
|
+
|
|
771
|
+
const scope = {};
|
|
772
|
+
const config = {};
|
|
773
|
+
|
|
774
|
+
configSchema(config, options || {});
|
|
775
|
+
|
|
776
|
+
const context = new Context(config);
|
|
777
|
+
const compiler = new Compiler(config);
|
|
778
|
+
const cache = new Cache();
|
|
779
|
+
const template = new Template(config, cache, compiler);
|
|
780
|
+
|
|
781
|
+
const output = function (path, scope) {
|
|
782
|
+
return template.get(path).then(function (callback) {
|
|
831
783
|
return callback.call(
|
|
832
784
|
scope,
|
|
833
785
|
scope,
|
|
@@ -836,80 +788,97 @@ class EJS {
|
|
|
836
788
|
safeValue
|
|
837
789
|
)
|
|
838
790
|
})
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
const
|
|
843
|
-
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const require = function (name) {
|
|
794
|
+
const filepath = ext(name, config.extension);
|
|
795
|
+
const scope = context.create({});
|
|
796
|
+
return output(filepath, scope).then(() => {
|
|
797
|
+
return scope.getMacro()
|
|
798
|
+
})
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
const render = function (name, data) {
|
|
802
|
+
const filepath = ext(name, config.extension);
|
|
803
|
+
const scope = context.create(data);
|
|
804
|
+
return output(filepath, scope).then((content) => {
|
|
844
805
|
if (scope.getExtend()) {
|
|
845
806
|
scope.setExtend(false);
|
|
846
807
|
const layout = scope.getLayout();
|
|
847
808
|
const data = scope.clone();
|
|
848
|
-
return
|
|
809
|
+
return render(layout, data)
|
|
849
810
|
}
|
|
850
811
|
return content
|
|
851
812
|
})
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
const scope = this.context.create({});
|
|
856
|
-
return this.output(filepath, scope).then(() => {
|
|
857
|
-
return scope.getMacro()
|
|
858
|
-
})
|
|
859
|
-
}
|
|
860
|
-
create(options = {}) {
|
|
861
|
-
return new EJS(options)
|
|
862
|
-
}
|
|
863
|
-
helpers(methods = {}) {
|
|
864
|
-
this.context.helpers(extend(this.scope, methods));
|
|
865
|
-
}
|
|
866
|
-
preload(list = {}) {
|
|
867
|
-
return this.cache.load(list)
|
|
868
|
-
}
|
|
869
|
-
compile(content, path) {
|
|
870
|
-
return this.compiler.compile(content, path)
|
|
871
|
-
}
|
|
872
|
-
__express(name, options, callback) {
|
|
873
|
-
if (isFunction(options)) {
|
|
874
|
-
callback = options;
|
|
875
|
-
options = {};
|
|
876
|
-
}
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
this.configure = function (options) {
|
|
877
816
|
options = options || {};
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
817
|
+
configSchema(config, options);
|
|
818
|
+
context.configure(config, scope);
|
|
819
|
+
compiler.configure(config);
|
|
820
|
+
cache.configure(config);
|
|
821
|
+
template.configure(config);
|
|
822
|
+
return config
|
|
823
|
+
};
|
|
824
|
+
|
|
825
|
+
this.render = function (name, data) {
|
|
826
|
+
return render(name, data)
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
this.helpers = function (methods) {
|
|
830
|
+
context.helpers(Object.assign(scope, methods || {}));
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
this.preload = function (list) {
|
|
834
|
+
return cache.load(list || {})
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
this.create = function (options) {
|
|
838
|
+
return new EJS(options)
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
this.compile = function (content, path) {
|
|
842
|
+
return compiler.compile(content, path)
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
this.context = function (data) {
|
|
846
|
+
return context.create(data)
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
this.helpers({ require, render });
|
|
898
850
|
}
|
|
899
851
|
|
|
900
|
-
const ejs = new EJS(
|
|
901
|
-
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
852
|
+
const ejs = new EJS();
|
|
853
|
+
|
|
854
|
+
const { render, context, compile, helpers, preload, configure, create } =
|
|
855
|
+
ejs;
|
|
856
|
+
|
|
857
|
+
function __express(name, options, callback) {
|
|
858
|
+
if (isFunction(options)) {
|
|
859
|
+
callback = options;
|
|
860
|
+
options = {};
|
|
861
|
+
}
|
|
862
|
+
options = options || {};
|
|
863
|
+
const settings = extend({}, options.settings);
|
|
864
|
+
const viewPath = typeProp(isString, defaults.path, settings['views']);
|
|
865
|
+
const viewCache = typeProp(
|
|
866
|
+
isBoolean,
|
|
867
|
+
defaults.cache,
|
|
868
|
+
settings['view cache']
|
|
869
|
+
);
|
|
870
|
+
const viewOptions = extend({}, settings['view options']);
|
|
871
|
+
const filename = path.relative(viewPath, name);
|
|
872
|
+
viewOptions.path = viewPath;
|
|
873
|
+
viewOptions.cache = viewCache;
|
|
874
|
+
configure(viewOptions);
|
|
875
|
+
return this.render(filename, options)
|
|
876
|
+
.then((content) => {
|
|
877
|
+
callback(null, content);
|
|
878
|
+
})
|
|
879
|
+
.catch((error) => {
|
|
880
|
+
callback(error);
|
|
881
|
+
})
|
|
882
|
+
}
|
|
914
883
|
|
|
915
884
|
export { EJS, __express, compile, configure, context, create, element, helpers, preload, render, safeValue };
|