@kosatyi/ejs 0.0.23 → 0.0.25

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
@@ -41,9 +41,9 @@ const isFunction = (v) => typeof v === 'function';
41
41
  const isString = (v) => typeof v === 'string';
42
42
  const isBoolean = (v) => typeof v === 'boolean';
43
43
 
44
- const isNode = new Function(
45
- 'try {return this===global;}catch(e){return false;}'
46
- );
44
+ const isNodeEnv = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
45
+
46
+ const isNode = () => isNodeEnv;
47
47
 
48
48
  const symbolEntities = {
49
49
  "'": "'",
@@ -98,6 +98,14 @@ const getPath = (context, name) => {
98
98
  return [data, prop]
99
99
  };
100
100
 
101
+ const ext = (path, defaults) => {
102
+ const ext = path.split('.').pop();
103
+ if (ext !== defaults) {
104
+ path = [path, defaults].join('.');
105
+ }
106
+ return path
107
+ };
108
+
101
109
  const extend = (...args) => {
102
110
  const target = args.shift();
103
111
  return args
@@ -213,25 +221,25 @@ const tags = [
213
221
  {
214
222
  symbol: '-',
215
223
  format(value) {
216
- return `'+\n${this.SAFE}(${value},1)+\n'`
224
+ return `')\n${this.BUFFER}(${this.SAFE}(${value},1))\n${this.BUFFER}('`
217
225
  },
218
226
  },
219
227
  {
220
228
  symbol: '=',
221
229
  format(value) {
222
- return `'+\n${this.SAFE}(${value})+\n'`
230
+ return `')\n${this.BUFFER}(${this.SAFE}(${value}))\n${this.BUFFER}('`
223
231
  },
224
232
  },
225
233
  {
226
234
  symbol: '#',
227
235
  format(value) {
228
- return `'+\n/**${value}**/+\n'`
236
+ return `')\n/**${value}**/\n${this.BUFFER}('`
229
237
  },
230
238
  },
231
239
  {
232
240
  symbol: '',
233
241
  format(value) {
234
- return `')\n${value}\n${this.BUFFER}('`
242
+ return `')\n${value.trim()}\n${this.BUFFER}('`
235
243
  },
236
244
  },
237
245
  ];
@@ -247,13 +255,8 @@ const match = (regex, text, callback) => {
247
255
  return match
248
256
  });
249
257
  };
