@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/index.js CHANGED
@@ -1,131 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs');
4
3
  var path = require('path');
5
-
6
- function _typeof(o) {
7
- "@babel/helpers - typeof";
8
-
9
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
10
- return typeof o;
11
- } : function (o) {
12
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
13
- }, _typeof(o);
14
- }
15
- function _classCallCheck(instance, Constructor) {
16
- if (!(instance instanceof Constructor)) {
17
- throw new TypeError("Cannot call a class as a function");
18
- }
19
- }
20
- function _defineProperties(target, props) {
21
- for (var i = 0; i < props.length; i++) {
22
- var descriptor = props[i];
23
- descriptor.enumerable = descriptor.enumerable || false;
24
- descriptor.configurable = true;
25
- if ("value" in descriptor) descriptor.writable = true;
26
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
27
- }
28
- }
29
- function _createClass(Constructor, protoProps, staticProps) {
30
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
31
- if (staticProps) _defineProperties(Constructor, staticProps);
32
- Object.defineProperty(Constructor, "prototype", {
33
- writable: false
34
- });
35
- return Constructor;
36
- }
37
- function _defineProperty(obj, key, value) {
38
- key = _toPropertyKey(key);
39
- if (key in obj) {
40
- Object.defineProperty(obj, key, {
41
- value: value,
42
- enumerable: true,
43
- configurable: true,
44
- writable: true
45
- });
46
- } else {
47
- obj[key] = value;
48
- }
49
- return obj;
50
- }
51
- function _unsupportedIterableToArray(o, minLen) {
52
- if (!o) return;
53
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
54
- var n = Object.prototype.toString.call(o).slice(8, -1);
55
- if (n === "Object" && o.constructor) n = o.constructor.name;
56
- if (n === "Map" || n === "Set") return Array.from(o);
57
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
58
- }
59
- function _arrayLikeToArray(arr, len) {
60
- if (len == null || len > arr.length) len = arr.length;
61
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
62
- return arr2;
63
- }
64
- function _createForOfIteratorHelper(o, allowArrayLike) {
65
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
66
- if (!it) {
67
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
68
- if (it) o = it;
69
- var i = 0;
70
- var F = function () {};
71
- return {
72
- s: F,
73
- n: function () {
74
- if (i >= o.length) return {
75
- done: true
76
- };
77
- return {
78
- done: false,
79
- value: o[i++]
80
- };
81
- },
82
- e: function (e) {
83
- throw e;
84
- },
85
- f: F
86
- };
87
- }
88
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
89
- }
90
- var normalCompletion = true,
91
- didErr = false,
92
- err;
93
- return {
94
- s: function () {
95
- it = it.call(o);
96
- },
97
- n: function () {
98
- var step = it.next();
99
- normalCompletion = step.done;
100
- return step;
101
- },
102
- e: function (e) {
103
- didErr = true;
104
- err = e;
105
- },
106
- f: function () {
107
- try {
108
- if (!normalCompletion && it.return != null) it.return();
109
- } finally {
110
- if (didErr) throw err;
111
- }
112
- }
113
- };
114
- }
115
- function _toPrimitive(input, hint) {
116
- if (typeof input !== "object" || input === null) return input;
117
- var prim = input[Symbol.toPrimitive];
118
- if (prim !== undefined) {
119
- var res = prim.call(input, hint || "default");
120
- if (typeof res !== "object") return res;
121
- throw new TypeError("@@toPrimitive must return a primitive value.");
122
- }
123
- return (hint === "string" ? String : Number)(input);
124
- }
125
- function _toPropertyKey(arg) {
126
- var key = _toPrimitive(arg, "string");
127
- return typeof key === "symbol" ? key : String(key);
128
- }
4
+ var fs = require('fs');
129
5
 
130
6
  var typeProp = function typeProp() {
131
7
  var args = [].slice.call(arguments);
@@ -141,9 +17,6 @@ var isString = function isString(v) {
141
17
  var isBoolean = function isBoolean(v) {
142
18
  return typeof v === 'boolean';
143
19
  };
144
- var isObject = function isObject(v) {
145
- return _typeof(v) === 'object';
146
- };
147
20
  var isUndefined = function isUndefined(v) {
148
21
  return typeof v === 'undefined';
149
22
  };
@@ -184,39 +57,33 @@ var symbols = function symbols(string) {
184
57
  return '\\' + symbolEntities[match];
185
58
  });
186
59
  };
