@kosatyi/ejs 0.0.109 → 0.0.111

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.
@@ -1,5 +1,68 @@
1
1
  'use strict';
2
2
 
3
+ function _assertClassBrand(e, t, n) {
4
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
5
+ throw new TypeError("Private element is not present on this object");
6
+ }
7
+ function _checkPrivateRedeclaration(e, t) {
8
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
9
+ }
10
+ function _classPrivateFieldGet2(s, a) {
11
+ return s.get(_assertClassBrand(s, a));
12
+ }
13
+ function _classPrivateFieldInitSpec(e, t, a) {
14
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
15
+ }
16
+ function _classPrivateFieldSet2(s, a, r) {
17
+ return s.set(_assertClassBrand(s, a), r), r;
18
+ }
19
+ function _classPrivateMethodInitSpec(e, a) {
20
+ _checkPrivateRedeclaration(e, a), a.add(e);
21
+ }
22
+ function _defineProperty(e, r, t) {
23
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
24
+ value: t,
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true
28
+ }) : e[r] = t, e;
29
+ }
30
+ function ownKeys(e, r) {
31
+ var t = Object.keys(e);
32
+ if (Object.getOwnPropertySymbols) {
33
+ var o = Object.getOwnPropertySymbols(e);
34
+ r && (o = o.filter(function (r) {
35
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
36
+ })), t.push.apply(t, o);
37
+ }
38
+ return t;
39
+ }
40
+ function _objectSpread2(e) {
41
+ for (var r = 1; r < arguments.length; r++) {
42
+ var t = null != arguments[r] ? arguments[r] : {};
43
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
44
+ _defineProperty(e, r, t[r]);
45
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
46
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
47
+ });
48
+ }
49
+ return e;
50
+ }
51
+ function _toPrimitive(t, r) {
52
+ if ("object" != typeof t || !t) return t;
53
+ var e = t[Symbol.toPrimitive];
54
+ if (void 0 !== e) {
55
+ var i = e.call(t, r);
56
+ if ("object" != typeof i) return i;
57
+ throw new TypeError("@@toPrimitive must return a primitive value.");
58
+ }
59
+ return ("string" === r ? String : Number)(t);
60
+ }
61
+ function _toPropertyKey(t) {
62
+ var i = _toPrimitive(t, "string");
63
+ return "symbol" == typeof i ? i : i + "";
64
+ }
65
+
3
66
  /**
4
67
  * @type {EjsConfig}
5
68
  */
@@ -46,7 +109,7 @@ const isArrayOfVariables = value => {
46
109
  if (!isArray(value)) return false;
47
110
  return value.filter(name => {
48
111
  const valid = jsVariableName.test(name);
49
- if (valid === false) console.log(`ejsConfig.globals: expected '${name}' to be valid variable name --> skipped`);
112
+ if (valid === false) console.log("ejsConfig.globals: expected '".concat(name, "' to be valid variable name --> skipped"));
50
113
  return valid;
51
114
  });
52
115
  };
