binhend 1.2.5 → 1.3.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.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -6,13 +6,15 @@ const ComponentFormat = require('./component.format');
6
6
  const ComponentBuild = require('./component.build');
7
7
 
8
8
  function WebBuilder(binh, Binh) {
9
- binh.context = function(module) {
10
- var context = { module };
9
+ binh.context = function(module, defaultRequire) {
10
+ var context = { module, require: defaultRequire };
11
11
  return {
12
12
  context,
13
13
  tag: tag.bind(context),
14
- script: script.bind(context)
15
- }
14
+ script: script.bind(context),
15
+ require: customRequire.bind(context),
16
+ css: css.bind(context)
17
+ };
16
18
  };
17
19
 
18
20
  function tag(tagNames) {
@@ -26,6 +28,17 @@ function WebBuilder(binh, Binh) {
26
28
  var links = this.component.links;
27
29
  links.push.apply(links, scripts);
28
30
  }
31
+
32
+ function customRequire(filePath) {
33
+ filePath = filePath.endsWith('.css') ? (filePath+'.js') : filePath;
34
+ return this.require(filePath);
35
+ }
36
+
37
+ function css() {
38
+ var csses = Array.from(arguments);
39
+ var appliedStyles = this.component.options.csses;
40
+ appliedStyles.push.apply(appliedStyles, csses);
41
+ }
29
42
 
30
43
  binh.component = function(context, ui, service, style) {
31
44
  var component = ui || service || style;
@@ -36,6 +49,7 @@ function WebBuilder(binh, Binh) {
36
49
  context.component = component;
37
50
  component.htmltags = [];
38
51
  component.links = [];
52
+ component.options = { csses: [] };
39
53
 
40
54
  binh.bundle(type, context.module, component);
41
55
  };
@@ -99,6 +113,7 @@ function WebBuilder(binh, Binh) {
99
113
 
100
114
  function alias(name) {
101
115
  this.alias = name;
116
+ return this;
102
117
  }
103
118
 
104
119
  Binh.web = function(webPath, sourcePath, modulePath) {
package/src/code.js CHANGED
@@ -49,10 +49,7 @@ function dependencies(component, root, metadata) {
49
49
  var { type, name, url, component } = current;
50
50
 
51
51
  if (existed) {
52
- var sameVariableName = (name === existed.name);
53
- if (sameVariableName) return;
54
-
55
- declaration += `${name} = Binh.${type}('${url}');\r\n`;
52
+ declaration += `Binh.later(function() {${name} = Binh.${type}('${url}');});\r\n`;
56
53
  variables[name] = true;
57
54
  }
58
55
  else {
@@ -61,6 +58,12 @@ function dependencies(component, root, metadata) {
61
58
  variables[name] = true;
62
59
  };
63
60
 
61
+ var optionCode = buildCodeApplyOptions(name, component);
62
+
63
+ if (optionCode) {
64
+ declaration += `Binh.later(function() {${optionCode};});\r\n`;
65
+ }
66
+
64
67
  code += IIF([
65
68
  dependencies(component, root, Object.assign({}, global, local)),
66
69
  declaration
@@ -78,6 +81,24 @@ function dependencies(component, root, metadata) {
78
81
  return code;
79
82
  }
80
83
 
84
+ function buildCodeApplyOptions(componentName, component) {
85
+ var options = component.options, variableNames = [];
86
+
87
+ if (!options) return '';
88
+
89
+ component.type === 'ui' && options.csses.forEach(function(cssModule) {
90
+ var filename = cssModule.filename;
91
+ var variableName = component.vars[filename] || parse(filename).name.replace(/-/g, '').replace(/\W.*/, '');
92
+ variableNames.push(variableName);
93
+ });
94
+
95
+ if (variableNames.length) {
96
+ return `${componentName}.style([${variableNames.join(',')}])`;
97
+ }
98
+
99
+ return '';
100
+ }
101
+
81
102
  function bundle(component, root) {
82
103
  var code = '';
83
104
 
@@ -85,7 +106,7 @@ function bundle(component, root) {
85
106
 
86
107
  code += '\r\n';
87
108
 
88
- code += `Binh.${component.type}(${component.toString()});\r\n`;
109
+ code += `Binh.${component.type}(${component.toString()})${buildCodeApplyOptions('', component)};\r\n`;
89
110
 
90
111
  return code;
91
112
  }
@@ -7,28 +7,35 @@ 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 } = binh.context(module);
10
+ `var { context, tag, 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
 
14
14
  function generate(sourceRootPath, outputRootPath, callbackDone) {
15
15
  if (sourceRootPath == undefined || outputRootPath == undefined) return;
16
- console.log('[BINHEND][COMPONENT] Format files from:', sourceRootPath);
17
- console.log('[BINHEND][COMPONENT] to:', outputRootPath);
16
+ console.info('[BINHEND][COMPONENT] Format files from:', sourceRootPath);
17
+ console.info('[BINHEND][COMPONENT] to:', outputRootPath);
18
18
 
19
19
  scanNestedFiles(sourceRootPath, (file, done) => {
20
20
  if (done) return callbackDone instanceof Function ? callbackDone() : null;
21
21
 
22
22
  var fileSourcePath = file.path;
23
23
  var fileOutputPath = fileSourcePath.replace(sourceRootPath, outputRootPath);
24
-
25
- makeFullDirPath(fileOutputPath);
26
-
27
- if (parse(file.name).ext !== '.js') {
28
- return cloneFile(fileSourcePath, fileOutputPath);
29
- }
30
24
 
31
25
  try {
26
+ makeFullDirPath(fileOutputPath);
27
+
28
+ var fileExtension = parse(file.name).ext;
29
+
30
+ if (fileExtension === '.css') {
31
+ var cssModuleCode = 'binh.css(module);';
32
+ writeFileSync(fileOutputPath + '.js', cssModuleCode, { encoding: 'utf8', flag: 'w' });
33
+ }
34
+
35
+ if (fileExtension !== '.js') {
36
+ return cloneFile(fileSourcePath, fileOutputPath);
37
+ }
38
+
32
39
  var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
33
40
  var code = PREFIX_CODE + content.trim() + '\r\n\r\n;binh.final(module);';
34
41
  writeFileSync(fileOutputPath, code, { encoding: 'utf8', flag: 'w' });