187
- var safeValue = function safeValue(value, escape, check) {
188
- return (check = value) == null ? '' : escape ? entities(check) : check;
60
+ var safeValue = function safeValue(value, escape) {
61
+ var check = value;
62
+ return check == null ? '' : escape === true ? entities(check) : check;
63
+ };
64
+ var instanceOf = function instanceOf(object, instance) {
65
+ return object instanceof instance;
189
66
  };
190
67
  var getPath = function getPath(context, name, strict) {
191
68
  var data = context;
192
69
  var chunks = String(name).split('.');
193
70
  var prop = chunks.pop();
194
- var _iterator = _createForOfIteratorHelper(chunks),
195
- _step;
196
- try {
197
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
198
- var part = _step.value;
199
- if (strict && data.hasOwnProperty(part) === false) {
200
- data = {};
201
- break;
202
- }
203
- data = data[part] = data[part] || {};
71
+ for (var i = 0; i < chunks.length; i++) {
72
+ var part = chunks[i];
73
+ if (isFunction(data['toJSON'])) {
74
+ data = data.toJSON();
204
75
  }
205
- } catch (err) {
206
- _iterator.e(err);
207
- } finally {
208
- _iterator.f();
76
+ if (strict && data.hasOwnProperty(part) === false) {
77
+ data = {};
78
+ break;
79
+ }
80
+ data = data[part] = data[part] || {};
81
+ }
82
+ if (isFunction(data['toJSON'])) {
83
+ data = data.toJSON();
209
84
  }
210
85
  return [data, prop];
211
86
  };
212
- var bindContext = function bindContext(object, context) {
213
- var methods = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
214
- methods.forEach(function (name) {
215
- if (name in object) {
216
- object[name] = object[name].bind(context);
217
- }
218
- });
219
- };
220
87
  var ext = function ext(path, defaults) {
221
88
  var ext = path.split('.').pop();
222
89
  if (ext !== defaults) {
@@ -285,7 +152,7 @@ var hasProp = function hasProp(object, prop) {
285
152
 
286
153
  var defaults = {};
287
154
  defaults["export"] = 'ejsPrecompiled';
288
- defaults.watch = false;
155
+ defaults.cache = true;
289
156
  defaults.chokidar = null;
290
157
  defaults.path = 'views';
291
158
  defaults.resolver = null;
@@ -308,6 +175,31 @@ defaults.token = {
308
175
  regex: '([\\s\\S]+?)'
309
176
  };
310
177
 
178
+ var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
179
+ var space = ' ';
180
+ var quote = '"';
181
+ var equal = '=';
182
+ var slash = '/';
183
+ var lt = '<';
184
+ var gt = '>';
185
+ var element = function element(tag, attrs, content) {
186
+ var result = [];
187
+ var hasClosedTag = selfClosed.indexOf(tag) === -1;
188
+ var attributes = map(attrs, function (value, key) {
189
+ if (value !== null && value !== undefined) {
190
+ return [entities(key), [quote, entities(value), quote].join('')].join(equal);
191
+ }
192
+ }).join(space);
193
+ result.push([lt, tag, space, attributes, gt].join(''));
194
+ if (content) {
195
+ result.push(content instanceof Array ? content.join('') : content);
196
+ }
197
+ if (hasClosedTag) {
198
+ result.push([lt, slash, tag, gt].join(''));
199
+ }
200
+ return result.join('');
201
+ };
202
+
311
203
  var configSchema = function configSchema(config, options) {
312
204
  extend(config, {
313
205
  path: typeProp(isString, defaults.path, config.path, options.path),
@@ -316,8 +208,7 @@ var configSchema = function configSchema(config, options) {
316
208
  extension: typeProp(isString, defaults.extension, config.extension, options.extension),
317
209
  withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
318
210
  rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
319
- watch: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
320
- chokidar: typeProp(isObject, defaults["export"], config["export"], options["export"]),
211
+ cache: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
321
212
  token: extend({}, defaults.token, config.token, options.token),
322
213
  vars: extend({}, defaults.vars, config.vars, options.vars)
323
214
  });
@@ -344,72 +235,41 @@ var fileSystem = function fileSystem(path, template) {
344
235
  });
345
236
  });
346
237
  };
347
- var Template = /*#__PURE__*/function () {
348
- function Template(config, cache, compiler) {
349
- _classCallCheck(this, Template);
350
- this.cache = cache;
351
- this.watcher = {
352
- unwatch: function unwatch() {},
353
- on: function on() {}
354
- };
355
- this.compiler = compiler;
356
- this.configure(config);
357
- }
358
- _createClass(Template, [{
359
- key: "configure",
360
- value: function configure(config) {
361
- var _this = this;
362
- this.path = config.path;
363
- this.chokidar = config.chokidar;
364
- this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
365
- if (config.watch && isNode()) {
366
- if (this.watcher) {
367
- this.watcher.unwatch('.');
368
- }
369
- if (this.chokidar) {
370
- this.watcher = this.chokidar.watch('.', {
371
- cwd: this.path
372
- }).on('change', function (name) {
373
- _this.cache.remove(name);
374
- });
375
- }
376
- }
377
- }
378
- }, {
379
- key: "resolve",
380
- value: function resolve(template) {
381
- return this.resolver(this.path, template);
382
- }
383
- }, {
384
- key: "result",
385
- value: function result(template, content) {
386
- this.cache.set(template, content);
238
+ var fileResolver = function fileResolver(resolver) {
239
+ return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest;
240
+ };
241
+ function Template(config, cache, compiler) {
242
+ if (instanceOf(this, Template) === false) return new Template(config, cache, compiler);
243
+ var template = {};
244
+ var result = function result(template, content) {
245
+ cache.set(template, content);
246
+ return content;
247
+ };
248
+ var resolve = function resolve(path) {
249
+ return template.resolver(template.path, path);
250
+ };
251
+ var compile = function compile(content, template) {
252
+ if (isFunction(content)) {
387
253
  return content;
254
+ } else {
255
+ return compiler.compile(content, template);
388
256
  }
389
- }, {
390
- key: "compile",
391
- value: function compile(content, template) {
392
- if (isFunction(content)) {
393
- return content;
394
- } else {
395
- return this.compiler.compile(content, template);
396
- }
397
- }
398
- }, {
399
- key: "get",
400
- value: function get(template) {
401
- var _this2 = this;
402
- if (this.cache.exist(template)) {
403
- return this.cache.resolve(template);
404
- }
405
- var content = this.resolve(template).then(function (content) {
406
- return _this2.result(template, _this2.compile(content, template));
407
- });
408
- return this.result(template, content);
257
+ };
258
+ this.configure = function (config) {
259
+ template.path = config.path;
260
+ template.cache = config.cache;
261
+ template.resolver = fileResolver(config.resolver);
262
+ };
263
+ this.get = function (template) {
264
+ if (cache.exist(template)) {
265
+ return cache.resolve(template);
409
266
  }
410
- }]);
411
- return Template;
412
- }();
267
+ return result(template, resolve(template).then(function (content) {
268
+ return result(template, compile(content, template));
269
+ }));
270
+ };
271
+ this.configure(config);
272
+ }
413
273
 
414
274
  var tagList = [{
415
275
  symbol: '-',
@@ -432,7 +292,7 @@ var tagList = [{
432
292
  return "')\n".concat(value.trim(), "\n").concat(this.BUFFER, "('");
433
293
  }
434
294
  }];
435
- var matchTokens = function matchTokens(regex, text, callback) {
295
+ function matchTokens(regex, text, callback) {
436
296
  var index = 0;
437
297
  text.replace(regex, function () {
438
298
  var params = [].slice.call(arguments, 0, -1);
@@ -442,165 +302,117 @@ var matchTokens = function matchTokens(regex, text, callback) {
442
302
  index = offset + match.length;
443
303
  return match;
444
304
  });
445
- };
446
- var Compiler = /*#__PURE__*/function () {
447
- function Compiler(config) {
448
- _classCallCheck(this, Compiler);
449
- this.configure(config);
450
- }
451
- _createClass(Compiler, [{
452
- key: "configure",
453
- value: function configure(config) {
454
- var _this = this;
455
- this.withObject = config.withObject;
456
- this.rmWhitespace = config.rmWhitespace;
457
- this.token = config.token;
458
- this.vars = config.vars;
459
- this.matches = [];
460
- this.formats = [];
461
- this.slurp = {
462
- match: '[ \\t]*',
463
- start: [this.token.start, '_'],
464
- end: ['_', this.token.end]
465
- };
466
- tagList.forEach(function (item) {
467
- _this.matches.push(_this.token.start.concat(item.symbol).concat(_this.token.regex).concat(_this.token.end));
468
- _this.formats.push(item.format.bind(_this.vars));
469
- });
470
- this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
471
- this.slurpStart = new RegExp([this.slurp.match, this.slurp.start.join('')].join(''), 'gm');
472
- this.slurpEnd = new RegExp([this.slurp.end.join(''), this.slurp.match].join(''), 'gm');
473
- }
474
- }, {
475
- key: "truncate",
476
- value: function truncate(value) {
477
- return value && value.replace(/^(?:\r\n|\r|\n)/, '');
478
- }
479
- }, {
480
- key: "compile",
481
- value: function compile(content, path) {
482
- var _this2 = this;
483
- var _this$vars = this.vars,
484
- SCOPE = _this$vars.SCOPE,
485
- SAFE = _this$vars.SAFE,
486
- BUFFER = _this$vars.BUFFER,
487
- COMPONENT = _this$vars.COMPONENT;
488
- if (this.rmWhitespace) {
489
- content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
490
- }
491
- content = content.replace(this.slurpStart, this.token.start).replace(this.slurpEnd, this.token.end);
492
- var source = "".concat(BUFFER, "('");
493
- matchTokens(this.regex, content, function (params, index, offset) {
494
- source += symbols(content.slice(index, offset));
495
- params.forEach(function (value, index) {
496
- if (value) {
497
- source += _this2.formats[index](value);
498
- }
499
- });
305
+ }
306
+ function Compiler(config) {
307
+ if (instanceOf(this, Compiler) === false) return new Compiler(config);
308
+ var compiler = {};
309
+ this.configure = function (config) {
310
+ compiler.withObject = config.withObject;
311
+ compiler.rmWhitespace = config.rmWhitespace;
312
+ compiler.token = config.token;
313
+ compiler.vars = config.vars;
314
+ compiler.matches = [];
315
+ compiler.formats = [];
316
+ compiler.slurp = {
317
+ match: '[ \\t]*',
318
+ start: [compiler.token.start, '_'],
319
+ end: ['_', compiler.token.end]
320
+ };
321
+ tagList.forEach(function (item) {
322
+ compiler.matches.push(compiler.token.start.concat(item.symbol).concat(compiler.token.regex).concat(compiler.token.end));
323
+ compiler.formats.push(item.format.bind(compiler.vars));
324
+ });
325
+ compiler.regex = new RegExp(compiler.matches.join('|').concat('|$'), 'g');
326
+ compiler.slurpStart = new RegExp([compiler.slurp.match, compiler.slurp.start.join('')].join(''), 'gm');
327
+ compiler.slurpEnd = new RegExp([compiler.slurp.end.join(''), compiler.slurp.match].join(''), 'gm');
328
+ };
329
+ this.compile = function (content, path) {
330
+ var _compiler$vars = compiler.vars,
331
+ SCOPE = _compiler$vars.SCOPE,
332
+ SAFE = _compiler$vars.SAFE,
333
+ BUFFER = _compiler$vars.BUFFER,
334
+ COMPONENT = _compiler$vars.COMPONENT;
335
+ if (compiler.rmWhitespace) {
336
+ content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
337
+ }
338
+ content = content.replace(compiler.slurpStart, compiler.token.start).replace(compiler.slurpEnd, compiler.token.end);
339
+ var source = "".concat(BUFFER, "('");
340
+ matchTokens(compiler.regex, content, function (params, index, offset) {
341
+ source += symbols(content.slice(index, offset));
342
+ params.forEach(function (value, index) {
343
+ if (value) {
344
+ source += compiler.formats[index](value);
345
+ }
500
346
  });
501
- source += "');";
502
- source = "try{".concat(source, "}catch(e){console.info(e)}");
503
- if (this.withObject) {
504
- source = "with(".concat(SCOPE, "){").concat(source, "}");
505
- }
506
- source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
507
- source += "\n//# sourceURL=".concat(path);
508
- var result = null;
509
- try {
510
- result = new Function(SCOPE, COMPONENT, BUFFER, SAFE, source);
511
- result.source = "(function(".concat(SCOPE, ",").concat(COMPONENT, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})");
512
- } catch (e) {
513
- e.filename = path;
514
- e.source = source;
515
- throw e;
516
- }
517
- return result;
518
- }
519
- }]);
520
- return Compiler;
521
- }();
347
+ });
348
+ source += "');";
349
+ source = "try{".concat(source, "}catch(e){console.info(e)}");
350
+ if (compiler.withObject) {
351
+ source = "with(".concat(SCOPE, "){").concat(source, "}");
352
+ }
353
+ source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
354
+ source += "\n//# sourceURL=".concat(path);
355
+ var result = null;
356
+ try {
357
+ result = new Function(SCOPE, COMPONENT, BUFFER, SAFE, source);
358
+ result.source = "(function(".concat(SCOPE, ",").concat(COMPONENT, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})");
359
+ } catch (e) {
360
+ e.filename = path;
361
+ e.source = source;
362
+ throw e;
363
+ }
364
+ return result;
365
+ };
366
+ this.configure(config);
367
+ }
522
368
 
523
369
  var global = typeof globalThis !== 'undefined' ? globalThis : window || self;
524
- var Cache = /*#__PURE__*/function () {
525
- function Cache(config) {
526
- _classCallCheck(this, Cache);
527
- _defineProperty(this, "list", {});
528
- this.configure(config);
529
- }
530
- _createClass(Cache, [{
531
- key: "configure",
532
- value: function configure(config) {
533
- this.list = {};
534
- if (isNode() === false) {
535
- this.load(global[config["export"]]);
536
- }
537
- }
538
- }, {
539
- key: "load",
540
- value: function load(data) {
541
- extend(this.list, data);
542
- return this;
543
- }
544
- }, {
545
- key: "exist",
546
- value: function exist(key) {
547
- return hasProp(this.list, key);
548
- }
549
- }, {
550
- key: "get",
551
- value: function get(key) {
552
- return this.list[key];
553
- }
554
- }, {
555
- key: "remove",
556
- value: function remove(key) {
557
- delete this.list[key];
370
+ function Cache(config) {
371
+ if (instanceOf(this, Cache) === false) return new Cache();
372
+ var cache = {
373
+ enabled: true,
374
+ list: {}
375
+ };
376
+ this.configure = function (config) {
377
+ cache.list = {};
378
+ cache.enabled = config.cache;
379
+ if (isNode() === false) {
380
+ this.load(global[config["export"]]);
558
381
  }
559
- }, {
560
- key: "resolve",
561
- value: function resolve(key) {
562
- return Promise.resolve(this.get(key));
382
+ };
383
+ this.load = function (data) {
384
+ if (cache.enabled) {
385
+ extend(cache.list, data || {});
563
386
  }
564
- }, {
565
- key: "set",
566
- value: function set(key, value) {
567
- this.list[key] = value;
568
- return this;
387
+ return this;
388
+ };
389
+ this.get = function (key) {
390
+ if (cache.enabled) {
391
+ return cache.list[key];
569
392
  }
570
- }]);
571
- return Cache;
572
- }();
573
-
574
- var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
575
- var space = ' ';
576
- var quote = '"';
577
- var equal = '=';
578
- var slash = '/';
579
- var lt = '<';
580
- var gt = '>';
581
- var element = function element(tag, attrs, content) {
582
- var result = [];
583
- var hasClosedTag = selfClosed.indexOf(tag) === -1;
584
- var attributes = map(attrs, function (value, key) {
585
- if (value !== null && value !== undefined) {
586
- return [entities(key), [quote, entities(value), quote].join('')].join(equal);
393
+ };
394
+ this.set = function (key, value) {
395
+ if (cache.enabled) {
396
+ cache.list[key] = value;
587
397
  }
588
- }).join(space);
589
- result.push([lt, tag, space, attributes, gt].join(''));
590
- if (content) {
591
- result.push(content instanceof Array ? content.join('') : content);
592
- }
593
- if (hasClosedTag) {
594
- result.push([lt, slash, tag, gt].join(''));
595
- }
596
- return result.join('');
597
- };
398
+ return this;
399
+ };
400
+ this.resolve = function (key) {
401
+ return Promise.resolve(this.get(key));
402
+ };
403
+ this.remove = function (key) {
404
+ delete cache.list[key];
405
+ };
406
+ this.exist = function (key) {
407
+ return hasProp(cache.list, key);
408
+ };
409
+ }
598
410
 
599
- var resolve = function resolve(list) {
600
- return Promise.all(list).then(function (list) {
411
+ function resolve(list) {
412
+ return Promise.all(list || []).then(function (list) {
601
413
  return list.join('');
602
414
  });
603
- };
415
+ }
604
416
  var createBuffer = function createBuffer() {
605
417
  var store = [],
606
418
  array = [];
@@ -628,329 +440,263 @@ var createBuffer = function createBuffer() {
628
440
  return buffer;
629
441
  };
630
442
 
631
- var Context = /*#__PURE__*/function () {
632
- function Context(config) {
633
- _classCallCheck(this, Context);
634
- this.configure(config);
635
- }
636
- _createClass(Context, [{
637
- key: "configure",
638
- value: function configure(config, methods) {
639
- var _config$vars = config.vars,
640
- EXTEND = _config$vars.EXTEND,
641
- LAYOUT = _config$vars.LAYOUT,
642
- BLOCKS = _config$vars.BLOCKS,
643
- BUFFER = _config$vars.BUFFER,
644
- MACRO = _config$vars.MACRO,
645
- COMPONENT = _config$vars.COMPONENT;
646
- this.create = function (data) {
647
- return new Scope(data);
443
+ function Context(config) {
444
+ if (instanceOf(this, Context) === false) return new Context(config);
445
+ this.configure = function (config, methods) {
446
+ var _config$vars = config.vars,
447
+ BLOCKS = _config$vars.BLOCKS,
448
+ MACRO = _config$vars.MACRO,
449
+ EXTEND = _config$vars.EXTEND,
450
+ LAYOUT = _config$vars.LAYOUT,
451
+ BUFFER = _config$vars.BUFFER,
452
+ COMPONENT = _config$vars.COMPONENT;
453
+ this.create = function (data) {
454
+ return new Scope(data);
455
+ };
456
+ this.helpers = function (methods) {
457
+ extend(Scope.prototype, methods || {});
458
+ };
459
+ function Scope(data) {
460
+ this[BLOCKS] = {};
461
+ this[MACRO] = {};
462
+ extend(this, data || {});
463
+ }
464
+ Scope.prototype = extend({}, methods || {});
465
+ Scope.method = Scope.define = function (name, value, writable, configurable, enumerable) {
466
+ Object.defineProperty(Scope.prototype, name, {
467
+ value: value,
468
+ writable: writable || false,
469
+ configurable: configurable || false,
470
+ enumerable: enumerable || false
471
+ });
472
+ };
473
+ Scope.define(BUFFER, createBuffer());
474
+ Scope.define(BLOCKS, {}, true);
475
+ Scope.define(MACRO, {}, true);
476
+ Scope.define(LAYOUT, false, true);
477
+ Scope.define(EXTEND, false, true);
478
+ Scope.method('getMacro', function () {
479
+ return this[MACRO];
480
+ });
481
+ Scope.method('getBuffer', function () {
482
+ return this[BUFFER];
483
+ });
484
+ Scope.method('getComponent', function () {
485
+ var context = this;
486
+ if (COMPONENT in context) {
487
+ return function () {
488
+ return context[COMPONENT].apply(context, arguments);
489
+ };
490
+ }
491
+ return function () {
492
+ console.log('%s function not defined', COMPONENT);
648
493
  };
649
- this.helpers = function (methods) {
650
- extend(Scope.prototype, methods);
494
+ });
495
+ Scope.method('getBlocks', function () {
496
+ return this[BLOCKS];
497
+ });
498
+ Scope.method('setExtend', function (value) {
499
+ this[EXTEND] = value;
500
+ });
501
+ Scope.method('getExtend', function () {
502
+ return this[EXTEND];
503
+ });
504
+ Scope.method('setLayout', function (layout) {
505
+ this[LAYOUT] = layout;
506
+ });
507
+ Scope.method('getLayout', function () {
508
+ return this[LAYOUT];
509
+ });
510
+ Scope.method('clone', function (exclude_blocks) {
511
+ var filter = [LAYOUT, EXTEND, BUFFER];
512
+ if (exclude_blocks === true) {
513
+ filter.push(BLOCKS);
514
+ }
515
+ return omit(this, filter);
516
+ });
517
+ Scope.method('extend', function (layout) {
518
+ this.setExtend(true);
519
+ this.setLayout(layout);
520
+ });
521
+ Scope.method('echo', function () {
522
+ var buffer = this.getBuffer();
523
+ var params = [].slice.call(arguments);
524
+ params.forEach(buffer);
525
+ });
526
+ Scope.method('fn', function (callback) {
527
+ var buffer = this.getBuffer();
528
+ var context = this;
529
+ return function () {
530
+ buffer.backup();
531
+ if (isFunction(callback)) {
532
+ callback.apply(context, arguments);
533
+ }
534
+ return buffer.restore();
651
535
  };
652
- function Scope() {
653
- var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
654
- this.initBlocks();
655
- this.initMacro();
656
- extend(this, data);
536
+ });
537
+ Scope.method('get', function (name, defaults) {
538
+ var path = getPath(this, name, true);
539
+ var result = path.shift();
540
+ var prop = path.pop();
541
+ return hasProp(result, prop) ? result[prop] : defaults;
542
+ });
543
+ Scope.method('set', function (name, value) {
544
+ var path = getPath(this, name, false);
545
+ var result = path.shift();
546
+ var prop = path.pop();
547
+ if (this.getExtend() && hasProp(result, prop)) {
548
+ return result[prop];
657
549
  }
658
- Scope.prototype = extend({}, methods || {});
659
- /**
660
- * @param {string} name
661
- * @param {*} value
662
- * @param {boolean} [writable]
663
- * @param {boolean} [configurable]
664
- * @param {boolean} [enumerable]
665
- */
666
- Scope.define = Scope.method = function (name, value, writable, configurable, enumerable) {
667
- return Object.defineProperty(Scope.prototype, name, {
668
- value: value,
669
- writable: writable || false,
670
- configurable: configurable || false,
671
- enumerable: enumerable || false
672
- });
550
+ return result[prop] = value;
551
+ });
552
+ Scope.method('macro', function (name, callback) {
553
+ var list = this.getMacro();
554
+ var macro = this.fn(callback);
555
+ var context = this;
556
+ list[name] = function () {
557
+ return context.echo(macro.apply(undefined, arguments));
673
558
  };
674
- Scope.define(BUFFER, createBuffer());
675
- Scope.define(BLOCKS, {}, true);
676
- Scope.define(MACRO, {}, true);
677
- Scope.define(LAYOUT, false, true);
678
- Scope.define(EXTEND, false, true);
679
- Scope.method('initBlocks', function () {
680
- this[BLOCKS] = {};
681
- });
682
- Scope.method('initMacro', function () {
683
- this[MACRO] = {};
684
- });
685
- Scope.method('getMacro', function () {
686
- return this[MACRO];
687
- });
688
- Scope.method('getBuffer', function () {
689
- return this[BUFFER];
690
- });
691
- Scope.method('getComponent', function () {
692
- var context = this;
693
- if (COMPONENT in context) {
559
+ });
560
+ Scope.method('call', function (name) {
561
+ var list = this.getMacro();
562
+ var macro = list[name];
563
+ var params = [].slice.call(arguments, 1);
564
+ if (isFunction(macro)) {
565
+ return macro.apply(macro, params);
566
+ }
567
+ });
568
+ Scope.method('block', function (name, callback) {
569
+ var _this = this;
570
+ var blocks = this.getBlocks();
571
+ blocks[name] = blocks[name] || [];
572
+ blocks[name].push(this.fn(callback));
573
+ if (this.getExtend()) return;
574
+ var list = Object.assign([], blocks[name]);
575
+ var current = function current() {
576
+ return list.shift();
577
+ };
578
+ var next = function next() {
579
+ var parent = current();
580
+ if (parent) {
694
581
  return function () {
695
- return context[COMPONENT].apply(context, arguments);
582
+ _this.echo(parent(next()));
696
583
  };
584
+ } else {
585
+ return noop;
697
586
  }
698
- return function () {
699
- console.log('%s function not defined', COMPONENT);
700
- };
701
- });
702
- Scope.method('getBlocks', function () {
703
- return this[BLOCKS];
704
- });
705
- Scope.method('setExtend', function (value) {
706
- this[EXTEND] = value;
707
- });
708
- Scope.method('getExtend', function () {
709
- return this[EXTEND];
710
- });
711
- Scope.method('setLayout', function (layout) {
712
- this[LAYOUT] = layout;
713
- });
714
- Scope.method('getLayout', function () {
715
- return this[LAYOUT];
716
- });
717
- Scope.method('clone', function (exclude_blocks) {
718
- var filter = [LAYOUT, EXTEND, BUFFER];
719
- if (exclude_blocks === true) {
720
- filter.push(BLOCKS);
721
- }
722
- return omit(this, filter);
723
- });
724
- Scope.method('extend', function (layout) {
725
- this.setExtend(true);
726
- this.setLayout(layout);
727
- });
728
- Scope.method('echo', function () {
729
- var buffer = this.getBuffer();
730
- var params = [].slice.call(arguments);
731
- params.forEach(buffer);
732
- });
733
- Scope.method('fn', function (callback) {
734
- var buffer = this.getBuffer();
735
- var context = this;
736
- return function () {
737
- buffer.backup();
738
- if (isFunction(callback)) {
739
- callback.apply(context, arguments);
740
- }
741
- return buffer.restore();
742
- };
743
- });
744
- Scope.method('get', function (name, defaults) {
745
- var path = getPath(this, name, true);
746
- var result = path.shift();
747
- var prop = path.pop();
748
- return hasProp(result, prop) ? result[prop] : defaults;
749
- });
750
- Scope.method('set', function (name, value) {
751
- var path = getPath(this, name, false);
752
- var result = path.shift();
753
- var prop = path.pop();
754
- if (this.getExtend() && hasProp(result, prop)) {
755
- return result[prop];
756
- }
757
- return result[prop] = value;
758
- });
759
- Scope.method('macro', function (name, callback) {
760
- var list = this.getMacro();
761
- var macro = this.fn(callback);
762
- var context = this;
763
- list[name] = function () {
764
- return context.echo(macro.apply(undefined, arguments));
765
- };
766
- });
767
- Scope.method('call', function (name) {
587
+ };
588
+ this.echo(current()(next()));
589
+ });
590
+ Scope.method('include', function (path, data, cx) {
591
+ var context = cx === false ? {} : this.clone(true);
592
+ var params = extend(context, data || {});
593
+ var promise = this.render(path, params);
594
+ this.echo(promise);
595
+ });
596
+ Scope.method('use', function (path, namespace) {
597
+ var promise = this.require(path);
598
+ this.echo(resolve$1(promise, function (exports) {
768
599
  var list = this.getMacro();
769
- var macro = list[name];
770
- var params = [].slice.call(arguments, 1);
771
- if (isFunction(macro)) {
772
- return macro.apply(macro, params);
773
- }
774
- });
775
- Scope.method('block', function (name, callback) {
776
- var _this = this;
777
- var blocks = this.getBlocks();
778
- blocks[name] = blocks[name] || [];
779
- blocks[name].push(this.fn(callback));
780
- if (this.getExtend()) return;
781
- var list = Object.assign([], blocks[name]);
782
- var current = function current() {
783
- return list.shift();
784
- };
785
- var next = function next() {
786
- var parent = current();
787
- if (parent) {
788
- return function () {
789
- _this.echo(parent(next()));
790
- };
791
- } else {
792
- return noop;
793
- }
794
- };
795
- this.echo(current()(next()));
796
- });
797
- Scope.method('include', function (path, data, cx) {
798
- var context = cx === false ? {} : this.clone(true);
799
- var params = extend(context, data || {});
800
- var promise = this.render(path, params);
801
- this.echo(promise);
802
- });
803
- Scope.method('use', function (path, namespace) {
804
- var promise = this.require(path);
805
- this.echo(resolve$1(promise, function (exports) {
806
- var list = this.getMacro();
807
- each(exports, function (macro, name) {
808
- list[[namespace, name].join('.')] = macro;
809
- });
810
- }, this));
811
- });
812
- Scope.method('async', function (promise, callback) {
813
- this.echo(resolve$1(promise, function (data) {
814
- return this.fn(callback)(data);
815
- }, this));
816
- });
817
- Scope.method('node', function (tag, attr, content) {
600
+ each(exports, function (macro, name) {
601
+ list[[namespace, name].join('.')] = macro;
602
+ });
603
+ }, this));
604
+ });
605
+ Scope.method('async', function (promise, callback) {
606
+ this.echo(resolve$1(promise, function (data) {
607
+ return this.fn(callback)(data);
608
+ }, this));
609
+ });
610
+ Scope.method('node', function (tag, attr, content) {
611
+ return element(tag, attr, content);
612
+ });
613
+ Scope.method('el', function (tag, attr, content) {
614
+ if (isFunction(content)) {
615
+ content = this.fn(content)();
616
+ }
617
+ this.echo(resolve$1(content, function (content) {
818
618
  return element(tag, attr, content);
819
- });
820
- Scope.method('el', function (tag, attr, content) {
821
- if (isFunction(content)) {
822
- content = this.fn(content)();
823
- }
824
- this.echo(resolve$1(content, function (content) {
825
- return element(tag, attr, content);
826
- }, this));
827
- });
828
- Scope.method('each', function (object, callback) {
829
- if (isString(object)) {
830
- object = this.get(object, []);
831
- }
832
- each(object, callback);
833
- });
834
- }
835
- }]);
836
- return Context;
837
- }();
619
+ }, this));
620
+ });
621
+ Scope.method('each', function (object, callback) {
622
+ if (isString(object)) {
623
+ object = this.get(object, []);
624
+ }
625
+ each(object, callback);
626
+ });
627
+ };
628
+ this.configure(config);
629
+ }
838
630
 
839
- var EJS = /*#__PURE__*/function () {
840
- function EJS() {
841
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
842
- _classCallCheck(this, EJS);
843
- _defineProperty(this, "export", ['cache', 'render', 'require', 'helpers', 'configure', 'preload', 'compile', 'create', '__express']);
844
- this.config = {};
845
- this.scope = {};
846
- configSchema(this.config, options);
847
- bindContext(this, this, this["export"]);
848
- this.context = new Context(this.config);
849
- this.compiler = new Compiler(this.config);
850
- this.cache = new Cache(this.config);
851
- this.template = new Template(this.config, this.cache, this.compiler);
852
- this.helpers({
853
- require: this.require,
854
- render: this.render
631
+ function EJS(options) {
632
+ if (instanceOf(this, EJS) === false) return new EJS(options);
633
+ var scope = {};
634
+ var config = {};
635
+ configSchema(config, options || {});
636
+ var context = new Context(config);
637
+ var compiler = new Compiler(config);
638
+ var cache = new Cache();
639
+ var template = new Template(config, cache, compiler);
640
+ var output = function output(path, scope) {
641
+ return template.get(path).then(function (callback) {
642
+ return callback.call(scope, scope, scope.getComponent(), scope.getBuffer(), safeValue);
855
643
  });
856
- }
857
- _createClass(EJS, [{
858
- key: "configure",
859
- value: function configure() {
860
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
861
- configSchema(this.config, options);
862
- this.context.configure(this.config, this.scope);
863
- this.compiler.configure(this.config);
864
- this.cache.configure(this.config);
865
- this.template.configure(this.config);
866
- return this.config;
867
- }
868
- }, {
869
- key: "output",
870
- value: function output(path, scope) {
871
- return this.template.get(path).then(function (callback) {
872
- return callback.call(scope, scope, scope.getComponent(), scope.getBuffer(), safeValue);
873
- });
874
- }
875
- }, {
876
- key: "render",
877
- value: function render(name, data) {
878
- var _this = this;
879
- var filepath = ext(name, this.config.extension);
880
- var scope = this.context.create(data);
881
- return this.output(filepath, scope).then(function (content) {
882
- if (scope.getExtend()) {
883
- scope.setExtend(false);
884
- var layout = scope.getLayout();
885
- var _data = scope.clone();
886
- return _this.render(layout, _data);
887
- }
888
- return content;
889
- });
890
- }
891
- }, {
892
- key: "require",
893
- value: function require(name) {
894
- var filepath = ext(name, this.config.extension);
895
- var scope = this.context.create({});
896
- return this.output(filepath, scope).then(function () {
897
- return scope.getMacro();
898
- });
899
- }
900
- }, {
901
- key: "create",
902
- value: function create() {
903
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
904
- return new EJS(options);
905
- }
906
- }, {
907
- key: "helpers",
908
- value: function helpers() {
909
- var methods = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
910
- this.context.helpers(extend(this.scope, methods));
911
- }
912
- }, {
913
- key: "preload",
914
- value: function preload() {
915
- var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
916
- return this.cache.load(list);
917
- }
918
- }, {
919
- key: "compile",
920
- value: function compile(content, path) {
921
- return this.compiler.compile(content, path);
922
- }
923
- }, {
924
- key: "__express",
925
- value: function __express(name, options, callback) {
926
- if (isFunction(options)) {
927
- callback = options;
928
- options = {};
644
+ };
645
+ var require = function require(name) {
646
+ var filepath = ext(name, config.extension);
647
+ var scope = context.create({});
648
+ return output(filepath, scope).then(function () {
649
+ return scope.getMacro();
650
+ });
651
+ };
652
+ var render = function render(name, data) {
653
+ var filepath = ext(name, config.extension);
654
+ var scope = context.create(data);
655
+ return output(filepath, scope).then(function (content) {
656
+ if (scope.getExtend()) {
657
+ scope.setExtend(false);
658
+ var layout = scope.getLayout();
659
+ var _data = scope.clone();
660
+ return render(layout, _data);
929
661
  }
930
- options = options || {};
931
- var settings = extend({}, options.settings);
932
- var viewPath = typeProp(isString, defaults.path, settings['views']);
933
- var viewCache = typeProp(isBoolean, defaults.cache, settings['view cache']);
934
- var viewOptions = extend({}, settings['view options']);
935
- var filename = path.relative(viewPath, name);
936
- viewOptions.path = viewPath;
937
- viewOptions.cache = viewCache;
938
- this.configure(viewOptions);
939
- return this.render(filename, options).then(function (content) {
940
- callback(null, content);
941
- })["catch"](function (error) {
942
- callback(error);
943
- });
944
- }
945
- }]);
946
- return EJS;
947
- }();
662
+ return content;
663
+ });
664
+ };
665
+ this.configure = function (options) {
666
+ options = options || {};
667
+ configSchema(config, options);
668
+ context.configure(config, scope);
669
+ compiler.configure(config);
670
+ cache.configure(config);
671
+ template.configure(config);
672
+ return config;
673
+ };
674
+ this.render = function (name, data) {
675
+ return render(name, data);
676
+ };
677
+ this.helpers = function (methods) {
678
+ context.helpers(Object.assign(scope, methods || {}));
679
+ };
680
+ this.preload = function (list) {
681
+ return cache.load(list || {});
682
+ };
683
+ this.create = function (options) {
684
+ return new EJS(options);
685
+ };
686
+ this.compile = function (content, path) {
687
+ return compiler.compile(content, path);
688
+ };
689
+ this.context = function (data) {
690
+ return context.create(data);
691
+ };
692
+ this.helpers({
693
+ require: require,
694
+ render: render
695
+ });
696
+ }
948
697
 
949
- var ejs = new EJS({
950
- /** defaults options **/
951
- });
952
- var __express = ejs.__express,
953
- render = ejs.render,
698
+ var ejs = new EJS();
699
+ var render = ejs.render,
954
700
  context = ejs.context,
955
701
  compile = ejs.compile,
956
702
  helpers = ejs.helpers,
@@ -958,6 +704,27 @@ var __express = ejs.__express,
958
704
  configure = ejs.configure,
959
705
  create = ejs.create;
960
706
 
707
+ function __express(name, options, callback) {
708
+ if (isFunction(options)) {
709
+ callback = options;
710
+ options = {};
711
+ }
712
+ options = options || {};
713
+ var settings = extend({}, options.settings);
714
+ var viewPath = typeProp(isString, defaults.path, settings['views']);
715
+ var viewCache = typeProp(isBoolean, defaults.cache, settings['view cache']);
716
+ var viewOptions = extend({}, settings['view options']);
717
+ var filename = path.relative(viewPath, name);
718
+ viewOptions.path = viewPath;
719
+ viewOptions.cache = viewCache;
720
+ configure(viewOptions);
721
+ return this.render(filename, options).then(function (content) {
722
+ callback(null, content);
723
+ })["catch"](function (error) {
724
+ callback(error);
725
+ });
726
+ }
727
+
961
728
  exports.EJS = EJS;
962
729
  exports.__express = __express;
963
730
  exports.compile = compile;