binhend 1.5.4 → 1.5.6

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.5.4",
3
+ "version": "1.5.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -75,23 +75,27 @@ function WebBuilder(binh, Binh) {
75
75
  component.vars = {};
76
76
  component.varname = getVariableName(component.filename);
77
77
  component.as = alias;
78
+ component.eachChild = loopComponentChildren;
78
79
  };
79
80
 
80
81
  binh.final = function(module) {
81
82
  var component = module.exports;
82
83
 
83
- if (!(component instanceof Function)) return;
84
- if (component && component.constructor !== Component) return;
85
-
86
- for (var index in module.children) {
87
- var dependency = module.children[index].exports;
88
- if (dependency.constructor !== Component) continue;
89
- if (dependency.alias) {
90
- component.vars[dependency.filename] = dependency.alias;
84
+ component.eachChild(childComponent => {
85
+ if (childComponent.alias) {
86
+ component.vars[childComponent.filename] = childComponent.alias;
91
87
  }
92
- }
88
+ });
93
89
  };
94
90
 
91
+ function loopComponentChildren(callback) {
92
+ this.module.children.forEach(childModule => {
93
+ var childComponent = childModule.exports;
94
+ if (!(childComponent instanceof Function) || childComponent.constructor !== Component) return;
95
+ callback(childComponent);
96
+ });
97
+ }
98
+
95
99
  binh.ui = function(module, component, htmltags, links) {
96
100
  binh.component({ module }, component);
97
101
  component.htmltags = htmltags;
@@ -169,8 +173,9 @@ function WebBuilder(binh, Binh) {
169
173
  return Binh.webModule({ source, module }, () => ComponentBuild.bundle(absolutePaths));
170
174
  };
171
175
 
172
- Binh.webLazy = function({ source, module, web }) {
176
+ Binh.webLazy = function({ source, module, web, external }) {
173
177
  var absolutePaths = processWebBuildPaths({ source, module, web });
178
+ absolutePaths.external = external || 'NPM';
174
179
  return Binh.webModule({ source, module }, () => ComponentBuild.lazyload(absolutePaths));
175
180
  };
176
181
 
package/src/code.js CHANGED
@@ -1,7 +1,9 @@
1
1
 
2
- function getCodeOfDependencies(component, rootPath, metadata) {
2
+ const path = require('path');
3
+
4
+ function getCodeOfDependencies(component, metadata) {
3
5
  var global = metadata || {};
4
- var local = getMetadataOfDependencies(component, rootPath);
6
+ var local = getMetadataOfDependencies(component);
5
7
  var codes = [], filenames = Object.keys(local);
6
8
 
7
9
  filenames.forEach(function(filename) {
@@ -11,9 +13,9 @@ function getCodeOfDependencies(component, rootPath, metadata) {
11
13
 
12
14
  var { type, url, dependency } = local[filename];
13
15
 
14
- var dependencyDelaration = getDependencyDelaration(dependency, rootPath);
16
+ var dependencyDelaration = getDependencyDelaration(dependency);
15
17
  var componentCode = dependency.toString();
16
- var optionCode = generateOptionCode(dependency, '', rootPath);
18
+ var optionCode = generateOptionCode(dependency, '');
17
19
  var htmlTagDeclaration = getHtmlTagDeclaration(dependency);
18
20
 
19
21
  if (dependencyDelaration) {
@@ -35,7 +37,7 @@ function getCodeOfDependencies(component, rootPath, metadata) {
35
37
 
36
38
  filenames.forEach(function(filename) {
37
39
  var { dependency } = local[filename];
38
- var codeOfDependencies = getCodeOfDependencies(dependency, rootPath, global);
40
+ var codeOfDependencies = getCodeOfDependencies(dependency, global);
39
41
  if (codeOfDependencies) codes.push(codeOfDependencies);
40
42
  });
41
43
 
@@ -46,8 +48,8 @@ function injectFunctionCodeContent(functionCodeString, injectionCode) {
46
48
  return functionCodeString.replace(/(\(.*?\)\s*{|\(.*?\)\s*=>\s*{)/, `$1\r\n${injectionCode}`);
47
49
  }
48
50
 
49
- function getDependencyDelaration(component, rootPath) {
50
- var dependencies = getMetadataOfDependencies(component, rootPath);
51
+ function getDependencyDelaration(component) {
52
+ var dependencies = getMetadataOfDependencies(component);
51
53
  var codes = [];
52
54
 
53
55
  for (var filePath in dependencies) {
@@ -71,21 +73,15 @@ function getDependencyDelaration(component, rootPath) {
71
73
  return codes.join('\r\n');
72
74
  }
73
75
 
74
- function getMetadataOfDependencies(component, rootPath) {
76
+ function getMetadataOfDependencies(component) {
75
77
  if (component.dependencies) return component.dependencies;
76
78
 
77
- var dependencies = component.module.children;
78
-
79
79
  component.dependencies = {};
80
80
 
81
- dependencies.forEach(function(module) {
82
- var dependency = module.exports;
83
-
84
- if (dependency.constructor !== component.constructor) return;
85
-
86
- var filename = dependency.filename;
87
- var name = component.vars[filename] || dependency.varname;
88
- var url = getRelativeFilePath(filename, rootPath);
81
+ component.eachChild(dependency => {
82
+ var filename = dependency.filename,
83
+ name = component.vars[filename] || dependency.varname,
84
+ url = dependency.url;
89
85
 
90
86
  component.dependencies[filename] = {
91
87
  name, url, dependency,
@@ -96,14 +92,13 @@ function getMetadataOfDependencies(component, rootPath) {
96
92
  return component.dependencies;
97
93
  }
98
94
 
99
- function generateOptionCode(component, varname, rootPath) {
95
+ function generateOptionCode(component, varname) {
100
96
  var options = component.options, urls = [];
101
97
 
102
98
  if (!options) return '';
103
99
 
104
100
  component.type === 'ui' && options.csses.forEach(function(cssModule) {
105
- var url = getRelativeFilePath(cssModule.filename, rootPath);
106
- urls.push(JSON.stringify(url));
101
+ urls.push(JSON.stringify(cssModule.url));
107
102
  });
108
103
 
109
104
  if (urls.length) {
@@ -114,14 +109,18 @@ function generateOptionCode(component, varname, rootPath) {
114
109
  }
115
110
 
116
111
  function getRelativeFilePath(filePath, rootPath) {
117
- if (getRelativeFilePath.cache.hasOwnProperty(filePath)) {
118
- return getRelativeFilePath.cache[filePath];
112
+ rootPath += path.sep;
113
+
114
+ var cache = getRelativeFilePath.cache[rootPath] || (getRelativeFilePath.cache[rootPath] = {});
115
+
116
+ if (cache.hasOwnProperty(filePath)) {
117
+ return cache[filePath];
119
118
  }
120
119
 
121
120
  var url = '';
122
121
 
123
122
  if (filePath.indexOf(rootPath) === 0) {
124
- url = filePath.replace(rootPath, '').replace(/\\/g, '/');
123
+ url = filePath.replace(rootPath, '/').replaceAll(path.sep, '/');
125
124
  }
126
125
  else {
127
126
  var nodeModulePaths = require.main.paths;
@@ -131,34 +130,36 @@ function getRelativeFilePath(filePath, rootPath) {
131
130
  var nodeModulePath = nodeModulePaths[i];
132
131
 
133
132
  if (filePath.indexOf(nodeModulePath) === 0) {
134
- url = filePath.replace(nodeModulePath, 'npm:/').replace(/\\/g, '/');
133
+ url = filePath.replace(nodeModulePath, 'npm:/').replaceAll(path.sep, '/');
135
134
  break;
136
135
  }
137
136
  }
138
137
  }
139
138
 
139
+ cache[filePath] = url;
140
+
140
141
  return url;
141
142
  }
142
143
 
143
144
  getRelativeFilePath.cache = {};
144
145
 
145
- function bundle(component, rootPath) {
146
- var codes = getComponentBaseCodes(component, rootPath);
146
+ function bundle(component) {
147
+ var codes = getComponentBaseCodes(component);
147
148
 
148
- var codeOfDependencies = getCodeOfDependencies(component, rootPath);
149
+ var codeOfDependencies = getCodeOfDependencies(component);
149
150
  if (codeOfDependencies) codes.push(codeOfDependencies);
150
151
 
151
152
  return codes.join('\r\n');
152
153
  }
153
154
 
154
- function lazyload(component, rootPath) {
155
- var codes = getComponentBaseCodes(component, rootPath);
155
+ function lazyload(component) {
156
+ var codes = getComponentBaseCodes(component);
156
157
  return codes.join('\r\n');
157
158
  }
158
159
 
159
- function getComponentBaseCodes(component, rootPath) {
160
- var dependencyDelaration = getDependencyDelaration(component, rootPath);
161
- var optionCode = generateOptionCode(component, '', rootPath);
160
+ function getComponentBaseCodes(component) {
161
+ var dependencyDelaration = getDependencyDelaration(component);
162
+ var optionCode = generateOptionCode(component, '');
162
163
  var componentCode = component.toString();
163
164
 
164
165
  if (dependencyDelaration) {
@@ -292,6 +293,6 @@ module.exports = {
292
293
  bundle, lazyload,
293
294
  bundleHtmlTags, lazyHtmlTags,
294
295
  bundleSvgTags, lazySvgTags,
295
- prequire,
296
- IIF
296
+ prequire, IIF,
297
+ getRelativeFilePath
297
298
  };
@@ -1,24 +1,28 @@
1
1
 
2
- const { parse } = require('path');
2
+ const path = require('path');
3
3
  const { scanNestedFiles, cloneFileIfNew, writeToFileIfNew, makeFullDirPath } = require('./component.file');
4
4
  const CodeFormat = require('./code');
5
5
  const Component = require('./component');
6
6
  const UglifyJS = require('uglify-js');
7
7
  const BeautifyJS = require('js-beautify/js');
8
8
 
9
- function processEachFile({ source: sourceRootPath, web: outputRootPath, module: stageRootPath }, buildCodeMethod) {
9
+ function processEachFile({ source: sourceRootPath, web: outputRootPath, module: stageRootPath, external: externalPath }, buildCodeMethod, callbackDone) {
10
10
  if (sourceRootPath == undefined || outputRootPath == undefined) return;
11
11
  stageRootPath = stageRootPath || outputRootPath;
12
12
  console.info('[BINHEND][COMPONENT] Build web components from:', stageRootPath);
13
13
  console.info('[BINHEND][COMPONENT] to:', outputRootPath);
14
14
 
15
- scanNestedFiles(stageRootPath, (file) => {
15
+ var ref = {};
16
+
17
+ scanNestedFiles(stageRootPath, (file, done) => {
18
+ if (done) return callbackDone instanceof Function ? callbackDone({ outputRootPath }) : null;
19
+
16
20
  var fileSourcePath = file.path.replace(stageRootPath, sourceRootPath);
17
21
  var fileOutputPath = file.path.replace(stageRootPath, outputRootPath);
18
22
 
19
23
  makeFullDirPath(fileOutputPath);
20
24
 
21
- if (parse(file.name).ext !== '.js') {
25
+ if (path.parse(file.name).ext !== '.js') {
22
26
  return cloneFileIfNew(fileSourcePath, fileOutputPath);
23
27
  }
24
28
 
@@ -33,18 +37,22 @@ function processEachFile({ source: sourceRootPath, web: outputRootPath, module:
33
37
  return cloneFileIfNew(fileSourcePath, fileOutputPath);
34
38
  }
35
39
 
36
- var code = buildCodeMethod(component, stageRootPath);
40
+ injectComponentURL(component, stageRootPath + path.sep, externalPath ? `/${externalPath}/` : null, ref);
41
+
42
+ if (buildCodeMethod === buildLazyCode) collectExternalComponents(component, externalPath);
43
+
44
+ var code = buildCodeMethod(component, externalPath);
37
45
  code = Component.minification ? minifyCode(code) : beautifyCode(code);
38
46
 
39
47
  writeToFileIfNew(fileOutputPath, code);
40
48
  });
41
49
  }
42
50
 
43
- function buildBundleCode(component, rootPath, shouldBundle = true) {
51
+ function buildBundleCode(component) {
44
52
  return wrapCode(component, [
45
53
  CodeFormat.bundleHtmlTags(component),
46
54
  CodeFormat.bundleSvgTags(component),
47
- CodeFormat.bundle(component, rootPath, shouldBundle)
55
+ CodeFormat.bundle(component)
48
56
  ]);
49
57
  }
50
58
 
@@ -53,18 +61,87 @@ function generate({ source, web, module }) {
53
61
  }
54
62
 
55
63
  function bundle({ source, web, module }) {
56
- generate({ source, web, module });
64
+ processEachFile({ source, web, module }, buildBundleCode);
65
+ }
66
+
67
+ function lazyload(paths) {
68
+ processEachFile(paths, buildLazyCode, copyExternalComponentFiles);
69
+ }
70
+
71
+ function copyExternalComponentFiles({ outputRootPath }) {
72
+ var cache = copyExternalComponentFiles.cache;
73
+
74
+ for (var npmPath in cache) {
75
+ var component = cache[npmPath].component;
76
+
77
+ var fileOutputPath = path.join(outputRootPath, npmPath);
78
+
79
+ makeFullDirPath(fileOutputPath);
80
+
81
+ var code = buildLazyCode(component);
82
+
83
+ code = Component.minification ? minifyCode(code) : beautifyCode(code);
84
+
85
+ writeToFileIfNew(fileOutputPath, code);
86
+ }
57
87
  }
58
88
 
59
- function lazyload({ source, web, module }) {
60
- processEachFile({ source, web, module }, buildLazyCode);
89
+ function collectExternalComponents(component, externalPath) {
90
+ var cache = copyExternalComponentFiles.cache || (copyExternalComponentFiles.cache = {});
91
+
92
+ component.eachChild(childComponent => {
93
+ var url = childComponent.url;
94
+
95
+ if (cache[url]) return;
96
+
97
+ if (url.startsWith(`/${externalPath}/`)) {
98
+ cache[url] = {
99
+ component: childComponent
100
+ };
101
+ }
102
+
103
+ collectExternalComponents(childComponent, externalPath);
104
+ });
105
+ }
106
+
107
+ function injectComponentURL(component, rootPathWithSeparator, externalPath, ref) {
108
+ var filePath = component.filename;
109
+
110
+ if (ref[filePath]) return;
111
+
112
+ var pathSeparator = path.sep,
113
+ relativePath = '';
114
+
115
+ if (filePath.indexOf(rootPathWithSeparator) === 0) {
116
+ relativePath = filePath.replace(rootPathWithSeparator, '/');
117
+ }
118
+ else {
119
+ var nodeModulePaths = require.main.paths,
120
+ length = nodeModulePaths.length;
121
+
122
+ for (var i = 0; i < length; i++) {
123
+ var nodeModulePath = nodeModulePaths[i] + pathSeparator;
124
+ if (filePath.indexOf(nodeModulePath) === 0) {
125
+ relativePath = filePath.replace(nodeModulePath, externalPath || 'npm://');
126
+ break;
127
+ }
128
+ }
129
+ }
130
+
131
+ component.url = relativePath.replaceAll(pathSeparator, '/');
132
+
133
+ ref[filePath] = true;
134
+
135
+ component.eachChild(childComponent => {
136
+ injectComponentURL(childComponent, rootPathWithSeparator, externalPath, ref);
137
+ });
61
138
  }
62
139
 
63
- function buildLazyCode(component, rootPath) {
140
+ function buildLazyCode(component) {
64
141
  return wrapCode(component, [
65
142
  CodeFormat.lazyHtmlTags(component),
66
143
  CodeFormat.lazySvgTags(component),
67
- CodeFormat.lazyload(component, rootPath)
144
+ CodeFormat.lazyload(component)
68
145
  ]);
69
146
  }
70
147
 
@@ -89,7 +89,7 @@ function cloneFile(filepath, outpath) {
89
89
  }
90
90
 
91
91
  function cloneFileIfNew(filepath, outpath) {
92
- if (!existsSync(filepath)) return rm(outpath, { recursive: true, force: true });
92
+ if (!existsSync(filepath)) return rm(outpath, { recursive: true, force: true }, () => {});
93
93
  if (isSameFile(filepath, outpath)) return;
94
94
  cloneFile(filepath, outpath);
95
95
  }