@kosatyi/ejs 0.0.106 → 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.
- package/README.md +4 -7
- package/bin/bundler.js +21 -9
- package/dist/cjs/browser.js +532 -650
- package/dist/cjs/bundler.js +110 -78
- package/dist/cjs/element.js +28 -63
- package/dist/cjs/index.js +540 -667
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/worker.js +553 -976
- package/dist/esm/browser.js +565 -588
- package/dist/esm/bundler.js +109 -78
- package/dist/esm/element.js +16 -50
- package/dist/esm/index.js +569 -604
- package/dist/esm/worker.js +563 -611
- package/dist/kosatyi-ejs-0.0.107.tgz +0 -0
- package/dist/umd/browser.js +533 -651
- package/dist/umd/browser.js.map +7 -0
- package/dist/umd/browser.min.js +1 -1
- package/dist/umd/browser.min.js.map +7 -0
- package/dist/umd/element.js +28 -63
- package/dist/umd/element.min.js +1 -1
- package/dist/umd/index.js +541 -668
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/worker.js +819 -1242
- package/dist/umd/worker.min.js +1 -3
- package/package.json +80 -71
- package/types/browser.d.ts +10 -0
- package/types/bundler.d.ts +11 -12
- package/types/context.d.ts +96 -0
- package/types/ejs.d.ts +111 -73
- package/types/element.d.ts +9 -0
- package/types/error.d.ts +14 -0
- package/types/global.d.ts +6 -6
- package/types/index.d.ts +16 -0
- package/types/worker.d.ts +31 -0
package/dist/cjs/bundler.js
CHANGED
|
@@ -1,60 +1,96 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var node_path = require('node:path');
|
|
5
|
-
var glob = require('glob');
|
|
3
|
+
var fs = require('node:fs/promises');
|
|
6
4
|
var globWatch = require('glob-watcher');
|
|
5
|
+
var glob = require('glob');
|
|
6
|
+
var node_path = require('node:path');
|
|
7
7
|
var index_js = require('./index.js');
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
|
|
9
|
+
const symbolEntities = {
|
|
10
|
+
"'": "'",
|
|
11
|
+
'\\': '\\',
|
|
12
|
+
'\r': 'r',
|
|
13
|
+
'\n': 'n',
|
|
14
|
+
'\t': 't',
|
|
15
|
+
'\u2028': 'u2028',
|
|
16
|
+
'\u2029': 'u2029',
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
const
|
|
14
|
-
|
|
19
|
+
const htmlEntities = {
|
|
20
|
+
'&': '&',
|
|
21
|
+
'<': '<',
|
|
22
|
+
'>': '>',
|
|
23
|
+
'"': '"',
|
|
24
|
+
"'": ''',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const regexKeys = (obj) =>
|
|
28
|
+
new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
|
|
29
|
+
|
|
30
|
+
regexKeys(htmlEntities);
|
|
31
|
+
|
|
32
|
+
regexKeys(symbolEntities);
|
|
33
|
+
|
|
34
|
+
const bindContext = (object, methods = []) => {
|
|
35
|
+
for (let i = 0, len = methods.length; i < len; i++) {
|
|
36
|
+
const name = methods[i];
|
|
37
|
+
if (name in object) {
|
|
38
|
+
object[name] = object[name].bind(object);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
15
41
|
};
|
|
16
42
|
|
|
17
|
-
|
|
18
|
-
|
|
43
|
+
class Bundler {
|
|
44
|
+
#templates = {}
|
|
45
|
+
#bundlerOptions = {
|
|
19
46
|
target: [],
|
|
20
47
|
umd: true,
|
|
21
|
-
minify: true
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
48
|
+
minify: true,
|
|
49
|
+
}
|
|
50
|
+
#compile
|
|
51
|
+
#ejsOptions
|
|
52
|
+
#buildInProgress
|
|
53
|
+
static exports = ['build', 'watch', 'concat', 'output']
|
|
54
|
+
constructor(bundlerOptions = {}, ejsOptions = {}) {
|
|
55
|
+
bindContext(this, this.constructor.exports);
|
|
56
|
+
const { compile, configure } = index_js.create(ejsOptions);
|
|
57
|
+
Object.assign(this.#bundlerOptions, bundlerOptions);
|
|
58
|
+
this.#compile = compile;
|
|
59
|
+
this.#ejsOptions = configure();
|
|
60
|
+
this.#buildInProgress = false;
|
|
61
|
+
this.#templates = {};
|
|
62
|
+
}
|
|
63
|
+
async #stageRead(path) {
|
|
64
|
+
return fs
|
|
65
|
+
.readFile(node_path.join(this.#ejsOptions.path, path))
|
|
30
66
|
.then((response) => response.toString())
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const umd =
|
|
37
|
-
const strict =
|
|
38
|
-
const module =
|
|
67
|
+
}
|
|
68
|
+
#stageCompile(content, name) {
|
|
69
|
+
return this.#compile(content, name).source
|
|
70
|
+
}
|
|
71
|
+
#getBundle() {
|
|
72
|
+
const umd = this.#bundlerOptions.umd;
|
|
73
|
+
const strict = this.#ejsOptions.strict;
|
|
74
|
+
const module = this.#ejsOptions.precompiled;
|
|
39
75
|
const out = [];
|
|
40
76
|
if (umd) {
|
|
41
77
|
out.push('(function(global,factory){');
|
|
42
78
|
out.push(
|
|
43
|
-
'typeof exports === "object" && typeof module !== "undefined" ?'
|
|
79
|
+
'typeof exports === "object" && typeof module !== "undefined" ?',
|
|
44
80
|
);
|
|
45
81
|
out.push('module.exports = factory():');
|
|
46
82
|
out.push(
|
|
47
|
-
'typeof define === "function" && define.amd ? define(factory):'
|
|
83
|
+
'typeof define === "function" && define.amd ? define(factory):',
|
|
48
84
|
);
|
|
49
85
|
out.push(
|
|
50
|
-
'(global = typeof globalThis !== "undefined" ? globalThis:'
|
|
86
|
+
'(global = typeof globalThis !== "undefined" ? globalThis:',
|
|
51
87
|
);
|
|
52
88
|
out.push(`global || self,global["${module}"] = factory())`);
|
|
53
89
|
out.push('})(this,(function(){');
|
|
54
90
|
}
|
|
55
91
|
if (strict) out.push(`'use strict'`);
|
|
56
92
|
out.push('const templates = {}');
|
|
57
|
-
Object.entries(templates).forEach(([name, content]) => {
|
|
93
|
+
Object.entries(this.#templates).forEach(([name, content]) => {
|
|
58
94
|
name = JSON.stringify(name);
|
|
59
95
|
content = String(content);
|
|
60
96
|
out.push(`templates[${name}] = ${content}`);
|
|
@@ -65,87 +101,83 @@ const Bundler = (params = {}, ejsParams = {}) => {
|
|
|
65
101
|
out.push('export default templates');
|
|
66
102
|
}
|
|
67
103
|
return out.join('\n')
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
104
|
+
}
|
|
105
|
+
async build() {
|
|
106
|
+
if (this.#buildInProgress === true) return false
|
|
107
|
+
this.#buildInProgress = true;
|
|
108
|
+
await this.concat().catch(console.error);
|
|
109
|
+
await this.output().catch(console.error);
|
|
110
|
+
console.log('✅', 'bundle complete:', this.#bundlerOptions.target);
|
|
111
|
+
this.#buildInProgress = false;
|
|
112
|
+
}
|
|
113
|
+
async watch() {
|
|
114
|
+
console.log('🔍', 'watch directory:', this.#ejsOptions.path);
|
|
115
|
+
const pattern = '**/*.'.concat(this.#ejsOptions.extension);
|
|
116
|
+
const watcher = globWatch(pattern, { cwd: this.#ejsOptions.path });
|
|
74
117
|
const state = { build: null };
|
|
75
118
|
watcher.on('change', (path) => {
|
|
76
119
|
if (state.build) return
|
|
77
120
|
console.log('⟳', 'file change:', path);
|
|
78
|
-
state.build = build().then(() => {
|
|
121
|
+
state.build = this.build().then(() => {
|
|
79
122
|
state.build = null;
|
|
80
123
|
});
|
|
81
124
|
});
|
|
82
125
|
watcher.on('add', (path) => {
|
|
83
126
|
if (state.build) return
|
|
84
127
|
console.log('+', 'file added:', path);
|
|
85
|
-
state.build = build().then(() => {
|
|
128
|
+
state.build = this.build().then(() => {
|
|
86
129
|
state.build = null;
|
|
87
130
|
});
|
|
88
131
|
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
132
|
+
}
|
|
133
|
+
async concat() {
|
|
134
|
+
const pattern = '**/*.'.concat(this.#ejsOptions.extension);
|
|
135
|
+
const list = await glob.glob(
|
|
136
|
+
pattern,
|
|
137
|
+
{ cwd: this.#ejsOptions.path },
|
|
138
|
+
() => {},
|
|
139
|
+
);
|
|
94
140
|
for (let template of list) {
|
|
95
141
|
let content = '';
|
|
96
|
-
content = await stageRead(template);
|
|
97
|
-
content = await stageCompile(content, template);
|
|
98
|
-
templates[template] = content;
|
|
142
|
+
content = await this.#stageRead(template);
|
|
143
|
+
content = await this.#stageCompile(content, template);
|
|
144
|
+
this.#templates[template] = content;
|
|
99
145
|
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
await concat().catch(console.error);
|
|
106
|
-
await output().catch(console.error);
|
|
107
|
-
console.log('✅', 'bundle complete:', config.target);
|
|
108
|
-
config.buildInProgress = false;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const output = async () => {
|
|
112
|
-
const target = [].concat(config.target);
|
|
113
|
-
const content = getBundle();
|
|
114
|
-
for (let file of target) {
|
|
146
|
+
}
|
|
147
|
+
async output() {
|
|
148
|
+
const target = [].concat(this.#bundlerOptions.target);
|
|
149
|
+
const content = this.#getBundle();
|
|
150
|
+
for (const file of target) {
|
|
115
151
|
const folderPath = node_path.dirname(file);
|
|
116
|
-
const folderExists = await
|
|
152
|
+
const folderExists = await fs
|
|
117
153
|
.stat(folderPath)
|
|
118
154
|
.then(() => true)
|
|
119
155
|
.catch(() => false);
|
|
120
156
|
if (folderExists === false) {
|
|
121
|
-
await
|
|
157
|
+
await fs.mkdir(folderPath, { recursive: true });
|
|
122
158
|
}
|
|
123
|
-
await
|
|
159
|
+
await fs.writeFile(file, content);
|
|
124
160
|
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
build,
|
|
129
|
-
watch,
|
|
130
|
-
concat,
|
|
131
|
-
output
|
|
132
161
|
}
|
|
162
|
+
}
|
|
133
163
|
|
|
164
|
+
const bundler = (bundlerOptions = {}, ejsOptions = {}) => {
|
|
165
|
+
return new Bundler(bundlerOptions, ejsOptions)
|
|
134
166
|
};
|
|
135
167
|
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const bundler = new Bundler(options, config);
|
|
168
|
+
const ejsRollupBundler = (bundlerOptions, ejsOptions) => {
|
|
169
|
+
const bundle = new Bundler(bundlerOptions, ejsOptions);
|
|
139
170
|
return {
|
|
140
171
|
name: 'ejs-bundler',
|
|
141
172
|
async buildStart() {
|
|
142
|
-
await
|
|
173
|
+
await bundle.concat();
|
|
143
174
|
},
|
|
144
175
|
async buildEnd() {
|
|
145
|
-
await
|
|
146
|
-
}
|
|
176
|
+
await bundle.output();
|
|
177
|
+
},
|
|
147
178
|
}
|
|
148
179
|
};
|
|
149
180
|
|
|
150
181
|
exports.Bundler = Bundler;
|
|
151
|
-
exports.
|
|
182
|
+
exports.bundler = bundler;
|
|
183
|
+
exports.ejsRollupBundler = ejsRollupBundler;
|
package/dist/cjs/element.js
CHANGED
|
@@ -1,88 +1,53 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
return typeof v === 'undefined';
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
|
|
8
|
-
var symbolEntities = {
|
|
3
|
+
const symbolEntities = {
|
|
9
4
|
"'": "'",
|
|
10
5
|
'\\': '\\',
|
|
11
6
|
'\r': 'r',
|
|
12
7
|
'\n': 'n',
|
|
13
8
|
'\t': 't',
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
'\u2028': 'u2028',
|
|
10
|
+
'\u2029': 'u2029'
|
|
16
11
|
};
|
|
17
|
-
|
|
12
|
+
const htmlEntities = {
|
|
18
13
|
'&': '&',
|
|
19
14
|
'<': '<',
|
|
20
15
|
'>': '>',
|
|
21
16
|
'"': '"',
|
|
22
17
|
"'": '''
|
|
23
18
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
var htmlEntitiesMatch = regexKeys(htmlEntities);
|
|
19
|
+
const regexKeys = obj => new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
|
|
20
|
+
const htmlEntitiesMatch = regexKeys(htmlEntities);
|
|
28
21
|
regexKeys(symbolEntities);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return ('' + string).replace(htmlEntitiesMatch,
|
|
32
|
-
return htmlEntities[match];
|
|
33
|
-
});
|
|
22
|
+
const entities = function () {
|
|
23
|
+
let string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
24
|
+
return ('' + string).replace(htmlEntitiesMatch, match => htmlEntities[match]);
|
|
34
25
|
};
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
const safeValue = (value, escape) => {
|
|
27
|
+
const check = value;
|
|
37
28
|
return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
|
|
38
29
|
};
|
|
39
|
-
var each = function each(object, callback) {
|
|
40
|
-
var prop;
|
|
41
|
-
for (prop in object) {
|
|
42
|
-
if (hasProp(object, prop)) {
|
|
43
|
-
callback(object[prop], prop, object);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
var map = function map(object, callback, context) {
|
|
48
|
-
var result = [];
|
|
49
|
-
each(object, function (value, key, object) {
|
|
50
|
-
var item = callback(value, key, object);
|
|
51
|
-
if (isUndefined(item) === false) {
|
|
52
|
-
result.push(item);
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
return result;
|
|
56
|
-
};
|
|
57
30
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
31
|
+
const selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
32
|
+
const space = ' ';
|
|
33
|
+
const quote = '"';
|
|
34
|
+
const equal = '=';
|
|
35
|
+
const slash = '/';
|
|
36
|
+
const lt = '<';
|
|
37
|
+
const gt = '>';
|
|
38
|
+
const eachAttribute = _ref => {
|
|
39
|
+
let [key, value] = _ref;
|
|
40
|
+
if (value !== null && value !== undefined) {
|
|
41
|
+
return [entities(key), [quote, entities(value), quote].join('')].join(equal);
|
|
42
|
+
}
|
|
66
43
|
};
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var equal = '=';
|
|
72
|
-
var slash = '/';
|
|
73
|
-
var lt = '<';
|
|
74
|
-
var gt = '>';
|
|
75
|
-
var element = function element(tag, attrs, content) {
|
|
76
|
-
var result = [];
|
|
77
|
-
var hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
78
|
-
var attributes = map(attrs, function (value, key) {
|
|
79
|
-
if (value !== null && value !== undefined) {
|
|
80
|
-
return [entities(key), [quote, entities(value), quote].join('')].join(equal);
|
|
81
|
-
}
|
|
82
|
-
}).join(space);
|
|
44
|
+
const element = (tag, attrs, content) => {
|
|
45
|
+
const result = [];
|
|
46
|
+
const hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
47
|
+
const attributes = Object.entries(attrs ?? {}).map(eachAttribute).filter(e => e).join(space);
|
|
83
48
|
result.push([lt, tag, space, attributes, gt].join(''));
|
|
84
49
|
if (content && hasClosedTag) {
|
|
85
|
-
result.push(content
|
|
50
|
+
result.push(Array.isArray(content) ? content.join('') : content);
|
|
86
51
|
}
|
|
87
52
|
if (hasClosedTag) {
|
|
88
53
|
result.push([lt, slash, tag, gt].join(''));
|