@kosatyi/ejs 0.0.54 → 0.0.55

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/esm/index.js CHANGED
@@ -1,38 +1,5 @@
1
- import path from 'path';
2
1
  import fs from 'fs';
3
-
4
- const defaults = {};
5
-
6
- defaults.export = 'ejsPrecompiled';
7
-
8
- defaults.watch = false;
9
-
10
- defaults.path = 'views';
11
-
12
- defaults.resolver = null;
13
-
14
- defaults.extension = 'ejs';
15
-
16
- defaults.rmWhitespace = true;
17
-
18
- defaults.withObject = true;
19
-
20
- defaults.vars = {
21
- SCOPE: 'ejs',
22
- COMPONENT: 'ui',
23
- EXTEND: '$$e',
24
- BUFFER: '$$a',
25
- LAYOUT: '$$l',
26
- BLOCKS: '$$b',
27
- MACRO: '$$m',
28
- SAFE: '$$v',
29
- };
30
-
31
- defaults.token = {
32
- start: '<%',
33
- end: '%>',
34
- regex: '([\\s\\S]+?)',
35
- };
2
+ import path from 'path';
36
3
 
37
4
  const typeProp = function () {
38
5
  const args = [].slice.call(arguments);
@@ -122,7 +89,8 @@ const extend = (...args) => {
122
89
  }, target)
123
90
  };
124
91
 
125
- const noop = () => {};
92
+ const noop = () => {
93
+ };
126
94
 
