binhend 1.5.5 → 1.5.7
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 +1 -1
- package/src/binh.web.builder.js +16 -10
- package/src/code.js +36 -35
- package/src/component.build.js +110 -12
- package/test.js +0 -48
package/package.json
CHANGED
package/src/binh.web.builder.js
CHANGED
|
@@ -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
|
-
|
|
84
|
-
|
|
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;
|
|
@@ -115,6 +119,7 @@ function WebBuilder(binh, Binh) {
|
|
|
115
119
|
var content = fs.readFileSync(cssFilePath, { encoding: 'utf8', flag: 'r' });
|
|
116
120
|
content = UglifyCSS.processString(content);
|
|
117
121
|
var component = new Function(`return function style() { return ${JSON.stringify(content)}; };`)();
|
|
122
|
+
component.cssFilePath = cssFilePath;
|
|
118
123
|
binh.component({ module }, null, null, component);
|
|
119
124
|
binh.final(module);
|
|
120
125
|
};
|
|
@@ -169,8 +174,9 @@ function WebBuilder(binh, Binh) {
|
|
|
169
174
|
return Binh.webModule({ source, module }, () => ComponentBuild.bundle(absolutePaths));
|
|
170
175
|
};
|
|
171
176
|
|
|
172
|
-
Binh.webLazy = function({ source, module, web }) {
|
|
177
|
+
Binh.webLazy = function({ source, module, web, external }) {
|
|
173
178
|
var absolutePaths = processWebBuildPaths({ source, module, web });
|
|
179
|
+
absolutePaths.external = external || 'NPM';
|
|
174
180
|
return Binh.webModule({ source, module }, () => ComponentBuild.lazyload(absolutePaths));
|
|
175
181
|
};
|
|
176
182
|
|
package/src/code.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
function getCodeOfDependencies(component, metadata) {
|
|
3
5
|
var global = metadata || {};
|
|
4
|
-
var local = getMetadataOfDependencies(component
|
|
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
|
|
16
|
+
var dependencyDelaration = getDependencyDelaration(dependency);
|
|
15
17
|
var componentCode = dependency.toString();
|
|
16
|
-
var optionCode = generateOptionCode(dependency, ''
|
|
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,
|
|
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
|
|
50
|
-
var dependencies = getMetadataOfDependencies(component
|
|
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
|
|
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
|
-
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
118
|
-
|
|
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, '').
|
|
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:/').
|
|
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
|
|
146
|
-
var codes = getComponentBaseCodes(component
|
|
146
|
+
function bundle(component) {
|
|
147
|
+
var codes = getComponentBaseCodes(component);
|
|
147
148
|
|
|
148
|
-
var codeOfDependencies = getCodeOfDependencies(component
|
|
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
|
|
155
|
-
var codes = getComponentBaseCodes(component
|
|
155
|
+
function lazyload(component) {
|
|
156
|
+
var codes = getComponentBaseCodes(component);
|
|
156
157
|
return codes.join('\r\n');
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
function getComponentBaseCodes(component
|
|
160
|
-
var dependencyDelaration = getDependencyDelaration(component
|
|
161
|
-
var optionCode = generateOptionCode(component, ''
|
|
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
|
-
|
|
296
|
+
prequire, IIF,
|
|
297
|
+
getRelativeFilePath
|
|
297
298
|
};
|
package/src/component.build.js
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
|
|
2
|
-
const
|
|
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
|
-
|
|
15
|
+
var ref = {};
|
|
16
|
+
externalPath = typeof externalPath === 'string' ? `/${externalPath}/` : null;
|
|
17
|
+
|
|
18
|
+
scanNestedFiles(stageRootPath, (file, done) => {
|
|
19
|
+
if (done) return callbackDone instanceof Function ? callbackDone({ outputRootPath }) : null;
|
|
20
|
+
|
|
16
21
|
var fileSourcePath = file.path.replace(stageRootPath, sourceRootPath);
|
|
17
22
|
var fileOutputPath = file.path.replace(stageRootPath, outputRootPath);
|
|
18
23
|
|
|
19
24
|
makeFullDirPath(fileOutputPath);
|
|
20
25
|
|
|
21
|
-
if (parse(file.name).ext !== '.js') {
|
|
26
|
+
if (path.parse(file.name).ext !== '.js') {
|
|
22
27
|
return cloneFileIfNew(fileSourcePath, fileOutputPath);
|
|
23
28
|
}
|
|
24
29
|
|
|
@@ -33,18 +38,22 @@ function processEachFile({ source: sourceRootPath, web: outputRootPath, module:
|
|
|
33
38
|
return cloneFileIfNew(fileSourcePath, fileOutputPath);
|
|
34
39
|
}
|
|
35
40
|
|
|
36
|
-
|
|
41
|
+
injectComponentURL(component, stageRootPath + path.sep, externalPath, ref);
|
|
42
|
+
|
|
43
|
+
if (buildCodeMethod === buildLazyCode) collectExternalComponents(component, externalPath);
|
|
44
|
+
|
|
45
|
+
var code = buildCodeMethod(component);
|
|
37
46
|
code = Component.minification ? minifyCode(code) : beautifyCode(code);
|
|
38
47
|
|
|
39
48
|
writeToFileIfNew(fileOutputPath, code);
|
|
40
49
|
});
|
|
41
50
|
}
|
|
42
51
|
|
|
43
|
-
function buildBundleCode(component
|
|
52
|
+
function buildBundleCode(component) {
|
|
44
53
|
return wrapCode(component, [
|
|
45
54
|
CodeFormat.bundleHtmlTags(component),
|
|
46
55
|
CodeFormat.bundleSvgTags(component),
|
|
47
|
-
CodeFormat.bundle(component
|
|
56
|
+
CodeFormat.bundle(component)
|
|
48
57
|
]);
|
|
49
58
|
}
|
|
50
59
|
|
|
@@ -53,18 +62,107 @@ function generate({ source, web, module }) {
|
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
function bundle({ source, web, module }) {
|
|
56
|
-
|
|
65
|
+
processEachFile({ source, web, module }, buildBundleCode);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function lazyload(paths) {
|
|
69
|
+
processEachFile(paths, buildLazyCode, copyExternalComponentFiles);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function copyExternalComponentFiles({ outputRootPath }) {
|
|
73
|
+
var cache = copyExternalComponentFiles.cache,
|
|
74
|
+
csses = copyExternalComponentFiles.csses;
|
|
75
|
+
|
|
76
|
+
for (var npmPath in cache) {
|
|
77
|
+
var component = cache[npmPath].component;
|
|
78
|
+
|
|
79
|
+
var fileOutputPath = path.join(outputRootPath, npmPath);
|
|
80
|
+
|
|
81
|
+
makeFullDirPath(fileOutputPath);
|
|
82
|
+
|
|
83
|
+
var code = buildLazyCode(component);
|
|
84
|
+
|
|
85
|
+
code = Component.minification ? minifyCode(code) : beautifyCode(code);
|
|
86
|
+
|
|
87
|
+
writeToFileIfNew(fileOutputPath, code);
|
|
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
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function collectExternalComponents(component, externalPath) {
|
|
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
|
+
});
|
|
112
|
+
|
|
113
|
+
component.eachChild(childComponent => {
|
|
114
|
+
var url = childComponent.url;
|
|
115
|
+
|
|
116
|
+
if (cache[url]) return;
|
|
117
|
+
|
|
118
|
+
if (url.startsWith(externalPath)) {
|
|
119
|
+
cache[url] = {
|
|
120
|
+
component: childComponent
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
collectExternalComponents(childComponent, externalPath);
|
|
125
|
+
});
|
|
57
126
|
}
|
|
58
127
|
|
|
59
|
-
function
|
|
60
|
-
|
|
128
|
+
function injectComponentURL(component, rootPathWithSeparator, externalPath, ref) {
|
|
129
|
+
var filePath = component.filename;
|
|
130
|
+
|
|
131
|
+
if (ref[filePath]) return;
|
|
132
|
+
|
|
133
|
+
var pathSeparator = path.sep,
|
|
134
|
+
relativePath = '';
|
|
135
|
+
|
|
136
|
+
if (filePath.indexOf(rootPathWithSeparator) === 0) {
|
|
137
|
+
relativePath = filePath.replace(rootPathWithSeparator, '/');
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
var nodeModulePaths = require.main.paths,
|
|
141
|
+
length = nodeModulePaths.length;
|
|
142
|
+
|
|
143
|
+
for (var i = 0; i < length; i++) {
|
|
144
|
+
var nodeModulePath = nodeModulePaths[i] + pathSeparator;
|
|
145
|
+
if (filePath.indexOf(nodeModulePath) === 0) {
|
|
146
|
+
relativePath = filePath.replace(nodeModulePath, externalPath || 'npm://');
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
component.url = relativePath.replaceAll(pathSeparator, '/');
|
|
153
|
+
|
|
154
|
+
ref[filePath] = true;
|
|
155
|
+
|
|
156
|
+
component.eachChild(childComponent => {
|
|
157
|
+
injectComponentURL(childComponent, rootPathWithSeparator, externalPath, ref);
|
|
158
|
+
});
|
|
61
159
|
}
|
|
62
160
|
|
|
63
|
-
function buildLazyCode(component
|
|
161
|
+
function buildLazyCode(component) {
|
|
64
162
|
return wrapCode(component, [
|
|
65
163
|
CodeFormat.lazyHtmlTags(component),
|
|
66
164
|
CodeFormat.lazySvgTags(component),
|
|
67
|
-
CodeFormat.lazyload(component
|
|
165
|
+
CodeFormat.lazyload(component)
|
|
68
166
|
]);
|
|
69
167
|
}
|
|
70
168
|
|
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
|
-
});
|