@kosatyi/ejs 0.0.54 → 0.0.56

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # EJS
2
2
 
3
- Embedded JavaScript templates
3
+ Embedded JavaScript templates with extend/block
4
4
 
5
5
  [![npm](https://img.shields.io/npm/v/@kosatyi/ejs.svg)](https://www.npmjs.com/package/@kosatyi/ejs)
6
6
  [![github-issues](https://img.shields.io/github/issues/kosatyi/ejs.svg)](https://github.com/kosatyi/ejs/issues)
package/dist/cjs/index.js CHANGED
@@ -1,40 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var path = require('path');
4
3
  var fs = require('fs');
4
+ var path = require('path');
5
5
 
6
- var defaults = {};
7
- defaults["export"] = 'ejsPrecompiled';
8
- defaults.watch = false;
9
- defaults.path = 'views';
10
- defaults.resolver = null;
11
- defaults.extension = 'ejs';
12
- defaults.rmWhitespace = true;
13
- defaults.withObject = true;
14
- defaults.vars = {
15
- SCOPE: 'ejs',
16
- COMPONENT: 'ui',
17
- EXTEND: '$$e',
18
- BUFFER: '$$a',
19
- LAYOUT: '$$l',
20
- BLOCKS: '$$b',
21
- MACRO: '$$m',
22
- SAFE: '$$v'
23
- };
24
- defaults.token = {
25
- start: '<%',
26
- end: '%>',
27
- regex: '([\\s\\S]+?)'
28
- };
29
-
30
- function _typeof(obj) {
6
+ function _typeof(o) {
31
7
  "@babel/helpers - typeof";
32
8
 
33
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
34
- return typeof obj;
35
- } : function (obj) {
36
- return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
37
- }, _typeof(obj);
9
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
10
+ return typeof o;
11
+ } : function (o) {
12
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
13
+ }, _typeof(o);
38
14
  }
39
15
  function _classCallCheck(instance, Constructor) {
40
16
  if (!(instance instanceof Constructor)) {
@@ -156,6 +132,14 @@ var getPath = function getPath(context, name) {
156
132
  });
157
133
  return [data, prop];
158
134
  };
135
+ var bindContext = function bindContext(object, context) {
136
+ var methods = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
137
+ methods.forEach(function (name) {
138
+ if (name in object) {
139
+ object[name] = object[name].bind(context);
140
+ }
141
+ });
142
+ };
159
143
  var ext = function ext(path, defaults) {
160
144
  var ext = path.split('.').pop();
161
145
  if (ext !== defaults) {
@@ -222,31 +206,134 @@ var hasProp = function hasProp(object, prop) {
222
206
  return object && object.hasOwnProperty(prop);
223
207
  };
224
208
 
225
- var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
226
- var space = ' ';
227
- var quote = '"';
228
- var equal = '=';
229
- var slash = '/';
230
- var lt = '<';
231
- var gt = '>';
232
- var element = function element(tag, attrs, content) {
233
- var result = [];
234
- var hasClosedTag = selfClosed.indexOf(tag) === -1;
235
- var attributes = map(attrs, function (value, key) {
236
- if (value !== null && value !== undefined) {
237
- return [entities(key), [quote, entities(value), quote].join('')].join(equal);
238
- }
239
- }).join(space);
240
- result.push([lt, tag, space, attributes, gt].join(''));
241
- if (content) {
242
- result.push(content instanceof Array ? content.join('') : content);
243
- }
244
- if (hasClosedTag) {
245
- result.push([lt, slash, tag, gt].join(''));
246
- }
247
- return result.join('');
209
+ var defaults = {};
210
+ defaults["export"] = 'ejsPrecompiled';
211
+ defaults.watch = false;
212
+ defaults.chokidar = null;
213
+ defaults.path = 'views';
214
+ defaults.resolver = null;
215
+ defaults.extension = 'ejs';
216
+ defaults.rmWhitespace = true;
217
+ defaults.withObject = true;
218
+ defaults.vars = {
219
+ SCOPE: 'ejs',
220
+ COMPONENT: 'ui',
221
+ EXTEND: '$$e',
222
+ BUFFER: '$$a',
223
+ LAYOUT: '$$l',
224
+ BLOCKS: '$$b',
225
+ MACRO: '$$m',
226
+ SAFE: '$$v'
227
+ };
228
+ defaults.token = {
229
+ start: '<%',
230
+ end: '%>',
231
+ regex: '([\\s\\S]+?)'
248
232
  };
249
233
 
234
+ var configSchema = function configSchema(config, options) {
235
+ extend(config, {
236
+ path: typeProp(isString, defaults.path, config.path, options.path),
237
+ "export": typeProp(isString, defaults["export"], config["export"], options["export"]),
238
+ resolver: typeProp(isFunction, defaults.resolver, config.resolver, options.resolver),
239
+ extension: typeProp(isString, defaults.extension, config.extension, options.extension),
240
+ withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
241
+ rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
242
+ watch: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
243
+ chokidar: typeProp(isObject, defaults["export"], config["export"], options["export"]),
244
+ token: extend({}, defaults.token, config.token, options.token),
245
+ vars: extend({}, defaults.vars, config.vars, options.vars)
246
+ });
247
+ };
248
+
249
+ var resolvePath = function resolvePath(path, template) {
250
+ template = [path, template].join('/');
251
+ template = template.replace(/\/\//g, '/');
252
+ return template;
253
+ };
254
+ var httpRequest = function httpRequest(path, template) {
255
+ return fetch(resolvePath(path, template)).then(function (response) {
256
+ return response.text();
257
+ });
258
+ };
259
+ var fileSystem = function fileSystem(path, template) {
260
+ return new Promise(function (resolve, reject) {
261
+ fs.readFile(resolvePath(path, template), function (error, data) {
262
+ if (error) {
263
+ reject(error);
264
+ } else {
265
+ resolve(data.toString());
266
+ }
267
+ });
268
+ });
269
+ };
270
+ var Template = /*#__PURE__*/function () {
271
+ function Template(config, cache, compiler) {
272
+ _classCallCheck(this, Template);
273
+ this.cache = cache;
274
+ this.watcher = {
275
+ unwatch: function unwatch() {},
276
+ on: function on() {}
277
+ };
278
+ this.compiler = compiler;
279
+ this.configure(config);
280
+ }
281
+ _createClass(Template, [{
282
+ key: "configure",
283
+ value: function configure(config) {
284
+ var _this = this;
285
+ this.path = config.path;
286
+ this.chokidar = config.chokidar;
287
+ this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
288
+ if (config.watch && isNode()) {
289
+ if (this.watcher) {
290
+ this.watcher.unwatch('.');
291
+ }
292
+ if (this.chokidar) {
293
+ this.watcher = this.chokidar.watch('.', {
294
+ cwd: this.path
295
+ }).on('change', function (name) {
296
+ _this.cache.remove(name);
297
+ });
298
+ }
299
+ }
300
+ }
301
+ }, {
302
+ key: "resolve",
303
+ value: function resolve(template) {
304
+ return this.resolver(this.path, template);
305
+ }
306
+ }, {
307
+ key: "result",
308
+ value: function result(template, content) {
309
+ this.cache.set(template, content);
310
+ return content;
311
+ }
312
+ }, {
313
+ key: "compile",
314
+ value: function compile(content, template) {
315
+ if (isFunction(content)) {
316
+ return content;
317
+ } else {
318
+ return this.compiler.compile(content, template);
319
+ }
320
+ }
321
+ }, {
322
+ key: "get",
323
+ value: function get(template) {
324
+ var _this2 = this;
325
+ if (this.cache.exist(template)) {
326
+ return this.cache.resolve(template);
327
+ }
328
+ var content = this.resolve(template).then(function (content) {
329
+ return _this2.result(template, _this2.compile(content, template));
330
+ });
331
+ return this.result(template, content);
332
+ }
333
+ }]);
334
+ return Template;
335
+ }();
336
+
250
337
  var tagList = [{
251
338
  symbol: '-',
252
339
  format: function format(value) {
@@ -356,91 +443,83 @@ var Compiler = /*#__PURE__*/function () {
356
443
  return Compiler;
357
444
  }();
358
445
 
359
- var resolvePath = function resolvePath(path, template) {
360
- template = [path, template].join('/');
361
- template = template.replace(/\/\//g, '/');
362
- return template;
363
- };
364
- var httpRequest = function httpRequest(path, template) {
365
- return fetch(resolvePath(path, template)).then(function (response) {
366
- return response.text();
367
- });
368
- };
369
- var fileSystem = function fileSystem(path, template) {
370
- return new Promise(function (resolve, reject) {
371
- fs.readFile(resolvePath(path, template), function (error, data) {
372
- if (error) {
373
- reject(error);
374
- } else {
375
- resolve(data.toString());
376
- }
377
- });
378
- });
379
- };
380
- var Template = /*#__PURE__*/function () {
381
- function Template(config, cache, compiler) {
382
- _classCallCheck(this, Template);
383
- this.cache = cache;
384
- this.watcher = null;
385
- this.compiler = compiler;
446
+ var global = typeof globalThis !== 'undefined' ? globalThis : window || self;
447
+ var Cache = /*#__PURE__*/function () {
448
+ function Cache(config) {
449
+ _classCallCheck(this, Cache);
450
+ _defineProperty(this, "list", {});
386
451
  this.configure(config);
452
+ if (isNode() === false) {
453
+ this.load(global[this.namespace]);
454
+ }
387
455
  }
388
- _createClass(Template, [{
456
+ _createClass(Cache, [{
389
457
  key: "configure",
390
458
  value: function configure(config) {
391
- var _this = this;
392
- this.path = config.path;
393
- this.chokidar = config.chokidar;
394
- this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
395
- if (config.watch && config.chokidar && isNode()) {
396
- if (this.watcher) {
397
- this.watcher.unwatch('.');
398
- }
399
- this.watcher = this.chokidar.watch('.', {
400
- cwd: this.path
401
- }).on('change', function (name) {
402
- _this.cache.remove(name);
403
- }).on('error', function (error) {
404
- console.log('watcher error: ' + error);
405
- });
406
- }
459
+ this.list = {};
460
+ this.namespace = config["export"];
407
461
  }
408
462
  }, {
409
- key: "resolve",
410
- value: function resolve(template) {
411
- return this.resolver(this.path, template);
463
+ key: "load",
464
+ value: function load(data) {
465
+ extend(this.list, data);
466
+ return this;
412
467
  }
413
468
  }, {
414
- key: "result",
415
- value: function result(template, content) {
416
- this.cache.set(template, content);
417
- return content;
469
+ key: "exist",
470
+ value: function exist(key) {
471
+ return hasProp(this.list, key);
418
472
  }
419
473
  }, {
420
- key: "compile",
421
- value: function compile(content, template) {
422
- if (isFunction(content)) {
423
- return content;
424
- } else {
425
- return this.compiler.compile(content, template);
426
- }
474
+ key: "get",
475
+ value: function get(key) {
476
+ return this.list[key];
427
477
  }
428
478
  }, {
429
- key: "get",
430
- value: function get(template) {
431
- var _this2 = this;
432
- if (this.cache.exist(template)) {
433
- return this.cache.resolve(template);
434
- }
435
- var content = this.resolve(template).then(function (content) {
436
- return _this2.result(template, _this2.compile(content, template));
437
- });
438
- return this.result(template, content);
479
+ key: "remove",
480
+ value: function remove(key) {
481
+ delete this.list[key];
482
+ }
483
+ }, {
484
+ key: "resolve",
485
+ value: function resolve(key) {
486
+ return Promise.resolve(this.get(key));
487
+ }
488
+ }, {
489
+ key: "set",
490
+ value: function set(key, value) {
491
+ this.list[key] = value;
492
+ return this;
439
493
  }
440
494
  }]);
441
- return Template;
495
+ return Cache;
442
496
  }();
443
497
 
498
+ var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
499
+ var space = ' ';
500
+ var quote = '"';
501
+ var equal = '=';
502
+ var slash = '/';
503
+ var lt = '<';
504
+ var gt = '>';
505
+ var element = function element(tag, attrs, content) {
506
+ var result = [];
507
+ var hasClosedTag = selfClosed.indexOf(tag) === -1;
508
+ var attributes = map(attrs, function (value, key) {
509
+ if (value !== null && value !== undefined) {
510
+ return [entities(key), [quote, entities(value), quote].join('')].join(equal);
511
+ }
512
+ }).join(space);
513
+ result.push([lt, tag, space, attributes, gt].join(''));
514
+ if (content) {
515
+ result.push(content instanceof Array ? content.join('') : content);
516
+ }
517
+ if (hasClosedTag) {
518
+ result.push([lt, slash, tag, gt].join(''));
519
+ }
520
+ return result.join('');
521
+ };
522
+
444
523
  var resolve = function resolve(list) {
445
524
  return Promise.all(list).then(function (list) {
446
525
  return list.join('');
@@ -681,166 +760,129 @@ var Context = /*#__PURE__*/function () {
681
760
  return Context;
682
761
  }();
683
762
 
684
- var global = typeof globalThis !== 'undefined' ? globalThis : window || self;
685
- var Cache = /*#__PURE__*/function () {
686
- function Cache(config) {
687
- _classCallCheck(this, Cache);
688
- _defineProperty(this, "list", {});
689
- this.configure(config);
690
- if (isNode() === false) {
691
- this.load(global[this.namespace]);
692
- }
763
+ var EJS = /*#__PURE__*/function () {
764
+ function EJS() {
765
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
766
+ _classCallCheck(this, EJS);
767
+ _defineProperty(this, "export", ['cache', 'render', 'require', 'helpers', 'configure', 'preload', 'compiler', '__express']);
768
+ this.config = {};
769
+ this.scope = {};
770
+ configSchema(this.config, options);
771
+ bindContext(this, this, this["export"]);
772
+ this.context = new Context(this.config);
773
+ this.compiler = new Compiler(this.config);
774
+ this.cache = new Cache(this.config);
775
+ this.template = new Template(this.config, this.cache, this.compiler);
776
+ this.helpers({
777
+ require: this.require,
778
+ render: this.render
779
+ });
693
780
  }
694
- _createClass(Cache, [{
781
+ _createClass(EJS, [{
695
782
  key: "configure",
696
- value: function configure(config) {
697
- this.list = {};
698
- this.namespace = config["export"];
783
+ value: function configure() {
784
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
785
+ configSchema(this.config, options);
786
+ this.context.configure(this.config, this.scope);
787
+ this.compiler.configure(this.config);
788
+ this.cache.configure(this.config);
789
+ this.template.configure(this.config);
790
+ return this.config;
699
791
  }
700
792
  }, {
701
- key: "load",
702
- value: function load(data) {
703
- extend(this.list, data);
704
- return this;
793
+ key: "output",
794
+ value: function output(path, scope) {
795
+ return this.template.get(path).then(function (callback) {
796
+ return callback.call(scope, scope, scope.getComponent(), scope.getBuffer(), safeValue);
797
+ });
705
798
  }
706
799
  }, {
707
- key: "exist",
708
- value: function exist(key) {
709
- return hasProp(this.list, key);
800
+ key: "render",
801
+ value: function render(name, data) {
802
+ var _this = this;
803
+ var filepath = ext(name, this.config.extension);
804
+ var scope = this.context.create(data);
805
+ return this.output(filepath, scope).then(function (content) {
806
+ if (scope.getExtend()) {
807
+ scope.setExtend(false);
808
+ var layout = scope.getLayout();
809
+ var _data = scope.clone();
810
+ return _this.render(layout, _data);
811
+ }
812
+ return content;
813
+ });
710
814
  }
711
815
  }, {
712
- key: "get",
713
- value: function get(key) {
714
- return this.list[key];
816
+ key: "require",
817
+ value: function require(name) {
818
+ var filepath = ext(name, this.config.extension);
819
+ var scope = this.context.create({});
820
+ return this.output(filepath, scope).then(function () {
821
+ return scope.getMacro();
822
+ });
715
823
  }
716
824
  }, {
717
- key: "remove",
718
- value: function remove(key) {
719
- delete this.list[key];
825
+ key: "create",
826
+ value: function create() {
827
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
828
+ return new EJS(options);
720
829
  }
721
830
  }, {
722
- key: "resolve",
723
- value: function resolve(key) {
724
- return Promise.resolve(this.get(key));
831
+ key: "helpers",
832
+ value: function helpers() {
833
+ var methods = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
834
+ this.context.helpers(extend(this.scope, methods));
725
835
  }
726
836
  }, {
727
- key: "set",
728
- value: function set(key, value) {
729
- this.list[key] = value;
730
- return this;
837
+ key: "preload",
838
+ value: function preload() {
839
+ var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
840
+ return this.cache.load(list);
841
+ }
842
+ }, {
843
+ key: "compile",
844
+ value: function compile(content, path) {
845
+ return this.compiler.compile(content, path);
846
+ }
847
+ }, {
848
+ key: "__express",
849
+ value: function __express(name, options, callback) {
850
+ if (isFunction(options)) {
851
+ callback = options;
852
+ options = {};
853
+ }
854
+ options = options || {};
855
+ var settings = extend({}, options.settings);
856
+ var viewPath = typeProp(isString, defaults.path, settings['views']);
857
+ var viewCache = typeProp(isBoolean, defaults.cache, settings['view cache']);
858
+ var viewOptions = extend({}, settings['view options']);
859
+ var filename = path.relative(viewPath, name);
860
+ viewOptions.path = viewPath;
861
+ viewOptions.cache = viewCache;
862
+ this.configure(viewOptions);
863
+ return this.render(filename, options).then(function (content) {
864
+ callback(null, content);
865
+ })["catch"](function (error) {
866
+ callback(error);
867
+ });
731
868
  }
732
869
  }]);
733
- return Cache;
870
+ return EJS;
734
871
  }();
735
872
 
736
- var configSchema = function configSchema(config, options) {
737
- extend(config, {
738
- path: typeProp(isString, defaults.path, config.path, options.path),
739
- "export": typeProp(isString, defaults["export"], config["export"], options["export"]),
740
- resolver: typeProp(isFunction, defaults.resolver, config.resolver, options.resolver),
741
- extension: typeProp(isString, defaults.extension, config.extension, options.extension),
742
- withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
743
- rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
744
- watch: typeProp(isBoolean, defaults.watch, config.watch, options.watch),
745
- chokidar: typeProp(isObject, defaults["export"], config["export"], options["export"]),
746
- token: extend({}, defaults.token, config.token, options.token),
747
- vars: extend({}, defaults.vars, config.vars, options.vars)
748
- });
749
- };
750
- var create = function create(options) {
751
- var config = {};
752
- var scope = {};
753
- configSchema(config, options || {});
754
- var context = new Context(config);
755
- var compiler = new Compiler(config);
756
- var cache = new Cache(config);
757
- var template = new Template(config, cache, compiler);
758
- var configure = function configure(options) {
759
- configSchema(config, options);
760
- context.configure(config, scope);
761
- compiler.configure(config);
762
- cache.configure(config);
763
- template.configure(config);
764
- return config;
765
- };
766
- var output = function output(path, scope) {
767
- return template.get(path).then(function (callback) {
768
- return callback.call(scope, scope, scope.getComponent(), scope.getBuffer(), safeValue);
769
- });
770
- };
771
- var require = function require(name) {
772
- var filepath = ext(name, config.extension);
773
- var scope = context.create({});
774
- return output(filepath, scope).then(function () {
775
- return scope.getMacro();
776
- });
777
- };
778
- var render = function render(name, data) {
779
- var filepath = ext(name, config.extension);
780
- var scope = context.create(data);
781
- return output(filepath, scope).then(function (content) {
782
- if (scope.getExtend()) {
783
- scope.setExtend(false);
784
- var layout = scope.getLayout();
785
- var _data = scope.clone();
786
- return render(layout, _data);
787
- }
788
- return content;
789
- });
790
- };
791
- var helpers = function helpers(methods) {
792
- context.helpers(extend(scope, methods || {}));
793
- };
794
- var __express = function __express(name, options, callback) {
795
- if (isFunction(options)) {
796
- callback = options;
797
- options = {};
798
- }
799
- options = options || {};
800
- var settings = extend({}, options.settings);
801
- var viewPath = typeProp(isString, defaults.path, settings['views']);
802
- var viewCache = typeProp(isBoolean, defaults.cache, settings['view cache']);
803
- var viewOptions = extend({}, settings['view options']);
804
- var filename = path.relative(viewPath, name);
805
- viewOptions.path = viewPath;
806
- viewOptions.cache = viewCache;
807
- configure(viewOptions);
808
- return render(filename, options).then(function (content) {
809
- callback(null, content);
810
- })["catch"](function (error) {
811
- callback(error);
812
- });
813
- };
814
- var preload = function preload(list) {
815
- return cache.load(list);
816
- };
817
- var compile = function compile(content, path) {
818
- return compiler.compile(content, path);
819
- };
820
- helpers({
821
- require: require,
822
- render: render
823
- });
824
- return {
825
- context: context,
826
- render: render,
827
- helpers: helpers,
828
- configure: configure,
829
- compile: compile,
830
- create: create,
831
- preload: preload,
832
- __express: __express
833
- };
834
- };
835
- var _create = create({}),
836
- context = _create.context,
837
- render = _create.render,
838
- helpers = _create.helpers,
839
- configure = _create.configure,
840
- compile = _create.compile,
841
- preload = _create.preload,
842
- __express = _create.__express;
873
+ var ejs = new EJS({
874
+ /** defaults options **/
875
+ });
876
+ var __express = ejs.__express,
877
+ render = ejs.render,
878
+ context = ejs.context,
879
+ compile = ejs.compile,
880
+ helpers = ejs.helpers,
881
+ preload = ejs.preload,
882
+ configure = ejs.configure,
883
+ create = ejs.create;
843
884
 
885
+ exports.EJS = EJS;
844
886
  exports.__express = __express;
845
887
  exports.compile = compile;
846
888
  exports.configure = configure;