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