@kosatyi/ejs 0.0.29 → 0.0.31

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
@@ -68,7 +68,6 @@ $ npm install ejs@npm:@kosatyi/ejs --save
68
68
  ```
69
69
 
70
70
  ```js
71
- // const ejs = require('ejs')
72
71
  const express = require('express')
73
72
  const app = express()
74
73
  app.set('views', 'views')
@@ -85,22 +84,22 @@ app.set('view options', {
85
84
  ```ejs
86
85
  <html>
87
86
  <head>
88
- <title><%-get('title')%></title>
89
- <% block('resources',()=>{ %>
87
+ <title><%-ejs.get('title')%></title>
88
+ <% ejs.block('resources',()=>{ %>
90
89
  <link rel="stylesheet" type="text/css" href="/dist/styles.css">
91
90
  <% }) %>
92
91
  </head>
93
92
  <body>
94
93
  <header>
95
- <% block('header',()=>{ %>
96
- <h1><%-get('title')%></h1>
94
+ <% ejs.block('header',()=>{ %>
95
+ <h1><%-ejs.get('title')%></h1>
97
96
  <% }) %>
98
97
  </header>
99
98
  <main>
100
- <% block('content') %>
99
+ <% ejs.block('content') %>
101
100
  </main>
102
101
  <footer>
103
- <% block('footer',()=>{ %>
102
+ <% ejs.block('footer',()=>{ %>
104
103
  Copyright
105
104
  <% }) %>
106
105
  </footer>
@@ -111,18 +110,18 @@ app.set('view options', {
111
110
  **page/index.ejs**
112
111
 
113
112
  ```ejs
114
- <% extend('layout/default') %>
113
+ <% ejs.extend('layout/default') %>
115
114
 
116
- <% set('title','Page Title') %>
115
+ <% ejs.set('title','Page Title') %>
117
116
 
118
- <% block('resources',(parent)=>{ %>
117
+ <% ejs.block('resources',(parent)=>{ %>
119
118
  <% parent() %>
120
119
  <script defer src="/dist/framework.js"></script>
121
120
  <% }) %>
122
121
 
123
- <% block('content',()=>{ %>
122
+ <% ejs.block('content',()=>{ %>
124
123
 
125
- <% each('posts',(post)=>{ %>
124
+ <% ejs.each('posts',(post)=>{ %>
126
125
  <article>
127
126
  <h3><%-post.title%></h3>
128
127
  <div><%=post.content%></div>
package/dist/ejs.cjs CHANGED
@@ -4,12 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
- require('chokidar');
7
+ var chokidar = require('chokidar');
8
8
 
9
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
10
 
11
11
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
12
12
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
13
+ var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
13
14
 
14
15
  var defaults = {};
15
16
  defaults["export"] = 'ejs.precompiled';
@@ -17,6 +18,7 @@ defaults.cache = true;
17
18
  defaults.path = 'views';
18
19
  defaults.resolver = null;
19
20
  defaults.extension = 'ejs';
21
+ defaults.rmWhitespace = true;
20
22
  defaults.withObject = false;
21
23
  defaults.vars = {
22
24
  SCOPE: 'ejs',
@@ -280,6 +282,7 @@ var Compiler = /*#__PURE__*/function () {
280
282
  value: function configure(config) {
281
283
  var _this = this;
282
284
  this.withObject = config.withObject;
285
+ this.rmWhitespace = config.rmWhitespace;
283
286
  this.token = config.token;
284
287
  this.vars = config.vars;
285
288
  this.matches = [];
@@ -294,8 +297,13 @@ var Compiler = /*#__PURE__*/function () {
294
297
  _this.formats.push(item.format.bind(_this.vars));
295
298
  });
296
299
  this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
297
- this.slurpStart = new RegExp([this.slurp.match, this.slurp.start].join(''), 'gm');
298
- this.slurpEnd = new RegExp([this.slurp.end, this.slurp.match].join(''), 'gm');
300
+ this.slurpStart = new RegExp([this.slurp.match, this.slurp.start.join('')].join(''), 'gm');
301
+ this.slurpEnd = new RegExp([this.slurp.end.join(''), this.slurp.match].join(''), 'gm');
302
+ }
303
+ }, {
304
+ key: "truncate",
305
+ value: function truncate(value) {
306
+ return value && value.replace(/^(?:\r\n|\r|\n)/, '');
299
307
  }
300
308
  }, {
301
309
  key: "compile",
@@ -305,13 +313,18 @@ var Compiler = /*#__PURE__*/function () {
305
313
  SCOPE = _this$vars.SCOPE,
306
314
  SAFE = _this$vars.SAFE,
307
315
  BUFFER = _this$vars.BUFFER;
308
- content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
309
- content = content.replace(this.slurpStart, this.slurp.start).replace(this.slurpEnd, this.slurp.end);
316
+ if (this.rmWhitespace) {
317
+ content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
318
+ }
319
+ content = content.replace(this.slurpStart, this.token.start).replace(this.slurpEnd, this.token.end);
310
320
  var source = "".concat(BUFFER, "('");
311
321
  matchTokens(this.regex, content, function (params, index, offset) {
312
322
  source += symbols(content.slice(index, offset));
313
323
  params.forEach(function (value, index) {
314
- if (value) source += _this2.formats[index](value);
324
+ //value = this.truncate(value)
325
+ if (value) {
326
+ source += _this2.formats[index](value);
327
+ }
315
328
  });
316
329
  });
317
330
  source += "');";
@@ -370,14 +383,19 @@ var Bundler = /*#__PURE__*/function () {
370
383
  return Bundler;
371
384
  }();
372
385
 
373
- var httpRequest = function httpRequest(template) {
374
- return fetch(template).then(function (response) {
386
+ var resolvePath = function resolvePath(path, template) {
387
+ template = [path, template].join('/');
388
+ template = template.replace(/\/\//g, '/');
389
+ return template;
390
+ };
391
+ var httpRequest = function httpRequest(path, template) {
392
+ return fetch(resolvePath(path, template)).then(function (response) {
375
393
  return response.text();
376
394
  });
377
395
  };
378
- var fileSystem = function fileSystem(template) {
396
+ var fileSystem = function fileSystem(path, template) {
379
397
  return new Promise(function (resolve, reject) {
380
- fs__default["default"].readFile(template, function (error, data) {
398
+ fs__default["default"].readFile(resolvePath(path, template), function (error, data) {
381
399
  if (error) {
382
400
  reject(error);
383
401
  } else {
@@ -386,15 +404,25 @@ var fileSystem = function fileSystem(template) {
386
404
  });
387
405
  });
388
406
  };
389
- var normalizePath = function normalizePath(path, template) {
390
- template = [path, template].join('/');
391
- template = template.replace(/\/\//g, '/');
392
- return template;
407
+ var disableWatcher = function disableWatcher(watcher) {
408
+ if (watcher) {
409
+ watcher.unwatch('.');
410
+ }
411
+ };
412
+ var enableWatcher = function enableWatcher(path, cache) {
413
+ return chokidar__default["default"].watch('.', {
414
+ cwd: path
415
+ }).on('change', function (name) {
416
+ cache.remove(name);
417
+ }).on('error', function (error) {
418
+ console.log('watcher error: ' + error);
419
+ });
393
420
  };
394
421
  var Template = /*#__PURE__*/function () {
395
422
  function Template(config, cache, compiler) {
396
423
  _classCallCheck(this, Template);
397
424
  this.cache = cache;
425
+ this.watcher = null;
398
426
  this.compiler = compiler;
399
427
  this.configure(config);
400
428
  }
@@ -403,18 +431,31 @@ var Template = /*#__PURE__*/function () {
403
431
  value: function configure(config) {
404
432
  this.path = config.path;
405
433
  this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
434
+ disableWatcher(this.watcher);
435
+ if (config.watch && isNode()) {
436
+ this.watcher = enableWatcher(this.cache, this.path);
437
+ }
406
438
  }
407
439
  }, {
408
440
  key: "resolve",
409
441
  value: function resolve(template) {
410
- return this.resolver(normalizePath(this.path, template));
442
+ return this.resolver(this.path, template);
411
443
  }
412
444
  }, {
413
445
  key: "result",
414
- value: function result(content, template) {
446
+ value: function result(template, content) {
415
447
  this.cache.set(template, content);
416
448
  return content;
417
449
  }
450
+ }, {
451
+ key: "compile",
452
+ value: function compile(content, template) {
453
+ if (isFunction(content)) {
454
+ return content;
455
+ } else {
456
+ return this.compiler.compile(content, template);
457
+ }
458
+ }
418
459
  }, {
419
460
  key: "get",
420
461
  value: function get(template) {
@@ -423,9 +464,9 @@ var Template = /*#__PURE__*/function () {
423
464
  return this.cache.resolve(template);
424
465
  }
425
466
  var content = this.resolve(template).then(function (content) {
426
- return _this.result(_this.compiler.compile(content, template), template);
467
+ return _this.result(template, _this.compile(content, template));
427
468
  });
428
- return this.result(content, template);
469
+ return this.result(template, content);
429
470
  }
430
471
  }]);
431
472
  return Template;
@@ -714,6 +755,7 @@ var configSchema = function configSchema(config, options) {
714
755
  resolver: typeProp(isFunction, defaults.resolver, config.resolver, options.resolver),
715
756
  extension: typeProp(isString, defaults.extension, config.extension, options.extension),
716
757
  withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
758
+ rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
717
759
  token: extend({}, defaults.token, config.token, options.token),
718
760
  vars: extend({}, defaults.vars, config.vars, options.vars)
719
761
  });
package/dist/ejs.js CHANGED
@@ -12,6 +12,7 @@
12
12
  defaults.path = 'views';
13
13
  defaults.resolver = null;
14
14
  defaults.extension = 'ejs';
15
+ defaults.rmWhitespace = true;
15
16
  defaults.withObject = false;
16
17
  defaults.vars = {
17
18
  SCOPE: 'ejs',
@@ -275,6 +276,7 @@
275
276
  value: function configure(config) {
276
277
  var _this = this;
277
278
  this.withObject = config.withObject;
279
+ this.rmWhitespace = config.rmWhitespace;
278
280
  this.token = config.token;
279
281
  this.vars = config.vars;
280
282
  this.matches = [];
@@ -289,8 +291,13 @@
289
291
  _this.formats.push(item.format.bind(_this.vars));
290
292
  });
291
293
  this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
292
- this.slurpStart = new RegExp([this.slurp.match, this.slurp.start].join(''), 'gm');
293
- this.slurpEnd = new RegExp([this.slurp.end, this.slurp.match].join(''), 'gm');
294
+ this.slurpStart = new RegExp([this.slurp.match, this.slurp.start.join('')].join(''), 'gm');
295
+ this.slurpEnd = new RegExp([this.slurp.end.join(''), this.slurp.match].join(''), 'gm');
296
+ }
297
+ }, {
298
+ key: "truncate",
299
+ value: function truncate(value) {
300
+ return value && value.replace(/^(?:\r\n|\r|\n)/, '');
294
301
  }
295
302
  }, {
296
303
  key: "compile",
@@ -300,13 +307,18 @@
300
307
  SCOPE = _this$vars.SCOPE,
301
308
  SAFE = _this$vars.SAFE,
302
309
  BUFFER = _this$vars.BUFFER;
303
- content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
304
- content = content.replace(this.slurpStart, this.slurp.start).replace(this.slurpEnd, this.slurp.end);
310
+ if (this.rmWhitespace) {
311
+ content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
312
+ }
313
+ content = content.replace(this.slurpStart, this.token.start).replace(this.slurpEnd, this.token.end);
305
314
  var source = "".concat(BUFFER, "('");
306
315
  matchTokens(this.regex, content, function (params, index, offset) {
307
316
  source += symbols(content.slice(index, offset));
308
317
  params.forEach(function (value, index) {
309
- if (value) source += _this2.formats[index](value);
318
+ //value = this.truncate(value)
319
+ if (value) {
320
+ source += _this2.formats[index](value);
321
+ }
310
322
  });
311
323
  });
312
324
  source += "');";
@@ -365,14 +377,19 @@
365
377
  return Bundler;
366
378
  }();
367
379
 
368
- var httpRequest = function httpRequest(template) {
369
- return fetch(template).then(function (response) {
380
+ var resolvePath = function resolvePath(path, template) {
381
+ template = [path, template].join('/');
382
+ template = template.replace(/\/\//g, '/');
383
+ return template;
384
+ };
385
+ var httpRequest = function httpRequest(path, template) {
386
+ return fetch(resolvePath(path, template)).then(function (response) {
370
387
  return response.text();
371
388
  });
372
389
  };
373
- var fileSystem = function fileSystem(template) {
390
+ var fileSystem = function fileSystem(path$1, template) {
374
391
  return new Promise(function (resolve, reject) {
375
- path.readFile(template, function (error, data) {
392
+ path.readFile(resolvePath(path$1, template), function (error, data) {
376
393
  if (error) {
377
394
  reject(error);
378
395
  } else {
@@ -381,15 +398,25 @@
381
398
  });
382
399
  });
383
400
  };
384
- var normalizePath = function normalizePath(path, template) {
385
- template = [path, template].join('/');
386
- template = template.replace(/\/\//g, '/');
387
- return template;
401
+ var disableWatcher = function disableWatcher(watcher) {
402
+ if (watcher) {
403
+ watcher.unwatch('.');
404
+ }
405
+ };
406
+ var enableWatcher = function enableWatcher(path$1, cache) {
407
+ return path.watch('.', {
408
+ cwd: path$1
409
+ }).on('change', function (name) {
410
+ cache.remove(name);
411
+ }).on('error', function (error) {
412
+ console.log('watcher error: ' + error);
413
+ });
388
414
  };
389
415
  var Template = /*#__PURE__*/function () {
390
416
  function Template(config, cache, compiler) {
391
417
  _classCallCheck(this, Template);
392
418
  this.cache = cache;
419
+ this.watcher = null;
393
420
  this.compiler = compiler;
394
421
  this.configure(config);
395
422
  }
@@ -398,18 +425,31 @@
398
425
  value: function configure(config) {
399
426
  this.path = config.path;
400
427
  this.resolver = isFunction(config.resolver) ? config.resolver : isNode() ? fileSystem : httpRequest;
428
+ disableWatcher(this.watcher);
429
+ if (config.watch && isNode()) {
430
+ this.watcher = enableWatcher(this.cache, this.path);
431
+ }
401
432
  }
402
433
  }, {
403
434
  key: "resolve",
404
435
  value: function resolve(template) {
405
- return this.resolver(normalizePath(this.path, template));
436
+ return this.resolver(this.path, template);
406
437
  }
407
438
  }, {
408
439
  key: "result",
409
- value: function result(content, template) {
440
+ value: function result(template, content) {
410
441
  this.cache.set(template, content);
411
442
  return content;
412
443
  }
444
+ }, {
445
+ key: "compile",
446
+ value: function compile(content, template) {
447
+ if (isFunction(content)) {
448
+ return content;
449
+ } else {
450
+ return this.compiler.compile(content, template);
451
+ }
452
+ }
413
453
  }, {
414
454
  key: "get",
415
455
  value: function get(template) {
@@ -418,9 +458,9 @@
418
458
  return this.cache.resolve(template);
419
459
  }
420
460
  var content = this.resolve(template).then(function (content) {
421
- return _this.result(_this.compiler.compile(content, template), template);
461
+ return _this.result(template, _this.compile(content, template));
422
462
  });
423
- return this.result(content, template);
463
+ return this.result(template, content);
424
464
  }
425
465
  }]);
426
466
  return Template;
@@ -709,6 +749,7 @@
709
749
  resolver: typeProp(isFunction, defaults.resolver, config.resolver, options.resolver),
710
750
  extension: typeProp(isString, defaults.extension, config.extension, options.extension),
711
751
  withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
752
+ rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
712
753
  token: extend({}, defaults.token, config.token, options.token),
713
754
  vars: extend({}, defaults.vars, config.vars, options.vars)
714
755
  });
package/dist/ejs.min.js CHANGED
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ejs={})}(this,(function(t){"use strict";var e={},n={export:"ejs.precompiled",cache:!0,path:"views",resolver:null,extension:"ejs",withObject:!1,vars:{SCOPE:"ejs",EXTEND:"$$e",BUFFER:"$$a",LAYOUT:"$$l",BLOCKS:"$$b",MACRO:"$$m",SAFE:"$$v"},token:{start:"<%",end:"%>",regex:"([\\s\\S]+?)"}},r=function(){var t=[].slice.call(arguments),e=t.shift();return t.filter(e).pop()},i=function(t){return"function"==typeof t},o=function(t){return"string"==typeof t},c=function(t){return"boolean"==typeof t},u="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),s=function(){return u},a={"'":"'","\\":"\\","\r":"r","\n":"n","\t":"t","\u2028":"u2028","\u2029":"u2029"},f={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;"},h=function(t){return new RegExp(["[",Object.keys(t).join(""),"]"].join(""),"g")},l=h(f),p=h(a),v=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return(""+t).replace(l,(function(t){return f[t]}))},d=function(t,e,n){return null==(n=t)?"":e?v(n):n},m=function(t,e){var n=t,r=e.split("."),i=r.pop();return r.forEach((function(t){n=n[t]=n[t]||{}})),[n,i]},g=function(t,e){var n=t.split(".").pop();return n!==e&&(t=[t,e].join(".")),t},y=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var r=e.shift();return e.filter((function(t){return t})).reduce((function(t,e){return Object.assign(t,e)}),r)},b=function(){},w=function(t,e){var n;for(n in t)k(t,n)&&e(t[n],n,t)},x=function(t,e){return function(t,e,n){var r=t instanceof Array,i=r?[]:{};return w(t,(function(t,n,o){var c=e(t,n,o);void 0!==c&&(r?i.push(c):i[n]=c)})),i}(t,(function(t,n){if(-1===e.indexOf(n))return t}))},j=function(t,e,n){return Promise.resolve(t).then(e.bind(n))},k=function(t,e){return t&&t.hasOwnProperty(e)},E=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"],O=" ",F='"',P="/",S="<",B=">",R=function(t,e,n){var r=[],i=-1===E.indexOf(t),o=function(t,e,n){var r=[];return w(t,(function(t,n,i){var o=e(t,n,i);void 0!==o&&r.push(o)})),r}(e,(function(t,e){if(null!=t)return[v(e),[F,v(t),F].join("")].join("=")})).join(O);return r.push([S,t,O,o,B].join("")),n&&r.push(n instanceof Array?n.join(""):n),i&&r.push([S,P,t,B].join("")),r.join("")};function $(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function T(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,A(r.key),r)}}function U(t,e,n){return e&&T(t.prototype,e),n&&T(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function A(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var M=[{symbol:"-",format:function(t){return"')\n".concat(this.BUFFER,"(").concat(this.SAFE,"(").concat(t,",1))\n").concat(this.BUFFER,"('")}},{symbol:"=",format:function(t){return"')\n".concat(this.BUFFER,"(").concat(this.SAFE,"(").concat(t,"))\n").concat(this.BUFFER,"('")}},{symbol:"#",format:function(t){return"')\n/**".concat(t,"**/\n").concat(this.BUFFER,"('")}},{symbol:"",format:function(t){return"')\n".concat(t.trim(),"\n").concat(this.BUFFER,"('")}}],_=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t){var e=this;this.withObject=t.withObject,this.token=t.token,this.vars=t.vars,this.matches=[],this.formats=[],this.slurp={match:"[ \\t]*",start:[this.token.start,"_"],end:["_",this.token.end]},M.forEach((function(t){e.matches.push(e.token.start.concat(t.symbol).concat(e.token.regex).concat(e.token.end)),e.formats.push(t.format.bind(e.vars))})),this.regex=new RegExp(this.matches.join("|").concat("|$"),"g"),this.slurpStart=new RegExp([this.slurp.match,this.slurp.start].join(""),"gm"),this.slurpEnd=new RegExp([this.slurp.end,this.slurp.match].join(""),"gm")}},{key:"compile",value:function(t,e){var n=this,r=this.vars,i=r.SCOPE,o=r.SAFE,c=r.BUFFER;t=(t=t.replace(/[\r\n]+/g,"\n").replace(/^\s+|\s+$/gm,"")).replace(this.slurpStart,this.slurp.start).replace(this.slurpEnd,this.slurp.end);var u,s,f,h="".concat(c,"('");u=this.regex,s=function(e,r,i){h+=(""+t.slice(r,i)).replace(p,(function(t){return"\\"+a[t]})),e.forEach((function(t,e){t&&(h+=n.formats[e](t))}))},f=0,t.replace(u,(function(){var t=[].slice.call(arguments,0,-1),e=t.pop(),n=t.shift();return s(t,f,e),f=e+n.length,n})),h="try{".concat(h+="');","}catch(e){console.info(e)}"),this.withObject&&(h="with(".concat(i,"){").concat(h,"}")),h="".concat(c,".start();").concat(h,"return ").concat(c,".end();"),h+="\n//# sourceURL=".concat(e);var l=null;try{(l=new Function(i,c,o,h)).source="(function(".concat(i,",").concat(c,",").concat(o,"){\n").concat(h,"\n})")}catch(t){throw t.filename=e,t.source=h,t}return l}}]),t}(),L=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t){this.namespace=t.export,this.useStrict=!1===t.withObject}},{key:"wrapper",value:function(t){var e="";return e+="(function(global,factory){",e+='typeof exports === "object" && typeof module !== "undefined" ?',e+="module.exports = factory():",e+='typeof define === "function" && define.amd ? define(factory):',e+='(global = typeof globalThis !== "undefined" ? globalThis:',e+='global || self,global["'+this.namespace+'"] = factory())',e+="})(this,(function(){",this.useStrict&&(e+="'use strict';\n"),e+="var list = {};\n",t.forEach((function(t){e+="list["+JSON.stringify(t.name)+"]="+String(t.content)+";\n"})),e+="return list;}));\n"}}]),t}(),C=function(t){return fetch(t).then((function(t){return t.text()}))},N=function(t){return new Promise((function(n,r){e.readFile(t,(function(t,e){t?r(t):n(e.toString())}))}))},q=function(){function t(e,n,r){$(this,t),this.cache=n,this.compiler=r,this.configure(e)}return U(t,[{key:"configure",value:function(t){this.path=t.path,this.resolver=i(t.resolver)?t.resolver:s()?N:C}},{key:"resolve",value:function(t){return this.resolver(function(t,e){return(e=[t,e].join("/")).replace(/\/\//g,"/")}(this.path,t))}},{key:"result",value:function(t,e){return this.cache.set(e,t),t}},{key:"get",value:function(t){var e=this;if(this.cache.exist(t))return this.cache.resolve(t);var n=this.resolve(t).then((function(n){return e.result(e.compiler.compile(n,t),t)}));return this.result(n,t)}}]),t}(),D=function(t){return Promise.all(t).then((function(t){return t.join("")}))},K=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t,e){var n=t.vars,r=n.EXTEND,c=n.LAYOUT,u=n.BLOCKS,s=n.BUFFER,a=n.MACRO;function f(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.initBlocks(),this.initMacro(),y(this,t)}this.create=function(t){return new f(t)},this.helpers=function(t){y(f.prototype,t)},f.prototype=y({},e||{}),f.defineProp=f.method=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=arguments.length>4&&void 0!==arguments[4]&&arguments[4];Object.defineProperty(f.prototype,t,{value:e,writable:n,configurable:r,enumerable:i})},f.defineProp(s,function(){var t=[],e=[];function n(t){e.push(t)}return n.start=function(){e=[]},n.backup=function(){t.push(e.concat()),e=[]},n.restore=function(){var n=e.concat();return e=t.pop(),D(n)},n.error=function(t){throw t},n.end=function(){return D(e)},n}()),f.defineProp(u,{},!0),f.defineProp(a,{},!0),f.defineProp(c,!1,!0),f.defineProp(r,!1,!0),f.method("initBlocks",(function(){this[u]={}})),f.method("initMacro",(function(){this[a]={}})),f.method("getMacro",(function(){return this[a]})),f.method("getBuffer",(function(){return this[s]})),f.method("getBlocks",(function(){return this[u]})),f.method("setExtend",(function(t){this[r]=t})),f.method("getExtend",(function(){return this[r]})),f.method("setLayout",(function(t){this[c]=t})),f.method("getLayout",(function(){return this[c]})),f.method("clone",(function(t){var e=[c,r,s];return!0===t&&e.push(u),x(this,e)})),f.method("extend",(function(t){this.setExtend(!0),this.setLayout(t)})),f.method("echo",(function(){var t=this.getBuffer(),e=[].slice.call(arguments);e.forEach(t)})),f.method("fn",(function(t){var e=this.getBuffer(),n=this;return function(){return e.backup(),i(t)&&t.apply(n,arguments),e.restore()}})),f.method("get",(function(t,e){var n=m(this,t),r=n.shift(),i=n.pop();return k(r,i)?r[i]:e})),f.method("set",(function(t,e){var n=m(this,t),r=n.shift(),i=n.pop();return this.getExtend()&&k(r,i)?r[i]:r[i]=e})),f.method("macro",(function(t,e){var n=this.getMacro(),r=this.fn(e),i=this;n[t]=function(){return i.echo(r.apply(void 0,arguments))}})),f.method("call",(function(t){var e=this.getMacro(),n=e[t],r=[].slice.call(arguments,1);if(i(n))return n.apply(n,r)})),f.method("block",(function(t,e){var n=this,r=this.getBlocks();if(r[t]=r[t]||[],r[t].push(this.fn(e)),!this.getExtend()){var i=Object.assign([],r[t]),o=function(){return i.shift()};this.echo(o()(function t(){var e=o();return e?function(){n.echo(e(t()))}:b}()))}})),f.method("include",(function(t,e,n){var r=!1===n?{}:this.clone(!0),i=y(r,e||{}),o=this.render(t,i);this.echo(o)})),f.method("use",(function(t,e){var n=this.require(t);this.echo(j(n,(function(t){var n=this.getMacro();w(t,(function(t,r){n[[e,r].join(".")]=t}))}),this))})),f.method("async",(function(t,e){this.echo(j(t,(function(t){return this.fn(e)(t)}),this))})),f.method("node",(function(t,e,n){return R(t,e,n)})),f.method("el",(function(t,e,n){i(n)&&(n=this.fn(n)()),this.echo(j(n,(function(n){return R(t,e,n)}),this))})),f.method("each",(function(t,e){o(t)&&(t=this.get(t,[])),w(t,e)}))}}]),t}(),X="undefined"!=typeof globalThis?globalThis:window||self,Y=function(){function t(e){var n,r,i;$(this,t),n=this,i={},(r=A(r="list"))in n?Object.defineProperty(n,r,{value:i,enumerable:!0,configurable:!0,writable:!0}):n[r]=i,this.configure(e),!1===s()&&this.load(X[this.namespace])}return U(t,[{key:"configure",value:function(t){this.list={},this.namespace=t.export}},{key:"load",value:function(t){return y(this.list,t),this}},{key:"exist",value:function(t){return k(this.list,t)}},{key:"get",value:function(t){return this.list[t]}},{key:"remove",value:function(t){delete this.list[t]}},{key:"resolve",value:function(t){return Promise.resolve(this.get(t))}},{key:"set",value:function(t,e){return this.list[t]=e,this}}]),t}(),J=function(t,e){y(t,{export:r(o,n.export,t.export,e.export),path:r(o,n.path,t.path,e.path),resolver:r(i,n.resolver,t.resolver,e.resolver),extension:r(o,n.extension,t.extension,e.extension),withObject:r(c,n.withObject,t.withObject,e.withObject),token:y({},n.token,t.token,e.token),vars:y({},n.vars,t.vars,e.vars)})},V=function t(u){var s={},a={};J(s,u||{});var f=new K(s),h=new _(s),l=new L(s),p=new Y(s),v=new q(s,p,h),m=function(t){J(s,t),f.configure(s,a),h.configure(s),l.configure(s),p.configure(s),v.configure(s)},b=function(t,e){return v.get(t).then((function(t){return t.call(e,e,e.getBuffer(),d)}))},w=function(t,e){var n=g(t,s.extension),r=f.create(e);return b(n,r).then((function(t){if(r.getExtend()){r.setExtend(!1);var e=r.getLayout(),n=r.clone();return w(e,n)}return t}))},x=function(t){f.helpers(y(a,t||{}))};return x({require:function(t){return function(t){var e=g(t,s.extension),n=f.create({});return b(e,n).then((function(){return n.getMacro()}))}(t)},render:function(t,e){return w(t,e)}}),{render:w,helpers:x,configure:m,wrapper:function(t){return l.wrapper(t)},compile:function(t,e){return h.compile(t,e)},create:function(e){return t(e)},preload:function(t){return p.load(t)},__express:function(t,u,s){i(u)&&(s=u,u={});var a=y({},(u=u||{}).settings),f=r(o,n.path,a.views),h=r(c,n.cache,a["view cache"]),l=y({},a["view options"]),p=e.relative(f,t);return l.path=f,l.cache=h,m(l),w(p,u).then((function(t){s(null,t)})).catch((function(t){s(t)}))}}}({}),z=V.render,G=V.helpers,H=V.configure,I=V.wrapper,Q=V.compile,W=V.create,Z=V.preload,tt=V.__express;t.__express=tt,t.compile=Q,t.configure=H,t.create=W,t.element=R,t.helpers=G,t.preload=Z,t.render=z,t.safeValue=d,t.wrapper=I,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ejs={})}(this,(function(t){"use strict";var e={},n={export:"ejs.precompiled",cache:!0,path:"views",resolver:null,extension:"ejs",rmWhitespace:!0,withObject:!1,vars:{SCOPE:"ejs",EXTEND:"$$e",BUFFER:"$$a",LAYOUT:"$$l",BLOCKS:"$$b",MACRO:"$$m",SAFE:"$$v"},token:{start:"<%",end:"%>",regex:"([\\s\\S]+?)"}},r=function(){var t=[].slice.call(arguments),e=t.shift();return t.filter(e).pop()},i=function(t){return"function"==typeof t},o=function(t){return"string"==typeof t},c=function(t){return"boolean"==typeof t},u="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),s=function(){return u},a={"'":"'","\\":"\\","\r":"r","\n":"n","\t":"t","\u2028":"u2028","\u2029":"u2029"},f={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;"},h=function(t){return new RegExp(["[",Object.keys(t).join(""),"]"].join(""),"g")},l=h(f),p=h(a),v=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return(""+t).replace(l,(function(t){return f[t]}))},d=function(t,e,n){return null==(n=t)?"":e?v(n):n},m=function(t,e){var n=t,r=e.split("."),i=r.pop();return r.forEach((function(t){n=n[t]=n[t]||{}})),[n,i]},g=function(t,e){var n=t.split(".").pop();return n!==e&&(t=[t,e].join(".")),t},y=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var r=e.shift();return e.filter((function(t){return t})).reduce((function(t,e){return Object.assign(t,e)}),r)},b=function(){},w=function(t,e){var n;for(n in t)k(t,n)&&e(t[n],n,t)},j=function(t,e){return function(t,e,n){var r=t instanceof Array,i=r?[]:{};return w(t,(function(t,n,o){var c=e(t,n,o);void 0!==c&&(r?i.push(c):i[n]=c)})),i}(t,(function(t,n){if(-1===e.indexOf(n))return t}))},x=function(t,e,n){return Promise.resolve(t).then(e.bind(n))},k=function(t,e){return t&&t.hasOwnProperty(e)},E=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"],O=" ",F='"',P="/",S="<",B=">",R=function(t,e,n){var r=[],i=-1===E.indexOf(t),o=function(t,e,n){var r=[];return w(t,(function(t,n,i){var o=e(t,n,i);void 0!==o&&r.push(o)})),r}(e,(function(t,e){if(null!=t)return[v(e),[F,v(t),F].join("")].join("=")})).join(O);return r.push([S,t,O,o,B].join("")),n&&r.push(n instanceof Array?n.join(""):n),i&&r.push([S,P,t,B].join("")),r.join("")};function $(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function T(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,A(r.key),r)}}function U(t,e,n){return e&&T(t.prototype,e),n&&T(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function A(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var M=[{symbol:"-",format:function(t){return"')\n".concat(this.BUFFER,"(").concat(this.SAFE,"(").concat(t,",1))\n").concat(this.BUFFER,"('")}},{symbol:"=",format:function(t){return"')\n".concat(this.BUFFER,"(").concat(this.SAFE,"(").concat(t,"))\n").concat(this.BUFFER,"('")}},{symbol:"#",format:function(t){return"')\n/**".concat(t,"**/\n").concat(this.BUFFER,"('")}},{symbol:"",format:function(t){return"')\n".concat(t.trim(),"\n").concat(this.BUFFER,"('")}}],_=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t){var e=this;this.withObject=t.withObject,this.rmWhitespace=t.rmWhitespace,this.token=t.token,this.vars=t.vars,this.matches=[],this.formats=[],this.slurp={match:"[ \\t]*",start:[this.token.start,"_"],end:["_",this.token.end]},M.forEach((function(t){e.matches.push(e.token.start.concat(t.symbol).concat(e.token.regex).concat(e.token.end)),e.formats.push(t.format.bind(e.vars))})),this.regex=new RegExp(this.matches.join("|").concat("|$"),"g"),this.slurpStart=new RegExp([this.slurp.match,this.slurp.start.join("")].join(""),"gm"),this.slurpEnd=new RegExp([this.slurp.end.join(""),this.slurp.match].join(""),"gm")}},{key:"truncate",value:function(t){return t&&t.replace(/^(?:\r\n|\r|\n)/,"")}},{key:"compile",value:function(t,e){var n=this,r=this.vars,i=r.SCOPE,o=r.SAFE,c=r.BUFFER;this.rmWhitespace&&(t=t.replace(/[\r\n]+/g,"\n").replace(/^\s+|\s+$/gm,"")),t=t.replace(this.slurpStart,this.token.start).replace(this.slurpEnd,this.token.end);var u,s,f,h="".concat(c,"('");u=this.regex,s=function(e,r,i){h+=(""+t.slice(r,i)).replace(p,(function(t){return"\\"+a[t]})),e.forEach((function(t,e){t&&(h+=n.formats[e](t))}))},f=0,t.replace(u,(function(){var t=[].slice.call(arguments,0,-1),e=t.pop(),n=t.shift();return s(t,f,e),f=e+n.length,n})),h="try{".concat(h+="');","}catch(e){console.info(e)}"),this.withObject&&(h="with(".concat(i,"){").concat(h,"}")),h="".concat(c,".start();").concat(h,"return ").concat(c,".end();"),h+="\n//# sourceURL=".concat(e);var l=null;try{(l=new Function(i,c,o,h)).source="(function(".concat(i,",").concat(c,",").concat(o,"){\n").concat(h,"\n})")}catch(t){throw t.filename=e,t.source=h,t}return l}}]),t}(),L=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t){this.namespace=t.export,this.useStrict=!1===t.withObject}},{key:"wrapper",value:function(t){var e="";return e+="(function(global,factory){",e+='typeof exports === "object" && typeof module !== "undefined" ?',e+="module.exports = factory():",e+='typeof define === "function" && define.amd ? define(factory):',e+='(global = typeof globalThis !== "undefined" ? globalThis:',e+='global || self,global["'+this.namespace+'"] = factory())',e+="})(this,(function(){",this.useStrict&&(e+="'use strict';\n"),e+="var list = {};\n",t.forEach((function(t){e+="list["+JSON.stringify(t.name)+"]="+String(t.content)+";\n"})),e+="return list;}));\n"}}]),t}(),W=function(t,e){return e=(e=[t,e].join("/")).replace(/\/\//g,"/")},C=function(t,e){return fetch(W(t,e)).then((function(t){return t.text()}))},N=function(t,n){return new Promise((function(r,i){e.readFile(W(t,n),(function(t,e){t?i(t):r(e.toString())}))}))},q=function(){function t(e,n,r){$(this,t),this.cache=n,this.watcher=null,this.compiler=r,this.configure(e)}return U(t,[{key:"configure",value:function(t){var n,r,o;this.path=t.path,this.resolver=i(t.resolver)?t.resolver:s()?N:C,(n=this.watcher)&&n.unwatch("."),t.watch&&s()&&(this.watcher=(r=this.cache,o=this.path,e.watch(".",{cwd:r}).on("change",(function(t){o.remove(t)})).on("error",(function(t){console.log("watcher error: "+t)}))))}},{key:"resolve",value:function(t){return this.resolver(this.path,t)}},{key:"result",value:function(t,e){return this.cache.set(t,e),e}},{key:"compile",value:function(t,e){return i(t)?t:this.compiler.compile(t,e)}},{key:"get",value:function(t){var e=this;if(this.cache.exist(t))return this.cache.resolve(t);var n=this.resolve(t).then((function(n){return e.result(t,e.compile(n,t))}));return this.result(t,n)}}]),t}(),D=function(t){return Promise.all(t).then((function(t){return t.join("")}))},K=function(){function t(e){$(this,t),this.configure(e)}return U(t,[{key:"configure",value:function(t,e){var n=t.vars,r=n.EXTEND,c=n.LAYOUT,u=n.BLOCKS,s=n.BUFFER,a=n.MACRO;function f(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.initBlocks(),this.initMacro(),y(this,t)}this.create=function(t){return new f(t)},this.helpers=function(t){y(f.prototype,t)},f.prototype=y({},e||{}),f.defineProp=f.method=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=arguments.length>4&&void 0!==arguments[4]&&arguments[4];Object.defineProperty(f.prototype,t,{value:e,writable:n,configurable:r,enumerable:i})},f.defineProp(s,function(){var t=[],e=[];function n(t){e.push(t)}return n.start=function(){e=[]},n.backup=function(){t.push(e.concat()),e=[]},n.restore=function(){var n=e.concat();return e=t.pop(),D(n)},n.error=function(t){throw t},n.end=function(){return D(e)},n}()),f.defineProp(u,{},!0),f.defineProp(a,{},!0),f.defineProp(c,!1,!0),f.defineProp(r,!1,!0),f.method("initBlocks",(function(){this[u]={}})),f.method("initMacro",(function(){this[a]={}})),f.method("getMacro",(function(){return this[a]})),f.method("getBuffer",(function(){return this[s]})),f.method("getBlocks",(function(){return this[u]})),f.method("setExtend",(function(t){this[r]=t})),f.method("getExtend",(function(){return this[r]})),f.method("setLayout",(function(t){this[c]=t})),f.method("getLayout",(function(){return this[c]})),f.method("clone",(function(t){var e=[c,r,s];return!0===t&&e.push(u),j(this,e)})),f.method("extend",(function(t){this.setExtend(!0),this.setLayout(t)})),f.method("echo",(function(){var t=this.getBuffer(),e=[].slice.call(arguments);e.forEach(t)})),f.method("fn",(function(t){var e=this.getBuffer(),n=this;return function(){return e.backup(),i(t)&&t.apply(n,arguments),e.restore()}})),f.method("get",(function(t,e){var n=m(this,t),r=n.shift(),i=n.pop();return k(r,i)?r[i]:e})),f.method("set",(function(t,e){var n=m(this,t),r=n.shift(),i=n.pop();return this.getExtend()&&k(r,i)?r[i]:r[i]=e})),f.method("macro",(function(t,e){var n=this.getMacro(),r=this.fn(e),i=this;n[t]=function(){return i.echo(r.apply(void 0,arguments))}})),f.method("call",(function(t){var e=this.getMacro(),n=e[t],r=[].slice.call(arguments,1);if(i(n))return n.apply(n,r)})),f.method("block",(function(t,e){var n=this,r=this.getBlocks();if(r[t]=r[t]||[],r[t].push(this.fn(e)),!this.getExtend()){var i=Object.assign([],r[t]),o=function(){return i.shift()};this.echo(o()(function t(){var e=o();return e?function(){n.echo(e(t()))}:b}()))}})),f.method("include",(function(t,e,n){var r=!1===n?{}:this.clone(!0),i=y(r,e||{}),o=this.render(t,i);this.echo(o)})),f.method("use",(function(t,e){var n=this.require(t);this.echo(x(n,(function(t){var n=this.getMacro();w(t,(function(t,r){n[[e,r].join(".")]=t}))}),this))})),f.method("async",(function(t,e){this.echo(x(t,(function(t){return this.fn(e)(t)}),this))})),f.method("node",(function(t,e,n){return R(t,e,n)})),f.method("el",(function(t,e,n){i(n)&&(n=this.fn(n)()),this.echo(x(n,(function(n){return R(t,e,n)}),this))})),f.method("each",(function(t,e){o(t)&&(t=this.get(t,[])),w(t,e)}))}}]),t}(),X="undefined"!=typeof globalThis?globalThis:window||self,Y=function(){function t(e){var n,r,i;$(this,t),n=this,i={},(r=A(r="list"))in n?Object.defineProperty(n,r,{value:i,enumerable:!0,configurable:!0,writable:!0}):n[r]=i,this.configure(e),!1===s()&&this.load(X[this.namespace])}return U(t,[{key:"configure",value:function(t){this.list={},this.namespace=t.export}},{key:"load",value:function(t){return y(this.list,t),this}},{key:"exist",value:function(t){return k(this.list,t)}},{key:"get",value:function(t){return this.list[t]}},{key:"remove",value:function(t){delete this.list[t]}},{key:"resolve",value:function(t){return Promise.resolve(this.get(t))}},{key:"set",value:function(t,e){return this.list[t]=e,this}}]),t}(),J=function(t,e){y(t,{export:r(o,n.export,t.export,e.export),path:r(o,n.path,t.path,e.path),resolver:r(i,n.resolver,t.resolver,e.resolver),extension:r(o,n.extension,t.extension,e.extension),withObject:r(c,n.withObject,t.withObject,e.withObject),rmWhitespace:r(c,n.rmWhitespace,t.rmWhitespace,e.rmWhitespace),token:y({},n.token,t.token,e.token),vars:y({},n.vars,t.vars,e.vars)})},V=function t(u){var s={},a={};J(s,u||{});var f=new K(s),h=new _(s),l=new L(s),p=new Y(s),v=new q(s,p,h),m=function(t){J(s,t),f.configure(s,a),h.configure(s),l.configure(s),p.configure(s),v.configure(s)},b=function(t,e){return v.get(t).then((function(t){return t.call(e,e,e.getBuffer(),d)}))},w=function(t,e){var n=g(t,s.extension),r=f.create(e);return b(n,r).then((function(t){if(r.getExtend()){r.setExtend(!1);var e=r.getLayout(),n=r.clone();return w(e,n)}return t}))},j=function(t){f.helpers(y(a,t||{}))};return j({require:function(t){return function(t){var e=g(t,s.extension),n=f.create({});return b(e,n).then((function(){return n.getMacro()}))}(t)},render:function(t,e){return w(t,e)}}),{render:w,helpers:j,configure:m,wrapper:function(t){return l.wrapper(t)},compile:function(t,e){return h.compile(t,e)},create:function(e){return t(e)},preload:function(t){return p.load(t)},__express:function(t,u,s){i(u)&&(s=u,u={});var a=y({},(u=u||{}).settings),f=r(o,n.path,a.views),h=r(c,n.cache,a["view cache"]),l=y({},a["view options"]),p=e.relative(f,t);return l.path=f,l.cache=h,m(l),w(p,u).then((function(t){s(null,t)})).catch((function(t){s(t)}))}}}({}),z=V.render,G=V.helpers,H=V.configure,I=V.wrapper,Q=V.compile,Z=V.create,tt=V.preload,et=V.__express;t.__express=et,t.compile=Q,t.configure=H,t.create=Z,t.element=R,t.helpers=G,t.preload=tt,t.render=z,t.safeValue=d,t.wrapper=I,Object.defineProperty(t,"__esModule",{value:!0})}));
package/dist/ejs.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs';
3
- import 'chokidar';
3
+ import chokidar from 'chokidar';
4
4
 
5
5
  const defaults = {};
6
6
 
@@ -14,6 +14,8 @@ defaults.resolver = null;
14
14
 
15
15
  defaults.extension = 'ejs';
16
16
 
17
+ defaults.rmWhitespace = true;
18
+
17
19
  defaults.withObject = false;
18
20
 
19
21
  defaults.vars = {
@@ -262,6 +264,7 @@ class Compiler {
262
264
  }
263
265
  configure(config) {
264
266
  this.withObject = config.withObject;
267
+ this.rmWhitespace = config.rmWhitespace;
265
268
  this.token = config.token;
266
269
  this.vars = config.vars;
267
270
  this.matches = [];
@@ -282,25 +285,35 @@ class Compiler {
282
285
  });
283
286
  this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
284
287
  this.slurpStart = new RegExp(
285
- [this.slurp.match, this.slurp.start].join(''),
288
+ [this.slurp.match, this.slurp.start.join('')].join(''),
286
289
  'gm'
287
290
  );
288
291
  this.slurpEnd = new RegExp(
289
- [this.slurp.end, this.slurp.match].join(''),
292
+ [this.slurp.end.join(''), this.slurp.match].join(''),
290
293
  'gm'
291
294
  );
292
295
  }
296
+ truncate(value) {
297
+ return value && value.replace(/^(?:\r\n|\r|\n)/, '')
298
+ }
293
299
  compile(content, path) {
294
300
  const { SCOPE, SAFE, BUFFER } = this.vars;
295
- content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
301
+ if (this.rmWhitespace) {
302
+ content = content
303
+ .replace(/[\r\n]+/g, '\n')
304
+ .replace(/^\s+|\s+$/gm, '');
305
+ }
296
306
  content = content
297
- .replace(this.slurpStart, this.slurp.start)
298
- .replace(this.slurpEnd, this.slurp.end);
307
+ .replace(this.slurpStart, this.token.start)
308
+ .replace(this.slurpEnd, this.token.end);
299
309
  let source = `${BUFFER}('`;
300
310
  matchTokens(this.regex, content, (params, index, offset) => {
301
311
  source += symbols(content.slice(index, offset));
302
312
  params.forEach((value, index) => {
303
- if (value) source += this.formats[index](value);
313
+ //value = this.truncate(value)
314
+ if (value) {
315
+ source += this.formats[index](value);
316
+ }
304
317
  });
305
318
  });
306
319
  source += `');`;
@@ -355,30 +368,53 @@ class Bundler {
355
368
  }
356
369
  }
357
370
 
358
- const httpRequest = (template) => {
359
- return fetch(template).then((response) => response.text())
371
+ const resolvePath = (path, template) => {
372
+ template = [path, template].join('/');
373
+ template = template.replace(/\/\//g, '/');
374
+ return template
375
+ };
376
+
377
+ const httpRequest = (path, template) => {
378
+ return fetch(resolvePath(path, template)).then((response) =>
379
+ response.text()
380
+ )
360
381
  };
361
382
 
362
- const fileSystem = (template) =>
363
- new Promise((resolve, reject) => {
364
- fs.readFile(template, (error, data) => {
383
+ const fileSystem = (path, template) => {
384
+ return new Promise((resolve, reject) => {
385
+ fs.readFile(resolvePath(path, template), (error, data) => {
365
386
  if (error) {
366
387
  reject(error);
367
388
  } else {
368
389
  resolve(data.toString());
369
390
  }
370
391
  });
371
- });
392
+ })
393
+ };
372
394
 
373
- const normalizePath = (path, template) => {
374
- template = [path, template].join('/');
375
- template = template.replace(/\/\//g, '/');
376
- return template
395
+ const disableWatcher = (watcher) => {
396
+ if (watcher) {
397
+ watcher.unwatch('.');
398
+ }
399
+ };
400
+
401
+ const enableWatcher = (path, cache) => {
402
+ return chokidar
403
+ .watch('.', {
404
+ cwd: path,
405
+ })
406
+ .on('change', (name) => {
407
+ cache.remove(name);
408
+ })
409
+ .on('error', (error) => {
410
+ console.log('watcher error: ' + error);
411
+ })
377
412
  };
378
413
 
379
414
  class Template {
380
415
  constructor(config, cache, compiler) {
381
416
  this.cache = cache;
417
+ this.watcher = null;
382
418
  this.compiler = compiler;
383
419
  this.configure(config);
384
420
  }
@@ -389,22 +425,33 @@ class Template {
389
425
  : isNode()
390
426
  ? fileSystem
391
427
  : httpRequest;
428
+ disableWatcher(this.watcher);
429
+ if (config.watch && isNode()) {
430
+ this.watcher = enableWatcher(this.cache, this.path);
431
+ }
392
432
  }
393
433
  resolve(template) {
394
- return this.resolver(normalizePath(this.path, template))
434
+ return this.resolver(this.path, template)
395
435
  }
396
- result(content, template) {
436
+ result(template, content) {
397
437
  this.cache.set(template, content);
398
438
  return content
399
439
  }
440
+ compile(content, template) {
441
+ if (isFunction(content)) {
442
+ return content
443
+ } else {
444
+ return this.compiler.compile(content, template)
445
+ }
446
+ }
400
447
  get(template) {
401
448
  if (this.cache.exist(template)) {
402
449
  return this.cache.resolve(template)
403
450
  }
404
451
  const content = this.resolve(template).then((content) =>
405
- this.result(this.compiler.compile(content, template), template)
452
+ this.result(template, this.compile(content, template))
406
453
  );
407
- return this.result(content, template)
454
+ return this.result(template, content)
408
455
  }
409
456
  }
410
457
 
@@ -701,6 +748,12 @@ const configSchema = (config, options) => {
701
748
  config.withObject,
702
749
  options.withObject
703
750
  ),
751
+ rmWhitespace: typeProp(
752
+ isBoolean,
753
+ defaults.rmWhitespace,
754
+ config.rmWhitespace,
755
+ options.rmWhitespace
756
+ ),
704
757
  token: extend({}, defaults.token, config.token, options.token),
705
758
  vars: extend({}, defaults.vars, config.vars, options.vars),
706
759
  });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@kosatyi/ejs",
3
3
  "description": "EJS Templates",
4
4
  "homepage": "https://github.com/kosatyi/ejs",
5
- "version": "0.0.29",
5
+ "version": "0.0.31",
6
6
  "main": "dist/ejs.cjs",
7
7
  "module": "dist/ejs.mjs",
8
8
  "browser": "dist/ejs.js",
@@ -24,14 +24,10 @@
24
24
  "postversion": "git push && git push --tags"
25
25
  },
26
26
  "devDependencies": {
27
- "@kosatyi/rollup": "^0.0.1",
28
27
  "@babel/core": "^7.20.12",
29
28
  "@babel/preset-env": "^7.20.2",
30
- "glob": "^10.2.7",
31
- "terser": "^5.16.1"
32
- },
33
- "directories": {
34
- "test": "test"
29
+ "@kosatyi/ejs-bundle": "^1.0.6",
30
+ "@kosatyi/rollup": "^0.0.1"
35
31
  },
36
32
  "license": "MIT",
37
33
  "bugs": {