binhend 1.5.6 → 1.5.8

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.
Files changed (41) hide show
  1. package/bin/commands/create-frontend.js +18 -0
  2. package/bin/index.js +32 -30
  3. package/bin/templates/frontend/.env +1 -0
  4. package/bin/templates/frontend/build.js +17 -0
  5. package/bin/templates/frontend/config.js +6 -0
  6. package/bin/templates/frontend/server.js +11 -0
  7. package/bin/templates/frontend/src/App.js +6 -0
  8. package/bin/templates/frontend/src/index.html +15 -0
  9. package/bin/templates/frontend/src/main.js +4 -0
  10. package/dest/.env +1 -0
  11. package/dest/build/module/App.js +11 -0
  12. package/dest/build/module/Hmm.js +7 -0
  13. package/dest/build/module/index.html +15 -0
  14. package/dest/build/module/index.js +9 -0
  15. package/dest/build/module/main.js +9 -0
  16. package/dest/build/web/App.js +1 -0
  17. package/dest/build/web/Hmm.js +0 -0
  18. package/dest/build/web/index.html +15 -0
  19. package/dest/build/web/index.js +4 -0
  20. package/dest/build/web/main.js +4 -0
  21. package/dest/build.dev.js +24 -0
  22. package/dest/build.js +19 -0
  23. package/dest/build.prod.js +13 -0
  24. package/dest/config.js +6 -0
  25. package/dest/index.dev.js +15 -0
  26. package/dest/index.js +29 -0
  27. package/dest/index.prod.js +13 -0
  28. package/dest/server.js +27 -0
  29. package/dest/src/App.js +6 -0
  30. package/dest/src/Hmm.js +0 -0
  31. package/dest/src/index.html +15 -0
  32. package/dest/src/index.js +4 -0
  33. package/dest/src/main.js +4 -0
  34. package/package.json +3 -2
  35. package/src/binh.server.config.js +5 -4
  36. package/src/binh.web.builder.js +1 -0
  37. package/src/component.build.js +26 -5
  38. package/src/server.js +1 -1
  39. package/bin/frontend.js +0 -3
  40. package/test.js +0 -48
  41. /package/bin/{backend.js → commands/create-backend.js} +0 -0
