@kosatyi/ejs 0.0.105 → 0.0.106

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.
@@ -3,10 +3,10 @@
3
3
  var node_fs = require('node:fs');
4
4
  var node_path = require('node:path');
5
5
  var glob = require('glob');
6
- var watch = require('glob-watcher');
6
+ var globWatch = require('glob-watcher');
7
7
  var index_js = require('./index.js');
8
8
 
9
- const isPlainObject = function (obj) {
9
+ const isPlainObject = function(obj) {
10
10
  return Object.prototype.toString.call(obj) === '[object Object]'
11
11
  };
12
12
 
@@ -14,46 +14,28 @@ const extend = (target, ...sources) => {
14
14
  return Object.assign(target, ...sources.filter(isPlainObject))
15
15
  };
16
16
 
17
- class Bundler {
18
- /**
19
- * @type {BundlerOptions}
20
- */
21
- options = {
17
+ const Bundler = (params = {}, ejsParams = {}) => {
18
+ const config = {
22
19
  target: [],
23
20
  umd: true,
24
- minify: true,
25
- }
26
- /**
27
- * @type {EjsConfig}
28
- */
29
- config = {}
30
-
31
- /**
32
- *
33
- * @param options
34
- * @param config
35
- */
36
- constructor(options, config) {
37
- extend(this.options, options || {});
38
- this.ejs = index_js.create();
39
- this.config = this.ejs.configure(config);
40
- this.templates = {};
41
- }
42
-
43
- stageRead(path) {
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) => {
44
28
  return node_fs.promises
45
- .readFile(node_path.join(this.config.path, path))
29
+ .readFile(node_path.join(ejsConfig.path, path))
46
30
  .then((response) => response.toString())
47
- }
48
-
49
- stageCompile(content, name) {
50
- return this.ejs.compile(content, name).source
51
- }
52
-
53
- getBundle() {
54
- const umd = this.options.umd;
55
- const strict = this.config.withObject === false;
56
- const module = this.config.export;
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;
57
39
  const out = [];
58
40
  if (umd) {
59
41
  out.push('(function(global,factory){');
@@ -72,7 +54,7 @@ class Bundler {
72
54
  }
73
55
  if (strict) out.push(`'use strict'`);
74
56
  out.push('const templates = {}');
75
- Object.entries(this.templates).forEach(([name, content]) => {
57
+ Object.entries(templates).forEach(([name, content]) => {
76
58
  name = JSON.stringify(name);
77
59
  content = String(content);
78
60
  out.push(`templates[${name}] = ${content}`);
@@ -83,50 +65,52 @@ class Bundler {
83
65
  out.push('export default templates');
84
66
  }
85
67
  return out.join('\n')
86
- }
87
- async watch() {
88
- console.log('🔍', 'watch directory:', this.config.path);
89
- const pattern = '**/*.'.concat(this.config.extension);
90
- const watcher = watch(pattern, { cwd: this.config.path });
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 });
91
74
  const state = { build: null };
92
75
  watcher.on('change', (path) => {
93
76
  if (state.build) return
94
77
  console.log('⟳', 'file change:', path);
95
- state.build = this.build().then(() => {
78
+ state.build = build().then(() => {
96
79
  state.build = null;
97
80
  });
98
81
  });
99
82
  watcher.on('add', (path) => {
100
83
  if (state.build) return
101
84
  console.log('+', 'file added:', path);
102
- state.build = this.build().then(() => {
85
+ state.build = build().then(() => {
103
86
  state.build = null;
104
87
  });
105
88
  });
106
- }
89
+ };
107
90
 
108
- async build() {
109
- if (this.buildInProgress === true) return false
110
- this.buildInProgress = true;
111
- await this.concat().catch(console.error);
112
- await this.output().catch(console.error);
113
- console.log('✅', 'bundle complete:', this.options.target);
114
- this.buildInProgress = false;
115
- }
116
-
117
- async concat() {
118
- const pattern = '**/*.'.concat(this.config.extension);
119
- const list = await glob.glob(pattern, { cwd: this.config.path });
91
+ const concat = async () => {
92
+ const pattern = '**/*.'.concat(ejsConfig.extension);
93
+ const list = await glob.glob(pattern, { cwd: ejsConfig.path });
120
94
  for (let template of list) {
121
95
  let content = '';
122
- content = await this.stageRead(template);
123
- content = await this.stageCompile(content, template);
124
- this.templates[template] = content;
96
+ content = await stageRead(template);
97
+ content = await stageCompile(content, template);
98
+ templates[template] = content;
125
99
  }
126
- }
127
- async output() {
128
- const target = [].concat(this.options.target);
129
- const content = this.getBundle();
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();
130
114
  for (let file of target) {
131
115
  const folderPath = node_path.dirname(file);
132
116
  const folderExists = await node_fs.promises
@@ -138,8 +122,17 @@ class Bundler {
138
122
  }
139
123
  await node_fs.promises.writeFile(file, content);
140
124
  }
125
+ };
126
+
127
+ return {
128
+ build,
129
+ watch,
130
+ concat,
131
+ output
141
132
  }
142
- }
133
+
134
+ };
135
+
143
136
 
144
137
  const ejsBundler = (options, config) => {
145
138
  const bundler = new Bundler(options, config);
@@ -150,7 +143,7 @@ const ejsBundler = (options, config) => {
150
143
  },
151
144
  async buildEnd() {
152
145
  await bundler.output();
153
- },
146
+ }
154
147
  }
155
148
  };
156
149