@kosatyi/ejs 0.0.25 → 0.0.27

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/ejs.cjs CHANGED
@@ -1,14 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var path = require('path');
4
6
  var fs = require('fs');
5
- var chokidar = require('chokidar');
7
+ require('chokidar');
6
8
 
7
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
10
 
9
11
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
10
12
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
11
- var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
12
13
 
13
14
  var defaults = {};
14
15
  defaults["export"] = 'ejs.precompiled';
@@ -168,7 +169,7 @@ var equal = '=';
168
169
  var slash = '/';
169
170
  var lt = '<';
170
171
  var gt = '>';
171
- function element(tag, attrs, content) {
172
+ var element = function element(tag, attrs, content) {
172
173
  var result = [];
173
174
  var hasClosedTag = selfClosed.indexOf(tag) === -1;
174
175
  var attributes = map(attrs, function (value, key) {
@@ -184,9 +185,60 @@ function element(tag, attrs, content) {
184
185
  result.push([lt, slash, tag, gt].join(''));
185
186
  }
186
187
  return result.join('');
188
+ };
189
+
190
+ function _classCallCheck(instance, Constructor) {
191
+ if (!(instance instanceof Constructor)) {
192
+ throw new TypeError("Cannot call a class as a function");
193
+ }
194
+ }
195
+ function _defineProperties(target, props) {
196
+ for (var i = 0; i < props.length; i++) {
197
+ var descriptor = props[i];
198
+ descriptor.enumerable = descriptor.enumerable || false;
199
+ descriptor.configurable = true;
200
+ if ("value" in descriptor) descriptor.writable = true;
201
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
202
+ }
203
+ }
204
+ function _createClass(Constructor, protoProps, staticProps) {
205
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
206
+ if (staticProps) _defineProperties(Constructor, staticProps);
207
+ Object.defineProperty(Constructor, "prototype", {
208
+ writable: false
209
+ });
210
+ return Constructor;
211
+ }
212
+ function _defineProperty(obj, key, value) {
213
+ key = _toPropertyKey(key);
214
+ if (key in obj) {
215
+ Object.defineProperty(obj, key, {
216
+ value: value,
217
+ enumerable: true,
218
+ configurable: true,
219
+ writable: true
220
+ });
221
+ } else {
222
+ obj[key] = value;
223
+ }
224
+ return obj;
225
+ }
226
+ function _toPrimitive(input, hint) {
227
+ if (typeof input !== "object" || input === null) return input;
228
+ var prim = input[Symbol.toPrimitive];
229
+ if (prim !== undefined) {
230
+ var res = prim.call(input, hint || "default");
231
+ if (typeof res !== "object") return res;
232
+ throw new TypeError("@@toPrimitive must return a primitive value.");
233
+ }
234
+ return (hint === "string" ? String : Number)(input);
235
+ }
236
+ function _toPropertyKey(arg) {
237
+ var key = _toPrimitive(arg, "string");
238
+ return typeof key === "symbol" ? key : String(key);
187
239
  }
188
240
 
189
- var tags = [{
241
+ var tagList = [{
190
242
  symbol: '-',
191
243
  format: function format(value) {
192
244
  return "')\n".concat(this.BUFFER, "(").concat(this.SAFE, "(").concat(value, ",1))\n").concat(this.BUFFER, "('");
@@ -207,7 +259,7 @@ var tags = [{
207
259
  return "')\n".concat(value.trim(), "\n").concat(this.BUFFER, "('");
208
260
  }
209
261
  }];
210
- var match = function match(regex, text, callback) {
262
+ var matchTokens = function matchTokens(regex, text, callback) {
211
263
  var index = 0;
212
264
  text.replace(regex, function () {
213
265
  var params = [].slice.call(arguments, 0, -1);
@@ -218,78 +270,105 @@ var match = function match(regex, text, callback) {
218
270
  return match;
219
271
  });
220
272
  };
221
- var configureCompiler = function configureCompiler(ejs, config) {
222
- var withObject = config.withObject;
223
- var token = config.token;
224
- var vars = config.vars;
225
- var matches = [];
226
- var formats = [];
227
- var slurp = {
228
- match: '[ \\t]*',
229
- start: [token.start, '_'],
230
- end: ['_', token.end]
231
- };
232
- tags.forEach(function (item) {
233
- matches.push(token.start.concat(item.symbol).concat(token.regex).concat(token.end));
234
- formats.push(item.format.bind(vars));
235
- });
236
- var regex = new RegExp(matches.join('|').concat('|$'), 'g');
237
- var slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
238
- var slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
239
- return function compiler(content, path) {
240
- var SCOPE = vars.SCOPE,
241
- SAFE = vars.SAFE,
242
- BUFFER = vars.BUFFER;
243
- content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
244
- content = content.replace(slurpStart, slurp.start).replace(slurpEnd, slurp.end);
245
- var source = "".concat(BUFFER, "('");
246
- match(regex, content, function (params, index, offset) {
247
- source += symbols(content.slice(index, offset));
248
- params.forEach(function (value, index) {
249
- if (value) source += formats[index](value);
273
+ var Compiler = /*#__PURE__*/function () {
274
+ function Compiler(config) {
275
+ _classCallCheck(this, Compiler);
276
+ this.configure(config);
277
+ }
278
+ _createClass(Compiler, [{
279
+ key: "configure",
280
+ value: function configure(config) {
281
+ var _this = this;
282
+ this.withObject = config.withObject;
283
+ this.token = config.token;
284
+ this.vars = config.vars;
285
+ this.matches = [];
286
+ this.formats = [];
287
+ this.slurp = {
288
+ match: '[ \\t]*',
289
+ start: [this.token.start, '_'],
290
+ end: ['_', this.token.end]
291
+ };
292
+ tagList.forEach(function (item) {
293
+ _this.matches.push(_this.token.start.concat(item.symbol).concat(_this.token.regex).concat(_this.token.end));
294
+ _this.formats.push(item.format.bind(_this.vars));
250
295
  });
251
- });
252
- source += "');";
253
- source = "try{".concat(source, "}catch(e){console.info(e)}");
254
- if (withObject) {
255
- source = "with(".concat(SCOPE, "){").concat(source, "}");
296
+ this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
297
+ this.slurpStart = new RegExp([this.slurp.match, this.slurp.start].join(''), 'gm');
298
+ this.slurpEnd = new RegExp([this.slurp.end, this.slurp.match].join(''), 'gm');
256
299
  }
257
- source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
258
- source += "\n//# sourceURL=".concat(path);
259
- var result = null;
260
- try {
261
- result = new Function(SCOPE, BUFFER, SAFE, source);
262
- result.source = "(function(".concat(SCOPE, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})");
263
- } catch (e) {
264
- e.filename = path;
265
- e.source = source;
266
- throw e;
300
+ }, {
301
+ key: "compile",
302
+ value: function compile(content, path) {
303
+ var _this2 = this;
304
+ var _this$vars = this.vars,
305
+ SCOPE = _this$vars.SCOPE,
306
+ SAFE = _this$vars.SAFE,
307
+ BUFFER = _this$vars.BUFFER;
308
+ content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
309
+ content = content.replace(this.slurpStart, this.slurp.start).replace(this.slurpEnd, this.slurp.end);
310
+ var source = "".concat(BUFFER, "('");
311
+ matchTokens(this.regex, content, function (params, index, offset) {
312
+ source += symbols(content.slice(index, offset));
313
+ params.forEach(function (value, index) {
314
+ if (value) source += _this2.formats[index](value);
315
+ });
316
+ });
317
+ source += "');";
318
+ source = "try{".concat(source, "}catch(e){console.info(e)}");
319
+ if (this.withObject) {
320
+ source = "with(".concat(SCOPE, "){").concat(source, "}");
321
+ }
322
+ source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
323
+ source += "\n//# sourceURL=".concat(path);
324
+ var result = null;
325
+ try {
326
+ result = new Function(SCOPE, BUFFER, SAFE, source);
327
+ result.source = "(function(".concat(SCOPE, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})");
328
+ } catch (e) {
329
+ e.filename = path;
330
+ e.source = source;
331
+ throw e;
332
+ }
333
+ return result;
267
334
  }
268
- return result;
269
- };
270
- };
335
+ }]);
336
+ return Compiler;
337
+ }();
271
338
 
272
- var configureWrapper = function configureWrapper(config) {
273
- var name = config["export"];
274
- var useStrict = config.withObject !== true;
275
- return function Wrapper(list) {
276
- var out = '';
277
- out += '(function(global,factory){';
278
- out += 'typeof exports === "object" && typeof module !== "undefined" ?';
279
- out += 'module.exports = factory():';
280
- out += 'typeof define === "function" && define.amd ? define(factory):';
281
- out += '(global = typeof globalThis !== "undefined" ? globalThis:';
282
- out += 'global || self,global["' + name + '"] = factory())';
283
- out += '})(this,(function(){';
284
- if (useStrict) out += "'use strict';\n";
285
- out += 'var list = {};\n';
286
- list.forEach(function (item) {
287
- out += 'list[' + JSON.stringify(item.name) + ']=' + String(item.content) + ';\n';
288
- });
289
- out += 'return list;}));\n';
290
- return out;
291
- };
292
- };
339
+ var Bundler = /*#__PURE__*/function () {
340
+ function Bundler(config) {
341
+ _classCallCheck(this, Bundler);
342
+ this.configure(config);
343
+ }
344
+ _createClass(Bundler, [{
345
+ key: "configure",
346
+ value: function configure(config) {
347
+ this.namespace = config["export"];
348
+ this.useStrict = config.withObject === false;
349
+ }
350
+ }, {
351
+ key: "wrapper",
352
+ value: function wrapper(list) {
353
+ var out = '';
354
+ out += '(function(global,factory){';
355
+ out += 'typeof exports === "object" && typeof module !== "undefined" ?';
356
+ out += 'module.exports = factory():';
357
+ out += 'typeof define === "function" && define.amd ? define(factory):';
358
+ out += '(global = typeof globalThis !== "undefined" ? globalThis:';
359
+ out += 'global || self,global["' + this.namespace + '"] = factory())';
360
+ out += '})(this,(function(){';
361
+ if (this.useStrict) out += "'use strict';\n";
362
+ out += 'var list = {};\n';
363
+ list.forEach(function (item) {
364
+ out += 'list[' + JSON.stringify(item.name) + ']=' + String(item.content) + ';\n';
365
+ });
366
+ out += 'return list;}));\n';
367
+ return out;
368
+ }
369
+ }]);
370
+ return Bundler;
371
+ }();
293
372
 
294
373
  var httpRequest = function httpRequest(template) {
295
374
  return fetch(template).then(function (response) {
@@ -307,46 +386,50 @@ var fileSystem = function fileSystem(template) {
307
386
  });
308
387
  });
309
388
  };
310
- var enableWatcher = function enableWatcher(path, cache) {
311
- return chokidar__default["default"].watch('.', {
312
- cwd: path
313
- }).on('change', function (name) {
314
- cache.remove(name);
315
- }).on('error', function (error) {
316
- console.log('watcher error: ' + error);
317
- });
318
- };
319
- var configureTemplate = function configureTemplate(ejs, config) {
320
- var path = config.path;
321
- var cache = ejs.cache,
322
- compile = ejs.compile;
323
- var resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
324
- var normalize = function normalize(template) {
325
- template = [path, template].join('/');
326
- template = template.replace(/\/\//g, '/');
327
- return template;
328
- };
329
- var resolve = function resolve(template) {
330
- return resolver(normalize(template));
331
- };
332
- var result = function result(content, template) {
333
- cache.set(template, content);
334
- return content;
335
- };
336
- var template = function template(_template) {
337
- if (cache.exist(_template)) {
338
- return cache.resolve(_template);
339
- }
340
- var content = resolve(_template).then(function (content) {
341
- return result(compile(content, _template), _template);
342
- });
343
- return result(content, _template);
344
- };
345
- if (config.watch && isNode()) {
346
- enableWatcher(path, cache);
347
- }
389
+ var normalizePath = function normalizePath(path, template) {
390
+ template = [path, template].join('/');
391
+ template = template.replace(/\/\//g, '/');
348
392
  return template;
349
393
  };
394
+ var Template = /*#__PURE__*/function () {
395
+ function Template(config, cache, compiler) {
396
+ _classCallCheck(this, Template);
397
+ this.cache = cache;
398
+ this.compiler = compiler;
399
+ this.configure(config);
400
+ }
401
+ _createClass(Template, [{
402
+ key: "configure",
403
+ value: function configure(config) {
404
+ this.path = config.path;
405
+ this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
406
+ }
407
+ }, {
408
+ key: "resolve",
409
+ value: function resolve(template) {
410
+ return this.resolver(normalizePath(this.path, template));
411
+ }
412
+ }, {
413
+ key: "result",
414
+ value: function result(content, template) {
415
+ this.cache.set(template, content);
416
+ return content;
417
+ }
418
+ }, {
419
+ key: "get",
420
+ value: function get(template) {
421
+ var _this = this;
422
+ if (this.cache.exist(template)) {
423
+ return this.cache.resolve(template);
424
+ }
425
+ var content = this.resolve(template).then(function (content) {
426
+ return _this.result(_this.compiler.compile(content, template), template);
427
+ });
428
+ return this.result(content, template);
429
+ }
430
+ }]);
431
+ return Template;
432
+ }();
350
433
 
351
434
  var resolve = function resolve(list) {
352
435
  return Promise.all(list).then(function (list) {
@@ -380,325 +463,376 @@ var createBuffer = function createBuffer() {
380
463
  return buffer;
381
464
  };
382
465
 
383
- var configureScope = function configureScope(ejs, config) {
384
- var _config$vars = config.vars,
385
- EXTEND = _config$vars.EXTEND,
386
- LAYOUT = _config$vars.LAYOUT,
387
- BLOCKS = _config$vars.BLOCKS,
388
- BUFFER = _config$vars.BUFFER,
389
- MACRO = _config$vars.MACRO;
390
- function Scope() {
391
- var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
392
- this.initBlocks();
393
- this.initMacro();
394
- extend(this, data);
466
+ var Context = /*#__PURE__*/function () {
467
+ function Context(config) {
468
+ _classCallCheck(this, Context);
469
+ this.configure(config);
395
470
  }
396
- Scope.helpers = function (methods) {
397
- extend(Scope.prototype, methods);
398
- };
399
- Scope.property = function (name, descriptor) {
400
- Object.defineProperty(Scope.prototype, name, descriptor);
401
- };
402
- Scope.method = function (name, method) {
403
- Object.defineProperty(Scope.prototype, name, {
404
- value: method,
405
- writable: false,
406
- configurable: false,
407
- enumerable: false
408
- });
409
- };
410
- Scope.property(BUFFER, {
411
- value: createBuffer(),
412
- writable: false,
413
- configurable: false,
414
- enumerable: false
415
- });
416
- Scope.property(BLOCKS, {
417
- value: {},
418
- writable: true,
419
- configurable: false,
420
- enumerable: false
421
- });
422
- Scope.property(MACRO, {
423
- value: {},
424
- writable: true,
425
- configurable: false,
426
- enumerable: false
427
- });
428
- Scope.property(LAYOUT, {
429
- value: false,
430
- writable: true,
431
- configurable: false,
432
- enumerable: false
433
- });
434
- Scope.property(EXTEND, {
435
- value: false,
436
- writable: true,
437
- configurable: false,
438
- enumerable: false
439
- });
440
- Scope.method('initBlocks', function () {
441
- this[BLOCKS] = {};
442
- });
443
- Scope.method('initMacro', function () {
444
- this[MACRO] = {};
445
- });
446
- Scope.method('getMacro', function () {
447
- return this[MACRO];
448
- });
449
- Scope.method('getBuffer', function () {
450
- return this[BUFFER];
451
- });
452
- Scope.method('getBlocks', function () {
453
- return this[BLOCKS];
454
- });
455
- Scope.method('setExtend', function (value) {
456
- this[EXTEND] = value;
457
- });
458
- Scope.method('getExtend', function () {
459
- return this[EXTEND];
460
- });
461
- Scope.method('setLayout', function (layout) {
462
- this[LAYOUT] = layout;
463
- });
464
- Scope.method('getLayout', function () {
465
- return this[LAYOUT];
466
- });
467
- Scope.method('clone', function (exclude_blocks) {
468
- var filter = [LAYOUT, EXTEND, BUFFER];
469
- if (exclude_blocks === true) {
470
- filter.push(BLOCKS);
471
- }
472
- return omit(this, filter);
473
- });
474
- Scope.method('extend', function (layout) {
475
- this.setExtend(true);
476
- this.setLayout(layout);
477
- });
478
- Scope.method('echo', function () {
479
- var buffer = this.getBuffer();
480
- var params = [].slice.call(arguments);
481
- params.forEach(function (item) {
482
- buffer(item);
483
- });
484
- });
485
- Scope.method('fn', function (callback) {
486
- var buffer = this.getBuffer();
487
- var context = this;
488
- return function () {
489
- buffer.backup();
490
- if (isFunction(callback)) {
491
- callback.apply(context, arguments);
471
+ _createClass(Context, [{
472
+ key: "configure",
473
+ value: function configure(config) {
474
+ var _config$vars = config.vars,
475
+ EXTEND = _config$vars.EXTEND,
476
+ LAYOUT = _config$vars.LAYOUT,
477
+ BLOCKS = _config$vars.BLOCKS,
478
+ BUFFER = _config$vars.BUFFER,
479
+ MACRO = _config$vars.MACRO;
480
+ this.create = function (data) {
481
+ return new Scope(data);
482
+ };
483
+ this.helpers = function (methods) {
484
+ extend(Scope.prototype, methods);
485
+ };
486
+ function Scope() {
487
+ var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
488
+ this.initBlocks();
489
+ this.initMacro();
490
+ extend(this, data);
492
491
  }
493
- return buffer.restore();
494
- };
495
- });
496
- Scope.method('get', function (name, defaults) {
497
- var path = getPath(this, name);
498
- var result = path.shift();
499
- var prop = path.pop();
500
- return hasProp(result, prop) ? result[prop] : defaults;
501
- });
502
- Scope.method('set', function (name, value) {
503
- var path = getPath(this, name);
504
- var result = path.shift();
505
- var prop = path.pop();
506
- if (this.getExtend() && hasProp(result, prop)) {
507
- return result[prop];
508
- }
509
- return result[prop] = value;
510
- });
511
- Scope.method('macro', function (name, callback) {
512
- var list = this.getMacro();
513
- var macro = this.fn(callback);
514
- var context = this;
515
- list[name] = function () {
516
- return context.echo(macro.apply(undefined, arguments));
517
- };
518
- });
519
- Scope.method('call', function (name) {
520
- var list = this.getMacro();
521
- var macro = list[name];
522
- var params = [].slice.call(arguments, 1);
523
- if (isFunction(macro)) {
524
- return macro.apply(macro, params);
525
- }
526
- });
527
- Scope.method('block', function (name, callback) {
528
- var _this = this;
529
- var blocks = this.getBlocks();
530
- blocks[name] = blocks[name] || [];
531
- blocks[name].push(this.fn(callback));
532
- if (this.getExtend()) return;
533
- var list = Object.assign([], blocks[name]);
534
- var current = function current() {
535
- return list.shift();
536
- };
537
- var next = function next() {
538
- var parent = current();
539
- if (parent) {
492
+ Scope.helpers = function (methods) {
493
+ extend(Scope.prototype, methods);
494
+ };
495
+ Scope.defineProp = Scope.method = function (name, value) {
496
+ var writable = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
497
+ var configurable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
498
+ var enumerable = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
499
+ Object.defineProperty(Scope.prototype, name, {
500
+ value: value,
501
+ writable: writable,
502
+ configurable: configurable,
503
+ enumerable: enumerable
504
+ });
505
+ };
506
+ Scope.defineProp(BUFFER, createBuffer());
507
+ Scope.defineProp(BLOCKS, {}, true);
508
+ Scope.defineProp(MACRO, {}, true);
509
+ Scope.defineProp(LAYOUT, false, true);
510
+ Scope.defineProp(EXTEND, false, true);
511
+ Scope.method('initBlocks', function () {
512
+ this[BLOCKS] = {};
513
+ });
514
+ Scope.method('initMacro', function () {
515
+ this[MACRO] = {};
516
+ });
517
+ Scope.method('getMacro', function () {
518
+ return this[MACRO];
519
+ });
520
+ Scope.method('getBuffer', function () {
521
+ return this[BUFFER];
522
+ });
523
+ Scope.method('getBlocks', function () {
524
+ return this[BLOCKS];
525
+ });
526
+ Scope.method('setExtend', function (value) {
527
+ this[EXTEND] = value;
528
+ });
529
+ Scope.method('getExtend', function () {
530
+ return this[EXTEND];
531
+ });
532
+ Scope.method('setLayout', function (layout) {
533
+ this[LAYOUT] = layout;
534
+ });
535
+ Scope.method('getLayout', function () {
536
+ return this[LAYOUT];
537
+ });
538
+ Scope.method('clone', function (exclude_blocks) {
539
+ var filter = [LAYOUT, EXTEND, BUFFER];
540
+ if (exclude_blocks === true) {
541
+ filter.push(BLOCKS);
542
+ }
543
+ return omit(this, filter);
544
+ });
545
+ Scope.method('extend', function (layout) {
546
+ this.setExtend(true);
547
+ this.setLayout(layout);
548
+ });
549
+ Scope.method('echo', function () {
550
+ var buffer = this.getBuffer();
551
+ var params = [].slice.call(arguments);
552
+ params.forEach(buffer);
553
+ });
554
+ Scope.method('fn', function (callback) {
555
+ var buffer = this.getBuffer();
556
+ var context = this;
540
557
  return function () {
541
- _this.echo(parent(next()));
558
+ buffer.backup();
559
+ if (isFunction(callback)) {
560
+ callback.apply(context, arguments);
561
+ }
562
+ return buffer.restore();
542
563
  };
543
- } else {
544
- return noop;
545
- }
546
- };
547
- this.echo(current()(next()));
548
- });
549
- Scope.method('include', function (path, data, cx) {
550
- var context = cx === false ? {} : this.clone(true);
551
- var params = extend(context, data || {});
552
- var promise = this.render(path, params);
553
- this.echo(promise);
554
- });
555
- Scope.method('use', function (path, namespace) {
556
- var promise = this.require(path);
557
- this.echo(resolve$1(promise, function (exports) {
558
- var list = this.getMacro();
559
- each(exports, function (macro, name) {
560
- list[[namespace, name].join('.')] = macro;
561
564
  });
562
- }, this));
563
- });
564
- Scope.method('async', function (promise, callback) {
565
- this.echo(resolve$1(promise, function (data) {
566
- return this.fn(callback)(data);
567
- }, this));
568
- });
569
- Scope.method('el', function (tag, attr, content) {
570
- if (isFunction(content)) {
571
- content = this.fn(content)();
572
- }
573
- this.echo(resolve$1(content, function (content) {
574
- return element(tag, attr, content);
575
- }, this));
576
- });
577
- Scope.method('each', function (object, callback) {
578
- if (isString(object)) {
579
- object = this.get(object, []);
565
+ Scope.method('get', function (name, defaults) {
566
+ var path = getPath(this, name);
567
+ var result = path.shift();
568
+ var prop = path.pop();
569
+ return hasProp(result, prop) ? result[prop] : defaults;
570
+ });
571
+ Scope.method('set', function (name, value) {
572
+ var path = getPath(this, name);
573
+ var result = path.shift();
574
+ var prop = path.pop();
575
+ if (this.getExtend() && hasProp(result, prop)) {
576
+ return result[prop];
577
+ }
578
+ return result[prop] = value;
579
+ });
580
+ Scope.method('macro', function (name, callback) {
581
+ var list = this.getMacro();
582
+ var macro = this.fn(callback);
583
+ var context = this;
584
+ list[name] = function () {
585
+ return context.echo(macro.apply(undefined, arguments));
586
+ };
587
+ });
588
+ Scope.method('call', function (name) {
589
+ var list = this.getMacro();
590
+ var macro = list[name];
591
+ var params = [].slice.call(arguments, 1);
592
+ if (isFunction(macro)) {
593
+ return macro.apply(macro, params);
594
+ }
595
+ });
596
+ Scope.method('block', function (name, callback) {
597
+ var _this = this;
598
+ var blocks = this.getBlocks();
599
+ blocks[name] = blocks[name] || [];
600
+ blocks[name].push(this.fn(callback));
601
+ if (this.getExtend()) return;
602
+ var list = Object.assign([], blocks[name]);
603
+ var current = function current() {
604
+ return list.shift();
605
+ };
606
+ var next = function next() {
607
+ var parent = current();
608
+ if (parent) {
609
+ return function () {
610
+ _this.echo(parent(next()));
611
+ };
612
+ } else {
613
+ return noop;
614
+ }
615
+ };
616
+ this.echo(current()(next()));
617
+ });
618
+ Scope.method('include', function (path, data, cx) {
619
+ var context = cx === false ? {} : this.clone(true);
620
+ var params = extend(context, data || {});
621
+ var promise = this.render(path, params);
622
+ this.echo(promise);
623
+ });
624
+ Scope.method('use', function (path, namespace) {
625
+ var promise = this.require(path);
626
+ this.echo(resolve$1(promise, function (exports) {
627
+ var list = this.getMacro();
628
+ each(exports, function (macro, name) {
629
+ list[[namespace, name].join('.')] = macro;
630
+ });
631
+ }, this));
632
+ });
633
+ Scope.method('async', function (promise, callback) {
634
+ this.echo(resolve$1(promise, function (data) {
635
+ return this.fn(callback)(data);
636
+ }, this));
637
+ });
638
+ Scope.method('el', function (tag, attr, content) {
639
+ if (isFunction(content)) {
640
+ content = this.fn(content)();
641
+ }
642
+ this.echo(resolve$1(content, function (content) {
643
+ return element(tag, attr, content);
644
+ }, this));
645
+ });
646
+ Scope.method('each', function (object, callback) {
647
+ if (isString(object)) {
648
+ object = this.get(object, []);
649
+ }
650
+ each(object, callback);
651
+ });
580
652
  }
581
- each(object, callback);
582
- });
583
- return Scope;
584
- };
653
+ }]);
654
+ return Context;
655
+ }();
585
656
 
586
657
  var global = typeof globalThis !== 'undefined' ? globalThis : window || self;
587
- function configureCache(config) {
588
- var namespace = config["export"];
589
- var list = {};
590
- var cache = {
591
- preload: function preload() {
592
- if (isNode() === false) {
593
- this.load(global[namespace]);
594
- }
658
+ var Cache = /*#__PURE__*/function () {
659
+ function Cache(config) {
660
+ _classCallCheck(this, Cache);
661
+ _defineProperty(this, "list", {});
662
+ this.configure(config);
663
+ if (isNode() === false) {
664
+ this.load(global[this.namespace]);
665
+ }
666
+ }
667
+ _createClass(Cache, [{
668
+ key: "configure",
669
+ value: function configure(config) {
670
+ this.list = {};
671
+ this.namespace = config["export"];
672
+ }
673
+ }, {
674
+ key: "load",
675
+ value: function load(data) {
676
+ extend(this.list, data);
595
677
  return this;
596
- },
597
- exist: function exist(key) {
598
- return hasProp(list, key);
599
- },
600
- get: function get(key) {
601
- return list[key];
602
- },
603
- remove: function remove(key) {
604
- delete list[key];
605
- },
606
- resolve: function resolve(key) {
678
+ }
679
+ }, {
680
+ key: "exist",
681
+ value: function exist(key) {
682
+ return hasProp(this.list, key);
683
+ }
684
+ }, {
685
+ key: "get",
686
+ value: function get(key) {
687
+ return this.list[key];
688
+ }
689
+ }, {
690
+ key: "remove",
691
+ value: function remove(key) {
692
+ delete this.list[key];
693
+ }
694
+ }, {
695
+ key: "resolve",
696
+ value: function resolve(key) {
607
697
  return Promise.resolve(this.get(key));
608
- },
609
- set: function set(key, value) {
610
- list[key] = value;
611
- return this;
612
- },
613
- load: function load(data) {
614
- extend(list, data);
698
+ }
699
+ }, {
700
+ key: "set",
701
+ value: function set(key, value) {
702
+ this.list[key] = value;
615
703
  return this;
616
704
  }
617
- };
618
- return cache.preload();
619
- }
705
+ }]);
706
+ return Cache;
707
+ }();
620
708
 
621
- function create(options) {
709
+ var configSchema = function configSchema(config, options) {
710
+ extend(config, {
711
+ "export": typeProp(isString, defaults["export"], options["export"]),
712
+ path: typeProp(isString, defaults.path, options.path),
713
+ resolver: typeProp(isFunction, defaults.resolver, options.resolver),
714
+ extension: typeProp(isString, defaults.extension, options.extension),
715
+ withObject: typeProp(isBoolean, defaults.withObject, options.withObject),
716
+ token: extend({}, defaults.token, options.token),
717
+ vars: extend({}, defaults.vars, options.vars)
718
+ });
719
+ };
720
+ var init = function init(options) {
721
+ /**
722
+ * EJS template
723
+ * @module ejs
724
+ */
622
725
  var config = {};
623
- var ejs = {
624
- safeValue: safeValue,
625
- element: element,
626
- output: function output(path, scope) {
627
- return ejs.template(path).then(function (template) {
628
- return template.call(scope, scope, scope.getBuffer(), safeValue);
629
- });
630
- },
631
- render: function render(name, data) {
632
- var filepath = ext(name, config.extension);
633
- var scope = new ejs.scope(data);
634
- return ejs.output(filepath, scope).then(function (content) {
635
- if (scope.getExtend()) {
636
- scope.setExtend(false);
637
- var layout = scope.getLayout();
638
- var _data = scope.clone();
639
- return ejs.render(layout, _data);
640
- }
641
- return content;
642
- });
643
- },
644
- require: function require(name) {
645
- var filepath = ext(name, config.extension);
646
- var scope = new ejs.scope({});
647
- return ejs.output(filepath, scope).then(function () {
648
- return scope.getMacro();
649
- });
650
- },
651
- helpers: function helpers(methods) {
652
- ejs.scope.helpers(methods);
653
- },
654
- configure: function configure(options) {
655
- config["export"] = typeProp(isString, defaults["export"], options["export"]);
656
- config.path = typeProp(isString, defaults.path, options.path);
657
- config.resolver = typeProp(isFunction, defaults.resolver, options.resolver);
658
- config.extension = typeProp(isString, defaults.extension, options.extension);
659
- config.withObject = typeProp(isBoolean, defaults.withObject, options.withObject);
660
- config.token = extend({}, defaults.token, options.token);
661
- config.vars = extend({}, defaults.vars, options.vars);
662
- ejs.scope = configureScope(ejs, config);
663
- ejs.compile = configureCompiler(ejs, config);
664
- ejs.wrapper = configureWrapper(ejs);
665
- ejs.cache = configureCache(ejs);
666
- ejs.template = configureTemplate(ejs, config);
667
- return ejs;
668
- },
669
- __express: function __express(name, options, callback) {
670
- if (isFunction(options)) {
671
- callback = options;
672
- options = {};
726
+ configSchema(config, options || {});
727
+ var context = new Context(config);
728
+ var compiler = new Compiler(config);
729
+ var bundler = new Bundler(config);
730
+ var cache = new Cache(config);
731
+ var template = new Template(config, cache, compiler);
732
+ var configure = function configure(options) {
733
+ configSchema(config, options);
734
+ context.configure(config);
735
+ compiler.configure(config);
736
+ bundler.configure(config);
737
+ cache.configure(config);
738
+ template.configure(config);
739
+ };
740
+ var output = function output(path, scope) {
741
+ return template.get(path).then(function (callback) {
742
+ return callback.call(scope, scope, scope.getBuffer(), safeValue);
743
+ });
744
+ };
745
+ var _require = function require(name) {
746
+ var filepath = ext(name, config.extension);
747
+ var scope = context.create({});
748
+ return output(filepath, scope).then(function () {
749
+ return scope.getMacro();
750
+ });
751
+ };
752
+ var _render = function render(name, data) {
753
+ var filepath = ext(name, config.extension);
754
+ var scope = context.create(data);
755
+ return output(filepath, scope).then(function (content) {
756
+ if (scope.getExtend()) {
757
+ scope.setExtend(false);
758
+ var layout = scope.getLayout();
759
+ var _data = scope.clone();
760
+ return _render(layout, _data);
673
761
  }
674
- options = options || {};
675
- var settings = extend({}, options.settings);
676
- var viewPath = typeProp(isString, settings['views'], defaults.path);
677
- var viewCache = typeProp(isBoolean, settings['view cache'], defaults.cache);
678
- var viewOptions = extend({}, settings['view options']);
679
- var filename = path__default["default"].relative(viewPath, name);
680
- viewOptions.path = viewPath;
681
- viewOptions.cache = viewCache;
682
- ejs.configure(viewOptions);
683
- return ejs.render(filename, options).then(function (content) {
684
- callback(null, content);
685
- })["catch"](function (error) {
686
- callback(error);
687
- });
762
+ return content;
763
+ });
764
+ };
765
+ var helpers = function helpers(methods) {
766
+ context.helpers(extend(callbacks, methods || {}));
767
+ };
768
+ var __express = function __express(name, options, callback) {
769
+ if (isFunction(options)) {
770
+ callback = options;
771
+ options = {};
688
772
  }
773
+ options = options || {};
774
+ var settings = extend({}, options.settings);
775
+ var viewPath = typeProp(isString, settings['views'], defaults.path);
776
+ var viewCache = typeProp(isBoolean, settings['view cache'], defaults.cache);
777
+ var viewOptions = extend({}, settings['view options']);
778
+ var filename = path__default["default"].relative(viewPath, name);
779
+ viewOptions.path = viewPath;
780
+ viewOptions.cache = viewCache;
781
+ configure(viewOptions);
782
+ return _render(filename, options).then(function (content) {
783
+ callback(null, content);
784
+ })["catch"](function (error) {
785
+ callback(error);
786
+ });
787
+ };
788
+ var preload = function preload(list) {
789
+ return cache.load(list);
790
+ };
791
+ var wrapper = function wrapper(list) {
792
+ return bundler.wrapper(list);
689
793
  };
690
- ejs.configure(options || {});
691
- ejs.helpers({
794
+ var compile = function compile(content, path) {
795
+ return compiler.compile(content, path);
796
+ };
797
+ var create = function create(options) {
798
+ return init(options);
799
+ };
800
+ helpers({
692
801
  require: function require(name) {
693
- return ejs.require(name, this);
802
+ return _require(name);
694
803
  },
695
804
  render: function render(name, data) {
696
- return ejs.render(name, data);
805
+ return _render(name, data);
697
806
  }
698
807
  });
699
- return ejs;
700
- }
701
- var instance = create();
702
- instance.create = create;
808
+ return {
809
+ render: _render,
810
+ helpers: helpers,
811
+ configure: configure,
812
+ wrapper: wrapper,
813
+ compile: compile,
814
+ create: create,
815
+ preload: preload,
816
+ __express: __express
817
+ };
818
+ };
819
+ var _init = init({}),
820
+ render = _init.render,
821
+ helpers = _init.helpers,
822
+ configure = _init.configure,
823
+ wrapper = _init.wrapper,
824
+ compile = _init.compile,
825
+ create = _init.create,
826
+ preload = _init.preload,
827
+ __express = _init.__express;
703
828
 
704
- module.exports = instance;
829
+ exports.__express = __express;
830
+ exports.compile = compile;
831
+ exports.configure = configure;
832
+ exports.create = create;
833
+ exports.element = element;
834
+ exports.helpers = helpers;
835
+ exports.preload = preload;
836
+ exports.render = render;
837
+ exports.safeValue = safeValue;
838
+ exports.wrapper = wrapper;