@kosatyi/ejs 0.0.113 → 0.0.115

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.
@@ -876,7 +876,8 @@ function _outputContent(name, data) {
876
876
  _defineProperty(EjsInstance, "exports", ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers']);
877
877
 
878
878
  const resolver = async (path, template, error) => {
879
- return fetch(joinPath(path, template)).then(response => {
879
+ const url = new URL(joinPath(path, template), location.origin);
880
+ return fetch(url).then(response => {
880
881
  if (response.ok) return response.text();
881
882
  return error(1, "template ".concat(template, " not found"));
882
883
  }, reason => error(0, reason));
@@ -958,7 +958,8 @@ class EjsInstance {
958
958
  }
959
959
 
960
960
  const resolver = async (path, template, error) => {
961
- return fetch(joinPath(path, template)).then(
961
+ const url = new URL(joinPath(path, template), location.origin);
962
+ return fetch(url).then(
962
963
  (response) => {
963
964
  if (response.ok) return response.text()
964
965
  return error(1, `template ${template} not found`)
@@ -0,0 +1,27 @@
1
+ import { E as EjsInstance } from './index-BiIhwNBq.js';
2
+ import { j as joinPath } from './element-CEdJaM42.js';
3
+ export { e as element, a as escapeValue } from './element-CEdJaM42.js';
4
+
5
+ const resolver = async (path, template, error) => {
6
+ return fetch(joinPath(path, template)).then(
7
+ (response) => {
8
+ if (response.ok) return response.text()
9
+ return error(1, `template ${template} not found`)
10
+ },
11
+ (reason) => error(0, reason),
12
+ )
13
+ };
14
+
15
+ const {
16
+ render,
17
+ configure,
18
+ create,
19
+ helpers,
20
+ createContext,
21
+ compile,
22
+ preload,
23
+ } = new EjsInstance({
24
+ resolver,
25
+ });
26
+
27
+ export { compile, configure, create, createContext, helpers, preload, render };
@@ -0,0 +1,155 @@
1
+ import fs from 'node:fs/promises';
2
+ import globWatch from 'glob-watcher';
3
+ import { join, dirname } from 'node:path';
4
+ import { glob } from 'glob';
5
+ import { b as bindContext } from './element-CEdJaM42.js';
6
+ import { create } from './index.js';
7
+ import './index-BiIhwNBq.js';
8
+
9
+ class EjsBundler {
10
+ #templates = {}
11
+ #bundlerOptions = {
12
+ target: [],
13
+ umd: true,
14
+ minify: true,
15
+ }
16
+ #compile
17
+ #ejsOptions
18
+ #buildInProgress
19
+ static exports = ['build', 'watch', 'concat', 'output']
20
+
21
+ constructor(bundlerOptions = {}, ejsOptions = {}) {
22
+ bindContext(this, this.constructor.exports);
23
+ const { compile, configure } = create(ejsOptions);
24
+ Object.assign(this.#bundlerOptions, bundlerOptions);
25
+ this.#compile = compile;
26
+ this.#ejsOptions = configure({});
27
+ this.#buildInProgress = false;
28
+ this.#templates = {};
29
+ }
30
+
31
+ async #stageRead(path) {
32
+ return fs
33
+ .readFile(join(this.#ejsOptions.path, path))
34
+ .then((response) => response.toString())
35
+ }
36
+
37
+ #stageCompile(content, name) {
38
+ return this.#compile(content, name).source
39
+ }
40
+
41
+ #getBundle() {
42
+ const umd = this.#bundlerOptions.umd;
43
+ const strict = this.#ejsOptions.strict;
44
+ const module = this.#ejsOptions.precompiled;
45
+ const out = [];
46
+ if (umd) {
47
+ out.push('(function(global,factory){');
48
+ out.push(
49
+ 'typeof exports === "object" && typeof module !== "undefined" ?',
50
+ );
51
+ out.push('module.exports = factory():');
52
+ out.push(
53
+ 'typeof define === "function" && define.amd ? define(factory):',
54
+ );
55
+ out.push(
56
+ '(global = typeof globalThis !== "undefined" ? globalThis:',
57
+ );
58
+ out.push(`global || self,global["${module}"] = factory())`);
59
+ out.push('})(this,(function(){');
60
+ }
61
+ if (strict) out.push(`'use strict'`);
62
+ out.push('const templates = {}');
63
+ Object.entries(this.#templates).forEach(([name, content]) => {
64
+ name = JSON.stringify(name);
65
+ content = String(content);
66
+ out.push(`templates[${name}] = ${content}`);
67
+ });
68
+ if (umd) {
69
+ out.push('return templates}))');
70
+ } else {
71
+ out.push('export default templates');
72
+ }
73
+ return out.join('\n')
74
+ }
75
+
76
+ async build() {
77
+ if (this.#buildInProgress === true) return false
78
+ this.#buildInProgress = true;
79
+ await this.concat().catch(console.error);
80
+ await this.output().catch(console.error);
81
+ console.log('✅', 'bundle complete:', this.#bundlerOptions.target);
82
+ this.#buildInProgress = false;
83
+ }
84
+
85
+ async watch() {
86
+ console.log('🔍', 'watch directory:', this.#ejsOptions.path);
87
+ const pattern = '**/*.'.concat(this.#ejsOptions.extension);
88
+ const watcher = globWatch(pattern, { cwd: this.#ejsOptions.path });
89
+ const state = { build: null };
90
+ watcher.on('change', (path) => {
91
+ if (state.build) return
92
+ console.log('⟳', 'file change:', path);
93
+ state.build = this.build().then(() => {
94
+ state.build = null;
95
+ });
96
+ });
97
+ watcher.on('add', (path) => {
98
+ if (state.build) return
99
+ console.log('+', 'file added:', path);
100
+ state.build = this.build().then(() => {
101
+ state.build = null;
102
+ });
103
+ });
104
+ }
105
+
106
+ async concat() {
107
+ const pattern = '**/*.'.concat(this.#ejsOptions.extension);
108
+ const list = await glob(
109
+ pattern,
110
+ { cwd: this.#ejsOptions.path },
111
+ () => {},
112
+ );
113
+ for (let template of list) {
114
+ let content = '';
115
+ content = await this.#stageRead(template);
116
+ content = await this.#stageCompile(content, template);
117
+ this.#templates[template] = content;
118
+ }
119
+ }
120
+
121
+ async output() {
122
+ const target = [].concat(this.#bundlerOptions.target);
123
+ const content = this.#getBundle();
124
+ for (const file of target) {
125
+ const folderPath = dirname(file);
126
+ const folderExists = await fs
127
+ .stat(folderPath)
128
+ .then(() => true)
129
+ .catch(() => false);
130
+ if (folderExists === false) {
131
+ await fs.mkdir(folderPath, { recursive: true });
132
+ }
133
+ await fs.writeFile(file, content);
134
+ }
135
+ }
136
+ }
137
+
138
+ const bundler = (bundlerOptions = {}, ejsOptions = {}) => {
139
+ return new EjsBundler(bundlerOptions, ejsOptions)
140
+ };
141
+
142
+ const ejsRollupBundler = (bundlerOptions, ejsOptions) => {
143
+ const bundle = bundler(bundlerOptions, ejsOptions);
144
+ return {
145
+ name: 'ejs-bundler',
146
+ async buildStart() {
147
+ await bundle.concat();
148
+ },
149
+ async buildEnd() {
150
+ await bundle.output();
151
+ },
152
+ }
153
+ };
154
+
155
+ export { EjsBundler, bundler, ejsRollupBundler };
@@ -0,0 +1,178 @@
1
+ const jsVariableName = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
2
+
3
+ const typeProp = function () {
4
+ const args = [].slice.call(arguments);
5
+ const callback = args.shift();
6
+ return args.filter(callback).pop()
7
+ };
8
+ const isArray = (value) => Array.isArray(value);
9
+ const isFunction = (value) => typeof value === 'function';
10
+ const isString = (value) => typeof value === 'string';
11
+ const isBoolean = (value) => typeof value === 'boolean';
12
+ const isArrayOfVariables = (value) => {
13
+ if (!isArray(value)) return false
14
+ return value.filter((name) => {
15
+ const valid = jsVariableName.test(name);
16
+ if (valid === false)
17
+ console.log(
18
+ `ejsConfig.globals: expected '${name}' to be valid variable name --> skipped`,
19
+ );
20
+ return valid
21
+ })
22
+ };
23
+
24
+ const symbolEntities = {
25
+ "'": "'",
26
+ '\\': '\\',
27
+ '\r': 'r',
28
+ '\n': 'n',
29
+ '\t': 't',
30
+ '\u2028': 'u2028',
31
+ '\u2029': 'u2029',
32
+ };
33
+
34
+ const htmlEntities = {
35
+ '&': '&',
36
+ '<': '&lt;',
37
+ '>': '&gt;',
38
+ '"': '&quot;',
39
+ "'": '&#x27;',
40
+ };
41
+
42
+ const regexKeys = (obj) =>
43
+ new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
44
+
45
+ const htmlEntitiesMatch = regexKeys(htmlEntities);
46
+
47
+ const symbolEntitiesMatch = regexKeys(symbolEntities);
48
+
49
+ const entities = (string = '') => {
50
+ return ('' + string).replace(
51
+ htmlEntitiesMatch,
52
+ (match) => htmlEntities[match],
53
+ )
54
+ };
55
+
56
+ const symbols = (string) => {
57
+ return ('' + string).replace(
58
+ symbolEntitiesMatch,
59
+ (match) => '\\' + symbolEntities[match],
60
+ )
61
+ };
62
+
63
+ const escapeValue = (value, escape) => {
64
+ const check = value;
65
+ return check == null
66
+ ? ''
67
+ : Boolean(escape) === true
68
+ ? entities(check)
69
+ : check
70
+ };
71
+
72
+ const getPath = (context, name, strict) => {
73
+ let data = context;
74
+ let chunks = String(name).split('.');
75
+ let prop = chunks.pop();
76
+ for (let i = 0; i < chunks.length; i++) {
77
+ const part = chunks[i];
78
+ if (isFunction(data['toJSON'])) {
79
+ data = data.toJSON();
80
+ }
81
+ if (strict && data.hasOwnProperty(part) === false) {
82
+ data = {};
83
+ break
84
+ }
85
+ data = data[part] = data[part] || {};
86
+ }
87
+ if (isFunction(data['toJSON'])) {
88
+ data = data.toJSON();
89
+ }
90
+ return [data, prop]
91
+ };
92
+
93
+ const each = (object, callback) => {
94
+ let prop;
95
+ for (prop in object) {
96
+ if (hasProp(object, prop)) {
97
+ callback(object[prop], prop, object);
98
+ }
99
+ }
100
+ };
101
+
102
+ const omit = (object, list) => {
103
+ const result = { ...object };
104
+ for (const key of list) {
105
+ delete result[key];
106
+ }
107
+ return result
108
+ };
109
+
110
+ const hasProp = (object, prop) => {
111
+ return object && Object.hasOwn(object, prop)
112
+ };
113
+
114
+ const joinPath = (path, template) => {
115
+ template = [path, template].join('/');
116
+ template = template.replace(/\/\//g, '/');
117
+ return template
118
+ };
119
+
120
+ const bindContext = (object, methods = []) => {
121
+ for (let i = 0, len = methods.length; i < len; i++) {
122
+ const name = methods[i];
123
+ if (name in object) {
124
+ object[name] = object[name].bind(object);
125
+ }
126
+ }
127
+ };
128
+
129
+ const selfClosed = [
130
+ 'area',
131
+ 'base',
132
+ 'br',
133
+ 'col',
134
+ 'embed',
135
+ 'hr',
136
+ 'img',
137
+ 'input',
138
+ 'link',
139
+ 'meta',
140
+ 'param',
141
+ 'source',
142
+ 'track',
143
+ 'wbr',
144
+ ];
145
+
146
+ const space = ' ';
147
+ const quote = '"';
148
+ const equal = '=';
149
+ const slash = '/';
150
+ const lt = '<';
151
+ const gt = '>';
152
+
153
+ const eachAttribute = ([key, value]) => {
154
+ if (value !== null && value !== undefined) {
155
+ return [entities(key), [quote, entities(value), quote].join('')].join(
156
+ equal,
157
+ )
158
+ }
159
+ };
160
+
161
+ const element = (tag, attrs, content) => {
162
+ const result = [];
163
+ const hasClosedTag = selfClosed.indexOf(tag) === -1;
164
+ const attributes = Object.entries(attrs ?? {})
165
+ .map(eachAttribute)
166
+ .filter((e) => e)
167
+ .join(space);
168
+ result.push([lt, tag, space, attributes, gt].join(''));
169
+ if (content && hasClosedTag) {
170
+ result.push(Array.isArray(content) ? content.join('') : content);
171
+ }
172
+ if (hasClosedTag) {
173
+ result.push([lt, slash, tag, gt].join(''));
174
+ }
175
+ return result.join('')
176
+ };
177
+
178
+ export { escapeValue as a, bindContext as b, isString as c, isBoolean as d, element as e, isArrayOfVariables as f, isArray as g, each as h, isFunction as i, joinPath as j, getPath as k, hasProp as l, omit as o, symbols as s, typeProp as t };
@@ -0,0 +1 @@
1
+ export { e as element, a as escapeValue } from './element-CEdJaM42.js';