binhend 1.4.3 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -14,16 +14,25 @@ function WebBuilder(binh, Binh) {
14
14
  return {
15
15
  context,
16
16
  tag: tag.bind(context),
17
+ svg: svg.bind(context),
17
18
  script: script.bind(context),
18
19
  require: customRequire.bind(context),
19
20
  css: css.bind(context)
20
21
  };
21
22
  };
22
23
 
24
+ function tagnames(tagNames) {
25
+ return typeof tagNames === 'string' ? tagNames.split(/\s+/) : [];
26
+ }
27
+
23
28
  function tag(tagNames) {
24
- var tags = typeof tagNames === 'string' ? tagNames.split(/\s+/) : [];
25
29
  var htmltags = this.component.htmltags;
26
- htmltags.push.apply(htmltags, tags);
30
+ htmltags.push.apply(htmltags, tagnames(tagNames));
31
+ }
32
+
33
+ function svg(tagNames) {
34
+ var svgtags = this.component.svgtags;
35
+ svgtags.push.apply(svgtags, tagnames(tagNames));
27
36
  }
28
37
 
29
38
  function script() {
@@ -60,6 +69,7 @@ function WebBuilder(binh, Binh) {
60
69
  component.type = type;
61
70
 
62
71
  component.htmltags = [];
72
+ component.svgtags = [];
63
73
  component.links = [];
64
74
  component.options = { csses: [] };
65
75
  component.vars = {};
@@ -125,72 +135,71 @@ function WebBuilder(binh, Binh) {
125
135
  return Binh;
126
136
  };
127
137
 
128
- Binh.web = function(webPath, sourcePath, modulePath) {
129
- switch (arguments.length) {
130
- case 1:
131
- Binh.webStatic(webPath);
132
- break;
138
+ Binh.webStatic = function(webPath) {
139
+ Binh.web.value = getAbsolutePath(webPath);
140
+ return Binh;
141
+ };
133
142
 
134
- case 2:
135
- Binh.webApp(sourcePath, webPath);
136
- break;
137
-
138
- default:
139
- if (arguments.length > 2) {
140
- Binh.webApp(webPath, sourcePath, modulePath);
141
- }
142
- break;
143
+ Binh.webModule = function({ source, module }, onDone) {
144
+ if (!(isString(source) && isString(module))) {
145
+ throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source and module. Current: { source: ${source}, module: ${module} }`);
143
146
  }
147
+
148
+ var { source, module } = getAbsolutePaths({ source, module });
149
+
150
+ ComponentFormat.generate(source, module, onDone);
144
151
 
145
152
  return Binh;
146
153
  };
147
154
 
148
- Binh.webStatic = function(webPath) {
149
- if (!isString(webPath)) return Binh;
150
- Binh.web.value = getAbsolutePaths(webPath).web;
155
+ Binh.webModuleBundle = function({ source, module, web }) {
156
+ var { source, module, web } = processWebBuildPaths({ source, module, web });
157
+ ComponentBuild.bundle({ source, module, web });
151
158
  return Binh;
152
159
  };
153
160
 
154
- Binh.webApp = function(sourcePath, webPath, modulePath) {
155
- if (!isString(sourcePath)) return Binh;
156
-
157
- modulePath = isString(modulePath) ? modulePath : isString(webPath) ? webPath : null;
158
-
159
- var { source, web, module } = getAbsolutePaths(webPath, sourcePath, modulePath);
160
-
161
- ComponentFormat.generate(source, module, () => ComponentBuild.generate(source, web, module));
162
-
163
- Binh.web.value = web;
164
-
161
+ Binh.webModuleLazy = function({ source, module, web }) {
162
+ var { source, module, web } = processWebBuildPaths({ source, module, web });
163
+ ComponentBuild.lazyload({ source, module, web });
165
164
  return Binh;
166
165
  };
167
166
 
168
- Binh.webModule = function(sourcePath, modulePath) {
169
- if (!isString(sourcePath)) return Binh;
170
-
171
- var { source, module } = getAbsolutePaths(null, sourcePath, modulePath);
172
-
173
- ComponentFormat.generate(source, module);
167
+ Binh.webBundle = function({ source, module, web }) {
168
+ var absolutePaths = processWebBuildPaths({ source, module, web });
169
+ return Binh.webModule({ source, module }, () => ComponentBuild.bundle(absolutePaths));
170
+ };
174
171
 
175
- return Binh;
172
+ Binh.webLazy = function({ source, module, web }) {
173
+ var absolutePaths = processWebBuildPaths({ source, module, web });
174
+ return Binh.webModule({ source, module }, () => ComponentBuild.lazyload(absolutePaths));
176
175
  };
177
176
 
178
- function getAbsolutePaths(webPath, sourcePath, modulePath) {
179
- webPath = isString(webPath) ? webPath : 'web/app';
180
- modulePath = isString(modulePath) ? modulePath : 'web/modules';
177
+ Binh.web = Binh.webBundle;
181
178
 
182
- var rootpath = Binh.getRootpath(),
183
- sourcePathAbsolute = isString(sourcePath) ? path.join(rootpath, sourcePath) : null,
184
- webPathAbsolute = path.join(rootpath, webPath),
185
- modulePathAbsolute = path.join(rootpath, modulePath);
179
+ function getAbsolutePath(relativePath) {
180
+ return isString(relativePath) ? path.join(Binh.getRootpath(), relativePath) : null;
181
+ }
186
182
 
183
+ function getAbsolutePaths({ source, module, web }) {
187
184
  return {
188
- web: webPathAbsolute,
189
- source: sourcePathAbsolute,
190
- module: modulePathAbsolute
185
+ source: getAbsolutePath(source),
186
+ module: getAbsolutePath(module),
187
+ web: getAbsolutePath(web)
191
188
  };
192
189
  }
193
190
 
191
+ function processWebBuildPaths({ source, module, web }) {
192
+ if (!(isString(source) && isString(module) && isString(web))) {
193
+ throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source, module and web. Current: { source: ${source}, module: ${module}, web: ${web} }`);
194
+ }
195
+
196
+ var absolutePaths = getAbsolutePaths({ source, module, web });
197
+
198
+ Binh.web.value = absolutePaths.web;
199
+
200
+ return absolutePaths;
201
+ }
202
+
194
203
  function isString(input) {
195
204
  return typeof input === 'string';
196
205
  }
package/src/code.js CHANGED
@@ -143,6 +143,20 @@ function getRelativeFilePath(filePath, rootPath) {
143
143
  getRelativeFilePath.cache = {};
144
144
 
145
145
  function bundle(component, rootPath) {
146
+ var codes = getComponentBaseCodes(component, rootPath);
147
+
148
+ var codeOfDependencies = getCodeOfDependencies(component, rootPath);
149
+ if (codeOfDependencies) codes.push(codeOfDependencies);
150
+
151
+ return codes.join('\r\n');
152
+ }
153
+
154
+ function lazyload(component, rootPath) {
155
+ var codes = getComponentBaseCodes(component, rootPath);
156
+ return codes.join('\r\n');
157
+ }
158
+
159
+ function getComponentBaseCodes(component, rootPath) {
146
160
  var dependencyDelaration = getDependencyDelaration(component, rootPath);
147
161
  var optionCode = generateOptionCode(component, '', rootPath);
148
162
  var componentCode = component.toString();
@@ -154,14 +168,12 @@ function bundle(component, rootPath) {
154
168
  var codes = [];
155
169
 
156
170
  if (componentCode) {
157
- codes.push(`Binh.${component.type}(${component.type})${optionCode};`);
171
+ var url = getRelativeFilePath(component.filename, rootPath);
172
+ codes.push(`Binh.${component.type}('${url}', ${component.type})${optionCode};`);
158
173
  codes.push(`${componentCode}\r\n`);
159
174
  }
160
-
161
- var codeOfDependencies = getCodeOfDependencies(component, rootPath);
162
- if (codeOfDependencies) codes.push(codeOfDependencies);
163
175
 
164
- return codes.join('\r\n');
176
+ return codes;
165
177
  }
166
178
 
167
179
  function getHtmlTagDeclaration(component) {
@@ -172,23 +184,54 @@ function getHtmlTagDeclaration(component) {
172
184
  return `var {${variablesList}} = Binh.els;\r\n`;
173
185
  }
174
186
 
175
- function htmltags(component) {
176
- var tags = distinctValues(component, 'htmltags');
187
+ function processElementTags(tags) {
188
+ var
189
+ variables = tags.join(','),
190
+ tagnames = tags.map((tag) => JSON.stringify(tag)).join(',');
191
+
192
+ return [variables, tagnames];
193
+ }
177
194
 
195
+ function htmlTags(tags) {
178
196
  if (!tags.length) return '';
197
+
198
+ var [variables, tagnames] = processElementTags(tags);
179
199
 
180
- var variablesList = tags.join(',');
181
- tags = tags.map((tag) => JSON.stringify(tag));
200
+ return [
201
+ `Binh.els = Binh.element(${tagnames});`, '',
202
+ `var {${variables}} = Binh.els;`, ''
203
+ ]
204
+ .join(`\r\n`);
205
+ }
206
+
207
+ function bundleHtmlTags(component) {
208
+ return htmlTags(distinctValues(component, 'htmltags'));
209
+ }
210
+
211
+ function lazyHtmlTags(component) {
212
+ return htmlTags(component.htmltags);
213
+ }
214
+
215
+ function svgTags(tags) {
216
+ if (!tags.length) return '';
217
+
218
+ var [variables, tagnames] = processElementTags(tags);
182
219
 
183
220
  return [
184
- `Binh.els = Binh.element(${tags.join(',')});`,
185
- '',
186
- `var {${variablesList}} = Binh.els;`,
187
- ''
221
+ `Binh.SVGs = Binh.svgs(${tagnames});`, '',
222
+ `var {${variables}} = Binh.SVGs;`, ''
188
223
  ]
189
224
  .join(`\r\n`);
190
225
  }
191
226
 
227
+ function bundleSvgTags(component) {
228
+ return svgTags(distinctValues(component, 'svgtags'));
229
+ }
230
+
231
+ function lazySvgTags(component) {
232
+ return svgTags(component.svgtags);
233
+ }
234
+
192
235
  function prequire(component, codes) {
193
236
  var code = '', links = distinctValues(component, 'links');
194
237
 
@@ -247,8 +290,9 @@ function uniquefy(arrays) {
247
290
  }
248
291
 
249
292
  module.exports = {
250
- bundle,
251
- htmltags,
293
+ bundle, lazyload,
294
+ bundleHtmlTags, lazyHtmlTags,
295
+ bundleSvgTags, lazySvgTags,
252
296
  prequire,
253
297
  IIF
254
298
  };
@@ -6,7 +6,7 @@ const Component = require('./component');
6
6
  const UglifyJS = require('uglify-js');
7
7
  const BeautifyJS = require('js-beautify/js');
8
8
 
9
- function generate(sourceRootPath, outputRootPath, stageRootPath) {
9
+ function processEachFile({ source: sourceRootPath, web: outputRootPath, module: stageRootPath }, buildCodeMethod) {
10
10
  if (sourceRootPath == undefined || outputRootPath == undefined) return;
11
11
  stageRootPath = stageRootPath || outputRootPath;
12
12
  console.info('[BINHEND][COMPONENT] Build web components from:', stageRootPath);
@@ -33,24 +33,47 @@ function generate(sourceRootPath, outputRootPath, stageRootPath) {
33
33
  return cloneFile(fileSourcePath, fileOutputPath);
34
34
  }
35
35
 
36
- var code = joinCodes(component, stageRootPath);
37
-
36
+ var code = buildCodeMethod(component, stageRootPath);
38
37
  code = Component.minification ? minifyCode(code) : beautifyCode(code);
39
-
40
38
  writeToFile(fileOutputPath, code);
41
39
  });
42
40
  }
43
41
 
44
- function joinCodes(component, rootPath) {
45
- var mainCode = CodeFormat.prequire(component, [
46
- `var _Binh = Binh;\r\n`,
47
- CodeFormat.htmltags(component),
48
- CodeFormat.bundle(component, rootPath)
42
+ function buildBundleCode(component, rootPath, shouldBundle = true) {
43
+ return wrapCode(component, [
44
+ CodeFormat.bundleHtmlTags(component),
45
+ CodeFormat.bundleSvgTags(component),
46
+ CodeFormat.bundle(component, rootPath, shouldBundle)
49
47
  ]);
48
+ }
49
+
50
+ function generate({ source, web, module }) {
51
+ processEachFile({ source, web, module }, buildBundleCode);
52
+ }
53
+
54
+ function bundle({ source, web, module }) {
55
+ generate({ source, web, module });
56
+ }
57
+
58
+ function lazyload({ source, web, module }) {
59
+ processEachFile({ source, web, module }, buildLazyCode);
60
+ }
61
+
62
+ function buildLazyCode(component, rootPath) {
63
+ return wrapCode(component, [
64
+ CodeFormat.lazyHtmlTags(component),
65
+ CodeFormat.lazySvgTags(component),
66
+ CodeFormat.lazyload(component, rootPath)
67
+ ]);
68
+ }
69
+
70
+ function wrapCode(component, inputCodes) {
71
+ var stackOfCodes = [`var _Binh = Binh;\r\n`];
72
+ stackOfCodes.push.apply(stackOfCodes, inputCodes);
50
73
 
51
74
  return CodeFormat.IIF([
52
75
  `var Binh = window.Binh;`,
53
- mainCode
76
+ CodeFormat.prequire(component, stackOfCodes)
54
77
  ]);
55
78
  }
56
79
 
@@ -65,5 +88,5 @@ function beautifyCode(code) {
65
88
  }
66
89
 
67
90
  module.exports = {
68
- generate
91
+ generate, bundle, lazyload
69
92
  };
@@ -1,5 +1,5 @@
1
1
 
2
- const { writeFileSync, readFileSync } = require('fs');
2
+ const { writeFileSync, readFileSync, existsSync } = require('fs');
3
3
  const { parse } = require('path');
4
4
  const { scanNestedFiles, cloneFile, printError, makeFullDirPath } = require('./component.file');
5
5
 
@@ -7,7 +7,7 @@ const { scanNestedFiles, cloneFile, printError, makeFullDirPath } = require('./c
7
7
  // [-] Enhance code by removing sync logics (except for existsSync, mkdirSync): readdir, readFileSync, (statSync?)
8
8
 
9
9
  const PREFIX_CODE =
10
- `var { context, tag, script, require, css } = binh.context(module, require);
10
+ `var { context, tag, svg, script, require, css } = binh.context(module, require);
11
11
  binh.component(context, ui, service, style);
12
12
  var ui = null, service = null, style = null;\r\n\r\n`;
13
13
 
@@ -38,7 +38,10 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
38
38
 
39
39
  var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
40
40
  var code = PREFIX_CODE + content.trim() + '\r\n\r\n;binh.final(module);';
41
- writeFileSync(fileOutputPath, code, { encoding: 'utf8', flag: 'w' });
41
+
42
+ if (!isSameContent(code, fileSourcePath)) {
43
+ writeFileSync(fileOutputPath, code, { encoding: 'utf8', flag: 'w' });
44
+ }
42
45
  }
43
46
  catch (error) {
44
47
  printError('Failed formatting file:', fileSourcePath, error);
@@ -46,6 +49,17 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
46
49
  });
47
50
  }
48
51
 
52
+ function isSameContent(code, filePath) {
53
+ var sameContent = false;
54
+
55
+ if (existsSync(filePath)) {
56
+ var current = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
57
+ sameContent = code === current;
58
+ }
59
+
60
+ return sameContent;
61
+ }
62
+
49
63
  module.exports = {
50
64
  generate
51
65
  };
package/test.js CHANGED
@@ -1,22 +1,48 @@
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
+
1
34
  const path = require('path');
2
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
+ }
3
44
 
4
- function parseFilePathToVariableName(filePath) {
5
- return path.parse(filePath).name.replace(/-/g, '').replace(/\W.*/, '');
6
- // try {
7
- // }
8
- // catch (error) {
9
- // return '';
10
- // }
11
- };
12
-
13
-
14
- // console.log(parseFilePathToVariableName(1));
15
- console.log(parseFilePathToVariableName(''));
16
- // console.log(parseFilePathToVariableName(null));
17
- // console.log(parseFilePathToVariableName());
18
- // console.log(parseFilePathToVariableName('abc.js'));
19
- // console.log(parseFilePathToVariableName('abc,.js'));
20
- // console.log(parseFilePathToVariableName('abc.css.js'));
21
- // console.log(parseFilePathToVariableName('./uhm/abc.css.js'));
22
- console.log(parseFilePathToVariableName('.css.js'));
45
+ abc({
46
+ a: 'a',
47
+ b: 'b'
48
+ });