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