@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/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import path from 'path';
2
1
  import fs from 'fs';
2
+ import path from 'path';
3
3
 
4
4
  const typeProp = function () {
5
5
  const args = [].slice.call(arguments);
@@ -161,12 +161,23 @@ const hasProp = (object, prop) => {
161
161
  return object && object.hasOwnProperty(prop)
162
162
  };
163
163
 
164
+ const joinPath = (path, template) => {
165
+ template = [path, template].join('/');
166
+ template = template.replace(/\/\//g, '/');
167
+ return template
168
+ };
169
+
164
170
  const defaults = {};
171
+
165
172
  defaults.export = 'ejsPrecompiled';
166
173
  defaults.cache = true;
167
174
  defaults.chokidar = null;
168
175
  defaults.path = 'views';
169
- defaults.resolver = null;
176
+ defaults.resolver = function (path, template) {
177
+ return Promise.resolve(
178
+ ['resolver is not defined', path, template].join(' ')
179
+ )
180
+ };
170
181
  defaults.extension = 'ejs';
171
182
  defaults.rmWhitespace = true;
172
183
  defaults.withObject = true;
@@ -186,51 +197,6 @@ defaults.token = {
186
197
  regex: '([\\s\\S]+?)',
187
198
  };
188
199
 
189
- const selfClosed = [
190
- 'area',
191
- 'base',
192
- 'br',
193
- 'col',
194
- 'embed',
195
- 'hr',
196
- 'img',
197
- 'input',
198
- 'link',
199
- 'meta',
200
- 'param',
201
- 'source',
202
- 'track',
203
- 'wbr',
204
- ];
205
-
206
- const space = ' ';
207
- const quote = '"';
208
- const equal = '=';
209
- const slash = '/';
210
- const lt = '<';
211
- const gt = '>';
212
-
213
- const element = (tag, attrs, content) => {
214
- const result = [];
215
- const hasClosedTag = selfClosed.indexOf(tag) === -1;
216
- const attributes = map(attrs, (value, key) => {
217
- if (value !== null && value !== undefined) {
218
- return [
219
- entities(key),
220
- [quote, entities(value), quote].join(''),
221
- ].join(equal)
222
- }
223
- }).join(space);
224
- result.push([lt, tag, space, attributes, gt].join(''));
225
- if (content) {
226
- result.push(content instanceof Array ? content.join('') : content);
227
- }
228
- if (hasClosedTag) {
229
- result.push([lt, slash, tag, gt].join(''));
230
- }
231
- return result.join('')
232
- };
233
-
234
200
  const configSchema = (config, options) => {
235
201
  extend(config, {
236
202
  path: typeProp(isString, defaults.path, config.path, options.path),
@@ -435,75 +401,99 @@ function Compiler(config) {
435
401
  this.configure(config);
436
402
  }
437
403
 
438
- const resolvePath = (path, template) => {
439
- template = [path, template].join('/');
440
- template = template.replace(/\/\//g, '/');
441
- return template
442
- };
443
-
444
- const httpRequest = (path, template) => {
445
- return fetch(resolvePath(path, template)).then((response) =>
446
- response.text()
447
- )
448
- };
449
-
450
- const fileSystem = (path, template) => {
451
- return new Promise((resolve, reject) => {
452
- fs.readFile(resolvePath(path, template), (error, data) => {
453
- if (error) {
454
- reject(error);
455
- } else {
456
- resolve(data.toString());
457
- }
458
- });
459
- })
460
- };
461
-
462
- const fileResolver = (resolver) => {
463
- return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest
464
- };
465
-
466
404
  function Template(config, cache, compiler) {
467
405
  if (instanceOf(this, Template) === false)
468
406
  return new Template(config, cache, compiler)
407
+
469
408
  if (instanceOf(cache, Cache) === false)
470
409
  throw new TypeError('cache is not instance of Cache')
410
+
471
411
  if (instanceOf(compiler, Compiler) === false)
472
412
  throw new TypeError('compiler is not instance of Compiler')
413
+
473
414
  const template = {};
474
- const result = function (template, content) {
415
+
416
+ const result = (template, content) => {
475
417
  cache.set(template, content);
476
418
  return content
477
419
  };
478
- const resolve = function (path) {
420
+
421
+ const resolve = (path) => {
479
422
  return template.resolver(template.path, path)
480
423
  };
481
- const compile = function (content, template) {
424
+
425
+ const compile = (content, template) => {
482
426
  if (isFunction(content)) {
483
427
  return content
484
428
  } else {
485
429
  return compiler.compile(content, template)
486
430
  }
487
431
  };
432
+
488
433
  this.configure = function (config) {
489
434
  template.path = config.path;
490
435
  template.cache = config.cache;
491
- template.resolver = fileResolver(config.resolver);
436
+ if (isFunction(config.resolver)) {
437
+ template.resolver = config.resolver;
438
+ }
492
439
  };
440
+
493
441
  this.get = function (template) {
494
442
  if (cache.exist(template)) {
495
443
  return cache.resolve(template)
496
444
  }
497
- return result(
498
- template,
499
- resolve(template).then((content) =>
500
- result(template, compile(content, template))
501
- )
445
+ return resolve(template).then((content) =>
446
+ result(template, compile(content, template))
502
447
  )
503
448
  };
504
449
  this.configure(config);
505
450
  }
506
451
 
452
+ const selfClosed = [
453
+ 'area',
454
+ 'base',
455
+ 'br',
456
+ 'col',
457
+ 'embed',
458
+ 'hr',
459
+ 'img',
460
+ 'input',
461
+ 'link',
462
+ 'meta',
463
+ 'param',
464
+ 'source',
465
+ 'track',
466
+ 'wbr',
467
+ ];
468
+
469
+ const space = ' ';
470
+ const quote = '"';
471
+ const equal = '=';
472
+ const slash = '/';
473
+ const lt = '<';
474
+ const gt = '>';
475
+
476
+ const element = (tag, attrs, content) => {
477
+ const result = [];
478
+ const hasClosedTag = selfClosed.indexOf(tag) === -1;
479
+ const attributes = map(attrs, (value, key) => {
480
+ if (value !== null && value !== undefined) {
481
+ return [
482
+ entities(key),
483
+ [quote, entities(value), quote].join(''),
484
+ ].join(equal)
485
+ }
486
+ }).join(space);
487
+ result.push([lt, tag, space, attributes, gt].join(''));
488
+ if (content) {
489
+ result.push(content instanceof Array ? content.join('') : content);
490
+ }
491
+ if (hasClosedTag) {
492
+ result.push([lt, slash, tag, gt].join(''));
493
+ }
494
+ return result.join('')
495
+ };
496
+
507
497
  function resolve(list) {
508
498
  return Promise.all(list || []).then((list) => list.join(''))
509
499
  }
@@ -871,14 +861,18 @@ function Context(config) {
871
861
 
872
862
  function EJS(options) {
873
863
  if (instanceOf(this, EJS) === false) return new EJS(options)
864
+
874
865
  const scope = {};
875
866
  const config = {};
867
+
876
868
  configSchema(config, options || {});
869
+
877
870
  const context = new Context(config);
878
871
  const compiler = new Compiler(config);
879
872
  const cache = new Cache();
880
873
  const template = new Template(config, cache, compiler);
881
- const output = function (path, scope) {
874
+
875
+ const output = (path, scope) => {
882
876
  return template.get(path).then(function (callback) {
883
877
  return callback.call(
884
878
  scope,
@@ -889,14 +883,14 @@ function EJS(options) {
889
883
  )
890
884
  })
891
885
  };
892
- const require = function (name) {
886
+ const require = (name) => {
893
887
  const filepath = ext(name, config.extension);
894
888
  const scope = context.create({});
895
889
  return output(filepath, scope).then(() => {
896
890
  return scope.getMacro()
897
891
  })
898
892
  };
899
- const render = function (name, data) {
893
+ const render = (name, data) => {
900
894
  const filepath = ext(name, config.extension);
901
895
  const scope = context.create(data);
902
896
  return output(filepath, scope).then((content) => {
@@ -940,36 +934,57 @@ function EJS(options) {
940
934
  return this
941
935
  }
942
936
 
943
- const ejs = new EJS();
944
-
945
- const { render, context, compile, helpers, preload, configure, create } =
946
- ejs;
937
+ function readFile(path, template) {
938
+ return new Promise((resolve, reject) => {
939
+ fs.readFile(joinPath(path, template), (error, data) => {
940
+ if (error) {
941
+ reject(error);
942
+ } else {
943
+ resolve(data.toString());
944
+ }
945
+ });
946
+ })
947
+ }
947
948
 
948
- function __express(name, options, callback) {
949
- if (isFunction(options)) {
950
- callback = options;
951
- options = {};
949
+ /**
950
+ *
951
+ * @param {EJS} ejs
952
+ * @return {function(name, options, callback): Promise<String>}
953
+ */
954
+ function expressRenderer(ejs) {
955
+ return function (name, options, callback) {
956
+ if (isFunction(options)) {
957
+ callback = options;
958
+ options = {};
959
+ }
960
+ options = options || {};
961
+ const settings = extend({}, options.settings);
962
+ const viewPath = typeProp(isString, defaults.path, settings['views']);
963
+ const viewCache = typeProp(
964
+ isBoolean,
965
+ defaults.cache,
966
+ settings['view cache']
967
+ );
968
+ const viewOptions = extend({}, settings['view options']);
969
+ const filename = path.relative(viewPath, name);
970
+ viewOptions.path = viewPath;
971
+ viewOptions.cache = viewCache;
972
+ ejs.configure(viewOptions);
973
+ return ejs
974
+ .render(filename, options)
975
+ .then((content) => {
976
+ callback(null, content);
977
+ })
978
+ .catch((error) => {
979
+ callback(error);
980
+ })
952
981
  }
953
- options = options || {};
954
- const settings = extend({}, options.settings);
955
- const viewPath = typeProp(isString, defaults.path, settings['views']);
956
- const viewCache = typeProp(
957
- isBoolean,
958
- defaults.cache,
959
- settings['view cache']
960
- );
961
- const viewOptions = extend({}, settings['view options']);
962
- const filename = path.relative(viewPath, name);
963
- viewOptions.path = viewPath;
964
- viewOptions.cache = viewCache;
965
- configure(viewOptions);
966
- return render(filename, options)
967
- .then((content) => {
968
- callback(null, content);
969
- })
970
- .catch((error) => {
971
- callback(error);
972
- })
973
982
  }
974
983
 
975
- export { EJS, __express, compile, configure, context, create, element, helpers, preload, render, safeValue };
984
+ const ejs = new EJS({ resolver: readFile });
985
+
986
+ const { render, context, compile, helpers, preload, configure, create } = ejs;
987
+
988
+ const __express = expressRenderer(ejs);
989
+
990
+ export { __express, compile, configure, context, create, helpers, preload, render };