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