@kosatyi/ejs 0.0.107 → 0.0.108

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