@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/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)) {
@@ -222,30 +198,133 @@ var hasProp = function hasProp(object, prop) {
222
198
  return object && object.hasOwnProperty(prop);
223
199
  };
224
200
 
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('');
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;
248
245
  };
246
+ var httpRequest = function httpRequest(path, template) {
247
+ return fetch(resolvePath(path, template)).then(function (response) {
248
+ return response.text();
249
+ });
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
+ }();
249
328
 
250
329
  var tagList = [{
251
330
  symbol: '-',
@@ -356,91 +435,83 @@ var Compiler = /*#__PURE__*/function () {
356
435
  return Compiler;
357
436
  }();
358
437
 
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;
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", {});
386
443
  this.configure(config);
444
+ if (isNode() === false) {
445
+ this.load(global[this.namespace]);
446
+ }
387
447
  }
388
- _createClass(Template, [{
448
+ _createClass(Cache, [{
389
449
  key: "configure",
390
450
  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
- }
451
+ this.list = {};
452
+ this.namespace = config["export"];
407
453
  }
408
454
  }, {
409
- key: "resolve",
410
- value: function resolve(template) {
411
- return this.resolver(this.path, template);
455
+ key: "load",
456
+ value: function load(data) {
457
+ extend(this.list, data);
458
+ return this;
412
459
  }
413
460
  }, {
414
- key: "result",
415
- value: function result(template, content) {
416
- this.cache.set(template, content);
417
- return content;
461
+ key: "exist",
462
+ value: function exist(key) {
463
+ return hasProp(this.list, key);
418
464
  }
419
465
  }, {
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
- }
466
+ key: "get",
467
+ value: function get(key) {
468
+ return this.list[key];
427
469
  }
428
470
  }, {
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);
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;
439
485
  }
440
486
  }]);
441
- return Template;
487
+ return Cache;
442
488
  }();
443
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
+
444
515
  var resolve = function resolve(list) {
445
516
  return Promise.all(list).then(function (list) {
446
517
  return list.join('');
@@ -681,166 +752,129 @@ var Context = /*#__PURE__*/function () {
681
752
  return Context;
682
753
  }();
683
754
 
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
- }
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
+ });
693
772
  }
694
- _createClass(Cache, [{
773
+ _createClass(EJS, [{
695
774
  key: "configure",
696
- value: function configure(config) {
697
- this.list = {};
698
- 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;
699
783
  }
700
784
  }, {
701
- key: "load",
702
- value: function load(data) {
703
- extend(this.list, data);
704
- 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
+ });
705
790
  }
706
791
  }, {
707
- key: "exist",
708
- value: function exist(key) {
709
- 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
+ });
710
806
  }
711
807
  }, {
712
- key: "get",
713
- value: function get(key) {
714
- 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
+ });
715
815
  }
716
816
  }, {
717
- key: "remove",
718
- value: function remove(key) {
719
- 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);
720
821
  }
721
822
  }, {
722
- key: "resolve",
723
- value: function resolve(key) {
724
- 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));
725
827
  }
726
828
  }, {
727
- key: "set",
728
- value: function set(key, value) {
729
- this.list[key] = value;
730
- 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
+ });
731
860
  }
732
861
  }]);
733
- return Cache;
862
+ return EJS;
734
863
  }();
735
864
 
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;
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;
843
876
 
877
+ exports.EJS = EJS;
844
878
  exports.__express = __express;
845
879
  exports.compile = compile;
846
880
  exports.configure = configure;