@@ -0,0 +1,18 @@
1
+
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ module.exports = (args) => {
6
+ try {
7
+ var currentDirectory = process.cwd(), // directory path where the command is executed
8
+ destinationPath = path.join(currentDirectory, args[0] || 'frontend'),
9
+ sourcePath = path.join(__dirname, '../templates/frontend');
10
+
11
+ console.log('[BINHEND][CODEGEN] Generate front-end structure:', destinationPath);
12
+ fs.cpSync(sourcePath, destinationPath, {recursive: true});
13
+ }
14
+ catch (error) {
15
+ console.error('[BINHEND][CODEGEN] Error generating front-end structure!');
16
+ throw error;
17
+ }
18
+ };
package/bin/index.js CHANGED
@@ -1,31 +1,33 @@
1
- #!/usr/bin/env node
2
- // console.log('Welcome to the binh!');
3
- // console.log('process.argv', process.argv);
4
-
5
- const argves = process.argv;
6
- const command = argves[2];
7
-
8
- // switch (command) {
9
- // case 'backend':
10
- // console.log('Generate back-end structure...');
11
- // break;
12
-
13
- // case 'frontend':
14
- // console.log('Generate front-end structure...');
15
- // break;
16
-
17
- // default:
18
- // console.log('Unknown command...');
19
- // break;
20
- // }
21
-
22
- const map = {
23
- backend: require('./backend'),
24
- frontend: require('./frontend'),
1
+ #!/usr/bin/env node
2
+
3
+ const commandMap = {
4
+ create: {
5
+ backend: () => require('./commands/create-backend'),
6
+ frontend: () => require('./commands/create-frontend'),
7
+ }
8
+ };
9
+
10
+ var command = commandMap,
11
+ args = process.argv.slice(2); // remove first 2 args in list: <path-to-nodejs> and <path-to-this-CLI-program-file>
12
+
13
+ while (args.length) {
14
+ var arg = args[0];
15
+
16
+ if (command.hasOwnProperty(arg)) {
17
+ command = command[arg];
18
+ args.shift();
19
+ }
20
+ else {
21
+ break;
22
+ }
23
+ };
24
+
25
+ if (command instanceof Function) {
26
+ command()(args);
27
+ }
28
+ else if (command?.default instanceof Function) {
29
+ command.default()(args);
30
+ }
31
+ else {
32
+ console.log('Unknown command...');
25
33
  }
26
-
27
- const handler = map[command];
28
-
29
- if (handler) handler();
30
- else console.log('Unknown command...');
31
-
@@ -0,0 +1 @@
1
+ port=1234
@@ -0,0 +1,17 @@
1
+ const { Binh } = require('binhend');
2
+
3
+ const app = new Binh();
4
+
5
+ app.config('config.js', afterLoadedConfigs); // import module from path './config.js' to run and load configs
6
+
7
+ function afterLoadedConfigs(configs) {
8
+ const options = {
9
+ source: 'src',
10
+ module: 'build/module',
11
+ web: 'build/web'
12
+ };
13
+
14
+ configs.env === 'prod' ? app.webBundle(options).minify() : app.webLazy(options);
15
+ }
16
+
17
+ module.exports = app;
@@ -0,0 +1,6 @@
1
+ const path = require('path');
2
+
3
+ module.exports = function(loader) {
4
+ loader.file(path.join(__dirname, '.env'));
5
+ loader.cli();
6
+ };
@@ -0,0 +1,11 @@
1
+ const app = require('./build');
2
+
3
+ app.cors(); // no input => use default middleware for CORS settings
4
+
5
+ app.port('port'); // get value from config via key 'port'
6
+
7
+ app.start((server, configs) => {
8
+ console.log('[BINHEND][SERVER] Loaded configs:', configs);
9
+ });
10
+
11
+ module.exports = app;
@@ -0,0 +1,6 @@
1
+
2
+ tag('h1');
3
+
4
+ function ui() {
5
+ return h1('Hello World');
6
+ }
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html lang="en">
4
+
5
+ <head>
6
+ <title>Binhjs App</title>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <script src="https://binhjs.pages.dev/dist/binh.min.js"></script>
10
+ <script src="/main.js"></script>
11
+ </head>
12
+
13
+ <body></body>
14
+
15
+ </html>
@@ -0,0 +1,4 @@
1
+
2
+ Binh({
3
+ '': '/App.js'
4
+ });
package/dest/.env ADDED
@@ -0,0 +1 @@
1
+ port=1234
@@ -0,0 +1,11 @@
1
+ var { context, tag, svg, script, require, css } = binh.context(module, require);
2
+ binh.component(context, ui, service, style);
3
+ var ui = null, service = null, style = null;
4
+
5
+ tag('h1');
6
+
7
+ function ui() {
8
+ return h1('Hello World');
9
+ }
10
+
11
+ ;binh.final(module);
@@ -0,0 +1,7 @@
1
+ var { context, tag, svg, script, require, css } = binh.context(module, require);
2
+ binh.component(context, ui, service, style);
3
+ var ui = null, service = null, style = null;
4
+
5
+
6
+
7
+ ;binh.final(module);
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html lang="en">
4
+
5
+ <head>
6
+ <title>Binhjs App</title>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <script src="https://binhjs.pages.dev/dist/binh.min.js"></script>
10
+ <script src="/main.js"></script>
11
+ </head>
12
+
13
+ <body></body>
14
+
15
+ </html>
@@ -0,0 +1,9 @@
1
+ var { context, tag, svg, script, require, css } = binh.context(module, require);
2
+ binh.component(context, ui, service, style);
3
+ var ui = null, service = null, style = null;
4
+
5
+ Binh({
6
+ '': '/App.js'
7
+ });
8
+
9
+ ;binh.final(module);
@@ -0,0 +1,9 @@
1
+ var { context, tag, svg, script, require, css } = binh.context(module, require);
2
+ binh.component(context, ui, service, style);
3
+ var ui = null, service = null, style = null;
4
+
5
+ Binh({
6
+ '': '/App.js'
7
+ });
8
+
9
+ ;binh.final(module);
@@ -0,0 +1 @@
1
+ !function(){var n,e=window.Binh;e.els=e.element("h1"),n=e.els.h1,e.ui(function(){return n("Hello World")})}();
File without changes
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html lang="en">
4
+
5
+ <head>
6
+ <title>Binhjs App</title>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <script src="https://binhjs.pages.dev/dist/binh.min.js"></script>
10
+ <script src="/main.js"></script>
11
+ </head>
12
+
13
+ <body></body>
14
+
15
+ </html>
@@ -0,0 +1,4 @@
1
+
2
+ Binh({
3
+ '': '/App.js'
4
+ });
@@ -0,0 +1,4 @@
1
+
2
+ Binh({
3
+ '': '/App.js'
4
+ });
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const { Binh } = require('binhend');
4
+
5
+ var app = new Binh();
6
+
7
+ app.webLazy({
8
+ source: 'src',
9
+ module: 'build/module',
10
+ web: 'build/web'
11
+ });
12
+
13
+ // app.cors().port(1234).start();
14
+
15
+ module.exports = app;
16
+
17
+
18
+ // module.exports = (app) => {
19
+ // app.webLazy({
20
+ // source: 'src',
21
+ // module: 'build/module',
22
+ // web: 'build/web'
23
+ // });
24
+ // };
package/dest/build.js ADDED
@@ -0,0 +1,19 @@
1
+ // const { Binh } = require('binhend');
2
+
3
+ const { Binh } = require('../src/binh');
4
+
5
+ const app = new Binh();
6
+
7
+ app.config('config.js', afterLoadedConfigs); // import middleware from path './config.js' to run and load configs
8
+
9
+ function afterLoadedConfigs(configs) {
10
+ const options = {
11
+ source: 'src',
12
+ module: 'build/module',
13
+ web: 'build/web'
14
+ };
15
+ console.log("configs.env === 'prod'", configs.env === 'prod');
16
+ configs.env === 'prod' ? app.webBundle(options).minify(true) : app.webLazy(options);
17
+ }
18
+
19
+ module.exports = app;
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const { Binh } = require('binhend');
4
+
5
+ var app = new Binh();
6
+
7
+ app.webBundle({
8
+ source: 'src',
9
+ module: 'build/module',
10
+ web: 'build/web'
11
+ });
12
+
13
+ module.exports = app;
package/dest/config.js ADDED
@@ -0,0 +1,6 @@
1
+ const path = require('path');
2
+
3
+ module.exports = function(loader) {
4
+ loader.file(path.join(__dirname, '.env'));
5
+ loader.cli();
6
+ };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const { Binh } = require('binhend');
4
+
5
+ var app = new Binh();
6
+
7
+ app.webLazy({
8
+ source: 'src',
9
+ module: 'build/module',
10
+ web: 'build/web'
11
+ });
12
+
13
+ app.cors().port(1234).start();
14
+
15
+ module.exports = app;
package/dest/index.js ADDED
@@ -0,0 +1,29 @@
1
+ const { Binh } = require('binhend');
2
+
3
+ var app = new Binh();
4
+
5
+ app.webBundle({
6
+ source: 'src',
7
+ module: 'build/module',
8
+ web: 'build/web'
9
+ });
10
+
11
+ // app.config('config.js') // execute middleware to load configs
12
+ // .cors()
13
+ // .port('port') // get value from config via key 'port'
14
+ // .start((server, configs) => {
15
+ // console.log('configs', configs);
16
+ // });
17
+
18
+ app.cors();
19
+
20
+ app.config('config.js'); // execute middleware to load configs
21
+
22
+ app.port('port'); // get value from config via key 'port'
23
+
24
+ app.start((server, configs) => {
25
+ console.log('configs', configs);
26
+ });
27
+
28
+
29
+ module.exports = app;
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const { Binh } = require('binhend');
4
+
5
+ var app = new Binh();
6
+
7
+ app.webBundle({
8
+ source: 'src',
9
+ module: 'build/module',
10
+ web: 'build/web'
11
+ });
12
+
13
+ module.exports = app;
package/dest/server.js ADDED
@@ -0,0 +1,27 @@
1
+ // const { Binh } = require('binhend');
2
+ // const { Binh } = require('../src/binh');
3
+
4
+ // var app = new Binh();
5
+
6
+ // app.config('config.js', (configs) => { // run middleware to load configs
7
+ // const options = {
8
+ // source: 'src',
9
+ // module: 'build/module',
10
+ // web: 'build/web'
11
+ // };
12
+
13
+ // configs.env === 'prod' ? app.webBundle(options) : app.webLazy(options);
14
+ // });
15
+
16
+ const app = require('./build');
17
+
18
+ app.cors();
19
+
20
+ app.port('port'); // get value from config via key 'port'
21
+
22
+ app.start((server, configs) => {
23
+ console.log('configs', configs);
24
+ });
25
+
26
+
27
+ module.exports = app;
@@ -0,0 +1,6 @@
1
+
2
+ tag('h1');
3
+
4
+ function ui() {
5
+ return h1('Hello World');
6
+ }
File without changes
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html lang="en">
4
+
5
+ <head>
6
+ <title>Binhjs App</title>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <script src="https://binhjs.pages.dev/dist/binh.min.js"></script>
10
+ <script src="/main.js"></script>
11
+ </head>
12
+
13
+ <body></body>
14
+
15
+ </html>
@@ -0,0 +1,4 @@
1
+
2
+ Binh({
3
+ '': '/App.js'
4
+ });
@@ -0,0 +1,4 @@
1
+
2
+ Binh({
3
+ '': '/App.js'
4
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -12,7 +12,8 @@
12
12
  "test": "echo \"Error: no test specified\" && exit 1",
13
13
  "example-api": "node example_api PORT=5555",
14
14
  "example-webcomp": "node example_webcomp",
15
- "example-web2": "node example_web2"
15
+ "example-web2": "node example_web2",
16
+ "server": "node dest/server.js env=dev"
16
17
  },
17
18
  "dependencies": {
18
19
  "express": "^4.17.1",
@@ -41,20 +41,21 @@ function ServerConfig(binh, Binh) {
41
41
  return new ConfigLoader(config);
42
42
  };
43
43
 
44
- Binh.config = function(module) {
44
+ Binh.config = function(module, doneCallback) {
45
45
  if (module instanceof Function) {
46
46
  loadConfigs = module;
47
- loadConfigs(configloader);
48
47
  }
49
48
  else if (typeof module === 'string') {
50
49
  loadConfigs = binh(module);
51
- loadConfigs(configloader);
52
50
  }
53
51
  else {
54
52
  loadConfigs = defaultConfigLoader;
55
- loadConfigs(configloader);
56
53
  }
57
54
 
55
+ loadConfigs(configloader);
56
+
57
+ if (doneCallback instanceof Function) configloader.done(doneCallback);
58
+
58
59
  groupLoadConfigs.push(loadConfigs);
59
60
 
60
61
  return Binh;
@@ -119,6 +119,7 @@ function WebBuilder(binh, Binh) {
119
119
  var content = fs.readFileSync(cssFilePath, { encoding: 'utf8', flag: 'r' });
120
120
  content = UglifyCSS.processString(content);
121
121
  var component = new Function(`return function style() { return ${JSON.stringify(content)}; };`)();
122
+ component.cssFilePath = cssFilePath;
122
123
  binh.component({ module }, null, null, component);
123
124
  binh.final(module);
124
125
  };
@@ -13,6 +13,7 @@ function processEachFile({ source: sourceRootPath, web: outputRootPath, module:
13
13
  console.info('[BINHEND][COMPONENT] to:', outputRootPath);
14
14
 
15
15
  var ref = {};
16
+ externalPath = typeof externalPath === 'string' ? `/${externalPath}/` : null;
16
17
 
17
18
  scanNestedFiles(stageRootPath, (file, done) => {
18
19
  if (done) return callbackDone instanceof Function ? callbackDone({ outputRootPath }) : null;
@@ -37,11 +38,11 @@ function processEachFile({ source: sourceRootPath, web: outputRootPath, module:
37
38
  return cloneFileIfNew(fileSourcePath, fileOutputPath);
38
39
  }
39
40
 
40
- injectComponentURL(component, stageRootPath + path.sep, externalPath ? `/${externalPath}/` : null, ref);
41
+ injectComponentURL(component, stageRootPath + path.sep, externalPath, ref);
41
42
 
42
43
  if (buildCodeMethod === buildLazyCode) collectExternalComponents(component, externalPath);
43
44
 
44
- var code = buildCodeMethod(component, externalPath);
45
+ var code = buildCodeMethod(component);
45
46
  code = Component.minification ? minifyCode(code) : beautifyCode(code);
46
47
 
47
48
  writeToFileIfNew(fileOutputPath, code);
@@ -69,7 +70,8 @@ function lazyload(paths) {
69
70
  }
70
71
 
71
72
  function copyExternalComponentFiles({ outputRootPath }) {
72
- var cache = copyExternalComponentFiles.cache;
73
+ var cache = copyExternalComponentFiles.cache,
74
+ csses = copyExternalComponentFiles.csses;
73
75
 
74
76
  for (var npmPath in cache) {
75
77
  var component = cache[npmPath].component;
@@ -84,17 +86,36 @@ function copyExternalComponentFiles({ outputRootPath }) {
84
86
 
85
87
  writeToFileIfNew(fileOutputPath, code);
86
88
  }
89
+
90
+ for (var cssFileSourcePath in csses) {
91
+ var cssURL = csses[cssFileSourcePath],
92
+ cssFileOutputPath = path.join(outputRootPath, cssURL);
93
+
94
+ makeFullDirPath(cssFileOutputPath);
95
+ cloneFileIfNew(cssFileSourcePath, cssFileOutputPath);
96
+ }
87
97
  }
88
98
 
89
99
  function collectExternalComponents(component, externalPath) {
90
- var cache = copyExternalComponentFiles.cache || (copyExternalComponentFiles.cache = {});
100
+ var cache = copyExternalComponentFiles.cache || (copyExternalComponentFiles.cache = {}),
101
+ csses = copyExternalComponentFiles.csses || (copyExternalComponentFiles.csses = {});
102
+
103
+ component.options.csses.forEach(cssModule => {
104
+ var cssFilePath = cssModule.cssFilePath;
105
+
106
+ if (csses[cssFilePath]) return;
107
+
108
+ if (cssModule.url.startsWith(externalPath)) {
109
+ csses[cssFilePath] = cssModule.url.replace(/.js$/, '');
110
+ }
111
+ });
91
112
 
92
113
  component.eachChild(childComponent => {
93
114
  var url = childComponent.url;
94
115
 
95
116
  if (cache[url]) return;
96
117
 
97
- if (url.startsWith(`/${externalPath}/`)) {
118
+ if (url.startsWith(externalPath)) {
98
119
  cache[url] = {
99
120
  component: childComponent
100
121
  };
package/src/server.js CHANGED
@@ -40,7 +40,7 @@ function Server(options) {
40
40
  }
41
41
 
42
42
  server.listen(PORT, function() {
43
- console.log(`[NODE] Server is listening on http://localhost:${PORT}/`);
43
+ console.log(`[BINHEND][SERVER] Server is listening on http://localhost:${PORT}/`);
44
44
 
45
45
  if (options.callback instanceof Function) {
46
46
  options.callback(server, options.configs);
package/bin/frontend.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = () => {
2
- console.log('Generate front-end structure...');
3
- };
package/test.js DELETED
@@ -1,48 +0,0 @@
1
- // const path = require('path');
2
-
3
- // function parseFilePathToVariableName(filePath) {
4
- // return path.parse(filePath).name.replace(/-/g, '').replace(/\W.*/, '');
5
- // // try {
6
- // // }
7
- // // catch (error) {
8
- // // return '';
9
- // // }
10
- // };
11
-
12
- // // console.log(parseFilePathToVariableName(1));
13
- // console.log(parseFilePathToVariableName(''));
14
- // // console.log(parseFilePathToVariableName(null));
15
- // // console.log(parseFilePathToVariableName());
16
- // // console.log(parseFilePathToVariableName('abc.js'));
17
- // // console.log(parseFilePathToVariableName('abc,.js'));
18
- // // console.log(parseFilePathToVariableName('abc.css.js'));
19
- // // console.log(parseFilePathToVariableName('./uhm/abc.css.js'));
20
- // console.log(parseFilePathToVariableName('.css.js'));
21
-
22
- // var abc = function ab() { return 123; };
23
- // var cc = () => {};
24
-
25
- // !function(callback1, callback2) {
26
- // delete callback1.name;
27
- // console.log(callback1.toString());
28
- // console.log(callback2.toString());
29
- // }(abc, cc);
30
-
31
- // console.log(abc.toString());
32
- // console.log(cc.toString());
33
-
34
- const path = require('path');
35
-
36
- // console.log(__dirname);
37
- // console.log(path.join(__dirname, null));
38
-
39
- function abc({ a, b, c }) {
40
- console.log(path.join(__dirname, a));
41
- console.log(path.join(__dirname, b));
42
- console.log(path.join(__dirname, c));
43
- }
44
-
45
- abc({
46
- a: 'a',
47
- b: 'b'
48
- });
File without changes