@kosatyi/ejs 0.0.110 → 0.0.112

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.
@@ -389,10 +389,10 @@ class EjsCompiler {
389
389
  const result = Function.apply(null, params.concat(OUTPUT));
390
390
  result.source = `(function(${params.join(',')}){\n${OUTPUT}\n});`;
391
391
  return result
392
- } catch (error) {
393
- error.filename = path;
394
- error.source = OUTPUT;
395
- throw error
392
+ } catch (e) {
393
+ e.filename = path;
394
+ e.source = OUTPUT;
395
+ error(0, e);
396
396
  }
397
397
  }
398
398
  }
@@ -504,13 +504,11 @@ const element = (tag, attrs, content) => {
504
504
  const resolve = (list) => {
505
505
  return Promise.all(list || [])
506
506
  .then((list) => list.join(''))
507
- .catch((e) => {
508
- return error(500, e)
509
- })
507
+ .catch((e) => error(0, e))
510
508
  };
511
509
 
512
510
  const reject = (e) => {
513
- return Promise.reject(error(500, e))
511
+ return Promise.reject(error(0, e))
514
512
  };
515
513
 
516
514
  const EjsBuffer = () => {
@@ -548,6 +546,7 @@ const EjsBuffer = () => {
548
546
  const PARENT = Symbol('EjsContext.parentTemplate');
549
547
 
550
548
  const createContext$1 = (config, methods) => {
549
+ const globals = config.globals || [];
551
550
  const {
552
551
  BLOCKS,
553
552
  MACRO,
@@ -559,40 +558,41 @@ const createContext$1 = (config, methods) => {
559
558
  COMPONENT,
560
559
  ELEMENT,
561
560
  } = config.vars;
562
- const globals = config.globals || [];
563
- function EjsContext(data) {
564
- this[PARENT] = null;
565
- this[BLOCKS] = {};
566
- this[MACRO] = {};
567
- Object.assign(
568
- this,
569
- omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]),
570
- );
561
+ class Context {
562
+ constructor(data) {
563
+ this[PARENT] = null;
564
+ this[BLOCKS] = {};
565
+ this[MACRO] = {};
566
+ Object.assign(
567
+ this,
568
+ omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]),
569
+ );
570
+ }
571
571
  }
572
- Object.defineProperty(EjsContext.prototype, BUFFER, {
573
- value: EjsBuffer(),
574
- });
575
- Object.defineProperty(EjsContext.prototype, BLOCKS, {
576
- value: {},
577
- writable: true,
578
- });
579
- Object.defineProperty(EjsContext.prototype, MACRO, {
580
- value: {},
581
- writable: true,
582
- });
583
- Object.defineProperty(EjsContext.prototype, LAYOUT, {
584
- value: false,
585
- writable: true,
586
- });
587
- Object.defineProperty(EjsContext.prototype, EXTEND, {
588
- value: false,
589
- writable: true,
590
- });
591
- Object.defineProperty(EjsContext.prototype, PARENT, {
592
- value: null,
593
- writable: true,
594
- });
595
- Object.defineProperties(EjsContext.prototype, {
572
+ Object.defineProperties(Context.prototype, {
573
+ [BUFFER]: {
574
+ value: EjsBuffer(),
575
+ },
576
+ [BLOCKS]: {
577
+ value: {},
578
+ writable: true,
579
+ },
580
+ [MACRO]: {
581
+ value: {},
582
+ writable: true,
583
+ },
584
+ [LAYOUT]: {
585
+ value: false,
586
+ writable: true,
587
+ },
588
+ [EXTEND]: {
589
+ value: false,
590
+ writable: true,
591
+ },
592
+ [PARENT]: {
593
+ value: null,
594
+ writable: true,
595
+ },
596
596
  setParentTemplate: {
597
597
  value(value) {
598
598
  this[PARENT] = value;
@@ -605,32 +605,34 @@ const createContext$1 = (config, methods) => {
605
605
  },
606
606
  },
607
607
  useEscapeValue: {
608
- get: () => escapeValue,
608
+ value() {
609
+ return escapeValue
610
+ },
609
611
  },
610
612
  useComponent: {
611
- get() {
613
+ value() {
612
614
  if (isFunction(this[COMPONENT])) {
613
615
  return this[COMPONENT].bind(this)
614
616
  } else {
615
617
  return function () {
616
- throw new Error(`${COMPONENT} must be a function`)
618
+ error(2, `${COMPONENT} must be a function`);
617
619
  }
618
620
  }
619
621
  },
620
622
  },
621
623
  useElement: {
622
- get() {
624
+ value() {
623
625
  if (isFunction(this[ELEMENT])) {
624
626
  return this[ELEMENT].bind(this)
625
627
  } else {
626
628
  return () => {
627
- throw new Error(`${ELEMENT} must be a function`)
629
+ error(2, `${ELEMENT} must be a function`);
628
630
  }
629
631
  }
630
632
  },
631
633
  },
632
634
  useBuffer: {
633
- get() {
635
+ value() {
634
636
  return this[BUFFER]
635
637
  },
636
638
  },
@@ -683,17 +685,18 @@ const createContext$1 = (config, methods) => {
683
685
  },
684
686
  echo: {
685
687
  value() {
686
- return [].slice.call(arguments).forEach(this.useBuffer)
688
+ return [].slice.call(arguments).forEach(this.useBuffer())
687
689
  },
688
690
  },
689
691
  fn: {
690
692
  value(callback) {
693
+ const buffer = this.useBuffer();
691
694
  const context = this;
692
695
  return function () {
693
696
  if (isFunction(callback)) {
694
- context.useBuffer.backup();
695
- context.useBuffer(callback.apply(context, arguments));
696
- return context.useBuffer.restore()
697
+ buffer.backup();
698
+ buffer(callback.apply(context, arguments));
699
+ return buffer.restore()
697
700
  }
698
701
  }
699
702
  },
@@ -810,14 +813,22 @@ const createContext$1 = (config, methods) => {
810
813
  value() {},
811
814
  writable: true,
812
815
  },
816
+ require: {
817
+ value() {},
818
+ writable: true,
819
+ },
820
+ render: {
821
+ value() {},
822
+ writable: true,
823
+ },
813
824
  });
814
825
  Object.entries(methods).forEach(([name, value]) => {
815
826
  if (isFunction(value) && globals.includes(name)) {
816
- value = value.bind(EjsContext.prototype);
827
+ value = value.bind(Context.prototype);
817
828
  }
818
- EjsContext.prototype[name] = value;
829
+ Context.prototype[name] = value;
819
830
  });
820
- return EjsContext
831
+ return Context
821
832
  };
822
833
 
823
834
  class EjsContext {
@@ -855,6 +866,10 @@ class EjsInstance {
855
866
  'compile',
856
867
  'helpers',
857
868
  ]
869
+ /**
870
+ *
871
+ * @param {EjsConfig} options
872
+ */
858
873
  constructor(options = {}) {
859
874
  bindContext(this, this.constructor.exports);
860
875
  this.#methods = {};
@@ -917,10 +932,10 @@ class EjsInstance {
917
932
  .then((callback) =>
918
933
  callback.apply(data, [
919
934
  data,
920
- data.useComponent,
921
- data.useElement,
922
- data.useBuffer,
923
- data.useEscapeValue,
935
+ data.useComponent(),
936
+ data.useElement(),
937
+ data.useBuffer(),
938
+ data.useEscapeValue(),
924
939
  ]),
925
940
  )
926
941
  }
@@ -942,13 +957,13 @@ class EjsInstance {
942
957
  }
943
958
  }
944
959
 
945
- const httpRequest = (path, template, error) => {
960
+ const resolver = async (path, template, error) => {
946
961
  return fetch(joinPath(path, template)).then(
947
962
  (response) => {
948
963
  if (response.ok) return response.text()
949
- return error(404, `template ${template} not found`)
964
+ return error(1, `template ${template} not found`)
950
965
  },
951
- (reason) => error(500, reason),
966
+ (reason) => error(0, reason),
952
967
  )
953
968
  };
954
969
 
@@ -961,7 +976,7 @@ const {
961
976
  compile,
962
977
  preload,
963
978
  } = new EjsInstance({
964
- resolver: httpRequest,
979
+ resolver,
965
980
  });
966
981
 
967
- export { compile, configure, create, createContext, helpers, preload, render };
982
+ export { compile, configure, create, createContext, element, escapeValue, helpers, preload, render };
@@ -1,8 +1,8 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import globWatch from 'glob-watcher';
3
3
  import { join, dirname } from 'node:path';
4
- import { create } from './index.js';
5
4
  import { glob } from 'glob';
5
+ import { create } from './index.js';
6
6
 
7
7
  const symbolEntities = {
8
8
  "'": "'",
@@ -49,6 +49,7 @@ class EjsBundler {
49
49
  #ejsOptions
50
50
  #buildInProgress
51
51
  static exports = ['build', 'watch', 'concat', 'output']
52
+
52
53
  constructor(bundlerOptions = {}, ejsOptions = {}) {
53
54
  bindContext(this, this.constructor.exports);
54
55
  const { compile, configure } = create(ejsOptions);
@@ -58,14 +59,17 @@ class EjsBundler {
58
59
  this.#buildInProgress = false;
59
60
  this.#templates = {};
60
61
  }
62
+
61
63
  async #stageRead(path) {
62
64
  return fs
63
65
  .readFile(join(this.#ejsOptions.path, path))
64
66
  .then((response) => response.toString())
65
67
  }
68
+
66
69
  #stageCompile(content, name) {
67
70
  return this.#compile(content, name).source
68
71
  }
72
+
69
73
  #getBundle() {
70
74
  const umd = this.#bundlerOptions.umd;
71
75
  const strict = this.#ejsOptions.strict;
@@ -100,6 +104,7 @@ class EjsBundler {
100
104
  }
101
105
  return out.join('\n')
102
106
  }
107
+
103
108
  async build() {
104
109
  if (this.#buildInProgress === true) return false
105
110
  this.#buildInProgress = true;
@@ -108,6 +113,7 @@ class EjsBundler {
108
113
  console.log('✅', 'bundle complete:', this.#bundlerOptions.target);
109
114
  this.#buildInProgress = false;
110
115
  }
116
+
111
117
  async watch() {
112
118
  console.log('🔍', 'watch directory:', this.#ejsOptions.path);
113
119
  const pattern = '**/*.'.concat(this.#ejsOptions.extension);
@@ -128,6 +134,7 @@ class EjsBundler {
128
134
  });
129
135
  });
130
136
  }
137
+
131
138
  async concat() {
132
139
  const pattern = '**/*.'.concat(this.#ejsOptions.extension);
133
140
  const list = await glob(
@@ -142,6 +149,7 @@ class EjsBundler {
142
149
  this.#templates[template] = content;
143
150
  }
144
151
  }
152
+
145
153
  async output() {
146
154
  const target = [].concat(this.#bundlerOptions.target);
147
155
  const content = this.#getBundle();