@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.
@@ -1,60 +1,96 @@
1
1
  'use strict';
2
2
 
3
- var node_fs = require('node:fs');
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 isPlainObject = function(obj) {
10
- return Object.prototype.toString.call(obj) === '[object Object]'
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 extend = (target, ...sources) => {
14
- return Object.assign(target, ...sources.filter(isPlainObject))
19
+ const htmlEntities = {
20
+ '&': '&',
21
+ '<': '&lt;',
22
+ '>': '&gt;',
23
+ '"': '&quot;',
24
+ "'": '&#x27;',
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
- const Bundler = (params = {}, ejsParams = {}) => {
18
- const config = {
43
+ class Bundler {
44
+ #templates = {}
45
+ #bundlerOptions = {
19
46
  target: [],
20
47
  umd: true,
21
- minify: true
22
- };
23
- const ejs = index_js.create();
24
- const ejsConfig = ejs.configure(ejsParams);
25
- const templates = {};
26
- extend(config, params || {});
27
- const stageRead = (path) => {
28
- return node_fs.promises
29
- .readFile(node_path.join(ejsConfig.path, path))
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
- const stageCompile = (content, name) => {
33
- return ejs.compile(content, name).source
34
- };
35
- const getBundle = () => {
36
- const umd = config.umd;
37
- const strict = ejsConfig.withObject === false;
38
- const module = ejsConfig.export;
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
- const watch = async () => {
71
- console.log('🔍', 'watch directory:', ejsConfig.path);
72
- const pattern = '**/*.'.concat(ejsConfig.extension);
73
- const watcher = globWatch(pattern, { cwd: ejsConfig.path });
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
- const concat = async () => {
92
- const pattern = '**/*.'.concat(ejsConfig.extension);
93
- const list = await glob.glob(pattern, { cwd: ejsConfig.path });
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
- const build = async () => {
103
- if (config.buildInProgress === true) return false
104
- config.buildInProgress = true;
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 node_fs.promises
152
+ const folderExists = await fs
117
153
  .stat(folderPath)
118
154
  .then(() => true)
119
155
  .catch(() => false);
120
156
  if (folderExists === false) {
121
- await node_fs.promises.mkdir(folderPath, { recursive: true });
157
+ await fs.mkdir(folderPath, { recursive: true });
122
158
  }
123
- await node_fs.promises.writeFile(file, content);
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 ejsBundler = (options, config) => {
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 bundler.concat();
173
+ await bundle.concat();
143
174
  },
144
175
  async buildEnd() {
145
- await bundler.output();
146
- }
176
+ await bundle.output();
177
+ },
147
178
  }
148
179
  };
149
180
 
150
181
  exports.Bundler = Bundler;
151
- exports.ejsBundler = ejsBundler;
182
+ exports.bundler = bundler;
183
+ exports.ejsRollupBundler = ejsRollupBundler;
@@ -1,88 +1,53 @@
1
1
  'use strict';
2
2
 
3
- var isUndefined = function isUndefined(v) {
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
- "\u2028": 'u2028',
15
- "\u2029": 'u2029'
9
+ '\u2028': 'u2028',
10
+ '\u2029': 'u2029'
16
11
  };
17
- var htmlEntities = {
12
+ const htmlEntities = {
18
13
  '&': '&amp;',
19
14
  '<': '&lt;',
20
15
  '>': '&gt;',
21
16
  '"': '&quot;',
22
17
  "'": '&#x27;'
23
18
  };
24
- var regexKeys = function regexKeys(obj) {
25
- return new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
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
- var entities = function entities() {
30
- var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
31
- return ('' + string).replace(htmlEntitiesMatch, function (match) {
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
- var safeValue = function safeValue(value, escape) {
36
- var check = value;
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
- * @param object
61
- * @param prop
62
- * @return {boolean}
63
- */
64
- var hasProp = function hasProp(object, prop) {
65
- return object && object.hasOwnProperty(prop);
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
- var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
69
- var space = ' ';
70
- var quote = '"';
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 instanceof Array ? content.join('') : 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(''));