127
95
  const each = (object, callback) => {
128
96
  let prop;
@@ -179,50 +147,165 @@ const hasProp = (object, prop) => {
179
147
  return object && object.hasOwnProperty(prop)
180
148
  };
181
149
 
182
- const selfClosed = [
183
- 'area',
184
- 'base',
185
- 'br',
186
- 'col',
187
- 'embed',
188
- 'hr',
189
- 'img',
190
- 'input',
191
- 'link',
192
- 'meta',
193
- 'param',
194
- 'source',
195
- 'track',
196
- 'wbr',
197
- ];
150
+ const defaults = {};
198
151
 
199
- const space = ' ';
200
- const quote = '"';
201
- const equal = '=';
202
- const slash = '/';
203
- const lt = '<';
204
- const gt = '>';
152
+ defaults.export = 'ejsPrecompiled';
205
153
 
206
- const element = (tag, attrs, content) => {
207
- const result = [];
208
- const hasClosedTag = selfClosed.indexOf(tag) === -1;
209
- const attributes = map(attrs, (value, key) => {
210
- if (value !== null && value !== undefined) {
211
- return [
212
- entities(key),
213
- [quote, entities(value), quote].join(''),
214
- ].join(equal)
154
+ defaults.watch = false;
155
+
156
+ defaults.chokidar = null;
157
+
158
+ defaults.path = 'views';
159
+
160
+ defaults.resolver = null;
161
+
162
+ defaults.extension = 'ejs';
163
+
164
+ defaults.rmWhitespace = true;
165
+
166
+ defaults.withObject = true;
167
+
168
+ defaults.vars = {
169
+ SCOPE: 'ejs',
170
+ COMPONENT: 'ui',
171
+ EXTEND: '$$e',
172
+ BUFFER: '$$a',
173
+ LAYOUT: '$$l',
174
+ BLOCKS: '$$b',
175
+ MACRO: '$$m',
176
+ SAFE: '$$v',
177
+ };
178
+
179
+ defaults.token = {
180
+ start: '<%',
181
+ end: '%>',
182
+ regex: '([\\s\\S]+?)',
183
+ };
184
+
185
+ const configSchema = (config, options) => {
186
+ extend(config, {
187
+ path: typeProp(isString, defaults.path, config.path, options.path),
188
+ export: typeProp(
189
+ isString,
190
+ defaults.export,
191
+ config.export,
192
+ options.export
193
+ ),
194
+ resolver: typeProp(
195
+ isFunction,
196
+ defaults.resolver,
197
+ config.resolver,
198
+ options.resolver
199
+ ),
200
+ extension: typeProp(
201
+ isString,
202
+ defaults.extension,
203
+ config.extension,
204
+ options.extension
205
+ ),
206
+ withObject: typeProp(
207
+ isBoolean,
208
+ defaults.withObject,
209
+ config.withObject,
210
+ options.withObject
211
+ ),
212
+ rmWhitespace: typeProp(
213
+ isBoolean,
214
+ defaults.rmWhitespace,
215
+ config.rmWhitespace,
216
+ options.rmWhitespace
217
+ ),
218
+ watch: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
219
+ chokidar: typeProp(
220
+ isObject,
221
+ defaults.export,
222
+ config.export,
223
+ options.export
224
+ ),
225
+ token: extend({}, defaults.token, config.token, options.token),
226
+ vars: extend({}, defaults.vars, config.vars, options.vars),
227
+ });
228
+ };
229
+
230
+ const resolvePath = (path, template) => {
231
+ template = [path, template].join('/');
232
+ template = template.replace(/\/\//g, '/');
233
+ return template
234
+ };
235
+
236
+ const httpRequest = (path, template) => {
237
+ return fetch(resolvePath(path, template)).then((response) =>
238
+ response.text()
239
+ )
240
+ };
241
+
242
+ const fileSystem = (path, template) => {
243
+ return new Promise((resolve, reject) => {
244
+ fs.readFile(resolvePath(path, template), (error, data) => {
245
+ if (error) {
246
+ reject(error);
247
+ } else {
248
+ resolve(data.toString());
249
+ }
250
+ });
251
+ })
252
+ };
253
+
254
+ class Template {
255
+ constructor(config, cache, compiler) {
256
+ this.cache = cache;
257
+ this.watcher = {
258
+ unwatch() {},
259
+ on() {},
260
+ };
261
+ this.compiler = compiler;
262
+ this.configure(config);
263
+ }
264
+ configure(config) {
265
+ this.path = config.path;
266
+ this.chokidar = config.chokidar;
267
+ this.resolver = isFunction(config.resolver)
268
+ ? config.resolver
269
+ : isNode()
270
+ ? fileSystem
271
+ : httpRequest;
272
+ if (config.watch && isNode()) {
273
+ if (this.watcher) {
274
+ this.watcher.unwatch('.');
275
+ }
276
+ if (this.chokidar) {
277
+ this.watcher = this.chokidar
278
+ .watch('.', { cwd: this.path })
279
+ .on('change', (name) => {
280
+ this.cache.remove(name);
281
+ });
282
+ }
215
283
  }
216
- }).join(space);
217
- result.push([lt, tag, space, attributes, gt].join(''));
218
- if (content) {
219
- result.push(content instanceof Array ? content.join('') : content);
220
284
  }
221
- if (hasClosedTag) {
222
- result.push([lt, slash, tag, gt].join(''));
285
+ resolve(template) {
286
+ return this.resolver(this.path, template)
223
287
  }
224
- return result.join('')
225
- };
288
+ result(template, content) {
289
+ this.cache.set(template, content);
290
+ return content
291
+ }
292
+ compile(content, template) {
293
+ if (isFunction(content)) {
294
+ return content
295
+ } else {
296
+ return this.compiler.compile(content, template)
297
+ }
298
+ }
299
+ get(template) {
300
+ if (this.cache.exist(template)) {
301
+ return this.cache.resolve(template)
302
+ }
303
+ const content = this.resolve(template).then((content) =>
304
+ this.result(template, this.compile(content, template))
305
+ );
306
+ return this.result(template, content)
307
+ }
308
+ }
226
309
 
227
310
  const tagList = [
228
311
  {
@@ -343,84 +426,87 @@ class Compiler {
343
426
  }
344
427
  }
345
428
 
346
- const resolvePath = (path, template) => {
347
- template = [path, template].join('/');
348
- template = template.replace(/\/\//g, '/');
349
- return template
350
- };
351
-
352
- const httpRequest = (path, template) => {
353
- return fetch(resolvePath(path, template)).then((response) =>
354
- response.text()
355
- )
356
- };
357
-
358
- const fileSystem = (path, template) => {
359
- return new Promise((resolve, reject) => {
360
- fs.readFile(resolvePath(path, template), (error, data) => {
361
- if (error) {
362
- reject(error);
363
- } else {
364
- resolve(data.toString());
365
- }
366
- });
367
- })
368
- };
429
+ const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
369
430
 
370
- class Template {
371
- constructor(config, cache, compiler) {
372
- this.cache = cache;
373
- this.watcher = null;
374
- this.compiler = compiler;
431
+ class Cache {
432
+ list = {}
433
+ constructor(config) {
375
434
  this.configure(config);
435
+ if (isNode() === false) {
436
+ this.load(global[this.namespace]);
437
+ }
376
438
  }
377
439
  configure(config) {
378
- this.path = config.path;
379
- this.chokidar = config.chokidar;
380
- this.resolver = isFunction(config.resolver)
381
- ? config.resolver
382
- : isNode()
383
- ? fileSystem
384
- : httpRequest;
385
- if (config.watch && config.chokidar && isNode()) {
386
- if (this.watcher) {
387
- this.watcher.unwatch('.');
388
- }
389
- this.watcher = this.chokidar
390
- .watch('.', { cwd: this.path })
391
- .on('change', (name) => {
392
- this.cache.remove(name);
393
- })
394
- .on('error', (error) => {
395
- console.log('watcher error: ' + error);
396
- });
397
- }
440
+ this.list = {};
441
+ this.namespace = config.export;
398
442
  }
399
- resolve(template) {
400
- return this.resolver(this.path, template)
443
+ load(data) {
444
+ extend(this.list, data);
445
+ return this
401
446
  }
402
- result(template, content) {
403
- this.cache.set(template, content);
404
- return content
447
+ exist(key) {
448
+ return hasProp(this.list, key)
405
449
  }
406
- compile(content, template) {
407
- if (isFunction(content)) {
408
- return content
409
- } else {
410
- return this.compiler.compile(content, template)
411
- }
450
+ get(key) {
451
+ return this.list[key]
412
452
  }
413
- get(template) {
414
- if (this.cache.exist(template)) {
415
- return this.cache.resolve(template)
416
- }
417
- const content = this.resolve(template).then((content) =>
418
- this.result(template, this.compile(content, template))
419
- );
420
- return this.result(template, content)
453
+ remove(key) {
454
+ delete this.list[key];
455
+ }
456
+ resolve(key) {
457
+ return Promise.resolve(this.get(key))
458
+ }
459
+ set(key, value) {
460
+ this.list[key] = value;
461
+ return this
421
462
  }
422
463
  }
423
464
 
465
+ const selfClosed = [
466
+ 'area',
467
+ 'base',
468
+ 'br',
469
+ 'col',
470
+ 'embed',
471
+ 'hr',
472
+ 'img',
473
+ 'input',
474
+ 'link',
475
+ 'meta',
476
+ 'param',
477
+ 'source',
478
+ 'track',
479
+ 'wbr',
480
+ ];
481
+
482
+ const space = ' ';
483
+ const quote = '"';
484
+ const equal = '=';
485
+ const slash = '/';
486
+ const lt = '<';
487
+ const gt = '>';
488
+
489
+ const element = (tag, attrs, content) => {
490
+ const result = [];
491
+ const hasClosedTag = selfClosed.indexOf(tag) === -1;
492
+ const attributes = map(attrs, (value, key) => {
493
+ if (value !== null && value !== undefined) {
494
+ return [
495
+ entities(key),
496
+ [quote, entities(value), quote].join(''),
497
+ ].join(equal)
498
+ }
499
+ }).join(space);
500
+ result.push([lt, tag, space, attributes, gt].join(''));
501
+ if (content) {
502
+ result.push(content instanceof Array ? content.join('') : content);
503
+ }
504
+ if (hasClosedTag) {
505
+ result.push([lt, slash, tag, gt].join(''));
506
+ }
507
+ return result.join('')
508
+ };
509
+
424
510
  const resolve = (list) => Promise.all(list).then((list) => list.join(''));
425
511
 
426
512
  const createBuffer = () => {
@@ -699,109 +785,29 @@ class Context {
699
785
  }
700
786
  }
701
787
 
702
- const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
703
-
704
- class Cache {
705
- list = {}
706
- constructor(config) {
707
- this.configure(config);
708
- if (isNode() === false) {
709
- this.load(global[this.namespace]);
710
- }
788
+ class EJS {
789
+ constructor(options = {}) {
790
+ this.config = {};
791
+ this.scope = {};
792
+ configSchema(this.config, options);
793
+ this.context = new Context(this.config);
794
+ this.compiler = new Compiler(this.config);
795
+ this.cache = new Cache(this.config);
796
+ this.template = new Template(this.config, this.cache, this.compiler);
797
+ const render = this.render.bind(this);
798
+ const require = this.require.bind(this);
799
+ this.helpers({ require, render });
711
800
  }
712
- configure(config) {
713
- this.list = {};
714
- this.namespace = config.export;
715
- }
716
- load(data) {
717
- extend(this.list, data);
718
- return this
719
- }
720
- exist(key) {
721
- return hasProp(this.list, key)
801
+ configure(options = {}) {
802
+ configSchema(this.config, options);
803
+ this.context.configure(this.config, this.scope);
804
+ this.compiler.configure(this.config);
805
+ this.cache.configure(this.config);
806
+ this.template.configure(this.config);
807
+ return this.config
722
808
  }
723
- get(key) {
724
- return this.list[key]
725
- }
726
- remove(key) {
727
- delete this.list[key];
728
- }
729
- resolve(key) {
730
- return Promise.resolve(this.get(key))
731
- }
732
- set(key, value) {
733
- this.list[key] = value;
734
- return this
735
- }
736
- }
737
-
738
- const configSchema = (config, options) => {
739
- extend(config, {
740
- path: typeProp(isString, defaults.path, config.path, options.path),
741
- export: typeProp(
742
- isString,
743
- defaults.export,
744
- config.export,
745
- options.export
746
- ),
747
- resolver: typeProp(
748
- isFunction,
749
- defaults.resolver,
750
- config.resolver,
751
- options.resolver
752
- ),
753
- extension: typeProp(
754
- isString,
755
- defaults.extension,
756
- config.extension,
757
- options.extension
758
- ),
759
- withObject: typeProp(
760
- isBoolean,
761
- defaults.withObject,
762
- config.withObject,
763
- options.withObject
764
- ),
765
- rmWhitespace: typeProp(
766
- isBoolean,
767
- defaults.rmWhitespace,
768
- config.rmWhitespace,
769
- options.rmWhitespace
770
- ),
771
- watch: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
772
- chokidar: typeProp(
773
- isObject,
774
- defaults.export,
775
- config.export,
776
- options.export
777
- ),
778
- token: extend({}, defaults.token, config.token, options.token),
779
- vars: extend({}, defaults.vars, config.vars, options.vars),
780
- });
781
- };
782
-
783
- const create = (options) => {
784
- const config = {};
785
- const scope = {};
786
-
787
- configSchema(config, options || {});
788
-
789
- const context = new Context(config);
790
- const compiler = new Compiler(config);
791
- const cache = new Cache(config);
792
- const template = new Template(config, cache, compiler);
793
-
794
- const configure = (options) => {
795
- configSchema(config, options);
796
- context.configure(config, scope);
797
- compiler.configure(config);
798
- cache.configure(config);
799
- template.configure(config);
800
- return config
801
- };
802
-
803
- const output = (path, scope) => {
804
- return template.get(path).then(function (callback) {
809
+ output(path, scope) {
810
+ return this.template.get(path).then(function (callback) {
805
811
  return callback.call(
806
812
  scope,
807
813
  scope,
@@ -810,35 +816,40 @@ const create = (options) => {
810
816
  safeValue
811
817
  )
812
818
  })
813
- };
814
-
815
- const require = (name) => {
816
- const filepath = ext(name, config.extension);
817
- const scope = context.create({});
818
- return output(filepath, scope).then(() => {
819
- return scope.getMacro()
820
- })
821
- };
822
-
823
- const render = (name, data) => {
824
- const filepath = ext(name, config.extension);
825
- const scope = context.create(data);
826
- return output(filepath, scope).then((content) => {
819
+ }
820
+ render(name, data) {
821
+ const filepath = ext(name, this.config.extension);
822
+ const scope = this.context.create(data);
823
+ return this.output(filepath, scope).then((content) => {
827
824
  if (scope.getExtend()) {
828
825
  scope.setExtend(false);
829
826
  const layout = scope.getLayout();
830
827
  const data = scope.clone();
831
- return render(layout, data)
828
+ return this.render(layout, data)
832
829
  }
833
830
  return content
834
831
  })
835
- };
836
-
837
- const helpers = (methods) => {
838
- context.helpers(extend(scope, methods || {}));
839
- };
840
-
841
- const __express = (name, options, callback) => {
832
+ }
833
+ require(name) {
834
+ const filepath = ext(name, this.config.extension);
835
+ const scope = this.context.create({});
836
+ return this.output(filepath, scope).then(() => {
837
+ return scope.getMacro()
838
+ })
839
+ }
840
+ create(options = {}) {
841
+ return new EJS(options)
842
+ }
843
+ helpers(methods = {}) {
844
+ this.context.helpers(extend(this.scope, methods));
845
+ }
846
+ preload(list = {}) {
847
+ return this.cache.load(list)
848
+ }
849
+ compile(content, path) {
850
+ return this.compiler.compile(content, path)
851
+ }
852
+ __express(name, options, callback) {
842
853
  if (isFunction(options)) {
843
854
  callback = options;
844
855
  options = {};
@@ -855,42 +866,30 @@ const create = (options) => {
855
866
  const filename = path.relative(viewPath, name);
856
867
  viewOptions.path = viewPath;
857
868
  viewOptions.cache = viewCache;
858
- configure(viewOptions);
859
- return render(filename, options)
869
+ this.configure(viewOptions);
870
+ return this.render(filename, options)
860
871
  .then((content) => {
861
872
  callback(null, content);
862
873
  })
863
874
  .catch((error) => {
864
875
  callback(error);
865
876
  })
866
- };
867
-
868
- const preload = (list) => cache.load(list);
869
-
870
- const compile = (content, path) => compiler.compile(content, path);
871
-
872
- helpers({ require, render });
873
-
874
- return {
875
- context,
876
- render,
877
- helpers,
878
- configure,
879
- compile,
880
- create,
881
- preload,
882
- __express,
883
877
  }
884
- };
878
+ }
879
+
880
+ const ejs = new EJS({
881
+ /** defaults options **/
882
+ });
885
883
 
886
884
  const {
887
- context,
885
+ __express,
888
886
  render,
889
- helpers,
890
- configure,
887
+ context,
891
888
  compile,
889
+ helpers,
892
890
  preload,
893
- __express,
894
- } = create({});
891
+ configure,
892
+ create,
893
+ } = ejs;
895
894
 
896
- export { __express, compile, configure, context, create, element, helpers, preload, render, safeValue };
895
+ export { EJS, __express, compile, configure, context, create, element, helpers, preload, render, safeValue };