@kosatyi/ejs 0.0.68 → 0.0.70

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/cjs/index.js CHANGED
@@ -1,7 +1,7 @@
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
6
  var typeProp = function typeProp() {
7
7
  var args = [].slice.call(arguments);
@@ -152,13 +152,20 @@ var resolve$1 = function resolve(value, callback, context) {
152
152
  var hasProp = function hasProp(object, prop) {
153
153
  return object && object.hasOwnProperty(prop);
154
154
  };
155
+ var joinPath = function joinPath(path, template) {
156
+ template = [path, template].join('/');
157
+ template = template.replace(/\/\//g, '/');
158
+ return template;
159
+ };
155
160
 
156
161
  var defaults = {};
157
162
  defaults["export"] = 'ejsPrecompiled';
158
163
  defaults.cache = true;
159
164
  defaults.chokidar = null;
160
165
  defaults.path = 'views';
161
- defaults.resolver = null;
166
+ defaults.resolver = function (path, template) {
167
+ return Promise.resolve(['resolver is not defined', path, template].join(' '));
168
+ };
162
169
  defaults.extension = 'ejs';
163
170
  defaults.rmWhitespace = true;
164
171
  defaults.withObject = true;
@@ -178,31 +185,6 @@ defaults.token = {
178
185
  regex: '([\\s\\S]+?)'
179
186
  };
180
187
 
181
- var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
182
- var space = ' ';
183
- var quote = '"';
184
- var equal = '=';
185
- var slash = '/';
186
- var lt = '<';
187
- var gt = '>';
188
- var element = function element(tag, attrs, content) {
189
- var result = [];
190
- var hasClosedTag = selfClosed.indexOf(tag) === -1;
191
- var attributes = map(attrs, function (value, key) {
192
- if (value !== null && value !== undefined) {
193
- return [entities(key), [quote, entities(value), quote].join('')].join(equal);
194
- }
195
- }).join(space);
196
- result.push([lt, tag, space, attributes, gt].join(''));
197
- if (content) {
198
- result.push(content instanceof Array ? content.join('') : content);
199
- }
200
- if (hasClosedTag) {
201
- result.push([lt, slash, tag, gt].join(''));
202
- }
203
- return result.join('');
204
- };
205
-
206
188
  var configSchema = function configSchema(config, options) {
207
189
  extend(config, {
208
190
  path: typeProp(isString, defaults.path, config.path, options.path),
@@ -356,30 +338,6 @@ function Compiler(config) {
356
338
  this.configure(config);
357
339
  }
358
340
 
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 fileResolver = function fileResolver(resolver) {
381
- return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest;
382
- };
383
341
  function Template(config, cache, compiler) {
384
342
  if (instanceOf(this, Template) === false) return new Template(config, cache, compiler);
385
343
  if (instanceOf(cache, Cache) === false) throw new TypeError('cache is not instance of Cache');
@@ -402,15 +360,17 @@ function Template(config, cache, compiler) {
402
360
  this.configure = function (config) {
403
361
  template.path = config.path;
404
362
  template.cache = config.cache;
405
- template.resolver = fileResolver(config.resolver);
363
+ if (isFunction(config.resolver)) {
364
+ template.resolver = config.resolver;
365
+ }
406
366
  };
407
367
  this.get = function (template) {
408
368
  if (cache.exist(template)) {
409
369
  return cache.resolve(template);
410
370
  }
411
- return result(template, resolve(template).then(function (content) {
371
+ return resolve(template).then(function (content) {
412
372
  return result(template, compile(content, template));
413
- }));
373
+ });
414
374
  };
415
375
  this.configure(config);
416
376
  }
@@ -444,6 +404,31 @@ function _toPropertyKey(arg) {
444
404
  return typeof key === "symbol" ? key : String(key);
445
405
  }
446
406
 
407
+ var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
408
+ var space = ' ';
409
+ var quote = '"';
410
+ var equal = '=';
411
+ var slash = '/';
412
+ var lt = '<';
413
+ var gt = '>';
414
+ var element = function element(tag, attrs, content) {
415
+ var result = [];
416
+ var hasClosedTag = selfClosed.indexOf(tag) === -1;
417
+ var attributes = map(attrs, function (value, key) {
418
+ if (value !== null && value !== undefined) {
419
+ return [entities(key), [quote, entities(value), quote].join('')].join(equal);
420
+ }
421
+ }).join(space);
422
+ result.push([lt, tag, space, attributes, gt].join(''));
423
+ if (content) {
424
+ result.push(content instanceof Array ? content.join('') : content);
425
+ }
426
+ if (hasClosedTag) {
427
+ result.push([lt, slash, tag, gt].join(''));
428
+ }
429
+ return result.join('');
430
+ };
431
+
447
432
  function resolve(list) {
448
433
  return Promise.all(list || []).then(function (list) {
449
434
  return list.join('');
@@ -835,7 +820,49 @@ function EJS(options) {
835
820
  return this;
836
821
  }
837
822
 
838
- var ejs = new EJS();
823
+ function readFile(path, template) {
824
+ return new Promise(function (resolve, reject) {
825
+ fs.readFile(joinPath(path, template), function (error, data) {
826
+ if (error) {
827
+ reject(error);
828
+ } else {
829
+ resolve(data.toString());
830
+ }
831
+ });
832
+ });
833
+ }
834
+
835
+ /**
836
+ *
837
+ * @param {EJS} ejs
838
+ * @return {function(name, options, callback): Promise<String>}
839
+ */
840
+ function expressRenderer(ejs) {
841
+ return function (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
+ ejs.configure(viewOptions);
855
+ return ejs.render(filename, options).then(function (content) {
856
+ callback(null, content);
857
+ })["catch"](function (error) {
858
+ callback(error);
859
+ });
860
+ };
861
+ }
862
+
863
+ var ejs = new EJS({
864
+ resolver: readFile
865
+ });
839
866
  var render = ejs.render,
840
867
  context = ejs.context,
841
868
  compile = ejs.compile,
@@ -843,36 +870,13 @@ var render = ejs.render,
843
870
  preload = ejs.preload,
844
871
  configure = ejs.configure,
845
872
  create = ejs.create;
873
+ var __express = expressRenderer(ejs);
846
874
 
847
- function __express(name, options, callback) {
848
- if (isFunction(options)) {
849
- callback = options;
850
- options = {};
851
- }
852
- options = options || {};
853
- var settings = extend({}, options.settings);
854
- var viewPath = typeProp(isString, defaults.path, settings['views']);
855
- var viewCache = typeProp(isBoolean, defaults.cache, settings['view cache']);
856
- var viewOptions = extend({}, settings['view options']);
857
- var filename = path.relative(viewPath, name);
858
- viewOptions.path = viewPath;
859
- viewOptions.cache = viewCache;
860
- configure(viewOptions);
861
- return render(filename, options).then(function (content) {
862
- callback(null, content);
863
- })["catch"](function (error) {
864
- callback(error);
865
- });
866
- }
867
-
868
- exports.EJS = EJS;
869
875
  exports.__express = __express;
870
876
  exports.compile = compile;
871
877
  exports.configure = configure;
872
878
  exports.context = context;
873
879
  exports.create = create;
874
- exports.element = element;
875
880
  exports.helpers = helpers;
876
881
  exports.preload = preload;
877
882
  exports.render = render;
878
- exports.safeValue = safeValue;