@cilix/lightjs 0.0.1 → 0.0.3
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/README.md +146 -157
- package/index.js +18 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,32 +56,170 @@ Light.js is **alpha** software. This documentation is a work in progress, but it
|
|
|
56
56
|
## Table of Contents
|
|
57
57
|
|
|
58
58
|
- [Quick Start](#quick-start)
|
|
59
|
+
- [Usage](#usage)
|
|
59
60
|
- [Syntax](#syntax)
|
|
60
61
|
- [Document](#document)
|
|
61
62
|
- [Data](#data)
|
|
62
63
|
- [Control Flow](#control-flow)
|
|
63
64
|
- [Components](#components)
|
|
64
65
|
- [Javascript](#javascript)
|
|
65
|
-
- [Usage](#usage)
|
|
66
66
|
|
|
67
67
|
## Quick Start
|
|
68
68
|
|
|
69
|
-
Create a project boilerplate using Light.js's built-in backend router:
|
|
69
|
+
Create a project boilerplate using Light.js's built-in backend router. Usage below:
|
|
70
70
|
|
|
71
71
|
```bash
|
|
72
72
|
npx lightjs create my-app
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
## Usage
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
### `lightjs.compile()`
|
|
78
|
+
|
|
79
|
+
Fundamentally, Light.js is a library that takes in a Light.js source tree and outputs a single HTML file or string.
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
const lightjs = require('lightjs');
|
|
83
|
+
|
|
84
|
+
lightjs.compile({
|
|
85
|
+
input: 'src/index.lightjs',
|
|
86
|
+
output: 'public/index.html'
|
|
87
|
+
});
|
|
79
88
|
```
|
|
80
89
|
|
|
81
|
-
|
|
90
|
+
No other step is required for a fully reactive, bundled, minified application.
|
|
82
91
|
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
This function may also be used to serve dynamic content in real time.
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
const express = require('express');
|
|
96
|
+
const lightjs = require('lightjs');
|
|
97
|
+
|
|
98
|
+
const app = express();
|
|
99
|
+
|
|
100
|
+
app.get('/', async (req, res) => {
|
|
101
|
+
const html = await lightjs.compile({
|
|
102
|
+
input: 'src/index.lightjs'
|
|
103
|
+
});
|
|
104
|
+
res.end(html);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
app.listen(3838);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
In order for the above example to have production quality speed, you must enable caching. And if desired, Javascript minification can be enabled as well:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
const html = await lightjs.compile({
|
|
114
|
+
input: 'src/index.lightjs',
|
|
115
|
+
cache: true,
|
|
116
|
+
minify: true
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The last thing to know about Light.js's compile function is document data.
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
const html = await lightjs.compile({
|
|
124
|
+
input: 'src/about.lightjs',
|
|
125
|
+
data: {
|
|
126
|
+
pageTitle: 'About me'
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
In your Light.js document pageTitle can be found on the global 'data' object:
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
// index.lightjs
|
|
135
|
+
html [
|
|
136
|
+
head []
|
|
137
|
+
body [
|
|
138
|
+
h1 '{{data.pageTitle}}'
|
|
139
|
+
]
|
|
140
|
+
]
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### `lightjs.app()`
|
|
144
|
+
|
|
145
|
+
As mentioned above, Light.js ships with a built-in backend router. This router is exposed through the 'app' function, which returns a Node.js HTTP request handler.
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
const http = require('http');
|
|
149
|
+
const light = require('lightjs');
|
|
150
|
+
|
|
151
|
+
const PORT = 4477;
|
|
152
|
+
|
|
153
|
+
const app = light.app({
|
|
154
|
+
publicDir: './public',
|
|
155
|
+
cache: true,
|
|
156
|
+
minify: true,
|
|
157
|
+
routes: {
|
|
158
|
+
// map a route directly to a light file
|
|
159
|
+
'/': 'src/index.light',
|
|
160
|
+
|
|
161
|
+
// or for more complex arrangements, you can map the route to the same arguments you would pass to light.compile()
|
|
162
|
+
'/blog/:id': {
|
|
163
|
+
input: 'src/blog.light',
|
|
164
|
+
data: async (req) => {
|
|
165
|
+
const post = 'hi' // get blog post with req.params.id
|
|
166
|
+
return post
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
http.createServer(app).listen(PORT);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Light.js's router is also completely compatible with Express:
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
const http = require('http');
|
|
179
|
+
const light = require('lightjs');
|
|
180
|
+
const express = require('express');
|
|
181
|
+
|
|
182
|
+
const PORT = 4477;
|
|
183
|
+
|
|
184
|
+
const app = express();
|
|
185
|
+
|
|
186
|
+
const lightRouter = light.app({
|
|
187
|
+
cache: true,
|
|
188
|
+
minify: true,
|
|
189
|
+
routes: {
|
|
190
|
+
// ...
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
app.use(express.static('public'));
|
|
195
|
+
app.use(lightRouter);
|
|
196
|
+
|
|
197
|
+
http.createServer(app).listen(PORT);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Or, as previously mentioned, you can simply use `light.compile` with Express:
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
const http = require('http');
|
|
204
|
+
const light = require('lightjs');
|
|
205
|
+
const express = require('express');
|
|
206
|
+
|
|
207
|
+
const PORT = 4477;
|
|
208
|
+
|
|
209
|
+
const app = express();
|
|
210
|
+
|
|
211
|
+
app.get('/', async (req, res) => {
|
|
212
|
+
const html = await light.compile({
|
|
213
|
+
input: 'src/index.light',
|
|
214
|
+
// for production
|
|
215
|
+
minify: true,
|
|
216
|
+
cache: true
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
res.end(html);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
http.createServer(app).listen(PORT);
|
|
85
223
|
```
|
|
86
224
|
|
|
87
225
|
## Syntax
|
|
@@ -856,155 +994,6 @@ html [
|
|
|
856
994
|
]
|
|
857
995
|
```
|
|
858
996
|
|
|
859
|
-
## Usage
|
|
860
|
-
|
|
861
|
-
### `lightjs.compile()`
|
|
862
|
-
|
|
863
|
-
Fundamentally, Light.js is a library that takes in a Light.js source tree and outputs a single HTML file or string.
|
|
864
|
-
|
|
865
|
-
```javascript
|
|
866
|
-
const lightjs = require('lightjs');
|
|
867
|
-
|
|
868
|
-
lightjs.compile({
|
|
869
|
-
input: 'src/index.lightjs',
|
|
870
|
-
output: 'public/index.html'
|
|
871
|
-
});
|
|
872
|
-
```
|
|
873
|
-
|
|
874
|
-
No other step is required for a fully reactive, bundled, minified application.
|
|
875
|
-
|
|
876
|
-
This function may also be used to serve dynamic content in real time.
|
|
877
|
-
|
|
878
|
-
```javascript
|
|
879
|
-
const express = require('express');
|
|
880
|
-
const lightjs = require('lightjs');
|
|
881
|
-
|
|
882
|
-
const app = express();
|
|
883
|
-
|
|
884
|
-
app.get('/', async (req, res) => {
|
|
885
|
-
const html = await lightjs.compile({
|
|
886
|
-
input: 'src/index.lightjs'
|
|
887
|
-
});
|
|
888
|
-
res.end(html);
|
|
889
|
-
});
|
|
890
|
-
|
|
891
|
-
app.listen(3838);
|
|
892
|
-
```
|
|
893
|
-
|
|
894
|
-
In order for the above example to have production quality speed, you must enable caching. And if desired, Javascript minification can be enabled as well:
|
|
895
|
-
|
|
896
|
-
```javascript
|
|
897
|
-
const html = await lightjs.compile({
|
|
898
|
-
input: 'src/index.lightjs',
|
|
899
|
-
cache: true,
|
|
900
|
-
minify: true
|
|
901
|
-
});
|
|
902
|
-
```
|
|
903
|
-
|
|
904
|
-
The last thing to know about Light.js's compile function is document data.
|
|
905
|
-
|
|
906
|
-
```javascript
|
|
907
|
-
const html = await lightjs.compile({
|
|
908
|
-
input: 'src/about.lightjs',
|
|
909
|
-
data: {
|
|
910
|
-
pageTitle: 'About me'
|
|
911
|
-
}
|
|
912
|
-
});
|
|
913
|
-
```
|
|
914
|
-
|
|
915
|
-
In your Light.js document pageTitle can be found on the global 'data' object:
|
|
916
|
-
|
|
917
|
-
```javascript
|
|
918
|
-
// index.lightjs
|
|
919
|
-
html [
|
|
920
|
-
head []
|
|
921
|
-
body [
|
|
922
|
-
h1 '{{data.pageTitle}}'
|
|
923
|
-
]
|
|
924
|
-
]
|
|
925
|
-
```
|
|
926
|
-
|
|
927
|
-
### `lightjs.app()`
|
|
928
|
-
|
|
929
|
-
As mentioned above, Light.js ships with a built-in backend router. This router is exposed through the 'app' function, which returns a Node.js HTTP request handler.
|
|
930
|
-
|
|
931
|
-
```javascript
|
|
932
|
-
const http = require('http');
|
|
933
|
-
const light = require('lightjs');
|
|
934
|
-
|
|
935
|
-
const PORT = 4477;
|
|
936
|
-
|
|
937
|
-
const app = light.app({
|
|
938
|
-
publicDir: './public',
|
|
939
|
-
cache: true,
|
|
940
|
-
minify: true,
|
|
941
|
-
routes: {
|
|
942
|
-
// map a route directly to a light file
|
|
943
|
-
'/': 'src/index.light',
|
|
944
|
-
|
|
945
|
-
// or for more complex arrangements, you can map the route to the same arguments you would pass to light.compile()
|
|
946
|
-
'/blog/:id': {
|
|
947
|
-
input: 'src/blog.light',
|
|
948
|
-
data: async (req) => {
|
|
949
|
-
const post = 'hi' // get blog post with req.params.id
|
|
950
|
-
return post
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
})
|
|
955
|
-
|
|
956
|
-
http.createServer(app).listen(PORT);
|
|
957
|
-
```
|
|
958
|
-
|
|
959
|
-
Light.js's router is also completely compatible with Express:
|
|
960
|
-
|
|
961
|
-
```javascript
|
|
962
|
-
const http = require('http');
|
|
963
|
-
const light = require('lightjs');
|
|
964
|
-
const express = require('express');
|
|
965
|
-
|
|
966
|
-
const PORT = 4477;
|
|
967
|
-
|
|
968
|
-
const app = express();
|
|
969
|
-
|
|
970
|
-
const lightRouter = light.app({
|
|
971
|
-
cache: true,
|
|
972
|
-
minify: true,
|
|
973
|
-
routes: {
|
|
974
|
-
// ...
|
|
975
|
-
}
|
|
976
|
-
})
|
|
977
|
-
|
|
978
|
-
app.use(express.static('public'));
|
|
979
|
-
app.use(lightRouter);
|
|
980
|
-
|
|
981
|
-
http.createServer(app).listen(PORT);
|
|
982
|
-
```
|
|
983
|
-
|
|
984
|
-
Or, as previously mentioned, you can simply use `light.compile` with Express:
|
|
985
|
-
|
|
986
|
-
```javascript
|
|
987
|
-
const http = require('http');
|
|
988
|
-
const light = require('lightjs');
|
|
989
|
-
const express = require('express');
|
|
990
|
-
|
|
991
|
-
const PORT = 4477;
|
|
992
|
-
|
|
993
|
-
const app = express();
|
|
994
|
-
|
|
995
|
-
app.get('/', async (req, res) => {
|
|
996
|
-
const html = await light.compile({
|
|
997
|
-
input: 'src/index.light',
|
|
998
|
-
// for production
|
|
999
|
-
minify: true,
|
|
1000
|
-
cache: true
|
|
1001
|
-
});
|
|
1002
|
-
|
|
1003
|
-
res.end(html);
|
|
1004
|
-
});
|
|
1005
|
-
|
|
1006
|
-
http.createServer(app).listen(PORT);
|
|
1007
|
-
```
|
|
1008
997
|
|
|
1009
998
|
This is the entire feature set of Lightjs. If there is even one aspect of Lightjs that is missing from this document, it is considered a bug.
|
|
1010
999
|
|
package/index.js
CHANGED
|
@@ -143,7 +143,12 @@ Light.compile = async (name, opts) => {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
if (e.origin === 'light') {
|
|
146
|
-
|
|
146
|
+
if (opts.cache) {
|
|
147
|
+
console.log(`${e.file}: ${e.msg}`);
|
|
148
|
+
msg = 'Server error';
|
|
149
|
+
} else {
|
|
150
|
+
msg = `<pre>${light.printError(e)}</pre>`;
|
|
151
|
+
}
|
|
147
152
|
} else {
|
|
148
153
|
console.log(e);
|
|
149
154
|
msg = `<pre>${e.message}</pre>`;
|
|
@@ -167,12 +172,14 @@ const serveStaticFile = (p, res) => {
|
|
|
167
172
|
return true;
|
|
168
173
|
}
|
|
169
174
|
const data = fs.readFileSync(p);
|
|
170
|
-
const mime =
|
|
175
|
+
const mime = mimeTypes[ext] || 'text/plain';
|
|
171
176
|
res.writeHead(200, { 'Content-type': `${mime}` });
|
|
172
177
|
res.end(data);
|
|
173
178
|
return true;
|
|
174
179
|
} catch (e) {
|
|
175
|
-
|
|
180
|
+
res.statusCode = 404;
|
|
181
|
+
res.end();
|
|
182
|
+
return true;
|
|
176
183
|
}
|
|
177
184
|
};
|
|
178
185
|
|
|
@@ -209,14 +216,6 @@ const parseBody = (req, max) => {
|
|
|
209
216
|
});
|
|
210
217
|
};
|
|
211
218
|
|
|
212
|
-
const parseQuery = (p) => {
|
|
213
|
-
const purl = url.parse(p);
|
|
214
|
-
if (purl.query) {
|
|
215
|
-
return qs.parse(purl.query);
|
|
216
|
-
}
|
|
217
|
-
return {};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
219
|
function matchRoute (istr, mstr) {
|
|
221
220
|
if (mstr === '*') return {};
|
|
222
221
|
const p0 = istr.split('/').filter(p => p);
|
|
@@ -286,12 +285,15 @@ Light.router = function (opts) {
|
|
|
286
285
|
}
|
|
287
286
|
}
|
|
288
287
|
return async function (req, res, next) {
|
|
289
|
-
const
|
|
290
|
-
if (publicDir && serveStaticFile(path.join(publicDir,
|
|
288
|
+
const purl = url.parse(req.url);
|
|
289
|
+
if (publicDir && serveStaticFile(path.join(publicDir, purl.pathname), res)) {
|
|
291
290
|
return;
|
|
292
291
|
}
|
|
293
|
-
|
|
294
|
-
|
|
292
|
+
if (purl.query) {
|
|
293
|
+
req.query = qs.parse(purl.query);
|
|
294
|
+
} else {
|
|
295
|
+
req.query = {};
|
|
296
|
+
}
|
|
295
297
|
try {
|
|
296
298
|
req.body = await parseBody(req, 1024 * 1024); // 1MB limit
|
|
297
299
|
} catch (e) {
|
|
@@ -306,7 +308,7 @@ Light.router = function (opts) {
|
|
|
306
308
|
let routeParams = {};
|
|
307
309
|
let routeConfig = null;
|
|
308
310
|
for (const [pattern, config] of Object.entries(routes)) {
|
|
309
|
-
const params = matchRoute(
|
|
311
|
+
const params = matchRoute(purl.pathname, pattern);
|
|
310
312
|
if (params !== null) {
|
|
311
313
|
matchedRoute = pattern;
|
|
312
314
|
routeParams = params;
|