vanilla-jet 1.1.10 → 1.2.0

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.
@@ -9,7 +9,6 @@ module.exports = function(grunt) {
9
9
  .replace('/vanilla-jet', '')
10
10
  .replace('/.grunt', '');
11
11
  }
12
- console.log('cwd 1: ', getCleanedCWD());
13
12
 
14
13
  // -- Content
15
14
  let adminContent = grunt.file.read(`${getCleanedCWD()}/assets/styles/less/admin.less`);
@@ -3,7 +3,8 @@ const path = require("path"),
3
3
  fs = require("fs"),
4
4
  nunjucks = require('nunjucks'),
5
5
  identifier = 'templates',
6
- chalk = require('chalk');
6
+ chalk = require('chalk'),
7
+ zlib = require('zlib');
7
8
 
8
9
  let Functions = require('../framework/functions.js');
9
10
  let Dipper = require('../framework/dipper.js');
@@ -182,12 +183,7 @@ function replaceInclude(lines, originalLine, templateCompiled) {
182
183
 
183
184
  // -- Step 4
184
185
  async function createHTMLFile(content, filePath) {
185
-
186
- //const { html } = require('js-beautify');
187
- ///content = html(content);
188
-
189
186
  const { minify } = require('html-minifier-terser');
190
- //console.log(typeof content);
191
187
  const minified = await minify(content, {
192
188
  collapseWhitespace: true,
193
189
  collapseInlineTagWhitespace: true,
@@ -202,8 +198,16 @@ async function createHTMLFile(content, filePath) {
202
198
  const publicPath = path.join(processCwd(), '/public/pages');
203
199
  fs.mkdirSync(publicPath, { recursive: true });
204
200
  const absolutePath = path.join(publicPath, filePath);
201
+
202
+ // Write the minified HTML file
205
203
  fs.writeFileSync(absolutePath, minified, 'utf8');
206
- //console.log(`Html :) file created at: ${absolutePath}`);
204
+
205
+ // Create gzipped version
206
+ const gzipped = zlib.gzipSync(minified, { level: 9 }); // Maximum compression level
207
+ fs.writeFileSync(`${absolutePath}.gz`, gzipped);
208
+
209
+ console.log(chalk.green(`Created HTML file at: ${absolutePath}`));
210
+ console.log(chalk.green(`Created gzipped version at: ${absolutePath}.gz`));
207
211
  }
208
212
 
209
213
  // -- Helpers
package/Gruntfile.js CHANGED
@@ -26,7 +26,6 @@ module.exports = function(grunt) {
26
26
  return newCwd;
27
27
  }
28
28
 
29
-
30
29
  // -- Vars
31
30
  const cssDestination = `${getCwd()}/public/styles/app.min.css`;
32
31
  const cssOrigin = `${getCwd()}/assets/styles/less/admin_build.less`;
@@ -153,13 +152,16 @@ module.exports = function(grunt) {
153
152
  compress: {
154
153
  main: {
155
154
  options: {
156
- mode: 'gzip'
155
+ mode: 'gzip',
156
+ compressionLevel: 9
157
157
  },
158
158
  files: [{
159
159
  expand: true,
160
- src: [`${getCwd()}/public/scripts/vanilla.min.js`],
160
+ src: [`${getCwd()}/public/scripts/vanilla.min.js`, `${getCwd()}/public/styles/app.min.css`],
161
161
  dest: '',
162
- ext: '.min.js.gz'
162
+ rename: function(dest, src) {
163
+ return src + '.gz';
164
+ }
163
165
  }]
164
166
  }
165
167
  },
@@ -69,10 +69,10 @@ Dipper.prototype.getFbAppId = function() {
69
69
 
70
70
  Dipper.prototype.addMeta = function(name, content, attribute) {
71
71
 
72
- var obj = this;
72
+ let obj = this;
73
73
  attribute = attribute || 'name';
74
74
  content = content || '';
75
- var meta = [];
75
+ let meta = [];
76
76
  meta['name'] = name;
77
77
  meta['content'] = content;
78
78
  meta['attribute'] = attribute;
@@ -81,20 +81,20 @@ Dipper.prototype.addMeta = function(name, content, attribute) {
81
81
 
82
82
  Dipper.prototype.metaTags = function() {
83
83
 
84
- var obj = this;
85
- var _ = require('underscore');
86
- var stringMeta = '';
84
+ let obj = this;
85
+ let _ = require('underscore');
86
+ let stringMeta = '';
87
87
 
88
- var keys = Object.keys(obj.metas);
88
+ let keys = Object.keys(obj.metas);
89
89
  _.each(keys, function(key) {
90
90
 
91
- var name = obj.metas[key]['name'],
91
+ let name = obj.metas[key]['name'],
92
92
  content = obj.metas[key]['content'],
93
93
  attribute = obj.metas[key]['attribute'];
94
94
 
95
- stringMeta += obj.metas[key]['content'] != '' ?
96
- '<meta ' + attribute + '=\"' + name + '\" content=\"' + content + '\">\n\t' :
97
- '<meta ' + attribute + '=\"' + name + '\">\n\t';
95
+ stringMeta += obj.metas[key]['content'] != '' ?
96
+ `<meta ${attribute}="${name}" content="${content}">\n\t` :
97
+ `<meta ${name}="${attribute}">\n\t`;
98
98
  });
99
99
  return stringMeta;
100
100
  }
@@ -108,27 +108,13 @@ Dipper.prototype.img = function(filename) {
108
108
  }
109
109
 
110
110
  Dipper.prototype.script = function(filename) {
111
-
112
- var obj = this,
113
- dir = this.getDir('scripts', false);
114
-
115
- var filenameParts = filename.split('.'),
116
- length = filenameParts.length;
117
- if (length > 0) {
118
-
119
- let position = length - 1;
120
- filenameParts[position] = 'min.' + filenameParts[position];
121
- filename = filenameParts.join('.');
122
- }
111
+ let dir = this.getDir('scripts', false);
123
112
  return this.urlTo(dir + filename);
124
113
  }
125
114
 
126
115
  Dipper.prototype.style = function(filename) {
127
-
128
- var obj = this,
129
- dir = this.getDir('styles', false),
130
- ret = this.urlTo(dir + filename);
131
- return ret;
116
+ let dir = this.getDir('styles', false);
117
+ return this.urlTo(dir + filename);;
132
118
  }
133
119
 
134
120
  Dipper.prototype.pdf = function(filename) {
@@ -530,21 +516,20 @@ Dipper.prototype.getSharedVar = function(name) {
530
516
 
531
517
  Dipper.prototype.get_google_fonts = function(fonts) {
532
518
 
533
- var obj = this,
534
- _ = require('underscore');
519
+ let _ = require('underscore');
535
520
 
536
521
  if (fonts != undefined) {
537
522
 
538
- var parts = [],
523
+ let parts = [],
539
524
  keys = Object.keys(fonts);
540
525
  _.each(keys, function(font) {
541
526
 
542
- var font_name = encodeURI(font),
527
+ let font_name = encodeURI(font),
543
528
  font_weight = fonts[font].join(',');
544
529
  parts.push(font_name + ':' + font_weight);
545
530
  });
546
531
 
547
- var params = parts.join('|'),
532
+ let params = parts.join('|'),
548
533
  ret = '//fonts.googleapis.com/css?family=' + params;
549
534
 
550
535
  return ret;
@@ -76,36 +76,35 @@ class Functions {
76
76
  }
77
77
  scriptsKeys.push('jquery');
78
78
  scriptsKeys.push('underscore');
79
- dipper.registerScript('vanillaJet', dipper.script('core/vanillaJet.js'), scriptsKeys);
79
+ dipper.registerScript('vanillaJet', dipper.script('core/vanillaJet.min.js'), scriptsKeys);
80
80
  dipper.enqueueScript('vanillaJet');
81
81
 
82
82
  // -- Main app
83
- dipper.registerScript('vanilla', dipper.script('vanilla.js'));
83
+ dipper.registerScript('vanilla', dipper.script('vanilla.min.js'));
84
84
  dipper.enqueueScript('vanilla');
85
85
 
86
+ // Add basic meta tags
86
87
  const basicMeta = [
87
- { name: 'charset', content: 'UTF-8' },
88
- { name: 'viewport', content: 'width=device-width, minimum-scale=1' },
88
+ { name: 'charset', attribute: 'UTF-8' },
89
+ { name: 'viewport', content: 'width=device-width, initial-scale=1.0, maximum-scale=5.0' },
89
90
  { name: 'theme-color', content: '#ffffff' }
90
91
  ];
91
-
92
- const openGraphMeta = {
93
- 'title': dipper.getPageTitle(),
94
- 'site_name': dipper.getSiteTitle(),
95
- 'description': dipper.getDescription(),
96
- 'image': dipper.img('logo.png'),
97
- 'type': 'website',
98
- 'url': dipper.urlTo('')
99
- };
100
-
101
- // Add basic meta tags
102
92
  basicMeta.forEach(meta => {
103
93
  dipper.addMeta(meta.name, meta.content);
104
94
  });
105
95
 
106
96
  // Add Open Graph meta tags
97
+ const openGraphMeta = {
98
+ 'og:title': dipper.getPageTitle(),
99
+ 'og:site_name': dipper.getSiteTitle(),
100
+ 'og:description': dipper.getDescription(),
101
+ 'og:image': dipper.img('logo.png'),
102
+ 'og:type': 'website',
103
+ 'og:url': dipper.urlTo(''),
104
+ 'og:locale': 'es_MX',
105
+ };
107
106
  Object.entries(openGraphMeta).forEach(([key, value]) => {
108
- dipper.addMeta(`og:${key}`, value, 'property');
107
+ dipper.addMeta(key, value, 'property');
109
108
  });
110
109
  }
111
110
  }
@@ -17,6 +17,11 @@ class Request {
17
17
  this.id = '';
18
18
  this.path = '';
19
19
  this.parts = [];
20
+
21
+ let acceptEncoding = req.headers['accept-encoding'] || '';
22
+ acceptEncoding = acceptEncoding.replace(/\s+/g, '');
23
+ this.acceptEncoding = acceptEncoding.split(',') || [];
24
+
20
25
  this.init(req, options);
21
26
  }
22
27
 
@@ -90,9 +95,8 @@ class Request {
90
95
  }
91
96
 
92
97
  get(name, value) {
93
-
94
- var obj = this, ret = value || '';
95
- // Try to retrieve parameter from GET object
98
+ let obj = this,
99
+ ret = value || '';
96
100
  if (typeof obj.params.get[name] !== 'undefined') {
97
101
  ret = obj.params.get[name];
98
102
  }
@@ -100,8 +104,7 @@ class Request {
100
104
  }
101
105
 
102
106
  body() {
103
-
104
- var obj = this;
107
+ let obj = this;
105
108
  return obj.params.body;
106
109
  }
107
110
  }
@@ -10,31 +10,23 @@ class Response {
10
10
  this.status = 200;
11
11
  this.headers = [];
12
12
  this.autoRespond = true;
13
- // Call initialization callback
14
13
  this.init(res);
15
14
  }
16
15
 
17
16
  init(res) {
18
-
19
- var obj = this;
20
- obj.res = res;
17
+ this.res = res;
21
18
  }
22
19
 
23
20
  setBody(body) {
24
-
25
- var obj = this;
26
- obj.body = body;
21
+ this.body = body;
27
22
  }
28
23
 
29
24
  setStatus(status) {
30
-
31
- var obj = this;
32
- obj.status = status;
25
+ this.status = status;
33
26
  }
34
27
 
35
28
  setHeader(name, value) {
36
-
37
- var obj = this;
29
+ let obj = this;
38
30
  obj.headers.push({
39
31
  name: name,
40
32
  value: value
@@ -42,20 +34,17 @@ class Response {
42
34
  }
43
35
 
44
36
  getBody() {
45
-
46
- var obj = this;
47
- return obj.body;
37
+ return this.body;
48
38
  }
49
39
 
50
40
  getStatus() {
51
-
52
- var obj = this;
53
- return obj.status;
41
+ return this.status;
54
42
  }
55
43
 
56
44
  getHeader(name) {
57
45
 
58
- var obj = this, ret = null;
46
+ let obj = this,
47
+ ret = null;
59
48
  ret = _.find(obj.headers, function (header) {
60
49
  return header.name == name;
61
50
  });
@@ -64,7 +53,7 @@ class Response {
64
53
 
65
54
  respond() {
66
55
 
67
- var obj = this;
56
+ let obj = this;
68
57
  this.setHeader('Access-Control-Allow-Origin', '*');
69
58
 
70
59
  _.each(obj.headers, function (header) {
@@ -75,23 +64,29 @@ class Response {
75
64
  }
76
65
 
77
66
  error404() {
78
-
79
67
  let obj = this;
80
68
  obj.res.writeHead(404, { "Content-Type": "text/plain" });
81
69
  obj.res.write("404 Not Found\n");
82
70
  obj.res.end();
83
71
  }
84
72
 
85
- render(template) {
73
+ render(request, template) {
86
74
 
87
75
  let obj = this,
88
76
  path = require("path"),
89
77
  fs = require("fs");
90
78
  template = 'pages/' + template;
91
79
 
80
+ const acceptEncoding = request.acceptEncoding || [];
81
+ if (acceptEncoding.includes('gzip')) {
82
+ template = template + '.gz';
83
+ obj.res.setHeader('Content-Encoding', 'gzip');
84
+ } else {
85
+ obj.res.setHeader('Content-Type', 'text/html');
86
+ }
87
+
92
88
  const filename = path.join(process.cwd(), 'public/' + template);
93
89
  const fileStream = fs.createReadStream(filename);
94
- obj.res.writeHead(200, { 'Content-Type': 'text/html' });
95
90
  fileStream.pipe(obj.res);
96
91
  }
97
92
  }
@@ -20,21 +20,18 @@ class Router {
20
20
  'png': 'image/png',
21
21
  'webp': 'image/webp',
22
22
  'jpg': 'image/jpg',
23
- 'css': 'text/css',
23
+ 'css': 'text/css; charset=utf-8',
24
24
  'gz': 'application/x-gzip',
25
25
  'gif': 'image/gif',
26
- 'js': 'text/javascript',
26
+ 'js': 'text/javascript; charset=utf-8',
27
27
  'svg': 'image/svg+xml',
28
28
  'ttf': 'application/x-font-ttf',
29
29
  'otf': 'application/x-font-opentype',
30
30
  'pdf': 'application/pdf',
31
31
  'json': 'application/json'
32
32
  };
33
- this.compressionMimes = {
34
- 'css': 'text/css',
35
- 'js': 'text/javascript',
36
- 'gz': 'application/x-gzip'
37
- };
33
+ this.compressionMimes = [ 'css', 'js' ];
34
+ this.compressionFiles = [ 'vanilla.min.js', 'app.min.css' ];
38
35
  }
39
36
 
40
37
  routeToRegExp(route) {
@@ -111,13 +108,20 @@ class Router {
111
108
  filename = path.join(rep, route),
112
109
  filePrivate = obj.isProtectedFile(route);
113
110
 
111
+ // -- Check if the file is a gzip file
112
+ if (request.acceptEncoding.includes('gzip') &&
113
+ obj.compressionMimes.includes(ext) &&
114
+ obj.compressionFiles.includes(filename.split('/').pop())
115
+ ) {
116
+ filename = filename + '.gz';
117
+ extHeader['Content-Encoding'] = 'gzip';
118
+ }
119
+
114
120
  fs.stat(filename, (err, stats) => {
115
121
 
116
122
  if (err || !stats.isFile() || filePrivate) {
117
123
  return obj.onNotFound(response);
118
124
  }
119
-
120
- // -- const acceptEncoding = req.headers['accept-encoding'] || '';
121
125
  const fileStream = fs.createReadStream(filename);
122
126
  fileStream.on('error', (streamErr) => {
123
127
  console.error("Error reading file:", streamErr);
@@ -138,7 +142,6 @@ class Router {
138
142
  }
139
143
 
140
144
  isProtectedFile(route) {
141
-
142
145
  let protectedDirs = ['framework', 'external', 'node_mudules'];
143
146
  let routeParts = route.split('/');
144
147
  if (routeParts[1] != undefined && routeParts.length > 2) {
@@ -148,7 +151,6 @@ class Router {
148
151
  }
149
152
 
150
153
  validateCallback(clazz, method) {
151
-
152
154
  let obj = this,
153
155
  endpoints = obj.server.endpoints;
154
156
 
@@ -166,9 +168,7 @@ class Router {
166
168
  * @param route: String name for the route
167
169
  **/
168
170
  setDefaultRoute(route) {
169
-
170
- let obj = this;
171
- obj.defaultRoute = route;
171
+ this.defaultRoute = route;
172
172
  }
173
173
 
174
174
  getDefaultRoute() {
@@ -114,7 +114,9 @@ class Server {
114
114
  }
115
115
 
116
116
  // -- Certs managed by NGINX, Google Cloud Run, etc.
117
- obj.httpx = http2.createServer({ allowHTTP1: true }, (req, res) => {
117
+ console.log('HTTP server created - Without self managed certs');
118
+ obj.httpx = http.createServer((req, res) => {
119
+ console.log('Request received: ', req.url);
118
120
  obj.router.onRequest.call(obj.router, req, res);
119
121
  });
120
122
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanilla-jet",
3
- "version": "1.1.10",
3
+ "version": "1.2.0",
4
4
  "description": "VannilaJet framework",
5
5
  "main": "index.js",
6
6
  "bin": {