binhend 1.4.4 → 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.4",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -135,72 +135,71 @@ function WebBuilder(binh, Binh) {
135
135
  return Binh;
136
136
  };
137
137
 
138
- Binh.web = function(webPath, sourcePath, modulePath) {
139
- switch (arguments.length) {
140
- case 1:
141
- Binh.webStatic(webPath);
142
- break;
143
-
144
- case 2:
145
- Binh.webApp(sourcePath, webPath);
146
- break;
147
-
148
- default:
149
- if (arguments.length > 2) {
150
- Binh.webApp(webPath, sourcePath, modulePath);
151
- }
152
- break;
153
- }
154
-
155
- return Binh;
156
- };
157
-
158
138
  Binh.webStatic = function(webPath) {
159
- if (!isString(webPath)) return Binh;
160
- Binh.web.value = getAbsolutePaths(webPath).web;
139
+ Binh.web.value = getAbsolutePath(webPath);
161
140
  return Binh;
162
141
  };
163
142
 
164
- Binh.webApp = function(sourcePath, webPath, modulePath) {
165
- if (!isString(sourcePath)) return Binh;
166
-
167
- modulePath = isString(modulePath) ? modulePath : isString(webPath) ? webPath : null;
168
-
169
- var { source, web, module } = getAbsolutePaths(webPath, sourcePath, modulePath);
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} }`);
146
+ }
147
+
148
+ var { source, module } = getAbsolutePaths({ source, module });
170
149
 
171
- ComponentFormat.generate(source, module, () => ComponentBuild.generate(source, web, module));
150
+ ComponentFormat.generate(source, module, onDone);
172
151
 
173
- Binh.web.value = web;
152
+ return Binh;
153
+ };
174
154
 
155
+ Binh.webModuleBundle = function({ source, module, web }) {
156
+ var { source, module, web } = processWebBuildPaths({ source, module, web });
157
+ ComponentBuild.bundle({ source, module, web });
175
158
  return Binh;
176
159
  };
177
160
 
178
- Binh.webModule = function(sourcePath, modulePath) {
179
- if (!isString(sourcePath)) return Binh;
180
-
181
- var { source, module } = getAbsolutePaths(null, sourcePath, modulePath);
161
+ Binh.webModuleLazy = function({ source, module, web }) {
162
+ var { source, module, web } = processWebBuildPaths({ source, module, web });
163
+ ComponentBuild.lazyload({ source, module, web });
164
+ return Binh;
165
+ };
182
166
 
183
- 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
+ };
184
171
 
185
- 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));
186
175
  };
187
176
 
188
- function getAbsolutePaths(webPath, sourcePath, modulePath) {
189
- webPath = isString(webPath) ? webPath : 'web/app';
190
- modulePath = isString(modulePath) ? modulePath : 'web/modules';
177
+ Binh.web = Binh.webBundle;
191
178
 
192
- var rootpath = Binh.getRootpath(),
193
- sourcePathAbsolute = isString(sourcePath) ? path.join(rootpath, sourcePath) : null,
194
- webPathAbsolute = path.join(rootpath, webPath),
195
- modulePathAbsolute = path.join(rootpath, modulePath);
179
+ function getAbsolutePath(relativePath) {
180
+ return isString(relativePath) ? path.join(Binh.getRootpath(), relativePath) : null;
181
+ }
196
182
 
183
+ function getAbsolutePaths({ source, module, web }) {
197
184
  return {
198
- web: webPathAbsolute,
199
- source: sourcePathAbsolute,
200
- module: modulePathAbsolute
185
+ source: getAbsolutePath(source),
186
+ module: getAbsolutePath(module),
187
+ web: getAbsolutePath(web)
201
188
  };
202
189
  }
203
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
+
204
203
  function isString(input) {
205
204
  return typeof input === 'string';
206
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,40 +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(',');
177
191
 
192
+ return [variables, tagnames];
193
+ }
194
+
195
+ function htmlTags(tags) {
178
196
  if (!tags.length) return '';
179
-
180
- var variablesList = tags.join(',');
181
- tags = tags.map((tag) => JSON.stringify(tag));
182
197
 
198
+ var [variables, tagnames] = processElementTags(tags);
199
+
183
200
  return [
184
- `Binh.els = Binh.element(${tags.join(',')});`,
185
- '',
186
- `var {${variablesList}} = Binh.els;`,
187
- ''
201
+ `Binh.els = Binh.element(${tagnames});`, '',
202
+ `var {${variables}} = Binh.els;`, ''
188
203
  ]
189
204
  .join(`\r\n`);
190
205
  }
191
206
 
192
- function svgtags(component) {
193
- var tags = distinctValues(component, 'svgtags');
207
+ function bundleHtmlTags(component) {
208
+ return htmlTags(distinctValues(component, 'htmltags'));
209
+ }
210
+
211
+ function lazyHtmlTags(component) {
212
+ return htmlTags(component.htmltags);
213
+ }
194
214
 
215
+ function svgTags(tags) {
195
216
  if (!tags.length) return '';
196
-
197
- var variablesList = tags.join(',');
198
- tags = tags.map((tag) => JSON.stringify(tag));
217
+
218
+ var [variables, tagnames] = processElementTags(tags);
199
219
 
200
220
  return [
201
- `Binh.SVGs = Binh.svgs(${tags.join(',')});`,
202
- '',
203
- `var {${variablesList}} = Binh.SVGs;`,
204
- ''
221
+ `Binh.SVGs = Binh.svgs(${tagnames});`, '',
222
+ `var {${variables}} = Binh.SVGs;`, ''
205
223
  ]
206
224
  .join(`\r\n`);
207
225
  }
208
226
 
227
+ function bundleSvgTags(component) {
228
+ return svgTags(distinctValues(component, 'svgtags'));
229
+ }
230
+
231
+ function lazySvgTags(component) {
232
+ return svgTags(component.svgtags);
233
+ }
234
+
209
235
  function prequire(component, codes) {
210
236
  var code = '', links = distinctValues(component, 'links');
211
237
 
@@ -264,9 +290,9 @@ function uniquefy(arrays) {
264
290
  }
265
291
 
266
292
  module.exports = {
267
- bundle,
268
- htmltags,
269
- svgtags,
293
+ bundle, lazyload,
294
+ bundleHtmlTags, lazyHtmlTags,
295
+ bundleSvgTags, lazySvgTags,
270
296
  prequire,
271
297
  IIF
272
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,25 +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.svgtags(component),
49
- 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)
50
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);
51
73
 
52
74
  return CodeFormat.IIF([
53
75
  `var Binh = window.Binh;`,
54
- mainCode
76
+ CodeFormat.prequire(component, stackOfCodes)
55
77
  ]);
56
78
  }
57
79
 
@@ -66,5 +88,5 @@ function beautifyCode(code) {
66
88
  }
67
89
 
68
90
  module.exports = {
69
- generate
91
+ generate, bundle, lazyload
70
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
 
@@ -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
+ });