@kosatyi/ejs 0.0.23 → 0.0.25

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/ejs.cjs CHANGED
@@ -47,7 +47,10 @@ var isBoolean = function isBoolean(v) {
47
47
  return typeof v === 'boolean';
48
48
  };
49
49
 
50
- var isNode = new Function('try {return this===global;}catch(e){return false;}');
50
+ var isNodeEnv = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
51
+ var isNode = function isNode() {
52
+ return isNodeEnv;
53
+ };
51
54
  var symbolEntities = {
52
55
  "'": "'",
53
56
  '\\': '\\',
@@ -92,6 +95,13 @@ var getPath = function getPath(context, name) {
92
95
  });
93
96
  return [data, prop];
94
97
  };
98
+ var ext = function ext(path, defaults) {
99
+ var ext = path.split('.').pop();
100
+ if (ext !== defaults) {
101
+ path = [path, defaults].join('.');
102
+ }
103
+ return path;
104
+ };
95
105
  var extend = function extend() {
96
106
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
97
107
  args[_key] = arguments[_key];
@@ -179,22 +189,22 @@ function element(tag, attrs, content) {
179
189
  var tags = [{
180
190
  symbol: '-',
181
191
  format: function format(value) {
182
- return "'+\n".concat(this.SAFE, "(").concat(value, ",1)+\n'");
192
+ return "')\n".concat(this.BUFFER, "(").concat(this.SAFE, "(").concat(value, ",1))\n").concat(this.BUFFER, "('");
183
193
  }
184
194
  }, {
185
195
  symbol: '=',
186
196
  format: function format(value) {
187
- return "'+\n".concat(this.SAFE, "(").concat(value, ")+\n'");
197
+ return "')\n".concat(this.BUFFER, "(").concat(this.SAFE, "(").concat(value, "))\n").concat(this.BUFFER, "('");
188
198
  }
189
199
  }, {
190
200
  symbol: '#',
191
201
  format: function format(value) {
192
- return "'+\n/**".concat(value, "**/+\n'");
202
+ return "')\n/**".concat(value, "**/\n").concat(this.BUFFER, "('");
193
203
  }
194
204
  }, {
195
205
  symbol: '',
196
206
  format: function format(value) {
197
- return "')\n".concat(value, "\n").concat(this.BUFFER, "('");
207
+ return "')\n".concat(value.trim(), "\n").concat(this.BUFFER, "('");
198
208
  }
199
209
  }];
200
210
  var match = function match(regex, text, callback) {
@@ -208,13 +218,7 @@ var match = function match(regex, text, callback) {
208
218
  return match;
209
219
  });
210
220
  };
211
- /**
212
- *
213
- * @param {object} config
214
- * @return {function(*, *): Function}
215
- * @constructor
216
- */
217
- var Compiler = function Compiler(config) {
221
+ var configureCompiler = function configureCompiler(ejs, config) {
218
222
  var withObject = config.withObject;
219
223
  var token = config.token;
220
224
  var vars = config.vars;
@@ -232,11 +236,7 @@ var Compiler = function Compiler(config) {
232
236
  var regex = new RegExp(matches.join('|').concat('|$'), 'g');
233
237
  var slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
234
238
  var slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
235
- /**
236
- * @type function
237
- * @name Compile
238
- */
239
- return function (content, path) {
239
+ return function compiler(content, path) {
240
240
  var SCOPE = vars.SCOPE,
241
241
  SAFE = vars.SAFE,
242
242
  BUFFER = vars.BUFFER;
@@ -269,24 +269,34 @@ var Compiler = function Compiler(config) {
269
269
  };
270
270
  };
271
271
 
272
- var Wrapper = function Wrapper(config) {
272
+ var configureWrapper = function configureWrapper(config) {
273
273
  var name = config["export"];
274
- return function (list) {
275
- var out = '(function(o){\n';
274
+ var useStrict = config.withObject !== true;
275
+ return function Wrapper(list) {
276
+ var out = '';
277
+ out += '(function(global,factory){';
278
+ out += 'typeof exports === "object" && typeof module !== "undefined" ?';
279
+ out += 'module.exports = factory():';
280
+ out += 'typeof define === "function" && define.amd ? define(factory):';
281
+ out += '(global = typeof globalThis !== "undefined" ? globalThis:';
282
+ out += 'global || self,global["' + name + '"] = factory())';
283
+ out += '})(this,(function(){';
284
+ if (useStrict) out += "'use strict';\n";
285
+ out += 'var list = {};\n';
276
286
  list.forEach(function (item) {
277
- out += 'o[' + JSON.stringify(item.name) + ']=' + String(item.content) + '\n';
287
+ out += 'list[' + JSON.stringify(item.name) + ']=' + String(item.content) + ';\n';
278
288
  });
279
- out += '})(window["' + name + '"] = window["' + name + '"] || {});\n';
289
+ out += 'return list;}));\n';
280
290
  return out;
281
291
  };
282
292
  };
283
293
 
284
- var HttpRequest = function HttpRequest(template) {
285
- return window.fetch(template).then(function (response) {
294
+ var httpRequest = function httpRequest(template) {
295
+ return fetch(template).then(function (response) {
286
296
  return response.text();
287
297
  });
288
298
  };
289
- var FileSystem = function FileSystem(template) {
299
+ var fileSystem = function fileSystem(template) {
290
300
  return new Promise(function (resolve, reject) {
291
301
  fs__default["default"].readFile(template, function (error, data) {
292
302
  if (error) {
@@ -297,7 +307,7 @@ var FileSystem = function FileSystem(template) {
297
307
  });
298
308
  });
299
309
  };
300
- var Watcher = function Watcher(path, cache) {
310
+ var enableWatcher = function enableWatcher(path, cache) {
301
311
  return chokidar__default["default"].watch('.', {
302
312
  cwd: path
303
313
  }).on('change', function (name) {
@@ -306,10 +316,11 @@ var Watcher = function Watcher(path, cache) {
306
316
  console.log('watcher error: ' + error);
307
317
  });
308
318
  };
309
- var Template = function Template(config, cache, compile) {
319
+ var configureTemplate = function configureTemplate(ejs, config) {
310
320
  var path = config.path;
311
- config.token || {};
312
- var resolver = isFunction(config.resolver) ? config.resolver : isNode() ? FileSystem : HttpRequest;
321
+ var cache = ejs.cache,
322
+ compile = ejs.compile;
323
+ var resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
313
324
  var normalize = function normalize(template) {
314
325
  template = [path, template].join('/');
315
326
  template = template.replace(/\/\//g, '/');
@@ -322,19 +333,19 @@ var Template = function Template(config, cache, compile) {
322
333
  cache.set(template, content);
323
334
  return content;
324
335
  };
325
- var get = function get(template) {
326
- if (cache.exist(template)) {
327
- return cache.resolve(template);
336
+ var template = function template(_template) {
337
+ if (cache.exist(_template)) {
338
+ return cache.resolve(_template);
328
339
  }
329
- var content = resolve(template).then(function (content) {
330
- return result(compile(content, template), template);
340
+ var content = resolve(_template).then(function (content) {
341
+ return result(compile(content, _template), _template);
331
342
  });
332
- return result(content, template);
343
+ return result(content, _template);
333
344
  };
334
345
  if (config.watch && isNode()) {
335
- Watcher(path, cache);
346
+ enableWatcher(path, cache);
336
347
  }
337
- return get;
348
+ return template;
338
349
  };
339
350
 
340
351
  var resolve = function resolve(list) {
@@ -342,11 +353,7 @@ var resolve = function resolve(list) {
342
353
  return list.join('');
343
354
  });
344
355
  };
345
- /**
346
- *
347
- * @return {function}
348
- */
349
- var Buffer = function Buffer() {
356
+ var createBuffer = function createBuffer() {
350
357
  var store = [],
351
358
  array = [];
352
359
  function buffer(value) {
@@ -373,7 +380,7 @@ var Buffer = function Buffer() {
373
380
  return buffer;
374
381
  };
375
382
 
376
- var configure = function configure(config, methods) {
383
+ var configureScope = function configureScope(ejs, config) {
377
384
  var _config$vars = config.vars,
378
385
  EXTEND = _config$vars.EXTEND,
379
386
  LAYOUT = _config$vars.LAYOUT,
@@ -401,7 +408,7 @@ var configure = function configure(config, methods) {
401
408
  });
402
409
  };
403
410
  Scope.property(BUFFER, {
404
- value: Buffer(),
411
+ value: createBuffer(),
405
412
  writable: false,
406
413
  configurable: false,
407
414
  enumerable: false
@@ -573,17 +580,17 @@ var configure = function configure(config, methods) {
573
580
  }
574
581
  each(object, callback);
575
582
  });
576
- Scope.helpers(methods);
577
583
  return Scope;
578
584
  };
579
585
 
580
- function Cache(config) {
586
+ var global = typeof globalThis !== 'undefined' ? globalThis : window || self;
587
+ function configureCache(config) {
581
588
  var namespace = config["export"];
582
589
  var list = {};
583
590
  var cache = {
584
591
  preload: function preload() {
585
592
  if (isNode() === false) {
586
- this.load(window[namespace]);
593
+ this.load(global[namespace]);
587
594
  }
588
595
  return this;
589
596
  },
@@ -611,53 +618,40 @@ function Cache(config) {
611
618
  return cache.preload();
612
619
  }
613
620
 
614
- function init(options) {
615
- /**
616
- * @type {Object}
617
- */
621
+ function create(options) {
618
622
  var config = {};
619
- var _helpers = {};
620
- var ext = function ext(path, defaults) {
621
- var ext = path.split('.').pop();
622
- if (ext !== defaults) {
623
- path = [path, defaults].join('.');
624
- }
625
- return path;
626
- };
627
- var view = {
623
+ var ejs = {
628
624
  safeValue: safeValue,
629
625
  element: element,
630
626
  output: function output(path, scope) {
631
- return view.template(path).then(function (template) {
627
+ return ejs.template(path).then(function (template) {
632
628
  return template.call(scope, scope, scope.getBuffer(), safeValue);
633
629
  });
634
630
  },
635
631
  render: function render(name, data) {
636
632
  var filepath = ext(name, config.extension);
637
- var scope = new view.scope(data);
638
- return view.output(filepath, scope).then(function (content) {
633
+ var scope = new ejs.scope(data);
634
+ return ejs.output(filepath, scope).then(function (content) {
639
635
  if (scope.getExtend()) {
640
636
  scope.setExtend(false);
641
637
  var layout = scope.getLayout();
642
638
  var _data = scope.clone();
643
- return view.render(layout, _data);
639
+ return ejs.render(layout, _data);
644
640
  }
645
641
  return content;
646
642
  });
647
643
  },
648
644
  require: function require(name) {
649
645
  var filepath = ext(name, config.extension);
650
- var scope = new view.scope({});
651
- return view.output(filepath, scope).then(function () {
646
+ var scope = new ejs.scope({});
647
+ return ejs.output(filepath, scope).then(function () {
652
648
  return scope.getMacro();
653
649
  });
654
650
  },
655
651
  helpers: function helpers(methods) {
656
- methods = methods || {};
657
- extend(_helpers, methods);
658
- view.scope.helpers(methods);
652
+ ejs.scope.helpers(methods);
659
653
  },
660
- configure: function configure$1(options) {
654
+ configure: function configure(options) {
661
655
  config["export"] = typeProp(isString, defaults["export"], options["export"]);
662
656
  config.path = typeProp(isString, defaults.path, options.path);
663
657
  config.resolver = typeProp(isFunction, defaults.resolver, options.resolver);
@@ -665,12 +659,12 @@ function init(options) {
665
659
  config.withObject = typeProp(isBoolean, defaults.withObject, options.withObject);
666
660
  config.token = extend({}, defaults.token, options.token);
667
661
  config.vars = extend({}, defaults.vars, options.vars);
668
- view.scope = configure(config, _helpers);
669
- view.compile = Compiler(config);
670
- view.wrapper = Wrapper(config);
671
- view.cache = Cache(config);
672
- view.template = Template(config, view.cache, view.compile);
673
- return view;
662
+ ejs.scope = configureScope(ejs, config);
663
+ ejs.compile = configureCompiler(ejs, config);
664
+ ejs.wrapper = configureWrapper(ejs);
665
+ ejs.cache = configureCache(ejs);
666
+ ejs.template = configureTemplate(ejs, config);
667
+ return ejs;
674
668
  },
675
669
  __express: function __express(name, options, callback) {
676
670
  if (isFunction(options)) {
@@ -685,31 +679,26 @@ function init(options) {
685
679
  var filename = path__default["default"].relative(viewPath, name);
686
680
  viewOptions.path = viewPath;
687
681
  viewOptions.cache = viewCache;
688
- view.configure(viewOptions);
689
- return view.render(filename, options).then(function (content) {
682
+ ejs.configure(viewOptions);
683
+ return ejs.render(filename, options).then(function (content) {
690
684
  callback(null, content);
691
685
  })["catch"](function (error) {
692
686
  callback(error);
693
687
  });
694
688
  }
695
689
  };
696
- /**
697
- *
698
- */
699
- view.configure(options || {});
700
- /**
701
- *
702
- */
703
- view.helpers({
690
+ ejs.configure(options || {});
691
+ ejs.helpers({
704
692
  require: function require(name) {
705
- return view.require(name, this);
693
+ return ejs.require(name, this);
706
694
  },
707
695
  render: function render(name, data) {
708
- return view.render(name, data);
696
+ return ejs.render(name, data);
709
697
  }
710
698
  });
711
- return view;
699
+ return ejs;
712
700
  }
713
- var index = init();
701
+ var instance = create();
702
+ instance.create = create;
714
703
 
715
- module.exports = index;
704
+ module.exports = instance;