@@ -92,7 +155,7 @@ const entities = function () {
92
155
  const symbols = string => {
93
156
  return ('' + string).replace(symbolEntitiesMatch, match => '\\' + symbolEntities[match]);
94
157
  };
95
- const safeValue = (value, escape) => {
158
+ const escapeValue = (value, escape) => {
96
159
  const check = value;
97
160
  return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
98
161
  };
@@ -125,9 +188,7 @@ const each = (object, callback) => {
125
188
  }
126
189
  };
127
190
  const omit = (object, list) => {
128
- const result = {
129
- ...object
130
- };
191
+ const result = _objectSpread2({}, object);
131
192
  for (const key of list) {
132
193
  delete result[key];
133
194
  }
@@ -151,48 +212,70 @@ const bindContext = function (object) {
151
212
  }
152
213
  };
153
214
 
154
- class Template {
155
- #path;
156
- #resolver;
157
- #cache;
158
- #compiler;
159
- static exports = ['configure', 'get', 'compile'];
215
+ class EjsError extends Error {
216
+ constructor(code, content) {
217
+ super(content);
218
+ this.code = code;
219
+ if (content instanceof Error) {
220
+ this.stack = content.stack;
221
+ this.message = content.message;
222
+ }
223
+ }
224
+ }
225
+ const error = (code, content) => {
226
+ throw new EjsError(code, content);
227
+ };
228
+
229
+ var _path$1 = /*#__PURE__*/new WeakMap();
230
+ var _resolver = /*#__PURE__*/new WeakMap();
231
+ var _cache$2 = /*#__PURE__*/new WeakMap();
232
+ var _compiler$1 = /*#__PURE__*/new WeakMap();
233
+ var _EjsTemplate_brand = /*#__PURE__*/new WeakSet();
234
+ class EjsTemplate {
160
235
  constructor(options, cache, compiler) {
236
+ _classPrivateMethodInitSpec(this, _EjsTemplate_brand);
237
+ _classPrivateFieldInitSpec(this, _path$1, void 0);
238
+ _classPrivateFieldInitSpec(this, _resolver, void 0);
239
+ _classPrivateFieldInitSpec(this, _cache$2, void 0);
240
+ _classPrivateFieldInitSpec(this, _compiler$1, void 0);
161
241
  bindContext(this, this.constructor.exports);
162
- this.#cache = cache;
163
- this.#compiler = compiler;
164
- this.configure(options ?? {});
242
+ _classPrivateFieldSet2(_cache$2, this, cache);
243
+ _classPrivateFieldSet2(_compiler$1, this, compiler);
244
+ this.configure(options !== null && options !== void 0 ? options : {});
165
245
  }
166
246
  configure(options) {
167
- this.#path = options.path;
247
+ _classPrivateFieldSet2(_path$1, this, options.path);
168
248
  if (isFunction(options.resolver)) {
169
- this.#resolver = options.resolver;
170
- }
171
- }
172
- #resolve(template) {
173
- const cached = this.#cache.get(template);
174
- if (cached instanceof Promise) return cached;
175
- const result = Promise.resolve(this.#resolver(this.#path, template));
176
- this.#cache.set(template, result);
177
- return result;
178
- }
179
- compile(content, template) {
180
- const cached = this.#cache.get(template);
181
- if (typeof cached === 'function') return cached;
182
- if (typeof content === 'string') {
183
- content = this.#compiler.compile(content, template);
184
- }
185
- if (typeof content === 'function') {
186
- this.#cache.set(template, content);
187
- return content;
249
+ _classPrivateFieldSet2(_resolver, this, options.resolver);
188
250
  }
189
251
  }
190
252
  get(template) {
191
- return this.#resolve(template).then(content => this.compile(content, template));
253
+ return _assertClassBrand(_EjsTemplate_brand, this, _resolve).call(this, template).then(content => {
254
+ return _assertClassBrand(_EjsTemplate_brand, this, _compile).call(this, content, template);
255
+ });
256
+ }
257
+ }
258
+ function _resolve(template) {
259
+ const cached = _classPrivateFieldGet2(_cache$2, this).get(template);
260
+ if (cached instanceof Promise) return cached;
261
+ const result = Promise.resolve(_classPrivateFieldGet2(_resolver, this).call(this, _classPrivateFieldGet2(_path$1, this), template, error));
262
+ _classPrivateFieldGet2(_cache$2, this).set(template, result);
263
+ return result;
264
+ }
265
+ function _compile(content, template) {
266
+ const cached = _classPrivateFieldGet2(_cache$2, this).get(template);
267
+ if (typeof cached === 'function') return cached;
268
+ if (typeof content === 'string') {
269
+ content = _classPrivateFieldGet2(_compiler$1, this).compile(content, template);
270
+ }
271
+ if (typeof content === 'function') {
272
+ _classPrivateFieldGet2(_cache$2, this).set(template, content);
273
+ return content;
192
274
  }
193
275
  }
276
+ _defineProperty(EjsTemplate, "exports", ['configure', 'get']);
194
277
 
195
- const tokenList = [['-', (v, b, s) => `')\n${b}(${s}(${v},1))\n${b}('`], ['=', (v, b, s) => `')\n${b}(${s}(${v}))\n${b}('`], ['#', (v, b) => `')\n/**${v}**/\n${b}('`], ['', (v, b) => `')\n${v}\n${b}('`]];
278
+ const tokenList = [['-', (v, b, s) => "')\n".concat(b, "(").concat(s, "(").concat(v, ",1))\n").concat(b, "('")], ['=', (v, b, s) => "')\n".concat(b, "(").concat(s, "(").concat(v, "))\n").concat(b, "('")], ['#', (v, b) => "')\n/**".concat(v, "**/\n").concat(b, "('")], ['', (v, b) => "')\n".concat(v, "\n").concat(b, "('")]];
196
279
  const tokensMatch = (regex, content, callback) => {
197
280
  let index = 0;
198
281
  content.replace(regex, function () {
@@ -204,133 +287,138 @@ const tokensMatch = (regex, content, callback) => {
204
287
  return match;
205
288
  });
206
289
  };
207
- class Compiler {
208
- #config = {};
209
- static exports = ['compile'];
290
+ var _config$1 = /*#__PURE__*/new WeakMap();
291
+ class EjsCompiler {
210
292
  constructor(options) {
293
+ _classPrivateFieldInitSpec(this, _config$1, {});
211
294
  bindContext(this, this.constructor.exports);
212
295
  this.configure(options);
213
296
  }
214
297
  configure(options) {
215
- this.#config.strict = options.strict;
216
- this.#config.rmWhitespace = options.rmWhitespace;
217
- this.#config.token = options.token;
218
- this.#config.vars = options.vars;
219
- this.#config.globals = options.globals;
220
- this.#config.legacy = options.legacy ?? true;
221
- this.#config.slurp = {
298
+ var _options$legacy;
299
+ _classPrivateFieldGet2(_config$1, this).strict = options.strict;
300
+ _classPrivateFieldGet2(_config$1, this).rmWhitespace = options.rmWhitespace;
301
+ _classPrivateFieldGet2(_config$1, this).token = options.token;
302
+ _classPrivateFieldGet2(_config$1, this).vars = options.vars;
303
+ _classPrivateFieldGet2(_config$1, this).globals = options.globals;
304
+ _classPrivateFieldGet2(_config$1, this).legacy = (_options$legacy = options.legacy) !== null && _options$legacy !== void 0 ? _options$legacy : true;
305
+ _classPrivateFieldGet2(_config$1, this).slurp = {
222
306
  match: '[s\t\n]*',
223
- start: [this.#config.token.start, '_'],
224
- end: ['_', this.#config.token.end]
307
+ start: [_classPrivateFieldGet2(_config$1, this).token.start, '_'],
308
+ end: ['_', _classPrivateFieldGet2(_config$1, this).token.end]
225
309
  };
226
- this.#config.matches = [];
227
- this.#config.formats = [];
310
+ _classPrivateFieldGet2(_config$1, this).matches = [];
311
+ _classPrivateFieldGet2(_config$1, this).formats = [];
228
312
  for (const [symbol, format] of tokenList) {
229
- this.#config.matches.push(this.#config.token.start.concat(symbol).concat(this.#config.token.regex).concat(this.#config.token.end));
230
- this.#config.formats.push(format);
313
+ _classPrivateFieldGet2(_config$1, this).matches.push(_classPrivateFieldGet2(_config$1, this).token.start.concat(symbol).concat(_classPrivateFieldGet2(_config$1, this).token.regex).concat(_classPrivateFieldGet2(_config$1, this).token.end));
314
+ _classPrivateFieldGet2(_config$1, this).formats.push(format);
231
315
  }
232
- this.#config.regex = new RegExp(this.#config.matches.join('|').concat('|$'), 'g');
233
- this.#config.slurpStart = new RegExp([this.#config.slurp.match, this.#config.slurp.start.join('')].join(''), 'gm');
234
- this.#config.slurpEnd = new RegExp([this.#config.slurp.end.join(''), this.#config.slurp.match].join(''), 'gm');
235
- if (this.#config.globals.length) {
236
- if (this.#config.legacy) {
237
- this.#config.globalVariables = `const ${this.#config.globals.map(name => `${name}=${this.#config.vars.SCOPE}.${name}`).join(',')};`;
316
+ _classPrivateFieldGet2(_config$1, this).regex = new RegExp(_classPrivateFieldGet2(_config$1, this).matches.join('|').concat('|$'), 'g');
317
+ _classPrivateFieldGet2(_config$1, this).slurpStart = new RegExp([_classPrivateFieldGet2(_config$1, this).slurp.match, _classPrivateFieldGet2(_config$1, this).slurp.start.join('')].join(''), 'gm');
318
+ _classPrivateFieldGet2(_config$1, this).slurpEnd = new RegExp([_classPrivateFieldGet2(_config$1, this).slurp.end.join(''), _classPrivateFieldGet2(_config$1, this).slurp.match].join(''), 'gm');
319
+ if (_classPrivateFieldGet2(_config$1, this).globals.length) {
320
+ if (_classPrivateFieldGet2(_config$1, this).legacy) {
321
+ _classPrivateFieldGet2(_config$1, this).globalVariables = "const ".concat(_classPrivateFieldGet2(_config$1, this).globals.map(name => "".concat(name, "=").concat(_classPrivateFieldGet2(_config$1, this).vars.SCOPE, ".").concat(name)).join(','), ";");
238
322
  } else {
239
- this.#config.globalVariables = `const {${this.#config.globals.join(',')}} = ${this.#config.vars.SCOPE};`;
323
+ _classPrivateFieldGet2(_config$1, this).globalVariables = "const {".concat(_classPrivateFieldGet2(_config$1, this).globals.join(','), "} = ").concat(_classPrivateFieldGet2(_config$1, this).vars.SCOPE, ";");
240
324
  }
241
325
  }
242
326
  }
243
327
  compile(content, path) {
244
- const GLOBALS = this.#config.globalVariables;
328
+ const GLOBALS = _classPrivateFieldGet2(_config$1, this).globalVariables;
245
329
  const {
246
330
  SCOPE,
247
331
  SAFE,
248
332
  BUFFER,
249
333
  COMPONENT,
250
334
  ELEMENT
251
- } = this.#config.vars;
252
- if (this.#config.rmWhitespace) {
335
+ } = _classPrivateFieldGet2(_config$1, this).vars;
336
+ if (_classPrivateFieldGet2(_config$1, this).rmWhitespace) {
253
337
  content = String(content).replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
254
338
  }
255
- content = String(content).replace(this.#config.slurpStart, this.#config.token.start).replace(this.#config.slurpEnd, this.#config.token.end);
256
- let OUTPUT = `${BUFFER}('`;
257
- tokensMatch(this.#config.regex, content, (params, index, offset) => {
339
+ content = String(content).replace(_classPrivateFieldGet2(_config$1, this).slurpStart, _classPrivateFieldGet2(_config$1, this).token.start).replace(_classPrivateFieldGet2(_config$1, this).slurpEnd, _classPrivateFieldGet2(_config$1, this).token.end);
340
+ let OUTPUT = "".concat(BUFFER, "('");
341
+ tokensMatch(_classPrivateFieldGet2(_config$1, this).regex, content, (params, index, offset) => {
258
342
  OUTPUT += symbols(content.slice(index, offset));
259
343
  params.forEach((value, index) => {
260
344
  if (value) {
261
- OUTPUT += this.#config.formats[index](value.trim(), BUFFER, SAFE);
345
+ OUTPUT += _classPrivateFieldGet2(_config$1, this).formats[index](value.trim(), BUFFER, SAFE);
262
346
  }
263
347
  });
264
348
  });
265
- OUTPUT += `');`;
266
- OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
267
- if (this.#config.strict === false) {
268
- OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
349
+ OUTPUT += "');";
350
+ OUTPUT = "try{".concat(OUTPUT, "}catch(e){return ").concat(BUFFER, ".error(e)}");
351
+ if (_classPrivateFieldGet2(_config$1, this).strict === false) {
352
+ OUTPUT = "with(".concat(SCOPE, "){").concat(OUTPUT, "}");
269
353
  }
270
- OUTPUT = `${BUFFER}.start();${OUTPUT}return ${BUFFER}.end();`;
271
- OUTPUT += `\n//# sourceURL=${path}`;
354
+ OUTPUT = "".concat(BUFFER, ".start();").concat(OUTPUT, "return ").concat(BUFFER, ".end();");
355
+ OUTPUT += "\n//# sourceURL=".concat(path);
272
356
  if (GLOBALS) {
273
- OUTPUT = `${GLOBALS}\n${OUTPUT}`;
357
+ OUTPUT = "".concat(GLOBALS, "\n").concat(OUTPUT);
274
358
  }
275
359
  try {
276
360
  const params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE];
277
361
  const result = Function.apply(null, params.concat(OUTPUT));
278
- result.source = `(function(${params.join(',')}){\n${OUTPUT}\n});`;
362
+ result.source = "(function(".concat(params.join(','), "){\n").concat(OUTPUT, "\n});");
279
363
  return result;
280
- } catch (error) {
281
- error.filename = path;
282
- error.source = OUTPUT;
283
- throw error;
364
+ } catch (e) {
365
+ e.filename = path;
366
+ e.source = OUTPUT;
367
+ error(0, e);
284
368
  }
285
369
  }
286
370
  }
371
+ _defineProperty(EjsCompiler, "exports", ['configure', 'compile']);
287
372
 
288
- class Cache {
289
- static exports = ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve'];
290
- #cache = true;
291
- #precompiled;
292
- #list = {};
373
+ var _cache$1 = /*#__PURE__*/new WeakMap();
374
+ var _precompiled = /*#__PURE__*/new WeakMap();
375
+ var _list = /*#__PURE__*/new WeakMap();
376
+ class EjsCache {
293
377
  constructor(options) {
378
+ _classPrivateFieldInitSpec(this, _cache$1, true);
379
+ _classPrivateFieldInitSpec(this, _precompiled, void 0);
380
+ _classPrivateFieldInitSpec(this, _list, {});
294
381
  bindContext(this, this.constructor.exports);
295
382
  this.configure(options);
296
383
  }
297
384
  get(key) {
298
- if (this.#cache) {
299
- return this.#list[key];
385
+ if (_classPrivateFieldGet2(_cache$1, this)) {
386
+ return _classPrivateFieldGet2(_list, this)[key];
300
387
  }
301
388
  }
302
389
  set(key, value) {
303
- if (this.#cache) {
304
- this.#list[key] = value;
390
+ if (_classPrivateFieldGet2(_cache$1, this)) {
391
+ _classPrivateFieldGet2(_list, this)[key] = value;
305
392
  }
306
393
  }
307
394
  exist(key) {
308
- if (this.#cache) {
309
- return this.#list.hasOwnProperty(key);
395
+ if (_classPrivateFieldGet2(_cache$1, this)) {
396
+ return _classPrivateFieldGet2(_list, this).hasOwnProperty(key);
310
397
  }
311
398
  }
312
399
  clear() {
313
- Object.keys(this.#list).forEach(this.remove);
400
+ Object.keys(_classPrivateFieldGet2(_list, this)).forEach(this.remove);
314
401
  }
315
402
  remove(key) {
316
- delete this.#list[key];
403
+ delete _classPrivateFieldGet2(_list, this)[key];
317
404
  }
318
405
  resolve(key) {
319
406
  return Promise.resolve(this.get(key));
320
407
  }
321
408
  load(data) {
322
- if (this.#cache) {
323
- Object.assign(this.#list, data || {});
409
+ if (_classPrivateFieldGet2(_cache$1, this)) {
410
+ Object.assign(_classPrivateFieldGet2(_list, this), data || {});
324
411
  }
325
412
  }
326
413
  configure(options) {
327
- this.#cache = options.cache;
328
- this.#precompiled = options.precompiled;
414
+ _classPrivateFieldSet2(_cache$1, this, options.cache);
415
+ _classPrivateFieldSet2(_precompiled, this, options.precompiled);
329
416
  if (typeof window === 'object') {
330
- this.load(window[this.#precompiled]);
417
+ this.load(window[_classPrivateFieldGet2(_precompiled, this)]);
331
418
  }
332
419
  }
333
420
  }
421
+ _defineProperty(EjsCache, "exports", ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve']);
334
422
 
335
423
  const selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
336
424
  const space = ' ';
@@ -348,7 +436,7 @@ const eachAttribute = _ref => {
348
436
  const element = (tag, attrs, content) => {
349
437
  const result = [];
350
438
  const hasClosedTag = selfClosed.indexOf(tag) === -1;
351
- const attributes = Object.entries(attrs ?? {}).map(eachAttribute).filter(e => e).join(space);
439
+ const attributes = Object.entries(attrs !== null && attrs !== void 0 ? attrs : {}).map(eachAttribute).filter(e => e).join(space);
352
440
  result.push([lt, tag, space, attributes, gt].join(''));
353
441
  if (content && hasClosedTag) {
354
442
  result.push(Array.isArray(content) ? content.join('') : content);
@@ -359,31 +447,11 @@ const element = (tag, attrs, content) => {
359
447
  return result.join('');
360
448
  };
361
449
 
362
- class TemplateError extends Error {
363
- name = 'TemplateError';
364
- constructor(error) {
365
- super(error);
366
- if (error instanceof Error) {
367
- this.stack = error.stack;
368
- this.filename = error.filename;
369
- this.lineno = error.lineno;
370
- }
371
- }
372
- }
373
- class TemplateNotFound extends TemplateError {
374
- name = 'TemplateNotFound';
375
- code = 404;
376
- }
377
- class TemplateSyntaxError extends TemplateError {
378
- name = 'TemplateSyntaxError';
379
- code = 500;
380
- }
381
-
382
450
  const resolve = list => {
383
- return Promise.all(list || []).then(list => list.join('')).catch(e => e);
451
+ return Promise.all(list || []).then(list => list.join('')).catch(e => error(0, e));
384
452
  };
385
- const reject = error => {
386
- return Promise.reject(new TemplateSyntaxError(error));
453
+ const reject = e => {
454
+ return Promise.reject(error(0, e));
387
455
  };
388
456
  const EjsBuffer = () => {
389
457
  let store = [];
@@ -408,7 +476,7 @@ const EjsBuffer = () => {
408
476
  array = store.pop();
409
477
  return resolve(result);
410
478
  };
411
- EjsBuffer.error = (e, filename) => {
479
+ EjsBuffer.error = e => {
412
480
  return reject(e);
413
481
  };
414
482
  EjsBuffer.end = () => {
@@ -419,6 +487,7 @@ const EjsBuffer = () => {
419
487
 
420
488
  const PARENT = Symbol('EjsContext.parentTemplate');
421
489
  const createContext$1 = (config, methods) => {
490
+ const globals = config.globals || [];
422
491
  const {
423
492
  BLOCKS,
424
493
  MACRO,
@@ -430,130 +499,113 @@ const createContext$1 = (config, methods) => {
430
499
  COMPONENT,
431
500
  ELEMENT
432
501
  } = config.vars;
433
- const globals = config.globals || [];
434
- function EjsContext(data) {
435
- this[PARENT] = null;
436
- this[BLOCKS] = {};
437
- this[MACRO] = {};
438
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
439
- }
440
- Object.entries(methods).forEach(_ref => {
441
- let [name, value] = _ref;
442
- if (isFunction(value) && globals.includes(name)) {
443
- value = value.bind(EjsContext.prototype);
502
+ class Context {
503
+ constructor(data) {
504
+ this[PARENT] = null;
505
+ this[BLOCKS] = {};
506
+ this[MACRO] = {};
507
+ Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
444
508
  }
445
- EjsContext.prototype[name] = value;
446
- });
447
- Object.defineProperty(EjsContext.prototype, BUFFER, {
448
- value: EjsBuffer()
449
- });
450
- Object.defineProperty(EjsContext.prototype, BLOCKS, {
451
- value: {},
452
- writable: true
453
- });
454
- Object.defineProperty(EjsContext.prototype, MACRO, {
455
- value: {},
456
- writable: true
457
- });
458
- Object.defineProperty(EjsContext.prototype, LAYOUT, {
459
- value: false,
460
- writable: true
461
- });
462
- Object.defineProperty(EjsContext.prototype, EXTEND, {
463
- value: false,
464
- writable: true
465
- });
466
- Object.defineProperty(EjsContext.prototype, PARENT, {
467
- value: null,
468
- writable: true
469
- });
470
- Object.defineProperties(EjsContext.prototype, {
471
- /** @type {function} */
509
+ }
510
+ Object.defineProperties(Context.prototype, {
511
+ [BUFFER]: {
512
+ value: EjsBuffer()
513
+ },
514
+ [BLOCKS]: {
515
+ value: {},
516
+ writable: true
517
+ },
518
+ [MACRO]: {
519
+ value: {},
520
+ writable: true
521
+ },
522
+ [LAYOUT]: {
523
+ value: false,
524
+ writable: true
525
+ },
526
+ [EXTEND]: {
527
+ value: false,
528
+ writable: true
529
+ },
530
+ [PARENT]: {
531
+ value: null,
532
+ writable: true
533
+ },
472
534
  setParentTemplate: {
473
535
  value(value) {
474
536
  this[PARENT] = value;
475
537
  return this;
476
538
  }
477
539
  },
478
- /** @type {function} */
479
540
  getParentTemplate: {
480
541
  value() {
481
542
  return this[PARENT];
482
543
  }
483
544
  },
484
- /** @type {function} */
485
- useSafeValue: {
486
- get: () => safeValue
545
+ useEscapeValue: {
546
+ value() {
547
+ return escapeValue;
548
+ }
487
549
  },
488
- /** @type {function} */
489
550
  useComponent: {
490
- get() {
551
+ value() {
491
552
  if (isFunction(this[COMPONENT])) {
492
553
  return this[COMPONENT].bind(this);
493
554
  } else {
494
- return () => {
495
- throw new Error(`${COMPONENT} must be a function`);
555
+ return function () {
556
+ error(2, "".concat(COMPONENT, " must be a function"));
496
557
  };
497
558
  }
498
559
  }
499
560
  },
500
- /** @type {function} */
501
561
  useElement: {
502
- get() {
562
+ value() {
503
563
  if (isFunction(this[ELEMENT])) {
504
564
  return this[ELEMENT].bind(this);
505
565
  } else {
506
566
  return () => {
507
- throw new Error(`${ELEMENT} must be a function`);
567
+ error(2, "".concat(ELEMENT, " must be a function"));
508
568
  };
509
569
  }
510
570
  }
511
571
  },
512
- /** @type {function} */
513
572
  useBuffer: {
514
- get() {
573
+ value() {
515
574
  return this[BUFFER];
516
575
  }
517
576
  },
518
- /** @type {function} */
519
577
  getMacro: {
520
578
  value() {
521
579
  return this[MACRO];
522
580
  }
523
581
  },
524
- /** @type {function} */
525
582
  getBlocks: {
526
583
  value() {
527
584
  return this[BLOCKS];
528
585
  }
529
586
  },
530
- /** @type {function} */
531
587
  setExtend: {
532
588
  value(value) {
533
589
  this[EXTEND] = value;
534
590
  return this;
535
591
  }
536
592
  },
537
- /** @type {function} */
538
593
  getExtend: {
539
594
  value() {
540
595
  return this[EXTEND];
541
596
  }
542
597
  },
543
- /** @type {function} */
544
598
  setLayout: {
545
599
  value(layout) {
546
600
  this[LAYOUT] = layout;
547
601
  return this;
548
602
  }
549
603
  },
550
- /** @type {function} */
551
604
  getLayout: {
552
605
  value() {
553
606
  return this[LAYOUT];
554
607
  }
555
608
  },
556
- /** @type {function} */
557
609
  clone: {
558
610
  value(exclude_blocks) {
559
611
  const filter = [LAYOUT, EXTEND, BUFFER];
@@ -563,32 +615,30 @@ const createContext$1 = (config, methods) => {
563
615
  return omit(this, filter);
564
616
  }
565
617
  },
566
- /** @type {function} */
567
618
  extend: {
568
619
  value(layout) {
569
620
  this.setExtend(true);
570
621
  this.setLayout(layout);
571
622
  }
572
623
  },
573
- /** @type {function} */
574
624
  echo: {
575
625
  value() {
576
- return [].slice.call(arguments).forEach(this.useBuffer);
626
+ return [].slice.call(arguments).forEach(this.useBuffer());
577
627
  }
578
628
  },
579
- /** @type {function} */
580
629
  fn: {
581
630
  value(callback) {
582
- return () => {
631
+ const buffer = this.useBuffer();
632
+ const context = this;
633
+ return function () {
583
634
  if (isFunction(callback)) {
584
- this.useBuffer.backup();
585
- this.useBuffer(callback.apply(this, arguments));
586
- return this.useBuffer.restore();
635
+ buffer.backup();
636
+ buffer(callback.apply(context, arguments));
637
+ return buffer.restore();
587
638
  }
588
639
  };
589
640
  }
590
641
  },
591
- /** @type {function} */
592
642
  macro: {
593
643
  value(name, callback) {
594
644
  const list = this.getMacro();
@@ -599,7 +649,6 @@ const createContext$1 = (config, methods) => {
599
649
  };
600
650
  }
601
651
  },
602
- /** @type {function} */
603
652
  call: {
604
653
  value(name) {
605
654
  const list = this.getMacro();
@@ -610,35 +659,35 @@ const createContext$1 = (config, methods) => {
610
659
  }
611
660
  }
612
661
  },
613
- /** @type {function} */
614
662
  block: {
615
663
  value(name, callback) {
616
664
  const blocks = this.getBlocks();
617
665
  blocks[name] = blocks[name] || [];
618
666
  blocks[name].push(this.fn(callback));
619
667
  if (this.getExtend()) return;
668
+ const context = this;
620
669
  const list = Object.assign([], blocks[name]);
621
- const shift = () => list.shift();
622
- const next = () => {
670
+ const shift = function () {
671
+ return list.shift();
672
+ };
673
+ const next = function () {
623
674
  const parent = shift();
624
675
  if (parent) {
625
- return () => {
626
- this.echo(parent(next()));
676
+ return function () {
677
+ context.echo(parent(next()));
627
678
  };
628
679
  } else {
629
- return () => {};
680
+ return function () {};
630
681
  }
631
682
  };
632
683
  this.echo(shift()(next()));
633
684
  }
634
685
  },
635
- /** @type {function} */
636
686
  hasBlock: {
637
687
  value(name) {
638
688
  return this.getBlocks().hasOwnProperty(name);
639
689
  }
640
690
  },
641
- /** @type {function} */
642
691
  include: {
643
692
  value(path, data, cx) {
644
693
  const context = cx === false ? {} : this.clone(true);
@@ -647,7 +696,6 @@ const createContext$1 = (config, methods) => {
647
696
  this.echo(promise);
648
697
  }
649
698
  },
650
- /** @type {function} */
651
699
  use: {
652
700
  value(path, namespace) {
653
701
  this.echo(Promise.resolve(this.require(path)).then(exports$1 => {
@@ -658,13 +706,6 @@ const createContext$1 = (config, methods) => {
658
706
  }));
659
707
  }
660
708
  },
661
- /** @type {function} */
662
- async: {
663
- value(promise, callback) {
664
- this.echo(Promise.resolve(promise).then(callback));
665
- }
666
- },
667
- /** @type {function} */
668
709
  get: {
669
710
  value(name, defaults) {
670
711
  const path = getPath(this, name, true);
@@ -673,7 +714,6 @@ const createContext$1 = (config, methods) => {
673
714
  return hasProp(result, prop) ? result[prop] : defaults;
674
715
  }
675
716
  },
676
- /** @type {function} */
677
717
  set: {
678
718
  value(name, value) {
679
719
  const path = getPath(this, name, false);
@@ -685,7 +725,6 @@ const createContext$1 = (config, methods) => {
685
725
  return result[prop] = value;
686
726
  }
687
727
  },
688
- /** @type {function} */
689
728
  each: {
690
729
  value(object, callback) {
691
730
  if (isString(object)) {
@@ -695,7 +734,6 @@ const createContext$1 = (config, methods) => {
695
734
  },
696
735
  writable: true
697
736
  },
698
- /** @type {function} */
699
737
  el: {
700
738
  value(tag, attr, content) {
701
739
  content = isFunction(content) ? this.fn(content)() : content;
@@ -703,49 +741,75 @@ const createContext$1 = (config, methods) => {
703
741
  },
704
742
  writable: true
705
743
  },
706
- /** @type {function} */
707
744
  ui: {
708
- value(layout) {},
745
+ value() {},
746
+ writable: true
747
+ },
748
+ require: {
749
+ value() {},
750
+ writable: true
751
+ },
752
+ render: {
753
+ value() {},
709
754
  writable: true
710
755
  }
711
756
  });
712
- return EjsContext;
757
+ Object.entries(methods).forEach(_ref => {
758
+ let [name, value] = _ref;
759
+ if (isFunction(value) && globals.includes(name)) {
760
+ value = value.bind(Context.prototype);
761
+ }
762
+ Context.prototype[name] = value;
763
+ });
764
+ return Context;
713
765
  };
714
- class Context {
715
- #context;
716
- static exports = ['create', 'globals', 'helpers'];
766
+ var _context$1 = /*#__PURE__*/new WeakMap();
767
+ class EjsContext {
717
768
  constructor(options, methods) {
769
+ _classPrivateFieldInitSpec(this, _context$1, void 0);
718
770
  bindContext(this, this.constructor.exports);
719
771
  this.configure(options, methods);
720
772
  }
721
773
  create(data) {
722
- return new this.#context(data);
774
+ return new (_classPrivateFieldGet2(_context$1, this))(data);
723
775
  }
724
776
  helpers(methods) {
725
- Object.assign(this.#context.prototype, methods);
777
+ Object.assign(_classPrivateFieldGet2(_context$1, this).prototype, methods);
726
778
  }
727
779
  configure(options, methods) {
728
- this.#context = createContext$1(options, methods);
780
+ _classPrivateFieldSet2(_context$1, this, createContext$1(options, methods));
729
781
  }
730
782
  }
783
+ _defineProperty(EjsContext, "exports", ['create', 'globals', 'helpers']);
731
784
 
785
+ var _cache = /*#__PURE__*/new WeakMap();
786
+ var _context = /*#__PURE__*/new WeakMap();
787
+ var _compiler = /*#__PURE__*/new WeakMap();
788
+ var _template = /*#__PURE__*/new WeakMap();
789
+ var _config = /*#__PURE__*/new WeakMap();
790
+ var _methods = /*#__PURE__*/new WeakMap();
791
+ var _EjsInstance_brand = /*#__PURE__*/new WeakSet();
732
792
  class EjsInstance {
733
- #cache;
734
- #context;
735
- #compiler;
736
- #template;
737
- #config = {};
738
- #methods = {};
739
- static exports = ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers'];
793
+ /**
794
+ *
795
+ * @param {EjsConfig} options
796
+ */
740
797
  constructor() {
741
798
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
799
+ _classPrivateMethodInitSpec(this, _EjsInstance_brand);
800
+ _classPrivateFieldInitSpec(this, _cache, void 0);
801
+ _classPrivateFieldInitSpec(this, _context, void 0);
802
+ _classPrivateFieldInitSpec(this, _compiler, void 0);
803
+ _classPrivateFieldInitSpec(this, _template, void 0);
804
+ _classPrivateFieldInitSpec(this, _config, {});
805
+ _classPrivateFieldInitSpec(this, _methods, {});
742
806
  bindContext(this, this.constructor.exports);
743
- this.#methods = {};
744
- this.#config = configSchema({}, options);
745
- this.#context = new Context(this.#config, this.#methods);
746
- this.#compiler = new Compiler(this.#config);
747
- this.#cache = new Cache(this.#config);
748
- this.#template = new Template(this.#config, this.#cache, this.#compiler);
807
+ _classPrivateFieldSet2(_methods, this, {});
808
+ _classPrivateFieldSet2(_config, this, configSchema({}, options));
809
+ _classPrivateFieldSet2(_context, this, new EjsContext(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_methods, this)));
810
+ _classPrivateFieldSet2(_compiler, this, new EjsCompiler(_classPrivateFieldGet2(_config, this)));
811
+ _classPrivateFieldSet2(_cache, this, new EjsCache(_classPrivateFieldGet2(_config, this)));
812
+ _classPrivateFieldSet2(_template, this, new EjsTemplate(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_cache, this), _classPrivateFieldGet2(_compiler, this)));
749
813
  this.helpers({
750
814
  render: this.render,
751
815
  require: this.require
@@ -756,84 +820,79 @@ class EjsInstance {
756
820
  }
757
821
  configure(options) {
758
822
  if (options) {
759
- configSchema(this.#config, options);
760
- this.#context.configure(this.#config, this.#methods);
761
- this.#compiler.configure(this.#config);
762
- this.#cache.configure(this.#config);
763
- this.#template.configure(this.#config);
823
+ configSchema(_classPrivateFieldGet2(_config, this), options);
824
+ _classPrivateFieldGet2(_context, this).configure(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_methods, this));
825
+ _classPrivateFieldGet2(_compiler, this).configure(_classPrivateFieldGet2(_config, this));
826
+ _classPrivateFieldGet2(_cache, this).configure(_classPrivateFieldGet2(_config, this));
827
+ _classPrivateFieldGet2(_template, this).configure(_classPrivateFieldGet2(_config, this));
764
828
  }
765
- return this.#config;
829
+ return _classPrivateFieldGet2(_config, this);
766
830
  }
767
831
  createContext(data) {
768
- return this.#context.create(data);
832
+ return _classPrivateFieldGet2(_context, this).create(data);
769
833
  }
770
834
  preload(list) {
771
- return this.#cache.load(list || {});
835
+ return _classPrivateFieldGet2(_cache, this).load(list || {});
772
836
  }
773
837
  compile(content, path) {
774
- return this.#compiler.compile(content, path);
838
+ return _classPrivateFieldGet2(_compiler, this).compile(content, path);
775
839
  }
776
840
  helpers(methods) {
777
- this.#context.helpers(Object.assign(this.#methods, methods));
841
+ _classPrivateFieldGet2(_context, this).helpers(Object.assign(_classPrivateFieldGet2(_methods, this), methods));
778
842
  }
779
843
  async render(name, params) {
780
844
  const data = this.createContext(params);
781
- return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
845
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(_assertClassBrand(_EjsInstance_brand, this, _outputContent).call(this, name, data));
782
846
  }
783
847
  async require(name) {
784
848
  const data = this.createContext({});
785
- return this.#output(this.#path(name), data).then(() => data.getMacro());
786
- }
787
- #path(name) {
788
- const ext = name.split('.').pop();
789
- if (ext !== this.#config.extension) {
790
- name = [name, this.#config.extension].join('.');
791
- }
792
- return name;
849
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(() => data.getMacro());
793
850
  }
794
- #output(path, data) {
795
- return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useSafeValue]));
796
- }
797
- #renderLayout(name, params, parentTemplate) {
798
- const data = this.createContext(params);
799
- if (parentTemplate) data.setParentTemplate(parentTemplate);
800
- return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
801
- }
802
- #outputContent(name, data) {
803
- return content => {
804
- if (data.getExtend()) {
805
- data.setExtend(false);
806
- return this.#renderLayout(data.getLayout(), data, name);
807
- }
808
- return content;
809
- };
851
+ }
852
+ function _path(name) {
853
+ const ext = name.split('.').pop();
854
+ if (ext !== _classPrivateFieldGet2(_config, this).extension) {
855
+ name = [name, _classPrivateFieldGet2(_config, this).extension].join('.');
810
856
  }
857
+ return name;
858
+ }
859
+ function _output(path, data) {
860
+ return _classPrivateFieldGet2(_template, this).get(path).then(callback => callback.apply(data, [data, data.useComponent(), data.useElement(), data.useBuffer(), data.useEscapeValue()]));
861
+ }
862
+ function _renderLayout(name, params, parentTemplate) {
863
+ const data = this.createContext(params);
864
+ if (parentTemplate) data.setParentTemplate(parentTemplate);
865
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(_assertClassBrand(_EjsInstance_brand, this, _outputContent).call(this, name, data));
866
+ }
867
+ function _outputContent(name, data) {
868
+ return content => {
869
+ if (data.getExtend()) {
870
+ data.setExtend(false);
871
+ return _assertClassBrand(_EjsInstance_brand, this, _renderLayout).call(this, data.getLayout(), data, name);
872
+ }
873
+ return content;
874
+ };
811
875
  }
876
+ _defineProperty(EjsInstance, "exports", ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers']);
812
877
 
813
- const httpRequest = (path, template) => {
878
+ const resolver = async (path, template, error) => {
814
879
  return fetch(joinPath(path, template)).then(response => {
815
880
  if (response.ok) return response.text();
816
- throw new TemplateNotFound(`template ${path} / ${template} not found`);
817
- }, reason => {
818
- return new TemplateError(reason);
819
- });
881
+ return error(1, "template ".concat(template, " not found"));
882
+ }, reason => error(0, reason));
820
883
  };
821
-
822
884
  const {
823
885
  render,
886
+ configure,
887
+ create,
888
+ helpers,
824
889
  createContext,
825
890
  compile,
826
- helpers,
827
- preload,
828
- configure,
829
- create
891
+ preload
830
892
  } = new EjsInstance({
831
- resolver: httpRequest
893
+ resolver
832
894
  });
833
895
 
834
- exports.TemplateError = TemplateError;
835
- exports.TemplateNotFound = TemplateNotFound;
836
- exports.TemplateSyntaxError = TemplateSyntaxError;
837
896
  exports.compile = compile;
838
897
  exports.configure = configure;
839
898
  exports.create = create;