vanilla-jet 1.0.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.
- package/.grunt/build_styles_task.js +19 -0
- package/.grunt/compile_html.js +212 -0
- package/Gruntfile.js +147 -0
- package/README.md +6 -0
- package/framework/dipper.js +522 -0
- package/framework/request.js +147 -0
- package/framework/response.js +102 -0
- package/framework/router.js +235 -0
- package/framework/server.js +116 -0
- package/index.js +5 -0
- package/package.json +53 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module.exports = function(grunt) {
|
|
2
|
+
grunt.registerTask('buildLess', 'Compile admin.less and add .section.less', function() {
|
|
3
|
+
let adminContent = grunt.file.read('assets/styles/less/admin.less');
|
|
4
|
+
let sectionFiles = grunt.file.expand([
|
|
5
|
+
'assets/styles/less/sections/**/*.section.less',
|
|
6
|
+
'assets/styles/less/sections/*.section.less'
|
|
7
|
+
]);
|
|
8
|
+
|
|
9
|
+
let combinedContent = adminContent + '\n';
|
|
10
|
+
sectionFiles.forEach(function(filePath) {
|
|
11
|
+
let sectionContent = grunt.file.read(filePath);
|
|
12
|
+
combinedContent += `\n/* ${filePath} */\n` + sectionContent;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// -- New file
|
|
16
|
+
grunt.file.write('assets/styles/less/admin_build.less', combinedContent);
|
|
17
|
+
grunt.task.run(['less']);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
console.log("Init Templete Compile/Rendering");
|
|
2
|
+
|
|
3
|
+
// -- Add dependencies
|
|
4
|
+
const path = require("path"),
|
|
5
|
+
fs = require("fs"),
|
|
6
|
+
nunjucks = require('nunjucks'),
|
|
7
|
+
identifier = 'templates';
|
|
8
|
+
|
|
9
|
+
let Functions = require('../core/external/functions.js');
|
|
10
|
+
let Dipper = require('../core/framework/dipper.js');
|
|
11
|
+
let Config = require('../core/external/config.js');
|
|
12
|
+
|
|
13
|
+
// -- Init Dipper
|
|
14
|
+
const env = process.argv[2] || 'development';
|
|
15
|
+
let settings = Config.settings;
|
|
16
|
+
settings['shared']['environment'] = env;
|
|
17
|
+
|
|
18
|
+
let opts = settings[env] || {},
|
|
19
|
+
shared = settings['shared'] || {};
|
|
20
|
+
const dipper = new Dipper(opts, shared);
|
|
21
|
+
|
|
22
|
+
// -- Hydrate dipper
|
|
23
|
+
Functions.hydrate(dipper);
|
|
24
|
+
|
|
25
|
+
// -- Making nunjucks
|
|
26
|
+
let nunjucksPath = path.join(processCwd(), '/assets');
|
|
27
|
+
nunjucks.configure(nunjucksPath, {
|
|
28
|
+
autoescape: false,
|
|
29
|
+
throwOnUndefined: true,
|
|
30
|
+
noCache: true
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// -- Template directory
|
|
34
|
+
const templatesDirectoryPath = '../assets/templates/';
|
|
35
|
+
|
|
36
|
+
// -- Call main function
|
|
37
|
+
main();
|
|
38
|
+
|
|
39
|
+
// -- Define mainfunctions on other functions
|
|
40
|
+
function main() {
|
|
41
|
+
|
|
42
|
+
// -- Get template files
|
|
43
|
+
const templates = getTemplates(templatesDirectoryPath);
|
|
44
|
+
//console.log(templates);
|
|
45
|
+
|
|
46
|
+
// -- Get home.html
|
|
47
|
+
let homePageName = 'home.html';
|
|
48
|
+
getHtmlFromPage(homePageName).then((htmlContent) => {
|
|
49
|
+
if (htmlContent) {
|
|
50
|
+
// -- Divide content line by line
|
|
51
|
+
const htmlContentLines = htmlContent.split('\n');
|
|
52
|
+
let lines = Array.from(htmlContentLines);
|
|
53
|
+
// -- Iterate over each line
|
|
54
|
+
for (let line of htmlContentLines) {
|
|
55
|
+
let originalLine = line;
|
|
56
|
+
// -- Remove spaces and tabs
|
|
57
|
+
line = cleanALine(line);
|
|
58
|
+
// -- Check is not empty and not a tag
|
|
59
|
+
if (line.length != 0 && !line.includes('<')) {
|
|
60
|
+
|
|
61
|
+
// -- Get template name
|
|
62
|
+
var templateName = line.replace('include::', '');
|
|
63
|
+
// -- Check if its name "templates" add all templates if not add specific one
|
|
64
|
+
if (templateName === identifier) {
|
|
65
|
+
|
|
66
|
+
let allTemplatesCompiled = '';
|
|
67
|
+
for (let templateName in templates) {
|
|
68
|
+
if (templateName.includes('template.html')) {
|
|
69
|
+
let templatePath = templates[templateName];
|
|
70
|
+
let templateCompiled = compileTemplate(templatePath);
|
|
71
|
+
allTemplatesCompiled += templateCompiled;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
lines = replaceInclude(lines, originalLine, allTemplatesCompiled);
|
|
75
|
+
|
|
76
|
+
} else {
|
|
77
|
+
let templatePath = templates[templateName];
|
|
78
|
+
let templateCompiled = compileTemplate(templatePath);
|
|
79
|
+
lines = replaceInclude(lines, originalLine, templateCompiled);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// -- Join lines
|
|
84
|
+
const newHtml = lines.join('\n');
|
|
85
|
+
// -- Create HTML file
|
|
86
|
+
createHTMLFile(newHtml, homePageName);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// -- Step 0
|
|
92
|
+
function getTemplates(directory) {
|
|
93
|
+
|
|
94
|
+
const results = {};
|
|
95
|
+
|
|
96
|
+
// -- Sub functions
|
|
97
|
+
function exploreDirectory(dir) {
|
|
98
|
+
const files = fs.readdirSync(dir);
|
|
99
|
+
files.forEach(function (file) {
|
|
100
|
+
//console.log(file);
|
|
101
|
+
if (!checkExcludes(file)) {
|
|
102
|
+
const filePath = path.join(dir, file);
|
|
103
|
+
//console.log(filePath);
|
|
104
|
+
const stats = fs.statSync(filePath);
|
|
105
|
+
if (stats.isFile()) {
|
|
106
|
+
const extension = path.extname(file).toLowerCase();
|
|
107
|
+
if (extension === '.html') {
|
|
108
|
+
results[file] = filePath;
|
|
109
|
+
}
|
|
110
|
+
} else if (stats.isDirectory()) {
|
|
111
|
+
exploreDirectory(filePath);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function checkExcludes(file) {
|
|
118
|
+
|
|
119
|
+
const excludes = ['.DS_Store'];
|
|
120
|
+
for (const esclude of excludes) {
|
|
121
|
+
if (file.includes(esclude)) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// -- Main code
|
|
129
|
+
const templatesPath = path.join(__dirname, directory);
|
|
130
|
+
exploreDirectory(templatesPath);
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// -- Step 1
|
|
135
|
+
async function getHtmlFromPage(page) {
|
|
136
|
+
|
|
137
|
+
const filename = path.join(processCwd(), '/assets/');
|
|
138
|
+
const exists = await fs.promises.access(filename, fs.constants.F_OK).then(() => true).catch(() => false);
|
|
139
|
+
if (!exists) {
|
|
140
|
+
console.log("Assets folder doesnt exists");
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let fileContent;
|
|
145
|
+
if (await fs.promises.stat(filename).then((stats) => stats.isDirectory())) {
|
|
146
|
+
const filePath = path.join(filename, 'pages/' + page);
|
|
147
|
+
try {
|
|
148
|
+
fileContent = await fs.promises.readFile(filePath, { encoding: 'utf8' });
|
|
149
|
+
} catch (err) {
|
|
150
|
+
console.log("Error in Home");
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return fileContent;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// -- Step 2
|
|
158
|
+
function compileTemplate(path) {
|
|
159
|
+
|
|
160
|
+
// -- Data
|
|
161
|
+
let data = { 'app': dipper }
|
|
162
|
+
|
|
163
|
+
// -- Render
|
|
164
|
+
return nunjucks.render(path, data);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// -- Step 3
|
|
168
|
+
function replaceInclude(lines, originalLine, templateCompiled) {
|
|
169
|
+
const index = lines.indexOf(originalLine);
|
|
170
|
+
if (index !== -1) {
|
|
171
|
+
lines.splice(index, 1, templateCompiled);
|
|
172
|
+
}
|
|
173
|
+
return lines;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// -- Step 4
|
|
177
|
+
async function createHTMLFile(content, filePath) {
|
|
178
|
+
|
|
179
|
+
//const { html } = require('js-beautify');
|
|
180
|
+
///content = html(content);
|
|
181
|
+
|
|
182
|
+
const { minify } = require('html-minifier-terser');
|
|
183
|
+
//console.log(typeof content);
|
|
184
|
+
const minified = await minify(content, {
|
|
185
|
+
collapseWhitespace: true,
|
|
186
|
+
collapseInlineTagWhitespace: true,
|
|
187
|
+
removeComments: true,
|
|
188
|
+
collapseBooleanAttributes: true,
|
|
189
|
+
useShortDoctype: true,
|
|
190
|
+
removeEmptyAttributes: true,
|
|
191
|
+
removeOptionalTags: true,
|
|
192
|
+
minifyJS: true
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const publicPath = path.join(processCwd(), '/public/pages');
|
|
196
|
+
fs.mkdirSync(publicPath, { recursive: true });
|
|
197
|
+
const absolutePath = path.join(publicPath, filePath);
|
|
198
|
+
fs.writeFileSync(absolutePath, minified, 'utf8');
|
|
199
|
+
console.log(`Html file created at: ${absolutePath}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// -- Helpers
|
|
203
|
+
function cleanALine(line) {
|
|
204
|
+
line = line.replaceAll(' ', '');
|
|
205
|
+
line = line.replaceAll('\t', '');
|
|
206
|
+
line = line.replaceAll('\n', '');
|
|
207
|
+
return line;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function processCwd() {
|
|
211
|
+
return process.cwd().replace('/.grunt', '');
|
|
212
|
+
}
|
package/Gruntfile.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
module.exports = function(grunt) {
|
|
2
|
+
|
|
3
|
+
require('jit-grunt')(grunt, {
|
|
4
|
+
concat: 'grunt-contrib-concat',
|
|
5
|
+
//compress: 'grunt-contrib-compress',
|
|
6
|
+
clean: 'grunt-contrib-clean'
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// -- Load modules
|
|
10
|
+
grunt.loadNpmTasks('grunt-contrib-cssmin');
|
|
11
|
+
grunt.loadNpmTasks('grunt-contrib-uglify');
|
|
12
|
+
grunt.loadNpmTasks('grunt-contrib-watch');
|
|
13
|
+
grunt.loadNpmTasks('grunt-shell');
|
|
14
|
+
grunt.loadNpmTasks('grunt-run');
|
|
15
|
+
require('./.grunt/build_styles_task')(grunt);
|
|
16
|
+
|
|
17
|
+
grunt.initConfig({
|
|
18
|
+
clean: {
|
|
19
|
+
build: ['public/scripts/vanilla.min.js'],
|
|
20
|
+
minified: ['public/scripts/api', 'public/scripts/controllers', 'public/scripts/views', 'public/scripts/app.min.js']
|
|
21
|
+
//minified: ['public/scripts/**/*.min.js', 'public/scripts/*', '!public/scripts/vanilla.min.js']
|
|
22
|
+
},
|
|
23
|
+
less: {
|
|
24
|
+
development: {
|
|
25
|
+
options: {
|
|
26
|
+
compress: true,
|
|
27
|
+
yuicompress: true,
|
|
28
|
+
optimization: 2,
|
|
29
|
+
strictImports: true
|
|
30
|
+
},
|
|
31
|
+
files: {
|
|
32
|
+
'public/styles/app.min.css' : 'assets/styles/less/admin_build.less'
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
watch: {
|
|
37
|
+
options: {
|
|
38
|
+
livereload: true
|
|
39
|
+
},
|
|
40
|
+
styles: {
|
|
41
|
+
files: [
|
|
42
|
+
'assets/styles/less/*.less',
|
|
43
|
+
'assets/styles/less/**/*.less',
|
|
44
|
+
'assets/styles/less/**/**/*.less',
|
|
45
|
+
'!assets/styles/less/admin_build.less'
|
|
46
|
+
],
|
|
47
|
+
tasks: ['buildLess'],
|
|
48
|
+
options: {
|
|
49
|
+
nospawn: true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
scripts: {
|
|
53
|
+
files: [
|
|
54
|
+
'assets/pages/*.html',
|
|
55
|
+
'assets/templates/**/*.html',
|
|
56
|
+
'assets/templates/**/**/*.html',
|
|
57
|
+
'external/view/*.js',
|
|
58
|
+
'external/*.js',
|
|
59
|
+
'framework/*.js'
|
|
60
|
+
],
|
|
61
|
+
tasks: ['shell:compileTemplates']
|
|
62
|
+
},
|
|
63
|
+
specificScripts: {
|
|
64
|
+
files: ['assets/scripts/*.js', 'assets/scripts/**/*.js', 'assets/scripts/**/**/*.js'],
|
|
65
|
+
tasks: ['uglify', 'clean:build', 'concat', 'clean:minified']
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
uglify: {
|
|
69
|
+
build: {
|
|
70
|
+
options: {
|
|
71
|
+
sourceMap: false,
|
|
72
|
+
compress: {
|
|
73
|
+
drop_console: false,
|
|
74
|
+
sequences: true,
|
|
75
|
+
dead_code: true,
|
|
76
|
+
conditionals: true,
|
|
77
|
+
booleans: true,
|
|
78
|
+
unused: true,
|
|
79
|
+
if_return: true,
|
|
80
|
+
join_vars: true
|
|
81
|
+
},
|
|
82
|
+
output: {
|
|
83
|
+
ascii_only: true
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
files: [{
|
|
87
|
+
expand: true,
|
|
88
|
+
src: [
|
|
89
|
+
'assets/scripts/*.js',
|
|
90
|
+
'assets/scripts/**/*.js',
|
|
91
|
+
'assets/scripts/**/**/*.js',
|
|
92
|
+
'assets/scripts/**/**/**/*.js'
|
|
93
|
+
],
|
|
94
|
+
dest: 'public/',
|
|
95
|
+
cwd: '',
|
|
96
|
+
rename : function (dest, src) {
|
|
97
|
+
|
|
98
|
+
var folder = src.substring(0, src.lastIndexOf('/')),
|
|
99
|
+
filename = src.substring(src.lastIndexOf('/'), src.length);
|
|
100
|
+
filename = filename.substring(0, filename.lastIndexOf('.'));
|
|
101
|
+
folder = folder.replaceAll('assets', '');
|
|
102
|
+
return dest + folder + filename + '.min.js';
|
|
103
|
+
}
|
|
104
|
+
}]
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
concat: {
|
|
108
|
+
build: {
|
|
109
|
+
src: [
|
|
110
|
+
// Order
|
|
111
|
+
'public/scripts/controllers/**/*.min.js',
|
|
112
|
+
'public/scripts/views/**/*.min.js',
|
|
113
|
+
'public/scripts/api/**/*.min.js',
|
|
114
|
+
'public/scripts/*.min.js',
|
|
115
|
+
|
|
116
|
+
// Ignore files
|
|
117
|
+
'!public/scripts/core/**',
|
|
118
|
+
'!public/scripts/plugins/**',
|
|
119
|
+
'!public/scripts/plugins/ui/**'
|
|
120
|
+
],
|
|
121
|
+
dest: 'public/scripts/vanilla.min.js'
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
compress: {
|
|
125
|
+
main: {
|
|
126
|
+
options: {
|
|
127
|
+
mode: 'gzip'
|
|
128
|
+
},
|
|
129
|
+
files: [{
|
|
130
|
+
expand: true,
|
|
131
|
+
src: ['public/scripts/vanilla.min.js'],
|
|
132
|
+
dest: '',
|
|
133
|
+
ext: '.min.js.gz'
|
|
134
|
+
}]
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
shell: {
|
|
138
|
+
compileTemplates: {
|
|
139
|
+
command: 'node .grunt/compile_html.js <%= grunt.option("env") %>'
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
grunt.registerTask('default', ['buildLess', 'uglify', 'clean:build', 'concat', 'clean:minified', 'shell:compileTemplates', 'watch']);
|
|
145
|
+
grunt.registerTask('build', ['buildLess', 'uglify', 'clean:build', 'concat', 'clean:minified', 'shell:compileTemplates']);
|
|
146
|
+
grunt.registerTask('server', ['buildLess']);
|
|
147
|
+
};
|