jsgui3-server 0.0.83 → 0.0.86

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/bundler/bundle.js CHANGED
@@ -1,5 +1,13 @@
1
1
  // Could make this a Collection?
2
2
 
3
+ // jsgui3-bundle possibly?
4
+ // Could save the whole thing as a ZIP file, or in-memory buffer in ZIP format?
5
+
6
+
7
+
8
+
9
+
10
+
3
11
  const {Collection} = require('jsgui3-html');
4
12
 
5
13
  class Bundle extends Collection {
@@ -0,0 +1,198 @@
1
+ const Bundler = require('./bundler');
2
+ const Bundle = require('./bundle');
3
+ const {obs, prom_or_cb} = require('fnl');
4
+ const {tof, each} = require('jsgui3-html');
5
+ const fnlfs = require('fnlfs');
6
+ const browserify = require('browserify');
7
+ const babel = require('@babel/core');
8
+ const stream_to_array = require('stream-to-array');
9
+ const util = require('util');
10
+ const Stream = require('stream');
11
+ // Will put the JS together. Maybe images?
12
+
13
+ const JS_AST_Node = require('./../resources/jsbuilder/JS_AST/JS_AST_Node');
14
+
15
+ // Will put the JS together. Maybe images?
16
+ // Get everything ready to serve.
17
+
18
+ // Would need a JS file that contains refs to all of the components used.
19
+ // Examine what is in the website and what JS it needs.
20
+
21
+ // Should be customisable which system gets used to make the bundle.
22
+ // eg babel or esbuild. Browserify still seems to work on code here at least, but esbuild seems better.
23
+
24
+ // JS bundling will become a bit more advanced in this server. Similar principles.
25
+
26
+ // JS_Bundler reporting css that gets found while bundling JS would make sense.
27
+
28
+ // jsgui3-jsbuilder could be a separate project too.
29
+ // or jsgui3-js-builder even.
30
+
31
+
32
+ // bundle_css_from_js_file?
33
+ // scan_js_file_for_css perhaps.
34
+ // scan_js_for_css
35
+ // maybe scan an AST or AST stream as it's coming in.
36
+
37
+
38
+
39
+ const bundle_css_from_js_str = (js_str, options = {}) => {
40
+
41
+ console.log('js_str.length', js_str.length);
42
+ // Moving towards using observables as a matter of course for longer-running functions.
43
+
44
+ // Seems like this makes a settimeout as well???
45
+ return obs((next, complete, error) => {
46
+
47
+ console.log('js_str.length', js_str.length);
48
+
49
+ // Returning a Bundle object when done could be best...?
50
+
51
+ const spec = {
52
+ source: js_str
53
+ };
54
+
55
+ const js_ast_node = JS_AST_Node.from_spec(spec);
56
+
57
+ console.log('!!js_ast_node', !!js_ast_node);
58
+
59
+ console.log('Object.keys(js_ast_node)', Object.keys(js_ast_node));
60
+
61
+ // count all
62
+
63
+ //console.log('js_ast_node.query("count all")', js_ast_node.query("count all"));
64
+
65
+ console.log('js_ast_node.query.count.all.exe()', js_ast_node.query.count.all.exe());
66
+
67
+ // A filter iterate query....? filter_deep_iterate could work.
68
+ // but we are looking specifically for 'ClassName.css'
69
+ // .css property assignments.
70
+
71
+ const ae_nodes = [];
72
+
73
+
74
+ // Just assigning a template literal to .css?
75
+ const css_ae_nodes = [];
76
+
77
+ js_ast_node.deep_iterate(node => {
78
+ //console.log('node', node);
79
+ //console.log('Object.keys(node)', Object.keys(node));
80
+ //console.log('node.type_signature', node.type_signature);
81
+ //console.log('node.signature', node.signature);
82
+ //console.log('node.type', node.type);
83
+ //console.log('node.abbreviated_type', node.abbreviated_type);
84
+
85
+ if (node.type === 'AssignmentExpression') {
86
+ //return true;
87
+ ae_nodes.push(node);
88
+ //console.log('node.source', node.source);
89
+
90
+ //console.log('Object.keys(node)', Object.keys(node));
91
+
92
+ //console.log('node.child_nodes.length', node.child_nodes.length);
93
+
94
+ const [node_assigned_to, node_assigned] = node.child_nodes;
95
+ //console.log('node_assigned_to.source', node_assigned_to.source);
96
+ //console.log('node_assigned_to.type', node_assigned_to.type);
97
+ //console.log('node_assigned.type', node_assigned.type);
98
+
99
+ if (node_assigned.type === 'TemplateLiteral') {
100
+
101
+ if (node_assigned_to.type === 'MemberExpression') {
102
+
103
+ //console.log('node_assigned_to', node_assigned_to);
104
+
105
+ // the last ID being .css?
106
+ const last_me_child = node_assigned_to.child_nodes[node_assigned_to.child_nodes.length - 1];
107
+ //console.log('last_me_child', last_me_child);
108
+ //console.log('last_me_child.source', last_me_child.source);
109
+
110
+ if (last_me_child.source === 'css') {
111
+ css_ae_nodes.push(node);
112
+ }
113
+
114
+
115
+
116
+ // does it end '.css'?
117
+ // break it down further?
118
+
119
+ }
120
+
121
+ ///console.log('node.source', node.source);
122
+ //throw 'stop';
123
+
124
+ }
125
+
126
+ //console.log('');
127
+ // look at the left part...
128
+ }
129
+
130
+ //throw 'stop';
131
+ });
132
+ //console.log('ae_nodes', ae_nodes);
133
+ //console.log('ae_nodes.length', ae_nodes.length);
134
+ //console.log('css_ae_nodes.length', css_ae_nodes.length);
135
+
136
+ const arr_css = [];
137
+
138
+ if (css_ae_nodes.length > 0) {
139
+ //console.log('css_ae_nodes', css_ae_nodes);
140
+
141
+ each(css_ae_nodes, css_ae_node => {
142
+ //console.log('css_ae_node.source', css_ae_node.source);
143
+ const tl = css_ae_node.child_nodes[1].child_nodes[0];
144
+ //console.log('tl', tl);
145
+ console.log('tl.source', tl.source);
146
+ arr_css.push(tl.source);
147
+
148
+
149
+ })
150
+ }
151
+
152
+ if (arr_css.length > 0) {
153
+ const str_css = arr_css.join('\n');
154
+
155
+ complete(str_css);
156
+ } else {
157
+ complete();
158
+ }
159
+
160
+
161
+
162
+
163
+
164
+
165
+ // Can then do query to get all .css properties that are string templates.
166
+ // is it a property of a Class object?
167
+
168
+
169
+
170
+
171
+
172
+ // Need to get an AST from it....
173
+ // Or could simply search (regex?) for .css = `...`?
174
+
175
+
176
+
177
+
178
+ // Go through each file? Just the first?
179
+
180
+ // Or should the whole bundled (browserified) JS be consulted?
181
+
182
+ const [stop, pause, resume] = [() => {}, () => {}, () => {}];
183
+ return [stop, pause, resume];
184
+
185
+ });
186
+
187
+ }
188
+
189
+
190
+
191
+ class CSS_Bundler extends Bundler {
192
+ constructor(spec = {}) {
193
+ super(spec);
194
+ }
195
+ }
196
+
197
+ CSS_Bundler.bundle_css_from_js_str = bundle_css_from_js_str;
198
+ module.exports = CSS_Bundler;
@@ -23,16 +23,27 @@ const Stream = require('stream');
23
23
 
24
24
  // JS_Bundler reporting css that gets found while bundling JS would make sense.
25
25
 
26
+ // jsgui3-jsbuilder could be a separate project too.
27
+ // or jsgui3-js-builder even.
28
+
29
+
26
30
 
27
31
 
28
32
 
29
33
 
30
34
  const bundle_js = (js_file_path, options = {}, callback) => {
31
35
 
36
+ // Could even split a file into its server and client components.
37
+ // Maybe providing found css would work well here.
38
+
39
+
40
+
32
41
  // Returning an observable and not using a callback would work best.
33
42
 
34
43
  const res = obs((next, complete, error) => {
35
44
 
45
+
46
+
36
47
  let a = arguments;
37
48
  if (typeof a[2] === 'function') {
38
49
  callback = a[2];
@@ -52,6 +63,15 @@ const bundle_js = (js_file_path, options = {}, callback) => {
52
63
 
53
64
  let fileContents = await fnlfs.load(js_file_path);
54
65
 
66
+
67
+ // Maybe do that post browserify - can read through whole thing to find the css template literals.
68
+
69
+ // Could use the CSS bundler to scan_js_for_css
70
+ // Seems as though it would be best as an observable.
71
+
72
+
73
+ // Could first get own system AST of the JS file.
74
+
55
75
  // Modify the original file contents so that only client-side parts appear?
56
76
  // Could be done by programatically removing a whole code block, what to do if it is run on the server.
57
77
 
@@ -98,6 +118,10 @@ const bundle_js = (js_file_path, options = {}, callback) => {
98
118
  });
99
119
 
100
120
  // May be able to better put the bundle stream info into the observable results.
121
+ // Browserify gets given the stream.
122
+
123
+ // Nice if this function could output a stream as well.
124
+
101
125
 
102
126
  let parts = await stream_to_array(b.bundle());
103
127
 
@@ -106,7 +130,22 @@ const bundle_js = (js_file_path, options = {}, callback) => {
106
130
  let buf_js = Buffer.concat(buffers);
107
131
  let str_js = buf_js.toString();
108
132
 
133
+ next({
134
+ 'lang': 'JavaScript',
135
+ 'operation': 'bundle',
136
+ 'compress': false,
137
+ 'type': 'single-string',
138
+ 'value': str_js
139
+ });
140
+
141
+ // full browserified (client) js app.
142
+
109
143
  let babel_option = options.babel
144
+
145
+
146
+ // could send str_js to a CSS extractor / bundler.
147
+
148
+
110
149
  //console.log('babel_option', babel_option);
111
150
  if (babel_option === 'es5') {
112
151
 
@@ -151,13 +190,11 @@ const bundle_js = (js_file_path, options = {}, callback) => {
151
190
  //plugins: ["minify-dead-code-elimination"]
152
191
  };
153
192
  if (options.include_sourcemaps) o_transform.sourceMaps = 'inline';
154
-
155
193
  let res_transform = babel.transform(str_js, o_transform);
156
194
  buf_js = Buffer.from(res_transform.code);
157
195
  } else {
158
196
  buf_js = Buffer.from(str_js);
159
197
  }
160
-
161
198
  complete(buf_js);
162
199
  })();
163
200
 
@@ -0,0 +1,74 @@
1
+
2
+ // Load in a JS file, such as square box client.
3
+ // Look for the CSS.
4
+
5
+
6
+ // Load a JS file into an OO structure
7
+ // Short term goals:
8
+
9
+ // Answering questions on the level of the file.
10
+ // Questions that are useful for finding out how to link the js files together.
11
+ // Don't want each file to be given it's own scope - want them to share local variable references to the the necessary things.
12
+ // Then those variable names won't be reused in inner scopes unless their replacement there is fine (ie they are otherwise unused).
13
+
14
+ // This will assemble somewhat detailed information about what happens inside a JavaScript file.
15
+ // The aim is to carry out and represent different kinds of analysis, but at the moment focusing of finding features.
16
+
17
+ // Root features
18
+
19
+ // Declaration features
20
+ // Object features
21
+
22
+ // Let's make it so that any node can be tagged as having / representing / being part of a feature of some kind.
23
+ // To begin with focus on what variables are being declared and used.
24
+
25
+ // Will be great to load in a whole load of JSGUI projects in such a way that the system / platform understands the ordering and
26
+ // recomposes them into a flat system where many / all declarations are local rather than using any import
27
+
28
+ // jsgui-lang
29
+ // ----------
30
+
31
+ // Recognise the basic / general signature of the full document.
32
+ // Recognise that the declarations are all within the root
33
+ // Recognise the exports object at the end exports an object that is composed from same-name references to items declared within lang-mini
34
+ // and only items declared within jsgui
35
+ // that it has no external references
36
+
37
+ // JSGUI_JS_File?
38
+
39
+
40
+
41
+ const JS_File = require('../resources/jsbuilder/JS_File/JS_File');
42
+ //const JS_File_Comprehension = require('../JS_File_Comprehension');
43
+ const path = require('path');
44
+ const fs = require('fs');
45
+ const {each} = require('lang-mini');
46
+
47
+ const JS_AST_Node = require('../resources/jsbuilder/JS_AST_Node_Extended/JS_AST_Node_Extended');
48
+
49
+ const test_js_file = () => {
50
+ const fnl_path = require.resolve('./../examples/square_box_client.js');
51
+ const file_path = fnl_path;
52
+ // path of lang mini...
53
+
54
+ // Write and test a simple and convenient way for analysing JS files and recompiling them.
55
+ // To start with, find the ways to arrange parts of the JS into 'platforms'.
56
+ // How to then build platforms into JS files.
57
+ // Will be about closures and sequences.
58
+ // A lot about unique naming, closures, and getting the sequence of definitions correct.
59
+ // ObjectPattern
60
+ const resolved_path = path.resolve(file_path);
61
+ //console.log('resolved_path', resolved_path);
62
+
63
+ const fstream = fs.createReadStream(resolved_path);
64
+
65
+ const jsf = JS_File.load_from_stream(fstream, file_path);
66
+ jsf.on('ready', () => {
67
+ const {js_ast_node_file} = jsf;
68
+ //const body_child_node_identifier_names = [];
69
+ //const map_bcnidns = {};
70
+ });
71
+
72
+ }
73
+
74
+ test_js_file();