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