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