binhend 2.0.5 → 2.0.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": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
package/src/web/index.js CHANGED
@@ -3,6 +3,7 @@ const path = require('path');
3
3
  const { isEmptyString, isString } = require('../utils/typeOf');
4
4
  const ComponentFormat = require('./component.format');
5
5
  const ComponentBuild = require('./component.build');
6
+ const Component = require('./component');
6
7
  const { server } = require('../server');
7
8
  const express = require('express');
8
9
 
@@ -1,216 +0,0 @@
1
-
2
- const path = require('path');
3
- const fs = require('fs');
4
-
5
- const Component = require('./component');
6
- const ComponentFormat = require('./component.format');
7
- const ComponentBuild = require('./component.build');
8
-
9
- const UglifyCSS = require('uglifycss');
10
-
11
- function WebBuilder(binh, Binh) {
12
- binh.context = function(module, defaultRequire) {
13
- var context = { module, require: defaultRequire };
14
- return {
15
- context,
16
- tag: tag.bind(context),
17
- svg: svg.bind(context),
18
- script: script.bind(context),
19
- require: customRequire.bind(context),
20
- css: css.bind(context)
21
- };
22
- };
23
-
24
- function tagnames(tagNames) {
25
- return typeof tagNames === 'string' ? tagNames.split(/\s+/) : [];
26
- }
27
-
28
- function tag(tagNames) {
29
- var htmltags = this.component.htmltags;
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));
36
- }
37
-
38
- function script() {
39
- var scripts = Array.from(arguments);
40
- var links = this.component.links;
41
- links.push.apply(links, scripts);
42
- }
43
-
44
- function customRequire(filePath) {
45
- filePath = filePath.endsWith('.css') ? (filePath+'.js') : filePath;
46
- return this.require(filePath);
47
- }
48
-
49
- function css() {
50
- var csses = Array.from(arguments);
51
- var appliedStyles = this.component.options.csses;
52
- appliedStyles.push.apply(appliedStyles, csses);
53
- }
54
-
55
- binh.component = function(context, ui, service, style) {
56
- var component = ui || service || style;
57
- var type = ui && 'ui' || service && 'service' || style && 'style';
58
-
59
- if (!(component instanceof Function)) return;
60
-
61
- context.component = component;
62
-
63
- var module = context.module;
64
- module.exports = component;
65
-
66
- component.module = module;
67
- component.constructor = Component;
68
- component.filename = module.filename;
69
- component.type = type;
70
-
71
- component.htmltags = [];
72
- component.svgtags = [];
73
- component.links = [];
74
- component.options = { csses: [] };
75
- component.vars = {};
76
- component.varname = getVariableName(component.filename);
77
- component.as = alias;
78
- component.eachChild = loopComponentChildren;
79
- };
80
-
81
- binh.final = function(module) {
82
- var component = module.exports;
83
-
84
- component.eachChild(childComponent => {
85
- if (childComponent.alias) {
86
- component.vars[childComponent.filename] = childComponent.alias;
87
- }
88
- });
89
- };
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
-
99
- binh.ui = function(module, component, htmltags, links) {
100
- binh.component({ module }, component);
101
- component.htmltags = htmltags;
102
- component.links = links;
103
- binh.final(module);
104
- };
105
-
106
- binh.service = function(module, component, links) {
107
- binh.component({ module }, null, component, null);
108
- component.links = links;
109
- binh.final(module);
110
- };
111
-
112
- binh.style = function(module, component) {
113
- binh.component({ module }, null, null, component);
114
- binh.final(module);
115
- };
116
-
117
- binh.css = function(module, cssFilename) {
118
- var cssFilePath = cssFilename && path.join(module.path, cssFilename) || module.filename.replace(/.js$/, '');
119
- var content = fs.readFileSync(cssFilePath, { encoding: 'utf8', flag: 'r' });
120
- content = UglifyCSS.processString(content);
121
- var component = new Function(`return function style() { return ${JSON.stringify(content)}; };`)();
122
- component.cssFilePath = cssFilePath;
123
- binh.component({ module }, null, null, component);
124
- binh.final(module);
125
- };
126
-
127
- function alias(name) {
128
- if (!isString(name)) return this;
129
- name = name.trim();
130
- this.alias = name;
131
- return this;
132
- }
133
-
134
- function getVariableName(filePath) {
135
- return path.parse(filePath).name.replace(/-/g, '').replace(/\W.*/, '').trim();
136
- }
137
-
138
- Binh.minify = function(flag = true) {
139
- Component.minification = flag;
140
- return Binh;
141
- };
142
-
143
- Binh.webStatic = function(webPath) {
144
- Binh.web.value = getAbsolutePath(webPath);
145
- return Binh;
146
- };
147
-
148
- Binh.webModule = function({ source, module }, onDone) {
149
- if (!(isString(source) && isString(module))) {
150
- throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source and module. Current: { source: ${source}, module: ${module} }`);
151
- }
152
-
153
- var { source, module } = getAbsolutePaths({ source, module });
154
-
155
- ComponentFormat.generate(source, module, onDone);
156
-
157
- return Binh;
158
- };
159
-
160
- Binh.webModuleBundle = function({ source, module, web }) {
161
- var { source, module, web } = processWebBuildPaths({ source, module, web });
162
- ComponentBuild.bundle({ source, module, web });
163
- return Binh;
164
- };
165
-
166
- Binh.webModuleLazy = function({ source, module, web }) {
167
- var { source, module, web } = processWebBuildPaths({ source, module, web });
168
- ComponentBuild.lazyload({ source, module, web });
169
- return Binh;
170
- };
171
-
172
- Binh.webBundle = function({ source, module, web }) {
173
- var absolutePaths = processWebBuildPaths({ source, module, web });
174
- return Binh.webModule({ source, module }, () => ComponentBuild.bundle(absolutePaths));
175
- };
176
-
177
- Binh.webLazy = function({ source, module, web, external }) {
178
- var absolutePaths = processWebBuildPaths({ source, module, web });
179
- absolutePaths.external = external || 'NPM';
180
- return Binh.webModule({ source, module }, () => ComponentBuild.lazyload(absolutePaths));
181
- };
182
-
183
- Binh.web = Binh.webBundle;
184
-
185
- function getAbsolutePath(relativePath) {
186
- return isString(relativePath) ? path.join(Binh.getRootpath(), relativePath) : null;
187
- }
188
-
189
- function getAbsolutePaths({ source, module, web }) {
190
- return {
191
- source: getAbsolutePath(source),
192
- module: getAbsolutePath(module),
193
- web: getAbsolutePath(web)
194
- };
195
- }
196
-
197
- function processWebBuildPaths({ source, module, web }) {
198
- if (!(isString(source) && isString(module) && isString(web))) {
199
- throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source, module and web. Current: { source: ${source}, module: ${module}, web: ${web} }`);
200
- }
201
-
202
- var absolutePaths = getAbsolutePaths({ source, module, web });
203
-
204
- Binh.web.value = absolutePaths.web;
205
-
206
- return absolutePaths;
207
- }
208
-
209
- function isString(input) {
210
- return typeof input === 'string';
211
- }
212
- }
213
-
214
- module.exports = {
215
- WebBuilder
216
- };