@kosatyi/ejs 0.0.82 → 0.0.83

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/bin/bundler.js CHANGED
@@ -8,7 +8,8 @@ const schema = argv(process.argv.slice(2))
8
8
 
9
9
  const params = schema({
10
10
  target: null,
11
- minify: false,
11
+ minify: true,
12
+ umd: true,
12
13
  withObject: false,
13
14
  export: 'ejsPrecompiled',
14
15
  path: 'views',
@@ -2,11 +2,12 @@
2
2
 
3
3
  var fs = require('fs');
4
4
  var glob = require('glob');
5
+ var watch = require('glob-watcher');
5
6
  var path = require('path');
6
7
  var terser = require('terser');
7
8
  var index_js = require('./index.js');
8
9
 
9
- const isPlainObject = function (obj) {
10
+ const isPlainObject = function(obj) {
10
11
  return Object.prototype.toString.call(obj) === '[object Object]'
11
12
  };
12
13
 
@@ -20,87 +21,95 @@ class Bundler {
20
21
  */
21
22
  options = {
22
23
  target: [],
23
- transform: false,
24
- minify: false,
25
- timestamp: true,
24
+ umd: true,
25
+ minify: true
26
26
  }
27
27
  /**
28
28
  * @type {EjsConfig}
29
29
  */
30
30
  config = {}
31
+
31
32
  constructor(options, config) {
32
33
  extend(this.options, options || {});
33
34
  this.config = index_js.configure(config || {});
34
35
  this.templates = {};
35
36
  }
37
+
36
38
  stageRead(path$1) {
37
39
  return fs.promises
38
40
  .readFile(path.join(this.config.path, path$1))
39
41
  .then((response) => response.toString())
40
42
  }
43
+
41
44
  stageCompile(content, name) {
42
45
  return index_js.compile(content, name).source
43
46
  }
47
+
48
+ stageWrapper(content){
49
+ const umd = this.options.umd;
50
+ const module = this.config.export;
51
+ const out = [];
52
+ if (umd) {
53
+ out.push('(function(global,factory){');
54
+ out.push('typeof exports === "object" && typeof module !== "undefined" ?');
55
+ out.push('module.exports = factory():');
56
+ out.push('typeof define === "function" && define.amd ? define(factory):');
57
+ out.push('(global = typeof globalThis !== "undefined" ? globalThis:');
58
+ out.push(`global || self,global["${module}"] = factory())`);
59
+ out.push('})(this,(function(){');
60
+ }
61
+ out.push(content);
62
+ if (umd) {
63
+ out.push('return templates}))');
64
+ } else {
65
+ out.push('export default templates');
66
+ }
67
+ return out.join('')
68
+ }
44
69
  async stageMinify(content) {
45
70
  if (this.options.minify === false) return content
46
71
  const config = {
47
72
  compress: {
48
73
  dead_code: false,
49
- side_effects: false,
50
- },
74
+ side_effects: false
75
+ }
51
76
  };
52
77
  const response = await terser.minify(content, config);
53
78
  return response.code
54
79
  }
80
+
55
81
  getBundle() {
56
- const transform = this.options.transform;
57
- const moduleId = this.config.export;
58
- const useStrict = this.config.withObject === false;
59
82
  const out = [];
60
- if (transform) {
61
- out.push('(function(global,factory){');
62
- out.push(
63
- 'typeof exports === "object" && typeof module !== "undefined" ?'
64
- );
65
- out.push('module.exports = factory():');
66
- out.push(
67
- 'typeof define === "function" && define.amd ? define(factory):'
68
- );
69
- out.push(
70
- '(global = typeof globalThis !== "undefined" ? globalThis:'
71
- );
72
- out.push('global || self,global["' + moduleId + '"] = factory())');
73
- out.push('})(this,(function(){');
74
- }
75
- if (useStrict) out.push("'use strict'");
83
+ if (this.config.withObject === false) out.push(`'use strict'`);
76
84
  out.push('const templates = {}');
77
85
  Object.entries(this.templates).forEach(([name, content]) => {
78
86
  name = JSON.stringify(name);
79
87
  content = String(content);
80
88
  out.push(`templates[${name}] = ${content}`);
81
89
  });
82
- if (transform) {
83
- out.push('return templates');
84
- out.push('}))');
85
- } else {
86
- out.push('export default templates');
87
- }
88
90
  return out.join('\n')
89
91
  }
90
92
  async watch() {
91
93
  console.log(`ejs-bundler: watching directory - ${this.config.path}`);
92
- try {
93
- const watcher = fs.promises.watch(this.config.path, { recursive: true });
94
- for await (const { filename } of watcher) {
95
- if (path.extname(filename).slice(1) === this.config.extension) {
96
- console.log(`ejs-bundler: file is changed - ${filename}`);
97
- await this.build();
98
- }
99
- }
100
- } catch (err) {
101
- throw err
102
- }
94
+ const pattern = '**/*.'.concat(this.config.extension);
95
+ const watcher = watch(pattern, { cwd: this.config.path });
96
+ const state = { build: null };
97
+ watcher.on('change', (path) => {
98
+ if(state.build) return;
99
+ console.log(`ejs-bundler: file is changed - ${path}`);
100
+ state.build = this.build().then(()=>{
101
+ state.build = null;
102
+ });
103
+ });
104
+ watcher.on('add', (path) => {
105
+ if(state.build) return;
106
+ console.log(`ejs-bundler: file is added - ${path}`);
107
+ state.build = this.build().then(()=>{
108
+ state.build = null;
109
+ });
110
+ });
103
111
  }
112
+
104
113
  async build() {
105
114
  if (this.buildInProgress === true) return false
106
115
  this.buildInProgress = true;
@@ -109,6 +118,7 @@ class Bundler {
109
118
  console.log(`ejs-bundler: bundle complete - ${this.options.target}`);
110
119
  this.buildInProgress = false;
111
120
  }
121
+
112
122
  async concat() {
113
123
  const pattern = '**/*.'.concat(this.config.extension);
114
124
  const list = await glob.glob(pattern, { cwd: this.config.path });
@@ -119,12 +129,14 @@ class Bundler {
119
129
  this.templates[template] = content;
120
130
  }
121
131
  }
132
+
122
133
  async output() {
123
134
  const target = [].concat(this.options.target);
124
135
  let content = this.getBundle();
125
136
  if (this.options.minify) {
126
137
  content = await this.stageMinify(content);
127
138
  }
139
+ content = this.stageWrapper(content);
128
140
  for (let file of target) {
129
141
  const folderPath = path.dirname(file);
130
142
  const folderExists = await fs.promises
@@ -148,7 +160,7 @@ const ejsBundler = (options, config) => {
148
160
  },
149
161
  async buildEnd() {
150
162
  await bundler.output();
151
- },
163
+ }
152
164
  }
153
165
  };
154
166
 
@@ -1,10 +1,11 @@
1
1
  import { promises } from 'fs';
2
2
  import { glob } from 'glob';
3
- import { join, extname, dirname } from 'path';
3
+ import watch from 'glob-watcher';
4
+ import { join, dirname } from 'path';
4
5
  import { minify } from 'terser';
5
6
  import { configure, compile } from './index.js';
6
7
 
7
- const isPlainObject = function (obj) {
8
+ const isPlainObject = function(obj) {
8
9
  return Object.prototype.toString.call(obj) === '[object Object]'
9
10
  };
10
11
 
@@ -18,87 +19,95 @@ class Bundler {
18
19
  */
19
20
  options = {
20
21
  target: [],
21
- transform: false,
22
- minify: false,
23
- timestamp: true,
22
+ umd: true,
23
+ minify: true
24
24
  }
25
25
  /**
26
26
  * @type {EjsConfig}
27
27
  */
28
28
  config = {}
29
+
29
30
  constructor(options, config) {
30
31
  extend(this.options, options || {});
31
32
  this.config = configure(config || {});
32
33
  this.templates = {};
33
34
  }
35
+
34
36
  stageRead(path) {
35
37
  return promises
36
38
  .readFile(join(this.config.path, path))
37
39
  .then((response) => response.toString())
38
40
  }
41
+
39
42
  stageCompile(content, name) {
40
43
  return compile(content, name).source
41
44
  }
45
+
46
+ stageWrapper(content){
47
+ const umd = this.options.umd;
48
+ const module = this.config.export;
49
+ const out = [];
50
+ if (umd) {
51
+ out.push('(function(global,factory){');
52
+ out.push('typeof exports === "object" && typeof module !== "undefined" ?');
53
+ out.push('module.exports = factory():');
54
+ out.push('typeof define === "function" && define.amd ? define(factory):');
55
+ out.push('(global = typeof globalThis !== "undefined" ? globalThis:');
56
+ out.push(`global || self,global["${module}"] = factory())`);
57
+ out.push('})(this,(function(){');
58
+ }
59
+ out.push(content);
60
+ if (umd) {
61
+ out.push('return templates}))');
62
+ } else {
63
+ out.push('export default templates');
64
+ }
65
+ return out.join('')
66
+ }
42
67
  async stageMinify(content) {
43
68
  if (this.options.minify === false) return content
44
69
  const config = {
45
70
  compress: {
46
71
  dead_code: false,
47
- side_effects: false,
48
- },
72
+ side_effects: false
73
+ }
49
74
  };
50
75
  const response = await minify(content, config);
51
76
  return response.code
52
77
  }
78
+
53
79
  getBundle() {
54
- const transform = this.options.transform;
55
- const moduleId = this.config.export;
56
- const useStrict = this.config.withObject === false;
57
80
  const out = [];
58
- if (transform) {
59
- out.push('(function(global,factory){');
60
- out.push(
61
- 'typeof exports === "object" && typeof module !== "undefined" ?'
62
- );
63
- out.push('module.exports = factory():');
64
- out.push(
65
- 'typeof define === "function" && define.amd ? define(factory):'
66
- );
67
- out.push(
68
- '(global = typeof globalThis !== "undefined" ? globalThis:'
69
- );
70
- out.push('global || self,global["' + moduleId + '"] = factory())');
71
- out.push('})(this,(function(){');
72
- }
73
- if (useStrict) out.push("'use strict'");
81
+ if (this.config.withObject === false) out.push(`'use strict'`);
74
82
  out.push('const templates = {}');
75
83
  Object.entries(this.templates).forEach(([name, content]) => {
76
84
  name = JSON.stringify(name);
77
85
  content = String(content);
78
86
  out.push(`templates[${name}] = ${content}`);
79
87
  });
80
- if (transform) {
81
- out.push('return templates');
82
- out.push('}))');
83
- } else {
84
- out.push('export default templates');
85
- }
86
88
  return out.join('\n')
87
89
  }
88
90
  async watch() {
89
91
  console.log(`ejs-bundler: watching directory - ${this.config.path}`);
90
- try {
91
- const watcher = promises.watch(this.config.path, { recursive: true });
92
- for await (const { filename } of watcher) {
93
- if (extname(filename).slice(1) === this.config.extension) {
94
- console.log(`ejs-bundler: file is changed - ${filename}`);
95
- await this.build();
96
- }
97
- }
98
- } catch (err) {
99
- throw err
100
- }
92
+ const pattern = '**/*.'.concat(this.config.extension);
93
+ const watcher = watch(pattern, { cwd: this.config.path });
94
+ const state = { build: null };
95
+ watcher.on('change', (path) => {
96
+ if(state.build) return;
97
+ console.log(`ejs-bundler: file is changed - ${path}`);
98
+ state.build = this.build().then(()=>{
99
+ state.build = null;
100
+ });
101
+ });
102
+ watcher.on('add', (path) => {
103
+ if(state.build) return;
104
+ console.log(`ejs-bundler: file is added - ${path}`);
105
+ state.build = this.build().then(()=>{
106
+ state.build = null;
107
+ });
108
+ });
101
109
  }
110
+
102
111
  async build() {
103
112
  if (this.buildInProgress === true) return false
104
113
  this.buildInProgress = true;
@@ -107,6 +116,7 @@ class Bundler {
107
116
  console.log(`ejs-bundler: bundle complete - ${this.options.target}`);
108
117
  this.buildInProgress = false;
109
118
  }
119
+
110
120
  async concat() {
111
121
  const pattern = '**/*.'.concat(this.config.extension);
112
122
  const list = await glob(pattern, { cwd: this.config.path });
@@ -117,12 +127,14 @@ class Bundler {
117
127
  this.templates[template] = content;
118
128
  }
119
129
  }
130
+
120
131
  async output() {
121
132
  const target = [].concat(this.options.target);
122
133
  let content = this.getBundle();
123
134
  if (this.options.minify) {
124
135
  content = await this.stageMinify(content);
125
136
  }
137
+ content = this.stageWrapper(content);
126
138
  for (let file of target) {
127
139
  const folderPath = dirname(file);
128
140
  const folderExists = await promises
@@ -146,7 +158,7 @@ const ejsBundler = (options, config) => {
146
158
  },
147
159
  async buildEnd() {
148
160
  await bundler.output();
149
- },
161
+ }
150
162
  }
151
163
  };
152
164
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "EJS Templates",
4
4
  "homepage": "https://github.com/kosatyi/ejs",
5
5
  "type": "module",
6
- "version": "0.0.82",
6
+ "version": "0.0.83",
7
7
  "main": "dist/cjs/index.js",
8
8
  "module": "dist/esm/index.js",
9
9
  "browser": "dist/umd/browser.js",
@@ -74,5 +74,8 @@
74
74
  "rollup": "^4.28.1",
75
75
  "rollup-plugin-copy": "^3.5.0",
76
76
  "rollup-plugin-ignore": "^1.0.10"
77
+ },
78
+ "dependencies": {
79
+ "glob-watcher": "^6.0.0"
77
80
  }
78
81
  }
@@ -11,6 +11,7 @@ export interface EjsConfig {
11
11
  export interface BundlerOptions {
12
12
  target: string[] | string
13
13
  minify?: boolean
14
+ umd?: boolean,
14
15
  }
15
16
 
16
17
  export function ejsBundle(