250
- /**
251
- *
252
- * @param {object} config
253
- * @return {function(*, *): Function}
254
- * @constructor
255
- */
256
- const Compiler = (config) => {
258
+
259
+ const configureCompiler = (ejs, config) => {
257
260
  const withObject = config.withObject;
258
261
  const token = config.token;
259
262
  const vars = config.vars;
@@ -276,11 +279,7 @@ const Compiler = (config) => {
276
279
  const regex = new RegExp(matches.join('|').concat('|$'), 'g');
277
280
  const slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
278
281
  const slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
279
- /**
280
- * @type function
281
- * @name Compile
282
- */
283
- return function (content, path) {
282
+ return function compiler(content, path) {
284
283
  const { SCOPE, SAFE, BUFFER } = vars;
285
284
  content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
286
285
  content = content
@@ -295,7 +294,7 @@ const Compiler = (config) => {
295
294
  });
296
295
  source += `');`;
297
296
  source = `try{${source}}catch(e){console.info(e)}`;
298
- if(withObject) {
297
+ if (withObject) {
299
298
  source = `with(${SCOPE}){${source}}`;
300
299
  }
301
300
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
@@ -313,27 +312,38 @@ const Compiler = (config) => {
313
312
  }
314
313
  };
315
314
 
316
- const Wrapper = (config) => {
315
+ const configureWrapper = (config) => {
317
316
  const name = config.export;
318
- return function (list) {
319
- let out = '(function(o){\n';
317
+ const useStrict = config.withObject !== true;
318
+ return function Wrapper(list) {
319
+ let out = '';
320
+ out += '(function(global,factory){';
321
+ out += 'typeof exports === "object" && typeof module !== "undefined" ?';
322
+ out += 'module.exports = factory():';
323
+ out += 'typeof define === "function" && define.amd ? define(factory):';
324
+ out += '(global = typeof globalThis !== "undefined" ? globalThis:';
325
+ out += 'global || self,global["' + name + '"] = factory())';
326
+ out += '})(this,(function(){';
327
+ if (useStrict) out += "'use strict';\n";
328
+ out += 'var list = {};\n';
320
329
  list.forEach((item) => {
321
330
  out +=
322
- 'o[' +
331
+ 'list[' +
323
332
  JSON.stringify(item.name) +
324
333
  ']=' +
325
334
  String(item.content) +
326
- '\n';
335
+ ';\n';
327
336
  });
328
- out += '})(window["' + name + '"] = window["' + name + '"] || {});\n';
337
+ out += 'return list;}));\n';
329
338
  return out
330
339
  }
331
340
  };
332
341
 
333
- const HttpRequest = (template) =>
334
- window.fetch(template).then((response) => response.text());
342
+ const httpRequest = (template) => {
343
+ return fetch(template).then((response) => response.text())
344
+ };
335
345
 
336
- const FileSystem = (template) =>
346
+ const fileSystem = (template) =>
337
347
  new Promise((resolve, reject) => {
338
348
  fs.readFile(template, (error, data) => {
339
349
  if (error) {
@@ -344,7 +354,7 @@ const FileSystem = (template) =>
344
354
  });
345
355
  });
346
356
 
347
- const Watcher = (path, cache) =>
357
+ const enableWatcher = (path, cache) =>
348
358
  chokidar
349
359
  .watch('.', {
350
360
  cwd: path,
@@ -356,15 +366,14 @@ const Watcher = (path, cache) =>
356
366
  console.log('watcher error: ' + error);
357
367
  });
358
368
 
359
- const Template = (config, cache, compile) => {
369
+ const configureTemplate = (ejs, config) => {
360
370
  const path = config.path;
361
- config.token || {};
371
+ const { cache, compile } = ejs;
362
372
  const resolver = isFunction(config.resolver)
363
373
  ? config.resolver
364
374
  : isNode()
365
- ? FileSystem
366
- : HttpRequest;
367
-
375
+ ? fileSystem
376
+ : httpRequest;
368
377
  const normalize = (template) => {
369
378
  template = [path, template].join('/');
370
379
  template = template.replace(/\/\//g, '/');
@@ -377,7 +386,7 @@ const Template = (config, cache, compile) => {
377
386
  cache.set(template, content);
378
387
  return content
379
388
  };
380
- const get = (template) => {
389
+ const template = (template) => {
381
390
  if (cache.exist(template)) {
382
391
  return cache.resolve(template)
383
392
  }
@@ -387,17 +396,14 @@ const Template = (config, cache, compile) => {
387
396
  return result(content, template)
388
397
  };
389
398
  if (config.watch && isNode()) {
390
- Watcher(path, cache);
399
+ enableWatcher(path, cache);
391
400
  }
392
- return get
401
+ return template
393
402
  };
394
403
 
395
404
  const resolve = (list) => Promise.all(list).then((list) => list.join(''));
396
- /**
397
- *
398
- * @return {function}
399
- */
400
- const Buffer = () => {
405
+
406
+ const createBuffer = () => {
401
407
  let store = [],
402
408
  array = [];
403
409
  function buffer(value) {
@@ -415,7 +421,7 @@ const Buffer = () => {
415
421
  array = store.pop();
416
422
  return resolve(result)
417
423
  };
418
- buffer.error = function(e){
424
+ buffer.error = function (e) {
419
425
  throw e
420
426
  };
421
427
  buffer.end = function () {
@@ -424,7 +430,7 @@ const Buffer = () => {
424
430
  return buffer
425
431
  };
426
432
 
427
- const configure = (config, methods) => {
433
+ const configureScope = (ejs, config) => {
428
434
  const { EXTEND, LAYOUT, BLOCKS, BUFFER, MACRO } = config.vars;
429
435
  function Scope(data = {}) {
430
436
  this.initBlocks();
@@ -442,88 +448,88 @@ const configure = (config, methods) => {
442
448
  value: method,
443
449
  writable: false,
444
450
  configurable: false,
445
- enumerable: false
451
+ enumerable: false,
446
452
  });
447
453
  };
448
454
  Scope.property(BUFFER, {
449
- value: Buffer(),
455
+ value: createBuffer(),
450
456
  writable: false,
451
457
  configurable: false,
452
- enumerable: false
458
+ enumerable: false,
453
459
  });
454
460
  Scope.property(BLOCKS, {
455
461
  value: {},
456
462
  writable: true,
457
463
  configurable: false,
458
- enumerable: false
464
+ enumerable: false,
459
465
  });
460
466
  Scope.property(MACRO, {
461
467
  value: {},
462
468
  writable: true,
463
469
  configurable: false,
464
- enumerable: false
470
+ enumerable: false,
465
471
  });
466
472
  Scope.property(LAYOUT, {
467
473
  value: false,
468
474
  writable: true,
469
475
  configurable: false,
470
- enumerable: false
476
+ enumerable: false,
471
477
  });
472
478
  Scope.property(EXTEND, {
473
479
  value: false,
474
480
  writable: true,
475
481
  configurable: false,
476
- enumerable: false
482
+ enumerable: false,
477
483
  });
478
- Scope.method('initBlocks', function() {
484
+ Scope.method('initBlocks', function () {
479
485
  this[BLOCKS] = {};
480
486
  });
481
- Scope.method('initMacro', function() {
487
+ Scope.method('initMacro', function () {
482
488
  this[MACRO] = {};
483
489
  });
484
- Scope.method('getMacro', function() {
490
+ Scope.method('getMacro', function () {
485
491
  return this[MACRO]
486
492
  });
487
- Scope.method('getBuffer', function() {
493
+ Scope.method('getBuffer', function () {
488
494
  return this[BUFFER]
489
495
  });
490
- Scope.method('getBlocks', function() {
496
+ Scope.method('getBlocks', function () {
491
497
  return this[BLOCKS]
492
498
  });
493
- Scope.method('setExtend', function(value) {
499
+ Scope.method('setExtend', function (value) {
494
500
  this[EXTEND] = value;
495
501
  });
496
- Scope.method('getExtend', function() {
502
+ Scope.method('getExtend', function () {
497
503
  return this[EXTEND]
498
504
  });
499
- Scope.method('setLayout', function(layout) {
505
+ Scope.method('setLayout', function (layout) {
500
506
  this[LAYOUT] = layout;
501
507
  });
502
- Scope.method('getLayout', function() {
508
+ Scope.method('getLayout', function () {
503
509
  return this[LAYOUT]
504
510
  });
505
- Scope.method('clone', function(exclude_blocks) {
511
+ Scope.method('clone', function (exclude_blocks) {
506
512
  const filter = [LAYOUT, EXTEND, BUFFER];
507
513
  if (exclude_blocks === true) {
508
514
  filter.push(BLOCKS);
509
515
  }
510
516
  return omit(this, filter)
511
517
  });
512
- Scope.method('extend', function(layout) {
518
+ Scope.method('extend', function (layout) {
513
519
  this.setExtend(true);
514
520
  this.setLayout(layout);
515
521
  });
516
- Scope.method('echo', function() {
522
+ Scope.method('echo', function () {
517
523
  const buffer = this.getBuffer();
518
524
  const params = [].slice.call(arguments);
519
- params.forEach(function(item) {
525
+ params.forEach(function (item) {
520
526
  buffer(item);
521
527
  });
522
528
  });
523
- Scope.method('fn', function(callback) {
529
+ Scope.method('fn', function (callback) {
524
530
  const buffer = this.getBuffer();
525
531
  const context = this;
526
- return function() {
532
+ return function () {
527
533
  buffer.backup();
528
534
  if (isFunction(callback)) {
529
535
  callback.apply(context, arguments);
@@ -531,30 +537,30 @@ const configure = (config, methods) => {
531
537
  return buffer.restore()
532
538
  }
533
539
  });
534
- Scope.method('get', function(name,defaults) {
540
+ Scope.method('get', function (name, defaults) {
535
541
  const path = getPath(this, name);
536
542
  const result = path.shift();
537
543
  const prop = path.pop();
538
544
  return hasProp(result, prop) ? result[prop] : defaults
539
545
  });
540
- Scope.method('set', function(name,value) {
546
+ Scope.method('set', function (name, value) {
541
547
  const path = getPath(this, name);
542
548
  const result = path.shift();
543
549
  const prop = path.pop();
544
550
  if (this.getExtend() && hasProp(result, prop)) {
545
551
  return result[prop]
546
552
  }
547
- return result[prop] = value
553
+ return (result[prop] = value)
548
554
  });
549
- Scope.method('macro', function(name, callback) {
555
+ Scope.method('macro', function (name, callback) {
550
556
  const list = this.getMacro();
551
557
  const macro = this.fn(callback);
552
558
  const context = this;
553
- list[name] = function() {
559
+ list[name] = function () {
554
560
  return context.echo(macro.apply(undefined, arguments))
555
561
  };
556
562
  });
557
- Scope.method('call', function(name) {
563
+ Scope.method('call', function (name) {
558
564
  const list = this.getMacro();
559
565
  const macro = list[name];
560
566
  const params = [].slice.call(arguments, 1);
@@ -562,13 +568,13 @@ const configure = (config, methods) => {
562
568
  return macro.apply(macro, params)
563
569
  }
564
570
  });
565
- Scope.method('block',function(name,callback){
571
+ Scope.method('block', function (name, callback) {
566
572
  const blocks = this.getBlocks();
567
573
  blocks[name] = blocks[name] || [];
568
574
  blocks[name].push(this.fn(callback));
569
575
  if (this.getExtend()) return
570
576
  const list = Object.assign([], blocks[name]);
571
- const current = function() {
577
+ const current = function () {
572
578
  return list.shift()
573
579
  };
574
580
  const next = () => {
@@ -583,55 +589,70 @@ const configure = (config, methods) => {
583
589
  };
584
590
  this.echo(current()(next()));
585
591
  });
586
- Scope.method('include',function(path, data, cx){
592
+ Scope.method('include', function (path, data, cx) {
587
593
  const context = cx === false ? {} : this.clone(true);
588
594
  const params = extend(context, data || {});
589
595
  const promise = this.render(path, params);
590
596
  this.echo(promise);
591
597
  });
592
- Scope.method('use',function(path, namespace){
598
+ Scope.method('use', function (path, namespace) {
593
599
  const promise = this.require(path);
594
- this.echo(resolve$1(promise,function(exports){
595
- const list = this.getMacro();
596
- each(exports, function(macro, name) {
597
- list[[namespace, name].join('.')] = macro;
598
- });
599
- },this));
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
+ );
600
612
  });
601
- Scope.method('async',function(promise,callback){
613
+ Scope.method('async', function (promise, callback) {
602
614
  this.echo(
603
- resolve$1(promise, function(data) {
604
- return this.fn(callback)(data)
605
- }, this)
615
+ resolve$1(
616
+ promise,
617
+ function (data) {
618
+ return this.fn(callback)(data)
619
+ },
620
+ this
621
+ )
606
622
  );
607
623
  });
608
- Scope.method('el',function(tag, attr, content){
624
+ Scope.method('el', function (tag, attr, content) {
609
625
  if (isFunction(content)) {
610
626
  content = this.fn(content)();
611
627
  }
612
628
  this.echo(
613
- resolve$1(content, function(content) {
614
- return element(tag, attr, content)
615
- }, this)
629
+ resolve$1(
630
+ content,
631
+ function (content) {
632
+ return element(tag, attr, content)
633
+ },
634
+ this
635
+ )
616
636
  );
617
637
  });
618
- Scope.method('each',function(object, callback){
638
+ Scope.method('each', function (object, callback) {
619
639
  if (isString(object)) {
620
640
  object = this.get(object, []);
621
641
  }
622
642
  each(object, callback);
623
643
  });
624
- Scope.helpers(methods);
625
644
  return Scope
626
645
  };
627
646
 
628
- function Cache(config) {
647
+ const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
648
+
649
+ function configureCache(config) {
629
650
  const namespace = config.export;
630
651
  const list = {};
631
652
  const cache = {
632
653
  preload() {
633
654
  if (isNode() === false) {
634
- this.load(window[namespace]);
655
+ this.load(global[namespace]);
635
656
  }
636
657
  return this
637
658
  },
@@ -659,51 +680,38 @@ function Cache(config) {
659
680
  return cache.preload()
660
681
  }
661
682
 
662
- function init(options) {
663
- /**
664
- * @type {Object}
665
- */
683
+ function create(options) {
666
684
  const config = {};
667
- const helpers = {};
668
- const ext = function (path, defaults) {
669
- const ext = path.split('.').pop();
670
- if (ext !== defaults) {
671
- path = [path, defaults].join('.');
672
- }
673
- return path
674
- };
675
- const view = {
676
- safeValue,
677
- element,
685
+ const ejs = {
686
+ safeValue: safeValue,
687
+ element: element,
678
688
  output(path, scope) {
679
- return view.template(path).then(function (template) {
689
+ return ejs.template(path).then(function (template) {
680
690
  return template.call(scope, scope, scope.getBuffer(), safeValue)
681
691
  })
682
692
  },
683
693
  render(name, data) {
684
694
  const filepath = ext(name, config.extension);
685
- const scope = new view.scope(data);
686
- return view.output(filepath, scope).then((content) => {
695
+ const scope = new ejs.scope(data);
696
+ return ejs.output(filepath, scope).then((content) => {
687
697
  if (scope.getExtend()) {
688
698
  scope.setExtend(false);
689
699
  const layout = scope.getLayout();
690
700
  const data = scope.clone();
691
- return view.render(layout, data)
701
+ return ejs.render(layout, data)
692
702
  }
693
703
  return content
694
704
  })
695
705
  },
696
706
  require(name) {
697
707
  const filepath = ext(name, config.extension);
698
- const scope = new view.scope({});
699
- return view.output(filepath, scope).then(() => {
708
+ const scope = new ejs.scope({});
709
+ return ejs.output(filepath, scope).then(() => {
700
710
  return scope.getMacro()
701
711
  })
702
712
  },
703
713
  helpers(methods) {
704
- methods = methods || {};
705
- extend(helpers, methods);
706
- view.scope.helpers(methods);
714
+ ejs.scope.helpers(methods);
707
715
  },
708
716
  configure(options) {
709
717
  config.export = typeProp(isString, defaults.export, options.export);
@@ -725,12 +733,12 @@ function init(options) {
725
733
  );
726
734
  config.token = extend({}, defaults.token, options.token);
727
735
  config.vars = extend({}, defaults.vars, options.vars);
728
- view.scope = configure(config, helpers);
729
- view.compile = Compiler(config);
730
- view.wrapper = Wrapper(config);
731
- view.cache = Cache(config);
732
- view.template = Template(config, view.cache, view.compile);
733
- return view
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
734
742
  },
735
743
  __express(name, options, callback) {
736
744
  if (isFunction(options)) {
@@ -753,8 +761,8 @@ function init(options) {
753
761
  const filename = path.relative(viewPath, name);
754
762
  viewOptions.path = viewPath;
755
763
  viewOptions.cache = viewCache;
756
- view.configure(viewOptions);
757
- return view
764
+ ejs.configure(viewOptions);
765
+ return ejs
758
766
  .render(filename, options)
759
767
  .then((content) => {
760
768
  callback(null, content);
@@ -764,24 +772,20 @@ function init(options) {
764
772
  })
765
773
  },
766
774
  };
767
- /**
768
- *
769
- */
770
- view.configure(options || {});
771
- /**
772
- *
773
- */
774
- view.helpers({
775
+ ejs.configure(options || {});
776
+ ejs.helpers({
775
777
  require(name) {
776
- return view.require(name, this)
778
+ return ejs.require(name, this)
777
779
  },
778
780
  render(name, data) {
779
- return view.render(name, data)
781
+ return ejs.render(name, data)
780
782
  },
781
783
  });
782
- return view
784
+ return ejs
783
785
  }
784
786
 
785
- var index = init();
787
+ const instance = create();
788
+
789
+ instance.create = create;
786
790
 
787
- export { index as default };
791
+ export { instance as default };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@kosatyi/ejs",
3
3
  "description": "EJS Templates",
4
4
  "homepage": "https://github.com/kosatyi/ejs",
5
- "version": "0.0.23",
5
+ "version": "0.0.25",
6
6
  "main": "dist/ejs.cjs",
7
7
  "module": "dist/ejs.mjs",
8
8
  "browser": "dist/ejs.js",
package/dist/templates.js DELETED
@@ -1,2 +0,0 @@
1
- (function(o){
2
- })(window["ejs.precompiled"] = window["ejs.precompiled"] || {});